ziv-conntour commited on
Commit
4968728
·
verified ·
1 Parent(s): 5b1cdda

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +96 -96
app.py CHANGED
@@ -227,125 +227,125 @@ def process_clip_from_file(prompt, frames, chatbot, fps, video_path, id):
227
 
228
  return chatbot
229
 
230
- # # Function to capture video frames
231
- # def analyze_stream(prompt, chatbot):
232
- # global stop_capture
233
- # global base_start_time
234
- # stop_capture = False
235
- # half_hour_in_secs = 1800 # long sf junction video length
236
- # video_start = int(int(time.time() - base_start_time) % half_hour_in_secs)
237
 
238
- # # stream = "https://streamapi2.eu.loclx.io/video_feed/101"
239
- # stream = VIDEO_PATH
240
- # cap = cv2.VideoCapture(stream or WEBCAM)
241
- # fps = cap.get(cv2.CAP_PROP_FPS)
242
- # # cap.set(cv2.CAP_PROP_POS_FRAMES, int(video_start*fps))
243
- # cap.set(cv2.CAP_PROP_POS_FRAMES, int(20 * 24))
244
 
245
- # print("Video start", video_start, fps, base_start_time)
246
 
247
- # frames = []
248
- # start_time = time.time()
249
- # id = video_start
250
- # while not stop_capture:
251
- # ret, frame = cap.read()
252
- # # if not ret:
253
- # # cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
254
- # frames.append(frame)
255
 
256
- # # Sample the frames every 5 seconds
257
- # if time.time() - start_time >= LENGTH:
258
- # # Start a new thread for processing the video clip
259
- # Thread(target=process_clip, args=(prompt, frames.copy(), chatbot, id)).start()
260
- # frames = []
261
- # start_time = time.time()
262
- # id=id+1
263
- # yield chatbot
264
 
265
- # cap.release()
266
- # return chatbot
267
 
268
 
269
- def analyze_stream(prompt, chatbot):
270
- global stop_capture
271
- global base_start_time
272
 
273
- stop_capture = False
274
- extra_frames = 6
275
- video_start = int(int(time.time() - base_start_time) % 1800)
276
 
277
- stream = VIDEO_PATH
278
- cap = cv2.VideoCapture(stream or WEBCAM)
279
 
280
- fps = cap.get(cv2.CAP_PROP_FPS)
281
- if fps <= 0:
282
- print("[DEBUG]: Could not find FPS")
283
- # Fallback, in case the FPS is reported as 0 or negative
284
- fps = 24.0
285
-
286
- # Convert `video_start` (in seconds) to frames
287
- start_frame = int(video_start * fps)
288
- print("[DEBUG]: Desired start_frame =", start_frame)
289
- print("[DEBUG]: Video start, fps, base_start_time =", video_start, fps, base_start_time)
290
 
291
- # Attempt to seek
292
- # success = cap.set(cv2.CAP_PROP_POS_FRAMES, start_frame)
293
 
294
- for _ in range(start_frame):
295
- ret, _ = cap.read()
296
 
297
- # Check if seeking was actually successful by reading a frame
298
- ret, test_frame = cap.read()
299
 
300
- # if not success or not ret:
301
- # # If seeking failed, fall back to manual skipping
302
- # print(f"Direct seek to frame {start_frame} failed. Falling back to manual skipping.")
303
- # # Reset capture to start
304
- # cap.release()
305
- # cap = cv2.VideoCapture(stream)
306
- # # Skip frames manually
307
- # for _ in range(start_frame):
308
- # ret, _ = cap.read()
309
- # if not ret:
310
- # print("Failed before reaching start_frame (manual skip).")
311
- # break
312
- # # We'll use 'test_frame' from the final read below
313
- # ret, test_frame = cap.read()
314
 
315
- frames = []
316
- start_time = time.time()
317
- clip_id = video_start
318
 
319
- print("Starting capture from the current position now.")
320
- if ret and test_frame is not None:
321
- # We already read one frame after seeking, so store it
322
- frames.append(test_frame)
323
 
324
 
325
 
326
- while not stop_capture:
327
- ret, frame = cap.read()
328
- if not ret:
329
- # You could optionally try restarting if desired
330
- print("No more frames or read error; stopping.")
331
- break
332
 
333
- frames.append(frame)
334
 
335
- # Sample the frames every LENGTH seconds
336
- if (time.time() - start_time) >= LENGTH:
337
- # Start a new thread for processing the video clip
338
- print("analyze_stream.clip_id", clip_id)
339
- Thread(target=process_clip, args=(prompt, frames.copy(), chatbot, clip_id)).start()
340
- frames = []
341
- start_time = time.time()
342
- clip_id += 1
343
 
344
- # Yield to the UI or chatbot loop
345
- yield chatbot
346
 
347
- cap.release()
348
- return chatbot
349
 
350
 
351
  def analyze_video_file(prompt, chatbot):
 
227
 
228
  return chatbot
