Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
@@ -9,7 +9,7 @@ import numpy as np
|
|
9 |
app = FastAPI()
|
10 |
|
11 |
# Load the YOLOv8 model
|
12 |
-
model = YOLO("
|
13 |
|
14 |
# Open the video file
|
15 |
video_path = "demo.mp4"
|
@@ -23,7 +23,7 @@ try:
|
|
23 |
if hasattr(cv2, 'legacy'):
|
24 |
trackers = cv2.legacy.MultiTracker_create()
|
25 |
else:
|
26 |
-
trackers = cv2.
|
27 |
except AttributeError:
|
28 |
trackers = None
|
29 |
tracker_initialized = False
|
@@ -31,63 +31,55 @@ except AttributeError:
|
|
31 |
def process_video() -> Generator[bytes, None, None]:
|
32 |
global bird_count, tracker_initialized, trackers
|
33 |
while cap.isOpened():
|
34 |
-
# Read a frame from the video
|
35 |
success, frame = cap.read()
|
|
|
|
|
36 |
|
37 |
-
|
38 |
-
frame_height, frame_width = frame.shape[:2]
|
39 |
-
if not tracker_initialized:
|
40 |
-
# Run YOLOv8 inference on the frame
|
41 |
-
results = model(frame)
|
42 |
-
|
43 |
-
# Extract the detected objects
|
44 |
-
detections = results[0].boxes.data.cpu().numpy()
|
45 |
-
|
46 |
-
# Filter results to include only the "bird" class (class id 14 in COCO)
|
47 |
-
bird_results = [detection for detection in detections if int(detection[5]) == 14]
|
48 |
-
|
49 |
-
# Initialize trackers for bird results
|
50 |
-
try:
|
51 |
-
if hasattr(cv2, 'legacy'):
|
52 |
-
trackers = cv2.legacy.MultiTracker_create()
|
53 |
-
else:
|
54 |
-
trackers = cv2.MultiTracker_create()
|
55 |
-
|
56 |
-
for res in bird_results:
|
57 |
-
x1, y1, x2, y2, confidence, class_id = res
|
58 |
-
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
|
59 |
-
if 0 <= x1 < frame_width and 0 <= y1 < frame_height and x2 <= frame_width and y2 <= frame_height:
|
60 |
-
bbox = (x1, y1, x2 - x1, y2 - y1)
|
61 |
-
tracker = cv2.legacy.TrackerCSRT_create() if hasattr(cv2, 'legacy') else cv2.TrackerCSRT_create()
|
62 |
-
trackers.add(tracker, frame, bbox)
|
63 |
-
|
64 |
-
bird_count = len(bird_results)
|
65 |
-
tracker_initialized = True
|
66 |
-
except AttributeError:
|
67 |
-
trackers = None
|
68 |
-
tracker_initialized = False
|
69 |
-
else:
|
70 |
-
# Update trackers and get updated positions
|
71 |
-
success, boxes = trackers.update(frame)
|
72 |
-
|
73 |
-
if success:
|
74 |
-
bird_count = len(boxes)
|
75 |
-
for box in boxes:
|
76 |
-
x, y, w, h = [int(v) for v in box]
|
77 |
-
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
|
78 |
-
cv2.putText(frame, 'bird', (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
|
79 |
-
else:
|
80 |
-
tracker_initialized = False
|
81 |
|
82 |
-
|
83 |
-
|
84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
|
86 |
-
# Use generator to yield the frame
|
87 |
-
yield (b'--frame\r\n'
|
88 |
-
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
|
89 |
else:
|
90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
cap.release()
|
92 |
|
93 |
templates = Jinja2Templates(directory="templates")
|
|
|
9 |
app = FastAPI()
|
10 |
|
11 |
# Load the YOLOv8 model
|
12 |
+
model = YOLO("yolov8n.pt")
|
13 |
|
14 |
# Open the video file
|
15 |
video_path = "demo.mp4"
|
|
|
23 |
if hasattr(cv2, 'legacy'):
|
24 |
trackers = cv2.legacy.MultiTracker_create()
|
25 |
else:
|
26 |
+
trackers = cv2.MultiTracker_create()
|
27 |
except AttributeError:
|
28 |
trackers = None
|
29 |
tracker_initialized = False
|
|
|
31 |
def process_video() -> Generator[bytes, None, None]:
|
32 |
global bird_count, tracker_initialized, trackers
|
33 |
while cap.isOpened():
|
|
|
34 |
success, frame = cap.read()
|
35 |
+
if not success:
|
36 |
+
break
|
37 |
|
38 |
+
frame_height, frame_width = frame.shape[:2]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
+
if not tracker_initialized:
|
41 |
+
results = model(frame)
|
42 |
+
detections = results[0].boxes.data.cpu().numpy()
|
43 |
+
bird_results = [detection for detection in detections if int(detection[5]) == 14]
|
44 |
+
|
45 |
+
try:
|
46 |
+
if hasattr(cv2, 'legacy'):
|
47 |
+
trackers = cv2.legacy.MultiTracker_create()
|
48 |
+
else:
|
49 |
+
trackers = cv2.MultiTracker_create()
|
50 |
+
|
51 |
+
for res in bird_results:
|
52 |
+
x1, y1, x2, y2, confidence, class_id = res
|
53 |
+
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
|
54 |
+
if 0 <= x1 < frame_width and 0 <= y1 < frame_height and x2 <= frame_width and y2 <= frame_height:
|
55 |
+
bbox = (x1, y1, x2 - x1, y2 - y1)
|
56 |
+
tracker = cv2.legacy.TrackerCSRT_create() if hasattr(cv2, 'legacy') else cv2.TrackerCSRT_create()
|
57 |
+
trackers.add(tracker, frame, bbox)
|
58 |
+
|
59 |
+
bird_count = len(bird_results)
|
60 |
+
tracker_initialized = True
|
61 |
+
except AttributeError:
|
62 |
+
trackers = None
|
63 |
+
tracker_initialized = False
|
64 |
|
|
|
|
|
|
|
65 |
else:
|
66 |
+
success, boxes = trackers.update(frame)
|
67 |
+
|
68 |
+
if success:
|
69 |
+
bird_count = len(boxes)
|
70 |
+
for box in boxes:
|
71 |
+
x, y, w, h = [int(v) for v in box]
|
72 |
+
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
|
73 |
+
cv2.putText(frame, 'bird', (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
|
74 |
+
else:
|
75 |
+
tracker_initialized = False
|
76 |
+
|
77 |
+
ret, buffer = cv2.imencode('.jpg', frame)
|
78 |
+
frame = buffer.tobytes()
|
79 |
+
|
80 |
+
yield (b'--frame\r\n'
|
81 |
+
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
|
82 |
+
|
83 |
cap.release()
|
84 |
|
85 |
templates = Jinja2Templates(directory="templates")
|