Aumkeshchy2003 commited on
Commit
73df658
·
verified ·
1 Parent(s): 1b2e063

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -21
app.py CHANGED
@@ -1,18 +1,15 @@
1
- from ultralytics import YOLO
2
  import torch
3
  import cv2
4
  import numpy as np
5
  import gradio as gr
6
  from PIL import Image
7
 
8
- # Load YOLOv8 model
9
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
10
- model = YOLO("yolov8x.pt") # Load a more powerful YOLOv8 model
11
- model.to(device)
12
- model.eval()
13
 
14
  # Load COCO class labels
15
- CLASS_NAMES = model.names # YOLO's built-in class names
16
 
17
  def preprocess_image(image):
18
  image = Image.fromarray(image)
@@ -21,23 +18,25 @@ def preprocess_image(image):
21
 
22
  def detect_objects(image):
23
  image = preprocess_image(image)
24
- results = model.predict(image) # Run YOLO inference
 
 
25
 
26
  # Convert results to bounding box format
27
  image = np.array(image)
28
- for result in results:
29
- for box, cls, conf in zip(result.boxes.xyxy, result.boxes.cls, result.boxes.conf):
30
- x1, y1, x2, y2 = map(int, box[:4])
31
- class_name = CLASS_NAMES[int(cls)] # Get class name
32
- confidence = conf.item() * 100 # Convert confidence to percentage
33
 
34
- # Draw a bolder bounding box
35
- cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 4) # Increased thickness
36
 
37
- # Larger text for class label
38
- label = f"{class_name} ({confidence:.1f}%)"
39
- cv2.putText(image, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX,
40
- 1, (0, 255, 0), 3, cv2.LINE_AA) # Larger text
41
 
42
  return image
43
 
@@ -46,10 +45,10 @@ iface = gr.Interface(
46
  fn=detect_objects,
47
  inputs=gr.Image(type="numpy", label="Upload Image"),
48
  outputs=gr.Image(type="numpy", label="Detected Objects"),
49
- title="Object Detection",
50
- description="Use webcam or Upload an image to detect objects.",
51
  allow_flagging="never",
52
  examples=["spring_street_after.jpg"]
53
  )
54
 
55
- iface.launch()
 
 
1
  import torch
2
  import cv2
3
  import numpy as np
4
  import gradio as gr
5
  from PIL import Image
6
 
7
+ # Load YOLOv5 model from Ultralytics' official repo
8
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
9
+ model = torch.hub.load('ultralytics/yolov5', 'yolov5x', pretrained=True).to(device) # Load YOLOv5x model
 
 
10
 
11
  # Load COCO class labels
12
+ CLASS_NAMES = model.names # YOLOv5's built-in class names
13
 
14
  def preprocess_image(image):
15
  image = Image.fromarray(image)
 
18
 
19
  def detect_objects(image):
20
  image = preprocess_image(image)
21
+
22
+ # Run inference using YOLOv5
23
+ results = model(image)
24
 
25
  # Convert results to bounding box format
26
  image = np.array(image)
27
+
28
+ for *box, conf, cls in results.xyxy[0]: # YOLOv5 format: [x1, y1, x2, y2, conf, class]
29
+ x1, y1, x2, y2 = map(int, box)
30
+ class_name = CLASS_NAMES[int(cls)] # Get class name
31
+ confidence = conf.item() * 100 # Convert confidence to percentage
32
 
33
+ # Draw a bolder bounding box
34
+ cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 4) # Increased thickness
35
 
36
+ # Larger text for class label
37
+ label = f"{class_name} ({confidence:.1f}%)"
38
+ cv2.putText(image, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX,
39
+ 1, (0, 255, 0), 3, cv2.LINE_AA) # Larger text
40
 
41
  return image
42
 
 
45
  fn=detect_objects,
46
  inputs=gr.Image(type="numpy", label="Upload Image"),
47
  outputs=gr.Image(type="numpy", label="Detected Objects"),
48
+ title="Object Detection with YOLOv5",
49
+ description="Use webcam or upload an image to detect objects.",
50
  allow_flagging="never",
51
  examples=["spring_street_after.jpg"]
52
  )
53
 
54
+ iface.launch()