229
 
230
+ # Function to capture video frames
231
+ def analyze_stream(prompt, chatbot):
232
+ global stop_capture
233
+ global base_start_time
234
+ stop_capture = False
235
+ half_hour_in_secs = 1800 # long sf junction video length
236
+ video_start = int(int(time.time() - base_start_time) % half_hour_in_secs)
237
 
238
+ # stream = "https://streamapi2.eu.loclx.io/video_feed/101"
239
+ stream = VIDEO_PATH
240
+ cap = cv2.VideoCapture(stream or WEBCAM)
241
+ fps = cap.get(cv2.CAP_PROP_FPS)
242
+ cap.set(cv2.CAP_PROP_POS_FRAMES, int(video_start*fps))
243
+ # cap.set(cv2.CAP_PROP_POS_FRAMES, int(20 * 24))
244
 
245
+ print("Video start", video_start, fps, base_start_time)
246
 
247
+ frames = []
248
+ start_time = time.time()
249
+ id = int(video_start / LENGTH)
250
+ while not stop_capture:
251
+ ret, frame = cap.read()
252
+ # if not ret:
253
+ # cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
254
+ frames.append(frame)
255
 
256
+ # Sample the frames every 5 seconds
257
+ if time.time() - start_time >= LENGTH:
258
+ # Start a new thread for processing the video clip
259
+ Thread(target=process_clip, args=(prompt, frames.copy(), chatbot, id)).start()
260
+ frames = []
261
+ start_time = time.time()
262
+ id=id+1
263
+ yield chatbot
264
 
265
+ cap.release()
266
+ return chatbot
267
 
268
 
269
+ # def analyze_stream(prompt, chatbot):
270
+ # global stop_capture
271
+ # global base_start_time
272
 
273
+ # stop_capture = False
274
+ # extra_frames = 6
275
+ # video_start = int(int(time.time() - base_start_time) % 1800)
276
 
277
+ # stream = VIDEO_PATH
278
+ # cap = cv2.VideoCapture(stream or WEBCAM)
279
 
280
+ # fps = cap.get(cv2.CAP_PROP_FPS)
281
+ # if fps <= 0:
282
+ # print("[DEBUG]: Could not find FPS")
283
+ # # Fallback, in case the FPS is reported as 0 or negative
284
+ # fps = 24.0
285
+
286
+ # # Convert `video_start` (in seconds) to frames
287
+ # start_frame = int(video_start * fps)
288
+ # print("[DEBUG]: Desired start_frame =", start_frame)
289
+ # print("[DEBUG]: Video start, fps, base_start_time =", video_start, fps, base_start_time)
290
 
291
+ # # Attempt to seek
292
+ # # success = cap.set(cv2.CAP_PROP_POS_FRAMES, start_frame)
293
 
294
+ # for _ in range(start_frame):
295
+ # ret, _ = cap.read()
296
 
297
+ # # Check if seeking was actually successful by reading a frame
298
+ # ret, test_frame = cap.read()
299
 
300
+ # # if not success or not ret:
301
+ # # # If seeking failed, fall back to manual skipping
302
+ # # print(f"Direct seek to frame {start_frame} failed. Falling back to manual skipping.")
303
+ # # # Reset capture to start
304
+ # # cap.release()
305
+ # # cap = cv2.VideoCapture(stream)
306
+ # # # Skip frames manually
307
+ # # for _ in range(start_frame):
308
+ # # ret, _ = cap.read()
309
+ # # if not ret:
310
+ # # print("Failed before reaching start_frame (manual skip).")
311
+ # # break
312
+ # # # We'll use 'test_frame' from the final read below
313
+ # # ret, test_frame = cap.read()
314
 
315
+ # frames = []
316
+ # start_time = time.time()
317
+ # clip_id = video_start
318
 
319
+ # print("Starting capture from the current position now.")
320
+ # if ret and test_frame is not None:
321
+ # # We already read one frame after seeking, so store it
322
+ # frames.append(test_frame)
323
 
324
 
325
 
326
+ # while not stop_capture:
327
+ # ret, frame = cap.read()
328
+ # if not ret:
329
+ # # You could optionally try restarting if desired
330
+ # print("No more frames or read error; stopping.")
331
+ # break
332
 
333
+ # frames.append(frame)
334
 
335
+ # # Sample the frames every LENGTH seconds
336
+ # if (time.time() - start_time) >= LENGTH:
337
+ # # Start a new thread for processing the video clip
338
+ # print("analyze_stream.clip_id", clip_id)
339
+ # Thread(target=process_clip, args=(prompt, frames.copy(), chatbot, clip_id)).start()
340
+ # frames = []
341
+ # start_time = time.time()
342
+ # clip_id += 1
343
 
344
+ # # Yield to the UI or chatbot loop
345
+ # yield chatbot
346
 
347
+ # cap.release()
348
+ # return chatbot
349
 
350
 
351
  def analyze_video_file(prompt, chatbot):