Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,32 +10,18 @@ import time
|
|
| 10 |
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 11 |
model = YOLO("yolov5s.pt").to(device)
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
|
|
|
| 16 |
|
| 17 |
def detect_objects(image):
|
| 18 |
-
"""Detect objects in an uploaded image with
|
| 19 |
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) # Convert to BGR for OpenCV
|
| 20 |
-
results = model.predict(image) #
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
for box in detections:
|
| 24 |
-
x1, y1, x2, y2, conf, cls = box[:6]
|
| 25 |
-
cls = int(cls) # Convert class to int
|
| 26 |
-
label = f"{model.names[cls]} {conf:.2f}"
|
| 27 |
-
color = tuple(map(int, colors[cls])) # Assign unique color
|
| 28 |
-
|
| 29 |
-
cv2.rectangle(image, (int(x1), int(y1)), (int(x2), int(y2)), color, 2)
|
| 30 |
-
cv2.putText(image, label, (int(x1), int(y1) - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
|
| 31 |
-
|
| 32 |
return cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # Convert back to RGB for Gradio
|
| 33 |
|
| 34 |
-
# Real-time webcam processing
|
| 35 |
-
cap = cv2.VideoCapture(0)
|
| 36 |
-
frame = None
|
| 37 |
-
lock = threading.Lock()
|
| 38 |
-
|
| 39 |
def process_webcam():
|
| 40 |
"""Continuously capture and process frames from the webcam."""
|
| 41 |
global frame
|
|
@@ -43,30 +29,21 @@ def process_webcam():
|
|
| 43 |
ret, img = cap.read()
|
| 44 |
if not ret:
|
| 45 |
continue
|
| 46 |
-
|
| 47 |
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # Convert to RGB
|
| 48 |
-
results = model.predict(img) # Explicitly call predict
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
for box in detections:
|
| 52 |
-
x1, y1, x2, y2, conf, cls = box[:6]
|
| 53 |
-
cls = int(cls) # Convert class to int
|
| 54 |
-
label = f"{model.names[cls]} {conf:.2f}"
|
| 55 |
-
color = tuple(map(int, colors[cls])) # Assign unique color
|
| 56 |
-
|
| 57 |
-
cv2.rectangle(img, (int(x1), int(y1)), (int(x2), int(y2)), color, 2)
|
| 58 |
-
cv2.putText(img, label, (int(x1), int(y1) - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
|
| 59 |
|
| 60 |
with lock:
|
| 61 |
-
frame = img
|
| 62 |
|
| 63 |
-
# Start the webcam thread
|
| 64 |
threading.Thread(target=process_webcam, daemon=True).start()
|
| 65 |
|
| 66 |
def get_webcam_frame():
|
| 67 |
"""Returns the latest processed webcam frame."""
|
| 68 |
with lock:
|
| 69 |
-
return frame
|
| 70 |
|
| 71 |
# Gradio UI
|
| 72 |
with gr.Blocks() as demo:
|
|
@@ -74,14 +51,14 @@ with gr.Blocks() as demo:
|
|
| 74 |
|
| 75 |
with gr.Tabs():
|
| 76 |
with gr.Tab("Real-Time Webcam"):
|
| 77 |
-
webcam_output = gr.Image(label="Live Webcam Feed")
|
| 78 |
-
|
| 79 |
def update_webcam():
|
| 80 |
while True:
|
| 81 |
with lock:
|
| 82 |
-
img = frame
|
| 83 |
webcam_output.update(img)
|
| 84 |
-
time.sleep(1/30) # ~30 FPS
|
| 85 |
|
| 86 |
threading.Thread(target=update_webcam, daemon=True).start()
|
| 87 |
|
|
|
|
| 10 |
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 11 |
model = YOLO("yolov5s.pt").to(device)
|
| 12 |
|
| 13 |
+
# Open webcam
|
| 14 |
+
cap = cv2.VideoCapture(0)
|
| 15 |
+
frame = np.zeros((480, 640, 3), dtype=np.uint8) # Default blank frame
|
| 16 |
+
lock = threading.Lock()
|
| 17 |
|
| 18 |
def detect_objects(image):
|
| 19 |
+
"""Detect objects in an uploaded image with YOLO."""
|
| 20 |
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) # Convert to BGR for OpenCV
|
| 21 |
+
results = model.predict(image, conf=0.4) # Set confidence threshold
|
| 22 |
+
image = results[0].plot() # Plot detections directly on image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
return cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # Convert back to RGB for Gradio
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
def process_webcam():
|
| 26 |
"""Continuously capture and process frames from the webcam."""
|
| 27 |
global frame
|
|
|
|
| 29 |
ret, img = cap.read()
|
| 30 |
if not ret:
|
| 31 |
continue
|
| 32 |
+
|
| 33 |
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # Convert to RGB
|
| 34 |
+
results = model.predict(img, conf=0.4) # Explicitly call predict
|
| 35 |
+
img = results[0].plot() # Directly draw detections on the frame
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
with lock:
|
| 38 |
+
frame = img # Update frame with detection overlay
|
| 39 |
|
| 40 |
+
# Start the webcam processing thread
|
| 41 |
threading.Thread(target=process_webcam, daemon=True).start()
|
| 42 |
|
| 43 |
def get_webcam_frame():
|
| 44 |
"""Returns the latest processed webcam frame."""
|
| 45 |
with lock:
|
| 46 |
+
return frame
|
| 47 |
|
| 48 |
# Gradio UI
|
| 49 |
with gr.Blocks() as demo:
|
|
|
|
| 51 |
|
| 52 |
with gr.Tabs():
|
| 53 |
with gr.Tab("Real-Time Webcam"):
|
| 54 |
+
webcam_output = gr.Image(label="Live Webcam Feed", type="numpy")
|
| 55 |
+
|
| 56 |
def update_webcam():
|
| 57 |
while True:
|
| 58 |
with lock:
|
| 59 |
+
img = frame
|
| 60 |
webcam_output.update(img)
|
| 61 |
+
time.sleep(1 / 30) # ~30 FPS
|
| 62 |
|
| 63 |
threading.Thread(target=update_webcam, daemon=True).start()
|
| 64 |
|