Aumkeshchy2003 commited on
Commit
3e3644e
·
verified ·
1 Parent(s): 0152e0c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -10
app.py CHANGED
@@ -5,7 +5,6 @@ import cv2
5
  import time
6
  from PIL import Image
7
 
8
- # Check device availability
9
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
10
 
11
  # Load YOLOv5 model
@@ -23,17 +22,18 @@ colors = {i: [int(c) for c in np.random.randint(0, 255, 3)] for i in range(len(m
23
  def detect_objects(image):
24
  start_time = time.time() # Start FPS measurement
25
 
26
- image_pil = Image.fromarray(image)
 
 
27
 
28
  with torch.no_grad():
29
- results = model(image_pil, conf=0.3, iou=0.3) # Apply NMS with IoU = 0.3
 
 
30
 
31
- rendered_images = results.render() # Get rendered image with default YOLOv5 visualization
32
 
33
- # Get bounding boxes and draw color-coded boxes
34
- img_cv = np.array(rendered_images[0]) if rendered_images else image
35
-
36
- for det in results.xyxy[0]: # Bounding box format: x1, y1, x2, y2, conf, cls
37
  x1, y1, x2, y2, conf, cls = map(int, det[:6])
38
  label = f"{model.names[cls]}: {conf:.2f}"
39
 
@@ -53,9 +53,8 @@ iface = gr.Interface(
53
  inputs=gr.Image(type="numpy", label="Upload Image"),
54
  outputs=gr.Image(type="numpy", label="Detected Objects"),
55
  title="Object Detection with YOLOv5",
56
- description="Use webcam or upload an image to detect objects. Optimized for speed and accuracy!",
57
  allow_flagging="never",
58
- examples=["spring_street_after.jpg", "pexels-hikaique-109919.jpg"],
59
  )
60
 
61
  iface.launch()
 
5
  import time
6
  from PIL import Image
7
 
 
8
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
9
 
10
  # Load YOLOv5 model
 
22
  def detect_objects(image):
23
  start_time = time.time() # Start FPS measurement
24
 
25
+
26
+ img_tensor = torch.from_numpy(image).permute(2, 0, 1).float().to(device) / 255.0
27
+ img_tensor = img_tensor.unsqueeze(0)
28
 
29
  with torch.no_grad():
30
+ results = model(img_tensor)
31
+
32
+ detections = results.xyxy[0].cpu().numpy()
33
 
34
+ img_cv = image.copy()
35
 
36
+ for det in detections:
 
 
 
37
  x1, y1, x2, y2, conf, cls = map(int, det[:6])
38
  label = f"{model.names[cls]}: {conf:.2f}"
39
 
 
53
  inputs=gr.Image(type="numpy", label="Upload Image"),
54
  outputs=gr.Image(type="numpy", label="Detected Objects"),
55
  title="Object Detection with YOLOv5",
56
+ description="Optimized for 30+ FPS real-time object detection!",
57
  allow_flagging="never",
 
58
  )
59
 
60
  iface.launch()