qianqianwang68 / omnimotion

Apache License 2.0
2.07k stars 121 forks source link

Best Practices JPG #21

Open Ryandonofrio3 opened 10 months ago

Ryandonofrio3 commented 10 months ago

This may be a silly question but I am looking to use this on videos. Yet the structure seems to be only .jpgs. I wrote these:

To downscale and then convert video to img's as that is the structure in the preprocessing read me. Does this make sense? Or should i be training on the videos?

` import cv2 import os from moviepy.editor import VideoFileClip

def downscale_video(video_path, output_path): cap = cv2.VideoCapture(video_path) fourcc = cv2.VideoWriter_fourcc(*"mp4v") out = cv2.VideoWriter(output_path, fourcc, 30.0, (640, 480))

while True:
    ret, frame = cap.read()
    if not ret:
        break
    resized_frame = cv2.resize(frame, (640, 480))
    out.write(resized_frame)

cap.release()
out.release()

def extract_60_frame_clips(video_path, output_dir, video_name): with VideoFileClip(video_path) as clip: duration = clip.duration fps = clip.fps n_frames = int(fps * duration)

    for i in range(0, n_frames, 60):
        start = i / fps
        end = min((i + 60) / fps, duration)
        short_clip = clip.subclip(start, end)
        short_clip.write_videofile(f"{output_dir}/{video_name}_{i//60}.mp4")

input_dir = "input" output_dir = "output_clips" temp_dir = "temp"

if not os.path.exists(output_dir): os.makedirs(output_dir) if not os.path.exists(temp_dir): os.makedirs(temp_dir)

for video_file in os.listdir(input_dir): if video_file.endswith(".mov"): video_path = os.path.join(input_dir, video_file) video_name = os.path.splitext(video_file)[0] temp_path = os.path.join(temp_dir, f"{video_name}_temp.mp4")

    downscale_video(video_path, temp_path)
    extract_60_frame_clips(temp_path, output_dir, video_name)

def extract_frames(video_path, output_folder, sequence_name): cap = cv2.VideoCapture(video_path) frame_count = 0 saved_frame_count = 0 # Counter for saved frames

while True:
    ret, frame = cap.read()
    if not ret:
        break

    if frame_count % 10 == 0:  # Save only every 10th frame
        frame_filename = os.path.join(
            output_folder, f"{sequence_name}_{saved_frame_count:05d}.jpg"
        )
        cv2.imwrite(frame_filename, frame)
        saved_frame_count += 1  # Increment the counter for saved frames

    frame_count += 1  # Increment the overall frame counter

cap.release()

video_folder = "output_clips" # Your output directory containing MP4 clips base_folder = "sequence_name" # Base folder for sequences

color_folder = os.path.join(base_folder, "color") # Folder to save JPEG frames

os.makedirs(color_folder, exist_ok=True)

for video_file in os.listdir(video_folder): if video_file.endswith(".mp4"): video_path = os.path.join(video_folder, video_file) sequence_name = os.path.splitext(video_file)[0]

    extract_frames(video_path, color_folder, sequence_name)

`

Is this proper practice for this? Sorry this is my first CNN! Thanks for any help