Spaces:
Sleeping
Sleeping
Create utils/video_processor.py
Browse files- utils/video_processor.py +86 -0
utils/video_processor.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
from skimage.metrics import structural_similarity
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
def compare_videos(video1_path, video2_path):
|
| 8 |
+
# Open video files
|
| 9 |
+
vidcap1 = cv2.VideoCapture(video1_path)
|
| 10 |
+
vidcap2 = cv2.VideoCapture(video2_path)
|
| 11 |
+
|
| 12 |
+
# Check if videos opened successfully
|
| 13 |
+
if not vidcap1.isOpened() or not vidcap2.isOpened():
|
| 14 |
+
raise ValueError("Error opening video files.")
|
| 15 |
+
|
| 16 |
+
# Get video properties
|
| 17 |
+
fps1 = vidcap1.get(cv2.CAP_PROP_FPS)
|
| 18 |
+
fps2 = vidcap2.get(cv2.CAP_PROP_FPS)
|
| 19 |
+
frame_count1 = int(vidcap1.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 20 |
+
frame_count2 = int(vidcap2.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 21 |
+
|
| 22 |
+
# Ensure videos have same duration (frame count)
|
| 23 |
+
if frame_count1 != frame_count2 or abs(fps1 - fps2) > 0.1:
|
| 24 |
+
vidcap1.release()
|
| 25 |
+
vidcap2.release()
|
| 26 |
+
raise ValueError("Videos must have the same duration and frame rate.")
|
| 27 |
+
|
| 28 |
+
# Initialize lists for SSIM scores
|
| 29 |
+
ssim_scores = []
|
| 30 |
+
frame_number = 0
|
| 31 |
+
|
| 32 |
+
# Video writer for difference video
|
| 33 |
+
width = int(vidcap1.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 34 |
+
height = int(vidcap1.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 35 |
+
out = cv2.VideoWriter(
|
| 36 |
+
"utils/output/difference_video.mp4",
|
| 37 |
+
cv2.VideoWriter_fourcc(*"mp4v"),
|
| 38 |
+
fps1,
|
| 39 |
+
(width, height),
|
| 40 |
+
isColor=True
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
while True:
|
| 44 |
+
success1, image1 = vidcap1.read()
|
| 45 |
+
success2, image2 = vidcap2.read()
|
| 46 |
+
|
| 47 |
+
if not success1 or not success2:
|
| 48 |
+
break
|
| 49 |
+
|
| 50 |
+
# Resize second video frame to match first video's resolution
|
| 51 |
+
image2 = cv2.resize(image2, (width, height))
|
| 52 |
+
|
| 53 |
+
# Convert to grayscale
|
| 54 |
+
image1_gray = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
|
| 55 |
+
image2_gray = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
|
| 56 |
+
|
| 57 |
+
# Compute SSIM
|
| 58 |
+
score, diff = structural_similarity(image1_gray, image2_gray, full=True)
|
| 59 |
+
ssim_scores.append((1 - score) * 100) # Convert to percentage difference
|
| 60 |
+
|
| 61 |
+
# Convert difference to 8-bit unsigned for visualization
|
| 62 |
+
diff = (diff * 255).astype("uint8")
|
| 63 |
+
diff_color = cv2.cvtColor(diff, cv2.COLOR_GRAY2BGR)
|
| 64 |
+
|
| 65 |
+
# Write to output video
|
| 66 |
+
out.write(diff_color)
|
| 67 |
+
|
| 68 |
+
frame_number += 1
|
| 69 |
+
|
| 70 |
+
# Release resources
|
| 71 |
+
vidcap1.release()
|
| 72 |
+
vidcap2.release()
|
| 73 |
+
out.release()
|
| 74 |
+
|
| 75 |
+
# Plot differences
|
| 76 |
+
plt.figure(figsize=(10, 6))
|
| 77 |
+
plt.plot(range(frame_number), ssim_scores, 'ro-', markersize=2)
|
| 78 |
+
plt.title("Frame-by-Frame Difference (SSIM)")
|
| 79 |
+
plt.xlabel("Frame Number")
|
| 80 |
+
plt.ylabel("Difference (%)")
|
| 81 |
+
plt.grid(True)
|
| 82 |
+
plot_path = "utils/output/difference_plot.png"
|
| 83 |
+
plt.savefig(plot_path)
|
| 84 |
+
plt.close()
|
| 85 |
+
|
| 86 |
+
return plot_path, "utils/output/difference_video.mp4"
|