Update app.py
Browse files
app.py
CHANGED
|
@@ -5,88 +5,62 @@ from ultralytics import YOLO
|
|
| 5 |
import cv2
|
| 6 |
import gradio as gr
|
| 7 |
import numpy as np
|
| 8 |
-
import torch
|
| 9 |
from ultralytics import YOLO
|
|
|
|
| 10 |
|
| 11 |
-
# Load
|
| 12 |
-
|
|
|
|
| 13 |
|
| 14 |
-
# Video Inference Function
|
| 15 |
def vid_inf(vid_path, contour_thresh):
|
| 16 |
cap = cv2.VideoCapture(vid_path)
|
| 17 |
-
|
| 18 |
-
# Get video properties
|
| 19 |
-
frame_width = int(cap.get(3))
|
| 20 |
-
frame_height = int(cap.get(4))
|
| 21 |
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
| 22 |
frame_size = (frame_width, frame_height)
|
| 23 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 24 |
output_video = "output_recorded.mp4"
|
| 25 |
-
|
| 26 |
-
# Video Writer
|
| 27 |
out = cv2.VideoWriter(output_video, fourcc, fps, frame_size)
|
| 28 |
|
| 29 |
-
# Background Subtraction Model
|
| 30 |
backSub = cv2.createBackgroundSubtractorMOG2(history=200, varThreshold=25, detectShadows=True)
|
| 31 |
-
|
| 32 |
-
# Check if video opened successfully
|
| 33 |
-
if not cap.isOpened():
|
| 34 |
-
print("Error opening video file")
|
| 35 |
-
return None, None
|
| 36 |
-
|
| 37 |
count = 0
|
| 38 |
|
| 39 |
while cap.isOpened():
|
| 40 |
ret, frame = cap.read()
|
| 41 |
if not ret:
|
| 42 |
break
|
| 43 |
-
|
| 44 |
-
# Apply Background Subtraction (Motion Detection)
|
| 45 |
fg_mask = backSub.apply(frame)
|
| 46 |
retval, mask_thresh = cv2.threshold(fg_mask, 200, 255, cv2.THRESH_BINARY)
|
| 47 |
-
|
| 48 |
-
# Erosion to remove noise
|
| 49 |
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
|
| 50 |
mask_eroded = cv2.morphologyEx(mask_thresh, cv2.MORPH_OPEN, kernel)
|
| 51 |
-
|
| 52 |
-
# Find Contours (Potential Moving Objects)
|
| 53 |
contours, _ = cv2.findContours(mask_eroded, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 54 |
large_contours = [cnt for cnt in contours if cv2.contourArea(cnt) > contour_thresh]
|
| 55 |
|
|
|
|
|
|
|
| 56 |
frame_out = frame.copy()
|
| 57 |
|
| 58 |
-
# Run YOLOv8 Object Detection
|
| 59 |
-
results = model(frame, verbose=False) # Perform object detection
|
| 60 |
-
|
| 61 |
for result in results:
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
for box, class_id, conf in zip(boxes, class_ids, confs):
|
| 67 |
-
if conf < 0.5: # Skip low-confidence detections
|
| 68 |
-
continue
|
| 69 |
-
|
| 70 |
-
class_name = model.names[int(class_id)] # Get class name
|
| 71 |
|
| 72 |
-
if
|
| 73 |
x1, y1, x2, y2 = map(int, box)
|
| 74 |
-
|
| 75 |
-
# Check if detected package overlaps with a moving contour
|
| 76 |
for cnt in large_contours:
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
frame_out_final = cv2.cvtColor(frame_out, cv2.COLOR_BGR2RGB)
|
| 85 |
out.write(frame_out)
|
| 86 |
|
| 87 |
-
# Display every 12th frame
|
| 88 |
if not count % 12:
|
| 89 |
-
yield frame_out_final, None
|
| 90 |
count += 1
|
| 91 |
|
| 92 |
cap.release()
|
|
@@ -94,7 +68,7 @@ def vid_inf(vid_path, contour_thresh):
|
|
| 94 |
cv2.destroyAllWindows()
|
| 95 |
yield None, output_video
|
| 96 |
|
| 97 |
-
# Gradio
|
| 98 |
input_video = gr.Video(label="Input Video")
|
| 99 |
contour_thresh = gr.Slider(0, 10000, value=500, label="Contour Threshold")
|
| 100 |
output_frames = gr.Image(label="Output Frames")
|
|
@@ -104,10 +78,9 @@ app = gr.Interface(
|
|
| 104 |
fn=vid_inf,
|
| 105 |
inputs=[input_video, contour_thresh],
|
| 106 |
outputs=[output_frames, output_video_file],
|
| 107 |
-
title="
|
| 108 |
-
description=
|
| 109 |
allow_flagging="never",
|
| 110 |
-
examples=[["./sample/car.mp4", "1000"], ["./sample/motion_test.mp4", "5000"], ["./sample/home.mp4", "4500"]],
|
| 111 |
cache_examples=False,
|
| 112 |
)
|
| 113 |
|
|
|
|
| 5 |
import cv2
|
| 6 |
import gradio as gr
|
| 7 |
import numpy as np
|
|
|
|
| 8 |
from ultralytics import YOLO
|
| 9 |
+
from PIL import Image
|
| 10 |
|
| 11 |
+
# Load YOLO model
|
| 12 |
+
yolo_model = YOLO('yolov8n.pt')
|
| 13 |
+
TARGET_LABEL = 'parcel' # Define the target object label
|
| 14 |
|
|
|
|
| 15 |
def vid_inf(vid_path, contour_thresh):
|
| 16 |
cap = cv2.VideoCapture(vid_path)
|
| 17 |
+
frame_width, frame_height = int(cap.get(3)), int(cap.get(4))
|
|
|
|
|
|
|
|
|
|
| 18 |
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
| 19 |
frame_size = (frame_width, frame_height)
|
| 20 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 21 |
output_video = "output_recorded.mp4"
|
|
|
|
|
|
|
| 22 |
out = cv2.VideoWriter(output_video, fourcc, fps, frame_size)
|
| 23 |
|
|
|
|
| 24 |
backSub = cv2.createBackgroundSubtractorMOG2(history=200, varThreshold=25, detectShadows=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
count = 0
|
| 26 |
|
| 27 |
while cap.isOpened():
|
| 28 |
ret, frame = cap.read()
|
| 29 |
if not ret:
|
| 30 |
break
|
| 31 |
+
|
|
|
|
| 32 |
fg_mask = backSub.apply(frame)
|
| 33 |
retval, mask_thresh = cv2.threshold(fg_mask, 200, 255, cv2.THRESH_BINARY)
|
|
|
|
|
|
|
| 34 |
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
|
| 35 |
mask_eroded = cv2.morphologyEx(mask_thresh, cv2.MORPH_OPEN, kernel)
|
|
|
|
|
|
|
| 36 |
contours, _ = cv2.findContours(mask_eroded, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 37 |
large_contours = [cnt for cnt in contours if cv2.contourArea(cnt) > contour_thresh]
|
| 38 |
|
| 39 |
+
# Run YOLO detection
|
| 40 |
+
results = yolo_model(frame)
|
| 41 |
frame_out = frame.copy()
|
| 42 |
|
|
|
|
|
|
|
|
|
|
| 43 |
for result in results:
|
| 44 |
+
for id, box in enumerate(result.boxes.xyxy):
|
| 45 |
+
class_id = int(result.boxes.cls[id])
|
| 46 |
+
label = yolo_model.names[class_id]
|
| 47 |
+
conf = result.boxes.conf[id]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
if label == TARGET_LABEL and conf >= 0.5:
|
| 50 |
x1, y1, x2, y2 = map(int, box)
|
| 51 |
+
center = ((x1 + x2) // 2, (y1 + y2) // 2)
|
|
|
|
| 52 |
for cnt in large_contours:
|
| 53 |
+
if cv2.contourArea(cnt) > 500:
|
| 54 |
+
x, y, w, h = cv2.boundingRect(cnt)
|
| 55 |
+
if x1 < x < x2 and y1 < y < y2:
|
| 56 |
+
cv2.rectangle(frame_out, (x1, y1), (x2, y2), (0, 255, 255), 2)
|
| 57 |
+
cv2.putText(frame_out, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 0), 2)
|
| 58 |
+
|
|
|
|
| 59 |
frame_out_final = cv2.cvtColor(frame_out, cv2.COLOR_BGR2RGB)
|
| 60 |
out.write(frame_out)
|
| 61 |
|
|
|
|
| 62 |
if not count % 12:
|
| 63 |
+
yield Image.fromarray(frame_out_final), None
|
| 64 |
count += 1
|
| 65 |
|
| 66 |
cap.release()
|
|
|
|
| 68 |
cv2.destroyAllWindows()
|
| 69 |
yield None, output_video
|
| 70 |
|
| 71 |
+
# Gradio UI Setup
|
| 72 |
input_video = gr.Video(label="Input Video")
|
| 73 |
contour_thresh = gr.Slider(0, 10000, value=500, label="Contour Threshold")
|
| 74 |
output_frames = gr.Image(label="Output Frames")
|
|
|
|
| 78 |
fn=vid_inf,
|
| 79 |
inputs=[input_video, contour_thresh],
|
| 80 |
outputs=[output_frames, output_video_file],
|
| 81 |
+
title="YOLO Motion Detection - Parcel Focus",
|
| 82 |
+
description='A video analysis tool using YOLOv8 for parcel detection with motion tracking.',
|
| 83 |
allow_flagging="never",
|
|
|
|
| 84 |
cache_examples=False,
|
| 85 |
)
|
| 86 |
|