DSatishchandra commited on
Commit
6319516
·
verified ·
1 Parent(s): b6dc529

Update services/video_service.py

Browse files
Files changed (1) hide show
  1. services/video_service.py +60 -28
services/video_service.py CHANGED
@@ -1,33 +1,65 @@
1
  import cv2
2
- import os
 
 
3
 
4
- # Global state
5
- VIDEO_DIR = "data"
6
- video_files = [os.path.join(VIDEO_DIR, file) for file in sorted(os.listdir(VIDEO_DIR)) if file.endswith((".mp4", ".avi"))]
7
- video_index = 0
8
- cap = None
9
-
10
- def get_next_video_frame():
11
- global cap, video_index
12
-
13
- if not video_files:
14
- raise RuntimeError("No video files found in the 'data' directory.")
15
-
16
- if cap is None or not cap.isOpened():
17
- cap = cv2.VideoCapture(video_files[video_index])
18
-
19
- ret, frame = cap.read()
20
-
21
- if not ret:
22
  cap.release()
23
- video_index = (video_index + 1) % len(video_files)
24
- cap = cv2.VideoCapture(video_files[video_index])
25
- ret, frame = cap.read()
26
- if not ret:
27
- raise RuntimeError(f"Cannot read video {video_files[video_index]}")
 
28
 
29
- return frame
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
- def reset_video_index():
32
- global video_index
33
- video_index = 0
 
 
 
 
 
 
 
 
1
  import cv2
2
+ from PIL import Image
3
+ import numpy as np
4
+ from datetime import datetime
5
 
6
+ class VideoService:
7
+ def load_video(self, video_path):
8
+ """Load video and return frames."""
9
+ cap = cv2.VideoCapture(video_path)
10
+ frames = []
11
+ while cap.isOpened():
12
+ ret, frame = cap.read()
13
+ if not ret:
14
+ break
15
+ frames.append(frame)
 
 
 
 
 
 
 
 
16
  cap.release()
17
+ return frames
18
+
19
+ def frame_to_pil(self, frame):
20
+ """Convert OpenCV frame to PIL Image."""
21
+ frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
22
+ return Image.fromarray(frame_rgb)
23
 
24
+ def draw_detections(self, frame, detections):
25
+ """Draw bounding boxes, labels, and timestamp on frame."""
26
+ frame_copy = frame.copy()
27
+ # Add timestamp overlay
28
+ timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
29
+ cv2.putText(
30
+ frame_copy,
31
+ timestamp,
32
+ (10, 30),
33
+ cv2.FONT_HERSHEY_SIMPLEX,
34
+ 0.7,
35
+ (255, 255, 255),
36
+ 2
37
+ )
38
+ # Draw bounding boxes and labels for detections
39
+ for detection in detections:
40
+ score = detection["score"]
41
+ label = detection["label"]
42
+ box = detection["box"]
43
+ x1, y1, x2, y2 = box["xmin"], box["ymin"], box["xmax"], box["ymax"]
44
+ cv2.rectangle(frame_copy, (x1, y1), (x2, y2), (0, 255, 0), 2)
45
+ cv2.putText(
46
+ frame_copy,
47
+ f"{label}: {score:.2f}",
48
+ (x1, y1 - 10),
49
+ cv2.FONT_HERSHEY_SIMPLEX,
50
+ 0.5,
51
+ (0, 255, 0),
52
+ 2
53
+ )
54
+ return frame_copy
55
 
56
+ def save_video(self, frames, output_path, fps=30):
57
+ """Save frames as a video."""
58
+ if not frames:
59
+ return
60
+ height, width, _ = frames[0].shape
61
+ fourcc = cv2.VideoWriter_fourcc(*"mp4v")
62
+ out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
63
+ for frame in frames:
64
+ out.write(frame)
65
+ out.release()