lokesh341 commited on
Commit
6835043
·
verified ·
1 Parent(s): 9c205dd

Update services/video_service.py

Browse files
Files changed (1) hide show
  1. services/video_service.py +9 -10
services/video_service.py CHANGED
@@ -6,7 +6,7 @@ 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
- FRAME_SKIP = 5 # Increased frame skipping for faster processing
10
 
11
  def get_next_video_frame():
12
  global cap, video_index
@@ -16,6 +16,8 @@ def get_next_video_frame():
16
 
17
  if cap is None or not cap.isOpened():
18
  cap = cv2.VideoCapture(video_files[video_index])
 
 
19
 
20
  for _ in range(FRAME_SKIP):
21
  ret, frame = cap.read()
@@ -23,18 +25,15 @@ def get_next_video_frame():
23
  cap.release()
24
  video_index = (video_index + 1) % len(video_files)
25
  cap = cv2.VideoCapture(video_files[video_index])
 
 
26
  ret, frame = cap.read()
27
  if not ret:
28
  raise RuntimeError(f"Cannot read video {video_files[video_index]}")
29
  break
30
 
31
- # Resize frame to a smaller resolution for faster processing
32
- frame = cv2.resize(frame, (320, 240)) # Reduced resolution
33
- return frame
34
 
35
- def reset_video_index():
36
- global video_index, cap
37
- video_index = 0
38
- if cap is not None:
39
- cap.release()
40
- cap = None
 
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
+ FRAME_SKIP = 5
10
 
11
  def get_next_video_frame():
12
  global cap, video_index
 
16
 
17
  if cap is None or not cap.isOpened():
18
  cap = cv2.VideoCapture(video_files[video_index])
19
+ if not cap.isOpened():
20
+ raise RuntimeError(f"Failed to open video file: {video_files[video_index]}")
21
 
22
  for _ in range(FRAME_SKIP):
23
  ret, frame = cap.read()
 
25
  cap.release()
26
  video_index = (video_index + 1) % len(video_files)
27
  cap = cv2.VideoCapture(video_files[video_index])
28
+ if not cap.isOpened():
29
+ raise RuntimeError(f"Failed to open video file: {video_files[video_index]}")
30
  ret, frame = cap.read()
31
  if not ret:
32
  raise RuntimeError(f"Cannot read video {video_files[video_index]}")
33
  break
34
 
35
+ if frame is None or frame.size == 0:
36
+ raise RuntimeError("Empty frame received. Check camera or video file.")
 
37
 
38
+ frame = cv2.resize(frame, (320, 240))
39
+ return frame