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

Update app.py

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