|
| 1 | +import cv2 |
| 2 | + |
| 3 | +from typing import Annotated |
| 4 | + |
| 5 | +from typer import Argument, Option |
| 6 | +from ..app import app |
| 7 | + |
| 8 | + |
| 9 | +def find_change_from_start_inner( |
| 10 | + video_path, threshold=0, min_changed_pixels=50 |
| 11 | +) -> tuple[int, float]: |
| 12 | + """ |
| 13 | + Consumes video one frame at a time to find where it diverges from Frame 1. |
| 14 | +
|
| 15 | + Args: |
| 16 | + video_path (str): Path to video file. |
| 17 | + threshold (int): Sensitivity (0-255). Lower = detects subtle changes. |
| 18 | + Higher = ignores compression artifacts. |
| 19 | + min_changed_pixels (int): How many pixels must change to trigger detection. |
| 20 | +
|
| 21 | + Returns: |
| 22 | + tuple[int, float]: Frame number where change is detected and FPS of the video. |
| 23 | + Returns -1 if no significant change is found. |
| 24 | + """ |
| 25 | + |
| 26 | + # 1. Open the video stream |
| 27 | + cap = cv2.VideoCapture(video_path) |
| 28 | + |
| 29 | + try: |
| 30 | + if not cap.isOpened(): |
| 31 | + raise FileNotFoundError(f"Could not open video file: {video_path}") |
| 32 | + |
| 33 | + # 2. Read Frame 1 (The Reference) |
| 34 | + ret, frame1 = cap.read() |
| 35 | + if not ret: |
| 36 | + raise ValueError("Video file is empty or unreadable.") |
| 37 | + |
| 38 | + # Convert to grayscale to reduce complexity and ignore color noise |
| 39 | + frame1_gray = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY) |
| 40 | + |
| 41 | + # Optional: Apply slight blur to reduce compression artifact noise |
| 42 | + frame1_gray = cv2.GaussianBlur(frame1_gray, (21, 21), 0) |
| 43 | + |
| 44 | + frame_count = 1 |
| 45 | + fps = cap.get(cv2.CAP_PROP_FPS) |
| 46 | + |
| 47 | + # 3. Loop through the stream one frame at a time |
| 48 | + while True: |
| 49 | + # returns (bool, numpy_array) |
| 50 | + ret, current_frame = cap.read() |
| 51 | + |
| 52 | + # If no frame is returned, we reached the end of the video |
| 53 | + if not ret: |
| 54 | + return -1, fps # No significant change found |
| 55 | + |
| 56 | + frame_count += 1 |
| 57 | + |
| 58 | + # Convert current frame to grayscale |
| 59 | + current_gray = cv2.cvtColor(current_frame, cv2.COLOR_BGR2GRAY) |
| 60 | + current_gray = cv2.GaussianBlur(current_gray, (21, 21), 0) |
| 61 | + |
| 62 | + # 4. Compute Absolute Difference between Frame 1 and Current Frame |
| 63 | + # This creates an image where black pixels means "no change" |
| 64 | + # and white pixels means "change" |
| 65 | + delta = cv2.absdiff(frame1_gray, current_gray) |
| 66 | + |
| 67 | + # 5. Apply Threshold |
| 68 | + # Any pixel difference < threshold becomes 0 (black). |
| 69 | + # Any pixel difference > threshold becomes 255 (white). |
| 70 | + thresh_img = cv2.threshold(delta, threshold, 255, cv2.THRESH_BINARY)[1] |
| 71 | + |
| 72 | + # 6. Check if enough pixels have changed |
| 73 | + # We count the white pixels in the threshold image |
| 74 | + changed_pixels = cv2.countNonZero(thresh_img) |
| 75 | + |
| 76 | + if changed_pixels > min_changed_pixels: |
| 77 | + # Optional: Save the frame to verify |
| 78 | + # cv2.imwrite(f"change_detected_frame_{frame_count}.jpg", current_frame) |
| 79 | + return frame_count, fps |
| 80 | + |
| 81 | + # 7. Release resources |
| 82 | + finally: |
| 83 | + cap.release() |
| 84 | + |
| 85 | + |
| 86 | +@app.command() |
| 87 | +def find_change_from_start( |
| 88 | + video_path: Annotated[ |
| 89 | + str, |
| 90 | + Argument( |
| 91 | + exists=True, |
| 92 | + file_okay=True, |
| 93 | + dir_okay=False, |
| 94 | + readable=True, |
| 95 | + resolve_path=True, |
| 96 | + help="Path to the video file to analyze.", |
| 97 | + ), |
| 98 | + ], |
| 99 | + threshold: int = Option( |
| 100 | + default=0, |
| 101 | + help="Sensitivity (0-255). Lower = detects subtle changes. Higher = ignores compression artifacts.", |
| 102 | + ), |
| 103 | + min_changed_pixels: int = Option( |
| 104 | + default=50, |
| 105 | + help="How many pixels must change to trigger detection.", |
| 106 | + ), |
| 107 | +): |
| 108 | + """Find where a video diverges from its first frame. |
| 109 | +
|
| 110 | + :param video_path: Path to the video file to analyze. |
| 111 | + :param threshold: Sensitivity (0-255). Lower = detects subtle changes. Higher = ignores compression artifacts. |
| 112 | + :param min_changed_pixels: How many pixels must change to trigger detection. |
| 113 | + """ |
| 114 | + frame_diff_at, fps = find_change_from_start_inner( |
| 115 | + video_path=video_path, |
| 116 | + threshold=threshold, |
| 117 | + min_changed_pixels=min_changed_pixels, |
| 118 | + ) |
| 119 | + frame_diff_at_secs = frame_diff_at / fps if frame_diff_at != -1 else -1 |
| 120 | + if frame_diff_at == -1: |
| 121 | + print("No significant change detected in the video.") |
| 122 | + else: |
| 123 | + print( |
| 124 | + f"Significant change detected at frame {frame_diff_at} ({frame_diff_at_secs} seconds)." |
| 125 | + ) |
0 commit comments