Spaces:
Runtime error
Runtime error
Commit
·
b0056be
1
Parent(s):
6c68290
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from PIL import Image
|
| 4 |
-
from torchvision.transforms import functional as F
|
| 5 |
from transformers import DetrImageProcessor, DetrForObjectDetection
|
| 6 |
import cv2
|
| 7 |
import numpy as np
|
|
@@ -32,15 +31,30 @@ def detect_objects(frame):
|
|
| 32 |
frame = cv2.putText(frame, f'{model.config.id2label[label.item()]}: {round(score.item(), 3)}',
|
| 33 |
(int(box[0]), int(box[1]) - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2, cv2.LINE_AA)
|
| 34 |
|
| 35 |
-
return frame
|
| 36 |
|
| 37 |
-
#
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
inputs=gr.Video(),
|
| 41 |
-
outputs="numpy_image",
|
| 42 |
-
live=True,
|
| 43 |
-
)
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from PIL import Image
|
|
|
|
| 4 |
from transformers import DetrImageProcessor, DetrForObjectDetection
|
| 5 |
import cv2
|
| 6 |
import numpy as np
|
|
|
|
| 31 |
frame = cv2.putText(frame, f'{model.config.id2label[label.item()]}: {round(score.item(), 3)}',
|
| 32 |
(int(box[0]), int(box[1]) - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2, cv2.LINE_AA)
|
| 33 |
|
| 34 |
+
return cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 35 |
|
| 36 |
+
# Function to capture video frames and process them
|
| 37 |
+
def capture_frames():
|
| 38 |
+
cap = cv2.VideoCapture(0) # Use 0 for default camera, you can change it if needed
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
while True:
|
| 41 |
+
ret, frame = cap.read()
|
| 42 |
+
if not ret:
|
| 43 |
+
break
|
| 44 |
+
|
| 45 |
+
# Process the frame
|
| 46 |
+
processed_frame = detect_objects(frame)
|
| 47 |
+
|
| 48 |
+
# Display the processed frame
|
| 49 |
+
cv2.imshow("Object Detection", processed_frame)
|
| 50 |
+
|
| 51 |
+
# Check for exit key (press 'q' to exit)
|
| 52 |
+
if cv2.waitKey(1) & 0xFF == ord('q'):
|
| 53 |
+
break
|
| 54 |
+
|
| 55 |
+
# Release the camera and close all windows
|
| 56 |
+
cap.release()
|
| 57 |
+
cv2.destroyAllWindows()
|
| 58 |
+
|
| 59 |
+
# Run the video capturing and processing function
|
| 60 |
+
capture_frames()
|