Skip to content

Instantly share code, notes, and snippets.

@tin2tin
Created November 17, 2023 12:15
Show Gist options
  • Save tin2tin/65a0d0595c0ff8a9dc6d94eb57c295d8 to your computer and use it in GitHub Desktop.
Save tin2tin/65a0d0595c0ff8a9dc6d94eb57c295d8 to your computer and use it in GitHub Desktop.
Find clapper board in video
import cv2
import numpy as np
def detect_clapperboard(frame, clapperboard_template):
# Convert the frame to grayscale
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Use template matching to find the clapperboard
result = cv2.matchTemplate(gray_frame, clapperboard_template, cv2.TM_CCOEFF_NORMED)
# Set a threshold for template matching results
threshold = 0.8
loc = np.where(result >= threshold)
# Draw rectangles around the clapperboard matches
for pt in zip(*loc[::-1]):
cv2.rectangle(frame, pt, (pt[0] + clapperboard_w, pt[1] + clapperboard_h), (0, 255, 0), 2)
# Display the frame with clapperboard detection
cv2.imshow('Clapperboard Detection', frame)
# Load your clapperboard template image
clapperboard_template = cv2.imread('clapperboard_template.png', cv2.IMREAD_GRAYSCALE)
# Get the width and height of the template
clapperboard_w, clapperboard_h = clapperboard_template.shape[::-1]
# Inside the main loop
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
detect_clapperboard(frame, clapperboard_template)
if cv2.waitKey(25) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment