Aumkeshchy2003 commited on
Commit
e9a3a08
·
verified ·
1 Parent(s): 248b9ce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -11
app.py CHANGED
@@ -6,24 +6,25 @@ from PIL import Image
6
  import random
7
 
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
- CLASS_NAMES = model.names
 
 
12
 
 
13
  random.seed(42)
14
  CLASS_COLORS = {cls: (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) for cls in CLASS_NAMES}
15
 
16
  def preprocess_image(image):
17
- image = Image.fromarray(image)
18
- image = image.convert("RGB")
19
  return image
20
 
21
  def detect_objects(image):
22
  image = preprocess_image(image)
23
 
24
- results = model(image)
25
 
26
- image = np.array(image)
27
 
28
  for *box, conf, cls in results.xyxy[0]:
29
  x1, y1, x2, y2 = map(int, box)
@@ -33,15 +34,12 @@ def detect_objects(image):
33
  color = CLASS_COLORS[class_name]
34
 
35
  cv2.rectangle(image, (x1, y1), (x2, y2), color, 4)
36
-
37
-
38
  label = f"{class_name} ({confidence:.1f}%)"
39
  cv2.putText(image, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX,
40
- 1, color, 3, cv2.LINE_AA) # Larger text
41
 
42
  return image
43
 
44
-
45
  iface = gr.Interface(
46
  fn=detect_objects,
47
  inputs=gr.Image(type="numpy", label="Upload Image"),
@@ -49,7 +47,7 @@ iface = gr.Interface(
49
  title="Object Detection with YOLOv5",
50
  description="Use webcam or upload an image to detect objects.",
51
  allow_flagging="never",
52
- examples=["spring_street_after.jpg", "pexels-hikaique-109919.jpg"]
53
  )
54
 
55
  iface.launch()
 
6
  import random
7
 
8
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
 
9
 
10
+ # Use a smaller model for faster inference
11
+ model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True).to(device)
12
+ model.eval()
13
 
14
+ CLASS_NAMES = model.names
15
  random.seed(42)
16
  CLASS_COLORS = {cls: (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) for cls in CLASS_NAMES}
17
 
18
  def preprocess_image(image):
19
+ image = Image.fromarray(image).convert("RGB").resize((640, 640))
 
20
  return image
21
 
22
  def detect_objects(image):
23
  image = preprocess_image(image)
24
 
25
+ results = model([image]) # Batch processing for efficiency
26
 
27
+ image = np.array(image)
28
 
29
  for *box, conf, cls in results.xyxy[0]:
30
  x1, y1, x2, y2 = map(int, box)
 
34
  color = CLASS_COLORS[class_name]
35
 
36
  cv2.rectangle(image, (x1, y1), (x2, y2), color, 4)
 
 
37
  label = f"{class_name} ({confidence:.1f}%)"
38
  cv2.putText(image, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX,
39
+ 1, color, 3, cv2.LINE_AA)
40
 
41
  return image
42
 
 
43
  iface = gr.Interface(
44
  fn=detect_objects,
45
  inputs=gr.Image(type="numpy", label="Upload Image"),
 
47
  title="Object Detection with YOLOv5",
48
  description="Use webcam or upload an image to detect objects.",
49
  allow_flagging="never",
50
+ examples=["examples/spring_street_after.jpg", "examples/pexels-hikaique-109919.jpg"]
51
  )
52
 
53
  iface.launch()