Sirivennela commited on
Commit
0c1183e
·
verified ·
1 Parent(s): 91a0ae1

Create video_utils.py

Browse files
Files changed (1) hide show
  1. video_utils.py +49 -0
video_utils.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import os
3
+ from PIL import Image
4
+ from model import predict_fault
5
+
6
+ def extract_frames(video_path, interval=30):
7
+ cap = cv2.VideoCapture(video_path)
8
+ frames = []
9
+ frame_count = 0
10
+
11
+ while cap.isOpened():
12
+ ret, frame = cap.read()
13
+ if not ret:
14
+ break
15
+
16
+ if frame_count % interval == 0:
17
+ img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
18
+ pil_img = Image.fromarray(img)
19
+ frames.append((frame_count, pil_img))
20
+
21
+ frame_count += 1
22
+
23
+ cap.release()
24
+ return frames
25
+
26
+ def annotate_video(video_path, predictions: dict, output_path="output.mp4"):
27
+ cap = cv2.VideoCapture(video_path)
28
+ width = int(cap.get(3))
29
+ height = int(cap.get(4))
30
+ fps = cap.get(cv2.CAP_PROP_FPS)
31
+
32
+ out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height))
33
+ frame_index = 0
34
+
35
+ while cap.isOpened():
36
+ ret, frame = cap.read()
37
+ if not ret:
38
+ break
39
+
40
+ if frame_index in predictions:
41
+ label = predictions[frame_index]
42
+ cv2.putText(frame, label, (10, 40), cv2.FONT_HERSHEY_SIMPLEX, 1.2, (0, 0, 255), 2)
43
+
44
+ out.write(frame)
45
+ frame_index += 1
46
+
47
+ cap.release()
48
+ out.release()
49
+ return output_path