dschandra commited on
Commit
4224427
·
verified ·
1 Parent(s): 333da55

Update video_processor.py

Browse files
Files changed (1) hide show
  1. video_processor.py +42 -35
video_processor.py CHANGED
@@ -1,52 +1,59 @@
1
  import cv2
2
  import os
3
- import tempfile
 
 
4
 
5
- def process_video(video_path, clip_duration=10, target_fps=25):
6
- """
7
- Processes the input video:
8
- - Trims the last `clip_duration` seconds
9
- - Samples frames at `target_fps`
 
10
 
11
- Returns:
12
- frames: List of BGR frames
13
- metadata: Dict with original FPS, trimmed duration, frame count
14
- """
15
- cap = cv2.VideoCapture(video_path)
16
- if not cap.isOpened():
17
- raise IOError("Cannot open video file.")
18
-
19
- orig_fps = cap.get(cv2.CAP_PROP_FPS)
20
  total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
21
- duration = total_frames / orig_fps
22
 
23
- # Calculate starting point for last `clip_duration` seconds
24
- start_time = max(0, duration - clip_duration)
25
- start_frame = int(start_time * orig_fps)
26
 
27
- # Set video to start point
28
  cap.set(cv2.CAP_PROP_POS_FRAMES, start_frame)
29
-
30
- # Read and sample frames
31
  frames = []
32
- frame_interval = int(orig_fps // target_fps) if target_fps < orig_fps else 1
33
- frame_idx = 0
34
-
35
  while True:
36
  ret, frame = cap.read()
37
  if not ret:
38
  break
39
- if frame_idx % frame_interval == 0:
40
- frames.append(frame)
41
- frame_idx += 1
 
 
 
 
 
 
 
 
42
 
 
 
 
 
 
 
 
 
 
 
43
  cap.release()
44
 
45
- metadata = {
46
- "original_fps": orig_fps,
47
- "sampled_fps": target_fps,
48
- "clip_duration": clip_duration,
49
- "sampled_frames": len(frames)
50
- }
51
 
52
- return frames, metadata
 
 
 
1
  import cv2
2
  import os
3
+ import uuid
4
+ import numpy as np
5
+ from utils import extract_frames, save_video
6
 
7
+ TEMP_DIR = "temp_videos"
8
+ os.makedirs(TEMP_DIR, exist_ok=True)
9
+
10
+ def process_live_video():
11
+ # Simulated live stream (replace with real buffer system if needed)
12
+ live_source = "sample_videos/lbw_sample.mp4"
13
 
14
+ cap = cv2.VideoCapture(live_source)
15
+ fps = cap.get(cv2.CAP_PROP_FPS)
 
 
 
 
 
 
 
16
  total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
17
+ duration = total_frames / fps
18
 
19
+ # Trim last 10 seconds
20
+ trim_start = max(0, duration - 10)
21
+ start_frame = int(trim_start * fps)
22
 
 
23
  cap.set(cv2.CAP_PROP_POS_FRAMES, start_frame)
 
 
24
  frames = []
 
 
 
25
  while True:
26
  ret, frame = cap.read()
27
  if not ret:
28
  break
29
+ frames.append(frame)
30
+ cap.release()
31
+
32
+ video_id = str(uuid.uuid4())
33
+ trimmed_path = os.path.join(TEMP_DIR, f"{video_id}_trimmed.mp4")
34
+ save_video(frames, trimmed_path, fps)
35
+
36
+ # Extract & Analyze
37
+ decision = "Analyzing..." # Placeholder, will use lbw_detector later
38
+ return trimmed_path, decision
39
+
40
 
41
+ def process_uploaded_video(video_file):
42
+ cap = cv2.VideoCapture(video_file)
43
+ fps = cap.get(cv2.CAP_PROP_FPS)
44
+
45
+ frames = []
46
+ while True:
47
+ ret, frame = cap.read()
48
+ if not ret:
49
+ break
50
+ frames.append(frame)
51
  cap.release()
52
 
53
+ video_id = str(uuid.uuid4())
54
+ saved_path = os.path.join(TEMP_DIR, f"{video_id}_upload.mp4")
55
+ save_video(frames, saved_path, fps)
 
 
 
56
 
57
+ # Extract & Analyze
58
+ decision = "Analyzing..." # Placeholder for lbw_detector output
59
+ return saved_path, decision