1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Problem Saving Processed Video and CSV with MoviePy and Mediapipe in Python

Discussão em 'Python' iniciado por Stack, Setembro 14, 2024.

  1. Stack

    Stack Membro Participativo

    I'm encountering an issue where the length of the processed video created using MoviePy is shorter than the corresponding CSV file generated by my Mediapipe-based video processing script. Specifically, the video sometimes ends up being 30 seconds long, while the CSV file records events for up to 45 seconds. The CSV file correctly logs timestamps and states, which I have verified. Additionally, the summary lines at the end of the CSV file are not appearing as expected.

    Here is the relevant portion of my code:

    import time
    import csv
    import cv2
    import mediapipe as mp
    import numpy as np
    import tkinter as tk
    from tkinter import filedialog, messagebox
    import os
    from datetime import datetime
    from moviepy.editor import ImageSequenceClip

    # ... [rest of the code]

    def process_video(self, camera, perclos_thresh, roll_thresh, pitch_thresh, yaw_thresh,
    ear_time_thresh, mar_time_thresh, gaze_time_thresh, pose_time_thresh,
    csv_filename, video_filename):
    # ... [processing code]
    frames_to_video = [] # List to store frames for the video

    # Initializing CSV
    with open(csv_filename, mode='w', newline='') as file:
    writer = csv.writer(file)
    writer.writerow(['Timestamp', 'Cansado', 'Dormindo', 'Olhando Fora', 'Distraído', 'Bocejando'])

    # ... [rest of the processing loop]

    # Finalizing CSV
    with open(csv_filename, mode='a', newline='') as file_append:
    writer_append = csv.writer(file_append)
    # Calculate final durations for active states
    # ... [write summary lines]

    # Create the video from the list of frames using moviepy
    try:
    video_clip = ImageSequenceClip(frames_to_video, fps=real_fps)
    video_clip.write_videofile(video_filename, codec='libx264')
    except Exception as e:
    messagebox.showerror("Erro ao salvar o vídeo", f"Ocorreu um erro ao salvar o vídeo: {e}")

    if __name__ == "__main__":
    app = VideoProcessingApp()
    app.mainloop()



    Issue:

    The video duration is sometimes shorter than the CSV file duration. The final summary lines are not appearing in the CSV file. What I've Tried:

    I've ensured that the CSV file correctly logs timestamps and states.

    Question: Can anyone help identify why the video duration might be shorter than the CSV file and why the summary lines are missing from the CSV file? Are there potential issues in how the frames are collected and saved, or in how the CSV file is being finalized?

    Continue reading...

Compartilhe esta Página