aje6 commited on
Commit
fd83372
·
verified ·
1 Parent(s): 58df0e6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -11
app.py CHANGED
@@ -1,32 +1,57 @@
1
  import gradio as gr
2
  import cv2
3
- from ultralytics import YOLO
 
4
 
5
- # Load the pretrained YOLOv5 model
6
- yolo_model = YOLO("yolov5su.pt") # Load the small version of YOLOv5
7
- webcamera = cv2.VideoCapture(0)
8
 
9
- # Function to perform object detection with a configurable confidence threshold
10
  def detect_objects(frame, confidence_threshold=0.5):
11
  # Convert the frame from BGR (OpenCV) to RGB
12
  image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
13
 
14
- # Perform inference with YOLO, passing the confidence threshold
15
- results = yolo_model(image, conf=confidence_threshold) # Set confidence threshold
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
  # Draw bounding boxes and labels on the image
18
- annotated_image = results.plot() # This automatically draws boxes and labels
 
 
 
 
19
 
20
  # Convert the image back to BGR for displaying in Gradio
21
- annotated_image = cv2.cvtColor(annotated_image, cv2.COLOR_RGB2BGR)
22
 
23
- return annotated_image
24
 
25
  # Gradio interface to use the webcam for real-time object detection
26
  # Added a slider for the confidence threshold
27
  iface = gr.Interface(fn=detect_objects,
28
  inputs=[
29
- gr.Video(source=webcamera, type="numpy"), # Webcam input
30
  gr.Slider(minimum=0.0, maximum=1.0, default=0.5, label="Confidence Threshold") # Confidence slider
31
  ],
32
  outputs="image") # Show output image with bounding boxes
 
1
  import gradio as gr
2
  import cv2
3
+ import numpy as np
4
+ import onnxruntime as ort
5
 
6
+ # Load the ONNX model using onnxruntime
7
+ onnx_model_path = "aje6/ASL-Fingerspelling-Detection/onnx/Model_IV.onnx" # Update with your ONNX model path
8
+ session = ort.InferenceSession(onnx_model_path)
9
 
10
+ # Function to perform object detection with the ONNX model
11
  def detect_objects(frame, confidence_threshold=0.5):
12
  # Convert the frame from BGR (OpenCV) to RGB
13
  image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
14
 
15
+ # Preprocessing: Resize and normalize the image
16
+ # Assuming YOLO model input is 640x640, update according to your model's input size
17
+ input_size = (640, 640)
18
+ image_resized = cv2.resize(image, input_size)
19
+ image_normalized = image_resized / 255.0 # Normalize to [0, 1]
20
+ image_input = np.transpose(image_normalized, (2, 0, 1)) # Change to CHW format
21
+ image_input = np.expand_dims(image_input, axis=0).astype(np.float32) # Add batch dimension
22
+
23
+ # Perform inference
24
+ inputs = {session.get_inputs()[0].name: image_input}
25
+ outputs = session.run(None, inputs)
26
+
27
+ # Assuming YOLO model outputs are in the form of [boxes, confidences, class_probs]
28
+ boxes, confidences, class_probs = outputs
29
+
30
+ # Post-processing: Filter boxes by confidence threshold
31
+ detections = []
32
+ for i, confidence in enumerate(confidences[0]):
33
+ if confidence >= confidence_threshold:
34
+ x1, y1, x2, y2 = boxes[0][i]
35
+ class_id = np.argmax(class_probs[0][i]) # Get class with highest probability
36
+ detections.append((x1, y1, x2, y2, confidence, class_id))
37
 
38
  # Draw bounding boxes and labels on the image
39
+ for (x1, y1, x2, y2, confidence, class_id) in detections:
40
+ color = (0, 255, 0) # Green color for bounding boxes
41
+ cv2.rectangle(image, (int(x1), int(y1)), (int(x2), int(y2)), color, 2)
42
+ label = f"Class {class_id}: {confidence:.2f}"
43
+ cv2.putText(image, label, (int(x1), int(y1)-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
44
 
45
  # Convert the image back to BGR for displaying in Gradio
46
+ image_bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
47
 
48
+ return image_bgr
49
 
50
  # Gradio interface to use the webcam for real-time object detection
51
  # Added a slider for the confidence threshold
52
  iface = gr.Interface(fn=detect_objects,
53
  inputs=[
54
+ gr.Video(source="webcam", type="numpy"), # Webcam input
55
  gr.Slider(minimum=0.0, maximum=1.0, default=0.5, label="Confidence Threshold") # Confidence slider
56
  ],
57
  outputs="image") # Show output image with bounding boxes