Browse Source

cleanedup automation

master
cailean 2 months ago
parent
commit
b6d6990dd1
  1. 52
      app.py
  2. 47
      process_video.py

52
app.py

@ -4,7 +4,7 @@ import os
import json
import numpy as np
folderPath = "videos/rte-archive-football"
baseFolderPath = "videos"
output_file = "embeddings.json"
embeddings = []
@ -23,11 +23,14 @@ backends = [
]
def main():
image_paths = setup()
run(image_paths)
video_folders = setup()
for folder_info in video_folders:
foldername, video_path, image_paths = folder_info
print(1)
run(image_paths, foldername, video_path)
saveToJson()
def processImage(image, idx):
def processImage(image, idx, foldername, video_path):
try:
anaylse_obj = DeepFace.analyze(
@ -46,31 +49,50 @@ def processImage(image, idx):
normalised_emotions = normalised_emotions.tolist()
entry = {
"index": idx,
"filename": os.path.basename(image), # Extract the filename from the path
"folder": foldername,
"frame": image, # Extract the filename from the path
"video": video_path,
"vector": normalised_emotions
}
# Add the entry to the JSON data list
embeddings.append(entry)
except:
pass
print("Could not locate face in image. Skipping.")
def run(image_paths):
for idx, image in enumerate(image_paths):
processImage(image, idx)
def setup():
# List of all images in the specified folder
image_files = glob.glob(os.path.join(folderPath, '*'))
video_folders = []
subfolders = [f.path for f in os.scandir(baseFolderPath) if f.is_dir()]
for subfolder in subfolders:
frames_folder_path = os.path.join(subfolder, "frames")
# Find the video file in the subfolder
video_files = [f for f in os.listdir(subfolder) if f.endswith(('.mp4', '.avi', '.mov'))]
if video_files:
video_path = os.path.join(subfolder, video_files[0])
# List of all image files in the frames folder
image_files = glob.glob(os.path.join(frames_folder_path, '*'))
# Optional: Filter by specific image extensions (e.g., .jpg, .png)
# Filter by specific image extensions (e.g., .jpg, .png)
image_paths = [img for img in image_files if img.endswith(('.jpg', '.jpeg', '.png', '.bmp', '.tiff', '.gif'))]
# Sort the image paths by filename
image_paths.sort(key=lambda x: os.path.basename(x))
# Sort the image paths by filename (assuming the filenames are numerically ordered)
image_paths.sort(key=lambda x: int(os.path.splitext(os.path.basename(x))[0]))
return image_paths
# Add the information to the list
video_folders.append((os.path.basename(subfolder), video_path, image_paths))
return video_folders
def run(image_paths, foldername, video_path):
for idx, image in enumerate(image_paths):
processImage(image, idx, foldername, video_path)
def l2_normalize(vector):
norm = np.linalg.norm(vector)

47
process_video.py

@ -1,6 +1,26 @@
import os
import subprocess
import re
import shutil
def convert_video(video_path, output_path):
print(f"Converting video, {video_path}")
# Codec that works with openframeworks!
command = [
"ffmpeg",
"-i", video_path, # Input file
"-c:v", "libx264", # Video codec
"-profile:v", "high", # Video profile
"-c:a", "copy", # Copy audio without re-encoding
output_path # Output file
]
results = subprocess.run(command, stderr=subprocess.PIPE, text=True)
output = results.stderr
print(f"Video converted {output}")
def get_frame_rate(video_path):
"""
@ -34,7 +54,7 @@ def extract_frames_from_video(video_path, output_folder):
'-i', video_path, # Input file
'-vf', 'fps=1', # Extract 1 frame per second
'-vsync', 'vfr', # Variable frame rate
os.path.join(output_folder, 'frame_%04d.png') # Output pattern
os.path.join(output_folder, 'frame_%09d.png') # Output pattern
]
subprocess.run(command, check=True)
@ -49,7 +69,7 @@ def rename_frames(output_folder, frame_rate):
original_frame_number = int(i * frame_rate)
# Create the new filename with the original frame number
new_filename = f"frame_{original_frame_number:05d}.png"
new_filename = f"{original_frame_number:09d}.png"
os.rename(
os.path.join(output_folder, filename),
os.path.join(output_folder, new_filename)
@ -61,17 +81,32 @@ def process_videos_in_folder(folder_path):
"""
for filename in os.listdir(folder_path):
if filename.lower().endswith(('.mp4', '.avi', '.mov', '.mkv', '.flv')):
video_path = os.path.join(folder_path, filename)
output_folder = os.path.join(folder_path, os.path.splitext(filename)[0]) # Create a folder for each video
# New location for .mp4
video_copy_location = os.path.join(output_folder, filename)
# Create frames folder
frames_folder = os.path.join(output_folder, "frames")
if not os.path.exists(output_folder):
os.mkdir(output_folder)
if not os.path.exists(frames_folder):
os.mkdir(frames_folder)
print(f'Processing video: {video_path}')
try:
frame_rate = get_frame_rate(video_path)
convert_video(video_path, video_copy_location)
frame_rate = get_frame_rate(video_copy_location)
print(f'Original frame rate: {frame_rate} fps')
extract_frames_from_video(video_path, output_folder)
rename_frames(output_folder, frame_rate)
print(f'Frames saved and renamed to: {output_folder}')
extract_frames_from_video(video_copy_location, frames_folder)
rename_frames(frames_folder, frame_rate)
print(f'Frames saved and renamed to: {frames_folder}')
except ValueError as e:
print(f'Error processing video {video_path}: {e}')

Loading…
Cancel
Save