Spaces:
No application file
No application file
Create utils/video_processing.py
Browse files- utils/video_processing.py +38 -0
utils/video_processing.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import tempfile
|
| 3 |
+
from utils.fault_detection import detect_faults
|
| 4 |
+
from utils.image_processing import preprocess_image
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
def process_video(video_path, frame_interval=30):
|
| 8 |
+
cap = cv2.VideoCapture(video_path)
|
| 9 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 10 |
+
frame_count = 0
|
| 11 |
+
fault_report = []
|
| 12 |
+
annotated_frames = []
|
| 13 |
+
|
| 14 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 15 |
+
temp_out = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
|
| 16 |
+
out = cv2.VideoWriter(temp_out.name, fourcc, fps, (int(cap.get(3)), int(cap.get(4))))
|
| 17 |
+
|
| 18 |
+
while cap.isOpened():
|
| 19 |
+
ret, frame = cap.read()
|
| 20 |
+
if not ret:
|
| 21 |
+
break
|
| 22 |
+
|
| 23 |
+
if frame_count % frame_interval == 0:
|
| 24 |
+
processed = preprocess_image(frame)
|
| 25 |
+
faults, annotated = detect_faults(processed)
|
| 26 |
+
for f in faults:
|
| 27 |
+
f["frame"] = frame_count
|
| 28 |
+
fault_report.append(f)
|
| 29 |
+
out.write(annotated)
|
| 30 |
+
else:
|
| 31 |
+
out.write(frame)
|
| 32 |
+
|
| 33 |
+
frame_count += 1
|
| 34 |
+
|
| 35 |
+
cap.release()
|
| 36 |
+
out.release()
|
| 37 |
+
|
| 38 |
+
return temp_out.name, fault_report
|