|
|
import cv2 |
|
|
import os |
|
|
import sys |
|
|
from tqdm import tqdm |
|
|
|
|
|
|
|
|
def capture_slides_frame_diff( |
|
|
video_path, output_dir_path, MIN_PERCENT_THRESH=0.06, ELAPSED_FRAME_THRESH=85 |
|
|
): |
|
|
prev_frame = None |
|
|
curr_frame = None |
|
|
screenshots_count = 0 |
|
|
capture_frame = False |
|
|
frame_elapsed = 0 |
|
|
|
|
|
|
|
|
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (7, 7)) |
|
|
|
|
|
|
|
|
cap = cv2.VideoCapture(video_path) |
|
|
|
|
|
if not cap.isOpened(): |
|
|
print("Unable to open video file: ", video_path) |
|
|
sys.exit() |
|
|
|
|
|
success, first_frame = cap.read() |
|
|
num_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) |
|
|
prog_bar = tqdm(total=num_frames) |
|
|
|
|
|
print("Using frame differencing for Background Subtraction...") |
|
|
print("---" * 10) |
|
|
|
|
|
|
|
|
|
|
|
if success: |
|
|
|
|
|
first_frame_gray = cv2.cvtColor(first_frame, cv2.COLOR_BGR2GRAY) |
|
|
|
|
|
prev_frame = first_frame_gray |
|
|
|
|
|
screenshots_count += 1 |
|
|
|
|
|
filename = f"{screenshots_count:03}.jpg" |
|
|
out_file_path = os.path.join(output_dir_path, filename) |
|
|
|
|
|
|
|
|
cv2.imwrite(out_file_path, first_frame, [cv2.IMWRITE_JPEG_QUALITY, 75]) |
|
|
prog_bar.update(1) |
|
|
|
|
|
|
|
|
while cap.isOpened(): |
|
|
ret, frame = cap.read() |
|
|
if not ret: |
|
|
break |
|
|
|
|
|
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) |
|
|
curr_frame = frame_gray |
|
|
|
|
|
if (prev_frame is not None) and (curr_frame is not None): |
|
|
frame_diff = cv2.absdiff(curr_frame, prev_frame) |
|
|
_, frame_diff = cv2.threshold(frame_diff, 80, 255, cv2.THRESH_BINARY) |
|
|
|
|
|
|
|
|
frame_diff = cv2.dilate(frame_diff, kernel) |
|
|
|
|
|
|
|
|
p_non_zero = (cv2.countNonZero(frame_diff) / (1.0 * frame_gray.size)) * 100 |
|
|
|
|
|
if p_non_zero >= MIN_PERCENT_THRESH and not capture_frame: |
|
|
capture_frame = True |
|
|
elif capture_frame: |
|
|
frame_elapsed += 1 |
|
|
|
|
|
if frame_elapsed >= ELAPSED_FRAME_THRESH: |
|
|
capture_frame = False |
|
|
frame_elapsed = 0 |
|
|
|
|
|
screenshots_count += 1 |
|
|
|
|
|
filename = f"{screenshots_count:03}.jpg" |
|
|
out_file_path = os.path.join(output_dir_path, filename) |
|
|
|
|
|
cv2.imwrite(out_file_path, frame, [cv2.IMWRITE_JPEG_QUALITY, 75]) |
|
|
prog_bar.set_postfix_str(f"Total Screenshots: {screenshots_count}") |
|
|
|
|
|
prev_frame = curr_frame |
|
|
prog_bar.update(1) |
|
|
|
|
|
|
|
|
prog_bar.close() |
|
|
cap.release() |
|
|
|