Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove adjacent duplicate frames #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions video2pdfslides.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import img2pdf
import glob
import argparse
from skimage.metrics import structural_similarity

############# Define constants

Expand All @@ -18,6 +19,7 @@
DETECT_SHADOWS = False # If true, the algorithm will detect shadows and mark them.
MIN_PERCENT = 0.1 # min % of diff between foreground and background to detect if motion has stopped
MAX_PERCENT = 3 # max % of diff between foreground and background to detect if frame is still in motion
SSIM_THRESHOLD = 0.9 # SSIM threshold of two consecutive frame


def get_frames(video_path):
Expand Down Expand Up @@ -71,6 +73,7 @@ def detect_unique_screenshots(video_path, output_folder_screenshot_path):
(W, H) = (None, None)

screenshoots_count = 0
last_screenshot_file_path = ""
for frame_count, frame_time, frame in get_frames(video_path):
orig = frame.copy() # clone the original frame (so we can save it later),
frame = imutils.resize(frame, width=600) # resize the frame
Expand All @@ -94,9 +97,18 @@ def detect_unique_screenshots(video_path, output_folder_screenshot_path):
filename = f"{screenshoots_count:03}_{round(frame_time/60, 2)}.png"

path = os.path.join(output_folder_screenshot_path, filename)
print("saving {}".format(path))
cv2.imwrite(path, orig)
screenshoots_count += 1

image_ssim = 0.0;
if last_screenshot_file_path != "":
image_last = cv2.imread(last_screenshot_file_path)
image_ssim = structural_similarity(image_last, orig, channel_axis=2, data_range=255)

if image_ssim < SSIM_THRESHOLD:
print("saving {}".format(path))
cv2.imwrite(path, orig)
last_screenshot_file_path = path
screenshoots_count += 1


# otherwise, either the scene is changing or we're still in warmup
# mode so let's wait until the scene has settled or we're finished
Expand Down