Aumkeshchy2003 commited on
Commit
ac43c04
·
verified ·
1 Parent(s): d791bba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -32
app.py CHANGED
@@ -9,12 +9,11 @@ from pathlib import Path
9
  from ultralytics import YOLO
10
 
11
  # Load YOLOv5 model for ONNX export
12
- model = YOLO("yolov5n.pt") # Use "yolov5x.pt" if you want the larger model
13
 
14
  # Export to ONNX format
15
  model.export(format="onnx", dynamic=True)
16
 
17
-
18
  os.makedirs("models", exist_ok=True)
19
 
20
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
@@ -44,49 +43,61 @@ inference_count = 0
44
 
45
  def detect_objects(image):
46
  global total_inference_time, inference_count
47
-
48
  if image is None:
49
  return None
50
-
51
  start_time = time.time()
52
-
53
- image = cv2.resize(image, (416, 416))
54
- image = image.astype(np.float32) / 255.0
55
- image = np.transpose(image, (2, 0, 1))
56
- image = np.expand_dims(image, axis=0)
57
-
58
- # Run inference
59
- inputs = {session.get_inputs()[0].name: image}
60
- output = session.run(None, inputs)
61
- detections = output[0][0]
62
-
 
 
 
 
 
 
63
  inference_time = time.time() - start_time
64
  total_inference_time += inference_time
65
  inference_count += 1
66
  avg_inference_time = total_inference_time / inference_count
67
  fps = 1 / inference_time
68
-
69
- # Draw bounding boxes
70
- output_image = image[0].transpose(1, 2, 0) * 255
71
- output_image = output_image.astype(np.uint8)
72
-
 
 
 
 
73
  for det in detections:
74
- x1, y1, x2, y2, conf, class_id = map(int, det[:6])
75
  if conf < 0.3: # Confidence threshold
76
  continue
77
-
78
- color = colors[class_id].tolist()
79
- cv2.rectangle(output_image, (x1, y1), (x2, y2), color, 3)
 
 
 
 
 
 
80
  label = f"Class {class_id} {conf:.2f}"
81
- cv2.putText(output_image, label, (x1, y1 - 10),
82
- cv2.FONT_HERSHEY_SIMPLEX, 0.9, color, 2)
83
-
84
  # Display FPS
85
- cv2.putText(output_image, f"FPS: {fps:.2f}", (20, 40),
86
- cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
87
- cv2.putText(output_image, f"Avg FPS: {1/avg_inference_time:.2f}", (20, 70),
88
- cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
89
-
90
  return output_image
91
 
92
  # Gradio Interface
 
9
  from ultralytics import YOLO
10
 
11
  # Load YOLOv5 model for ONNX export
12
+ model = YOLO("yolov5n.pt")
13
 
14
  # Export to ONNX format
15
  model.export(format="onnx", dynamic=True)
16
 
 
17
  os.makedirs("models", exist_ok=True)
18
 
19
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 
43
 
44
  def detect_objects(image):
45
  global total_inference_time, inference_count
 
46
  if image is None:
47
  return None
48
+
49
  start_time = time.time()
50
+
51
+ # Preprocess image
52
+ original_shape = image.shape
53
+ input_shape = (416, 416)
54
+ image_resized = cv2.resize(image, input_shape)
55
+ image_norm = image_resized.astype(np.float32) / 255.0
56
+ image_transposed = np.transpose(image_norm, (2, 0, 1))
57
+ image_batch = np.expand_dims(image_transposed, axis=0)
58
+
59
+ # Get input name and run inference
60
+ input_name = session.get_inputs()[0].name
61
+ outputs = session.run(None, {input_name: image_batch})
62
+
63
+ # Process detections
64
+ detections = outputs[0][0] # First batch, all detections
65
+
66
+ # Calculate timing
67
  inference_time = time.time() - start_time
68
  total_inference_time += inference_time
69
  inference_count += 1
70
  avg_inference_time = total_inference_time / inference_count
71
  fps = 1 / inference_time
72
+
73
+ # Create a copy of the original image for visualization
74
+ output_image = image.copy()
75
+
76
+ # Scale factor for bounding box coordinates
77
+ scale_x = original_shape[1] / input_shape[0]
78
+ scale_y = original_shape[0] / input_shape[1]
79
+
80
+ # Draw bounding boxes and labels
81
  for det in detections:
82
+ x1, y1, x2, y2, conf, class_id = det[:6]
83
  if conf < 0.3: # Confidence threshold
84
  continue
85
+
86
+ # Convert to original image coordinates
87
+ x1, x2 = int(x1 * scale_x), int(x2 * scale_x)
88
+ y1, y2 = int(y1 * scale_y), int(y2 * scale_y)
89
+ class_id = int(class_id)
90
+
91
+ # Draw rectangle and label
92
+ color = tuple(map(int, colors[class_id]))
93
+ cv2.rectangle(output_image, (x1, y1), (x2, y2), color, 2)
94
  label = f"Class {class_id} {conf:.2f}"
95
+ cv2.putText(output_image, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
96
+
 
97
  # Display FPS
98
+ cv2.putText(output_image, f"FPS: {fps:.2f}", (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
99
+ cv2.putText(output_image, f"Avg FPS: {1/avg_inference_time:.2f}", (20, 70), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
100
+
 
 
101
  return output_image
102
 
103
  # Gradio Interface