Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -46,6 +46,24 @@ class LiveFeedState:
|
|
| 46 |
self.logs = deque(maxlen=10) # Last 10 log entries
|
| 47 |
self.frame_count = 0 # Frame counter
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
def live_feed_generator(video_type, confidence_threshold=0.9):
|
| 50 |
"""Generator for live feed with real-time detection."""
|
| 51 |
state = LiveFeedState()
|
|
@@ -59,18 +77,26 @@ def live_feed_generator(video_type, confidence_threshold=0.9):
|
|
| 59 |
cap = cv2.VideoCapture(video_path)
|
| 60 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 61 |
frame_interval = 1 / fps # Time between frames for real-time simulation
|
|
|
|
| 62 |
|
| 63 |
while cap.isOpened():
|
| 64 |
ret, frame = cap.read()
|
|
|
|
| 65 |
if not ret or frame is None or frame.size == 0:
|
| 66 |
-
print(
|
| 67 |
cap.set(cv2.CAP_PROP_POS_FRAMES, 0) # Loop the video
|
| 68 |
continue
|
| 69 |
else:
|
| 70 |
print(f"Frame shape: {frame.shape} at frame count {state.frame_count}")
|
| 71 |
|
| 72 |
state.frame_count += 1
|
|
|
|
| 73 |
frame_pil = video_service.frame_to_pil(frame)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
# Perform detection based on video type
|
| 76 |
if video_type == "Thermal Feed":
|
|
@@ -86,10 +112,12 @@ def live_feed_generator(video_type, confidence_threshold=0.9):
|
|
| 86 |
print(f"Detections for Frame {state.frame_count}: {detections}")
|
| 87 |
alert_type = "General"
|
| 88 |
|
|
|
|
|
|
|
| 89 |
# Draw detections on frame
|
| 90 |
annotated_frame = video_service.draw_detections(frame, detections)
|
| 91 |
annotated_frame_rgb = cv2.cvtColor(annotated_frame, cv2.COLOR_BGR2RGB)
|
| 92 |
-
print(f"
|
| 93 |
cv2.imshow("Debug Frame", annotated_frame_rgb)
|
| 94 |
cv2.waitKey(1)
|
| 95 |
print(f"Annotated frame shape: {annotated_frame_rgb.shape}")
|
|
@@ -156,13 +184,14 @@ def live_feed_generator(video_type, confidence_threshold=0.9):
|
|
| 156 |
|
| 157 |
# Yield updated UI components
|
| 158 |
yield (
|
| 159 |
-
annotated_frame_rgb,
|
| 160 |
-
metrics_str,
|
| 161 |
-
"\n".join(state.logs),
|
| 162 |
-
trend_plot,
|
| 163 |
-
anomaly_types_str,
|
| 164 |
-
list(state.captured_events),
|
| 165 |
-
timestamp_str
|
|
|
|
| 166 |
)
|
| 167 |
|
| 168 |
|
|
|
|
| 46 |
self.logs = deque(maxlen=10) # Last 10 log entries
|
| 47 |
self.frame_count = 0 # Frame counter
|
| 48 |
|
| 49 |
+
def simple_test_generator():
|
| 50 |
+
import numpy as np
|
| 51 |
+
import time
|
| 52 |
+
img = np.zeros((480, 640, 3), dtype=np.uint8) # black image
|
| 53 |
+
count = 0
|
| 54 |
+
while True:
|
| 55 |
+
count += 1
|
| 56 |
+
yield (
|
| 57 |
+
img,
|
| 58 |
+
f"Test metrics count: {count}",
|
| 59 |
+
f"Test logs count: {count}",
|
| 60 |
+
None,
|
| 61 |
+
"Test anomaly types",
|
| 62 |
+
[],
|
| 63 |
+
"Test timestamp"
|
| 64 |
+
)
|
| 65 |
+
time.sleep(0.5)
|
| 66 |
+
|
| 67 |
def live_feed_generator(video_type, confidence_threshold=0.9):
|
| 68 |
"""Generator for live feed with real-time detection."""
|
| 69 |
state = LiveFeedState()
|
|
|
|
| 77 |
cap = cv2.VideoCapture(video_path)
|
| 78 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 79 |
frame_interval = 1 / fps # Time between frames for real-time simulation
|
| 80 |
+
print(f"[DEBUG] Starting live feed for {video_type} at path: {video_path}")
|
| 81 |
|
| 82 |
while cap.isOpened():
|
| 83 |
ret, frame = cap.read()
|
| 84 |
+
print(f"[DEBUG] Frame read success: {ret}, frame count: {state.frame_count}")
|
| 85 |
if not ret or frame is None or frame.size == 0:
|
| 86 |
+
print("[DEBUG] End of video reached or cannot read frame. Looping video.")
|
| 87 |
cap.set(cv2.CAP_PROP_POS_FRAMES, 0) # Loop the video
|
| 88 |
continue
|
| 89 |
else:
|
| 90 |
print(f"Frame shape: {frame.shape} at frame count {state.frame_count}")
|
| 91 |
|
| 92 |
state.frame_count += 1
|
| 93 |
+
# Convert frame to PIL Image
|
| 94 |
frame_pil = video_service.frame_to_pil(frame)
|
| 95 |
+
if frame_pil is None:
|
| 96 |
+
print("[DEBUG] Failed to convert frame to PIL image. Skipping frame.")
|
| 97 |
+
continue
|
| 98 |
+
|
| 99 |
+
print(f"[DEBUG] Processing frame {state.frame_count}")
|
| 100 |
|
| 101 |
# Perform detection based on video type
|
| 102 |
if video_type == "Thermal Feed":
|
|
|
|
| 112 |
print(f"Detections for Frame {state.frame_count}: {detections}")
|
| 113 |
alert_type = "General"
|
| 114 |
|
| 115 |
+
print(f"[DEBUG] Detections: {detections}")
|
| 116 |
+
|
| 117 |
# Draw detections on frame
|
| 118 |
annotated_frame = video_service.draw_detections(frame, detections)
|
| 119 |
annotated_frame_rgb = cv2.cvtColor(annotated_frame, cv2.COLOR_BGR2RGB)
|
| 120 |
+
print(f"[DEBUG] Yielding frame {state.frame_count}, shape: {annotated_frame_rgb.shape}")
|
| 121 |
cv2.imshow("Debug Frame", annotated_frame_rgb)
|
| 122 |
cv2.waitKey(1)
|
| 123 |
print(f"Annotated frame shape: {annotated_frame_rgb.shape}")
|
|
|
|
| 184 |
|
| 185 |
# Yield updated UI components
|
| 186 |
yield (
|
| 187 |
+
gr.update(value=annotated_frame_rgb),
|
| 188 |
+
gr.update(value=metrics_str),
|
| 189 |
+
gr.update(value="\n".join(state.logs)),
|
| 190 |
+
gr.update(value=trend_plot),
|
| 191 |
+
gr.update(value=anomaly_types_str),
|
| 192 |
+
gr.update(value=list(state.captured_events)),
|
| 193 |
+
gr.update(value=timestamp_str)
|
| 194 |
+
|
| 195 |
)
|
| 196 |
|
| 197 |
|