Skip to content

When calling the VideoWrite.write method, Segmentation fault occurred #244

@mujinbao

Description

@mujinbao

@skvark @native-api

This is my code.

import os
import cv2
from vidgear.gears import WriteGear
from concurrent.futures import ThreadPoolExecutor

# init variable
w1, h1, w2, h2 = 1920, 1080, 1920, 1080
fps1, fps2 = 28, 18


def cv_init(channel):
    cap = cv2.VideoCapture(channel)

    # ---------- create avi video out --------------
    code = cv2.VideoWriter_fourcc('X', 'V', 'I', 'D')
    out_avi = cv2.VideoWriter(os.path.join('video','original.avi'), code, 25, (w1, h1), True)
    detection_avi_out = cv2.VideoWriter(os.path.join('video','detection.avi'), code, fps2, (w2, h2), True)

    # ---------- create h264 of mp4 video out ---------------
    output_params = {"-vcodec": "libx264", "-input_framerate": str(fps1), "-crf":28, "-preset":"ultrafast", "-tune":"zerolatency"}
    detection_output_params = {"-vcodec": "libx264", "-input_framerate": str(fps2), "-crf":28, "-preset":"ultrafast", "-tune":"zerolatency"}
    out = WriteGear(output_filename=os.path.join('video', 'original.mp4'),
                compression_mode=True,
                **output_params)
    detection_out = WriteGear(output_filename=os.path.join('video', 'detection.mp4'),
                          compression_mode=True,
                          **detection_output_params)


    return cap, out, detection_out, out_avi, detection_avi_out


def write_video(out, image):

    out.write(image)


if __name__ == '__main__':

    cap, out, detection_out, out_avi, detection_avi_out = cv_init('{{video channel}}')
    # create thread pool
    pool = ThreadPoolExecutor(max_workers=1500)
    while cap.isOpened():

        ret, frame = cap.read()

        # relevant algorithms processing for pictures.....
        # pool.submit(compute_image, frame)

        # deep learning detection target
        # pool.submit(detection, frame)

        pool.submit(write_video, out, frame)
        pool.submit(write_video, out_avi, frame)

        pool.submit(write_video, detection_out, frame)
        pool.submit(write_video, detection_avi_out, frame)

Allow me to explain this function code:
According to the needs of Party A, I need to create four video output objects to save the original image and the image after deep learning recognition, and video formats require both AVI and mp4.
As the latency requirement was minimized, I had to use thread pool to optimize processing speed.
Therefore, in the "write_video" function, the same image is written twice, and then the image is detected by in-depth learning. The new image after detection is also written twice. This code also happens "core dump".

With GDB debugging, this error not seem to occur, but only in the case of fast execution.

About vidgear.WriteGear :
link: https://github.com/abhiTronix/vidgear
WriteGear can call ffmpeg to complete video coding. I used this to create MP4 video and encoding it as H264,After my test, writing only MP4 won't cause a segment fault

Please help me, thank you very much~!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions