Skip to content

Instantly share code, notes, and snippets.

Last active October 27, 2020 03:37
Show Gist options
  • Save in03/657313d28863ee09f1d25a536abcac30 to your computer and use it in GitHub Desktop.
Save in03/657313d28863ee09f1d25a536abcac30 to your computer and use it in GitHub Desktop.
Video Blanking Detection
import sys
import os
import cv2
import numpy as np
import time
from tqdm import tqdm
from plyer import notification
# Detection threshold margin in pixels
spatial_thresh = 5 # How many black pixels will trigger detection
lum_thresh = 5 # How dark the black pixels need to be to trigger detection
skip_frames = 2 # Skip a frame unless it's a multiple of...
manually_approve = True # Confirmation prompt on detection?
window_height = 1080 # Vertical height to resize window to if img too small or too large
args = sys.argv
if (len(args) > 2):
print(f"One file at a time please!")
# Create a VideoCapture object and read from input file
cap = cv2.VideoCapture(sys.argv[1])
filename = os.path.basename(sys.argv[1])
w_res = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
h_res = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT ))
fps = int(cap.get(cv2.CAP_PROP_FPS))
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
scale_factor = window_height/h_res
print(f"Video resolution: {w_res}x{h_res}")
print(f"Video framerate: {fps}")
print(f"Video length: {total_frames} frames")
print(f"Window scale factor: {scale_factor}")
x_thresh = spatial_thresh
y_thresh = spatial_thresh
w_thresh = w_res - spatial_thresh
h_thresh = h_res - spatial_thresh
# Check if camera opened successfully
if (cap.isOpened()== False):
print(f"Error opening {filename}")
# Loop variables
count = 0
waiting_reset = False
found = []
# Progress bar
for i in tqdm (range (total_frames), desc="Analysing Video..."):
# Read until video is completed
count += 1
# Capture frame-by-frame
ret, frame =
if ret == True:
if skip_frames:
if (count % skip_frames) == 0:
# Resize frame
frame = cv2.resize(frame, (0, 0), fx=scale_factor, fy=scale_factor, interpolation = cv2.INTER_AREA)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
_,thresh = cv2.threshold(gray, 0, lum_thresh, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnt = contours[0]
x,y,w,h = cv2.boundingRect(cnt)
# tqdm.write(f"X-{x}, Y-{y}, W-{w}, H-{h}")
# tqdm.write(f"Margins: X-{x_thresh}, Y-{y_thresh}, W-{w_thresh}, H-{h_thresh}")
cv2.imshow('Frame', frame)
if x > x_thresh or y > y_thresh or w < w_thresh or h < h_thresh:
if waiting_reset == False:
if manually_approve == True:
cv2.rectangle(frame, (x, y), (w, h), (255, 0, 0), 2)
cv2.imshow('Frame', frame)
check = input(f"\nAt frame {count}: Any blanking visible here?")
check = "y"
if "y" in check.lower():
tqdm.write("Marked frame")
waiting_reset = True
# else:
# tqdm.write("Waiting for next unqiue detection...")
waiting_reset = False
# Press Q on keyboard to exit
if cv2.waitKey(25) & 0xFF == ord('q'):
# Break the loop
print(f"Blanking found in frames: {found}")
# Toast notify
title='Video Blanking Checker',
message=f'Done analysing "{filename}"',
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment