Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -223,42 +223,106 @@ def process_clip_from_file(prompt, frames, chatbot, fps, video_path, id):
|
|
223 |
return chatbot
|
224 |
|
225 |
# Function to capture video frames
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
226 |
def analyze_stream(prompt, chatbot):
|
227 |
global stop_capture
|
228 |
global base_start_time
|
|
|
229 |
stop_capture = False
|
230 |
-
|
231 |
-
|
232 |
-
|
|
|
233 |
stream = VIDEO_PATH
|
234 |
cap = cv2.VideoCapture(stream or WEBCAM)
|
|
|
235 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
236 |
-
|
237 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
238 |
|
239 |
-
print("
|
240 |
|
241 |
frames = []
|
242 |
start_time = time.time()
|
243 |
-
|
|
|
244 |
while not stop_capture:
|
245 |
ret, frame = cap.read()
|
246 |
-
|
247 |
-
|
|
|
|
|
|
|
248 |
frames.append(frame)
|
249 |
-
|
250 |
-
# Sample the frames every
|
251 |
if time.time() - start_time >= LENGTH:
|
252 |
# Start a new thread for processing the video clip
|
253 |
-
Thread(target=process_clip, args=(prompt, frames.copy(), chatbot,
|
254 |
frames = []
|
255 |
start_time = time.time()
|
256 |
-
|
|
|
|
|
257 |
yield chatbot
|
258 |
|
259 |
cap.release()
|
260 |
return chatbot
|
261 |
|
|
|
262 |
def analyze_video_file(prompt, chatbot):
|
263 |
global stop_capture
|
264 |
stop_capture = False # Reset the stop flag when analysis starts
|
|
|
223 |
return chatbot
|
224 |
|
225 |
# Function to capture video frames
|
226 |
+
# def analyze_stream(prompt, chatbot):
|
227 |
+
# global stop_capture
|
228 |
+
# global base_start_time
|
229 |
+
# stop_capture = False
|
230 |
+
# video_start = int(time.time() - base_start_time) % 1800
|
231 |
+
|
232 |
+
# # stream = "https://streamapi2.eu.loclx.io/video_feed/101"
|
233 |
+
# stream = VIDEO_PATH
|
234 |
+
# cap = cv2.VideoCapture(stream or WEBCAM)
|
235 |
+
# fps = cap.get(cv2.CAP_PROP_FPS)
|
236 |
+
# # cap.set(cv2.CAP_PROP_POS_FRAMES, int(video_start*fps))
|
237 |
+
# cap.set(cv2.CAP_PROP_POS_FRAMES, int(20 * 24))
|
238 |
+
|
239 |
+
# print("Video start", video_start, fps, base_start_time)
|
240 |
+
|
241 |
+
# frames = []
|
242 |
+
# start_time = time.time()
|
243 |
+
# id = 0
|
244 |
+
# while not stop_capture:
|
245 |
+
# ret, frame = cap.read()
|
246 |
+
# # if not ret:
|
247 |
+
# # cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
|
248 |
+
# frames.append(frame)
|
249 |
+
|
250 |
+
# # Sample the frames every 5 seconds
|
251 |
+
# if time.time() - start_time >= LENGTH:
|
252 |
+
# # Start a new thread for processing the video clip
|
253 |
+
# Thread(target=process_clip, args=(prompt, frames.copy(), chatbot, id)).start()
|
254 |
+
# frames = []
|
255 |
+
# start_time = time.time()
|
256 |
+
# id=id+1
|
257 |
+
# yield chatbot
|
258 |
+
|
259 |
+
# cap.release()
|
260 |
+
# return chatbot
|
261 |
+
|
262 |
+
|
263 |
def analyze_stream(prompt, chatbot):
|
264 |
global stop_capture
|
265 |
global base_start_time
|
266 |
+
|
267 |
stop_capture = False
|
268 |
+
# Calculate how many seconds in we want to start (example: 6 + modulo logic)
|
269 |
+
video_start = (int(time.time() - base_start_time) % 1800) + 6
|
270 |
+
|
271 |
+
# OpenCV video source; could be local file or http(s) link
|
272 |
stream = VIDEO_PATH
|
273 |
cap = cv2.VideoCapture(stream or WEBCAM)
|
274 |
+
|
275 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
276 |
+
if fps <= 0:
|
277 |
+
# Fallback, in case the FPS is reported as 0 or negative
|
278 |
+
fps = 24.0
|
279 |
+
|
280 |
+
# Convert `video_start` (in seconds) to frames
|
281 |
+
start_frame = int(video_start * fps)
|
282 |
+
print("Desired start_frame =", start_frame)
|
283 |
+
print("Video start, fps, base_start_time =", video_start, fps, base_start_time)
|
284 |
+
|
285 |
+
# --- Manual frame skipping ---
|
286 |
+
# Read and discard frames until we reach `start_frame`
|
287 |
+
if start_frame > 0:
|
288 |
+
print(f"Skipping {start_frame} frames...")
|
289 |
+
for _ in range(start_frame):
|
290 |
+
ret, _ = cap.read()
|
291 |
+
if not ret:
|
292 |
+
# If the video ends or fails before skipping all frames, break
|
293 |
+
print("Failed before reaching start_frame.")
|
294 |
+
break
|
295 |
|
296 |
+
print("Starting capture from the current position now.")
|
297 |
|
298 |
frames = []
|
299 |
start_time = time.time()
|
300 |
+
clip_id = 0
|
301 |
+
|
302 |
while not stop_capture:
|
303 |
ret, frame = cap.read()
|
304 |
+
if not ret:
|
305 |
+
# You could optionally try restarting if desired
|
306 |
+
print("No more frames or read error; stopping.")
|
307 |
+
break
|
308 |
+
|
309 |
frames.append(frame)
|
310 |
+
|
311 |
+
# Sample the frames every LENGTH seconds
|
312 |
if time.time() - start_time >= LENGTH:
|
313 |
# Start a new thread for processing the video clip
|
314 |
+
Thread(target=process_clip, args=(prompt, frames.copy(), chatbot, clip_id)).start()
|
315 |
frames = []
|
316 |
start_time = time.time()
|
317 |
+
clip_id += 1
|
318 |
+
|
319 |
+
# Yield to the UI or chatbot loop
|
320 |
yield chatbot
|
321 |
|
322 |
cap.release()
|
323 |
return chatbot
|
324 |
|
325 |
+
|
326 |
def analyze_video_file(prompt, chatbot):
|
327 |
global stop_capture
|
328 |
stop_capture = False # Reset the stop flag when analysis starts
|