mkhug98 commited on
Commit
549be87
·
verified ·
1 Parent(s): 6bb3cd0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -4
app.py CHANGED
@@ -1,7 +1,41 @@
1
  import gradio as gr
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
5
 
6
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from huggingface_hub import hf_hub_download
3
+ import torch
4
 
5
+ # Download the YOLOv5 model from Hugging Face
6
+ model_path = hf_hub_download(repo_id="ultralytics/yolov5", filename="yolov5s.pt", revision="v7.0")
7
 
8
+ # Load the YOLOv5 model
9
+ model = torch.hub.load('ultralytics/yolov5', 'custom', path=model_path)
10
+
11
+ # Function to perform object detection
12
+ def detect_objects(image):
13
+ # Perform inference with the YOLOv5 model
14
+ results = model(image)
15
+
16
+ # Get the detected bounding boxes and labels
17
+ bboxes = results.xyxy[0].cpu().numpy()
18
+ labels = results.names
19
+
20
+ # Create a list of dictionaries for each detected object
21
+ detections = []
22
+ for bbox, label in zip(bboxes, labels):
23
+ x1, y1, x2, y2, conf, cls = bbox
24
+ detections.append({
25
+ 'label': label,
26
+ 'confidence': float(conf),
27
+ 'x1': float(x1),
28
+ 'y1': float(y1),
29
+ 'x2': float(x2),
30
+ 'y2': float(y2)
31
+ })
32
+
33
+ return detections
34
+
35
+ # Create the Gradio app
36
+ app = gr.Interface(detect_objects, gr.Image(type="pil"), "label", examples=[
37
+ ["example_image.jpg"] # Replace with your own example image
38
+ ])
39
+
40
+ # Run the app
41
+ app.launch()