Created
June 27, 2024 17:24
-
-
Save langheran/c02f472f7b81a02aaf9ae5c15cfa8de0 to your computer and use it in GitHub Desktop.
C:\Users\NisimHurst\NDS\scripts\extract_presentation_youtube\youtube_presentation.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# %% | |
# Importing necessary libraries | |
import cv2 | |
from skimage.metrics import structural_similarity as ssim | |
import numpy as np | |
import os | |
from pptx import Presentation | |
from pptx.util import Inches | |
# %% | |
# Function to compare frames using SSIM | |
def is_different(frame1, frame2, threshold=0.5): | |
gray_frame1 = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY) | |
gray_frame2 = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY) | |
score, _ = ssim(gray_frame1, gray_frame2, full=True) | |
return score < threshold | |
# %% | |
# Load the video | |
video_path = 'input.mp4' | |
cap = cv2.VideoCapture(video_path) | |
frame_rate = cap.get(cv2.CAP_PROP_FPS) | |
total_frames = cap.get(cv2.CAP_PROP_FRAME_COUNT) | |
interval = int(frame_rate) # Extract one frame per second | |
print(interval) | |
# %% | |
# Extract frames | |
from tqdm import tqdm | |
success, frame = cap.read() | |
count = 0 | |
different_frames = [] | |
prev_frame=False | |
pbar = tqdm(total=total_frames) | |
while success: | |
if (count % (interval*5)) == 0: | |
if prev_frame: | |
if is_different(frame, prev_frame): | |
different_frames.append(frame) | |
else: | |
different_frames.append(frame) | |
# print("different_frames: ", len(different_frames), "count:", count) | |
pbar.update(interval*5) | |
# print(count/total_frames*100) | |
success, frame = cap.read() | |
count += 1 | |
pbar.close() | |
# %% | |
# Save different frames | |
output_folder = 'output_frames' | |
os.makedirs(output_folder, exist_ok=True) | |
for i, frame in enumerate(different_frames): | |
cv2.imwrite(f'{output_folder}/frame_{i}.jpg', frame) | |
# %% | |
# Create PowerPoint presentation | |
prs = Presentation() | |
for i in range(len(different_frames)): | |
slide = prs.slides.add_slide(prs.slide_layouts[5]) | |
img_path = f'{output_folder}/frame_{i}.jpg' | |
slide.shapes.add_picture(img_path, Inches(1), Inches(1), width=Inches(8), height=Inches(6)) | |
prs.save('video_frames_presentation.pptx') | |
print("Presentation created successfully.") | |
# %% | |
# | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment