Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,57 @@
|
|
1 |
import gradio as gr
|
2 |
import cv2
|
3 |
-
|
|
|
4 |
|
5 |
-
# Load the
|
6 |
-
|
7 |
-
|
8 |
|
9 |
-
# Function to perform object detection with
|
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 |
-
#
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
# Draw bounding boxes and labels on the image
|
18 |
-
|
|
|
|
|
|
|
|
|
19 |
|
20 |
# Convert the image back to BGR for displaying in Gradio
|
21 |
-
|
22 |
|
23 |
-
return
|
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=
|
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
|