Skip to content

Instantly share code, notes, and snippets.

@CoffeeVampir3
Created February 4, 2024 05:47
Show Gist options
  • Save CoffeeVampir3/83972700f358db70f1b0d8999a160481 to your computer and use it in GitHub Desktop.
Save CoffeeVampir3/83972700f358db70f1b0d8999a160481 to your computer and use it in GitHub Desktop.
video stuff
from PIL import Image
import os
import numpy as np
import sys
import cv2
def video_to_frames(video_file, output_dir, one_every_n_frames):
# Create VideoCapture object
vidcap = cv2.VideoCapture(video_file)
fps = int(vidcap.get(cv2.CAP_PROP_FPS))
# Make output directory if it doesn't already exist
os.makedirs(output_dir, exist_ok=True)
success, image = vidcap.read()
count = 0
n = one_every_n_frames
filename = os.path.splitext(os.path.basename(video_file))[0]
while success:
# Save frame as JPEG file
if count % n == 0:
cv2.imwrite(os.path.join(output_dir, f"{filename}{count}.jpg"), image)
success, image = vidcap.read()
count += 1
# Usage
video_to_frames(sys.argv[1], "frames: " + sys.argv[1], int(sys.argv[2]))
import cv2
import numpy as np
import os, sys
import re
def natural_sort_key(s):
return [int(text) if text.isdigit() else text.lower()
for text in re.split(r'(\d+)', s)]
def histogram_similarity(image1, image2):
hist1 = cv2.calcHist([image1], [0, 1, 2], None, [8, 8, 8], [0, 256, 0, 256, 0, 256])
hist2 = cv2.calcHist([image2], [0, 1, 2], None, [8, 8, 8], [0, 256, 0, 256, 0, 256])
cv2.normalize(hist1, hist1)
cv2.normalize(hist2, hist2)
similarity = cv2.compareHist(hist1, hist2, cv2.HISTCMP_CORREL)
return similarity
def detect_scene_changes(image_folder, output_folder, threshold):
images = sorted(os.listdir(image_folder), key=natural_sort_key)
scene_number = 1
frame_number = 1
hist_sim_values = []
scene_hist_sim_values = []
for i in range(len(images) - 1):
current_image = cv2.imread(os.path.join(image_folder, images[i]))
next_image = cv2.imread(os.path.join(image_folder, images[i + 1]))
current_hist_sim = histogram_similarity(current_image, next_image)
hist_sim_values.append(current_hist_sim)
scene_hist_sim_values.append(current_hist_sim)
label = 's{}_p{}'.format(scene_number, frame_number)
cv2.imwrite(os.path.join(output_folder, label + '.jpg'), current_image)
if current_hist_sim < threshold:
print(f"Average histogram similarity for scene {scene_number}: {np.mean(scene_hist_sim_values)}")
scene_number += 1
frame_number = 1
scene_hist_sim_values = []
else:
frame_number += 1
# Handle the last image in the sequence
last_image = cv2.imread(os.path.join(image_folder, images[-1]))
label = 's{}_p{}'.format(scene_number, frame_number)
cv2.imwrite(os.path.join(output_folder, label + '.jpg'), last_image)
if scene_hist_sim_values:
print(f"Average histogram similarity for scene {scene_number}: {np.mean(scene_hist_sim_values)}")
print(f"Total average histogram similarity: {np.mean(hist_sim_values)}")
image_folder = sys.argv[1]
output_folder = sys.argv[2]
os.makedirs(output_folder, exist_ok=True)
threshold = 0.5
detect_scene_changes(image_folder, output_folder, threshold)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment