Spaces:
Sleeping
Sleeping
Update video_processor.py
Browse files- video_processor.py +42 -35
video_processor.py
CHANGED
@@ -1,52 +1,59 @@
|
|
1 |
import cv2
|
2 |
import os
|
3 |
-
import
|
|
|
|
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
|
|
10 |
|
11 |
-
|
12 |
-
|
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 /
|
22 |
|
23 |
-
#
|
24 |
-
|
25 |
-
start_frame = int(
|
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 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
cap.release()
|
44 |
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
"clip_duration": clip_duration,
|
49 |
-
"sampled_frames": len(frames)
|
50 |
-
}
|
51 |
|
52 |
-
|
|
|
|
|
|
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
|