Python opencv writes frames to videos, memcpy errors leading to segmentation errors

asked 2019-09-16 22:42:42 -0500

bob mu gravatar image

updated 2019-09-17 07:16:18 -0500

This is the GDB info:

Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Core was generated by `python /home/yufeng/colonoscopy_py/server.py`.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  __memmove_avx_unaligned ()
    at ../sysdeps/x86_64/multiarch/memcpy-avx-unaligned.S:238
238 ../sysdeps/x86_64/multiarch/memcpy-avx-unaligned.S: No file or directory

This error was thrown by the line "out_avi.write (image)" or "detection_out_avi.write(frame)".

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, out_avi, image):

    out.write(image)
    out_avi.write(image)

def detection(frame, image, detection_out, detection_out_avi):
    # deep learning detect objects.....
    box = (deep learning detection result...)
    # tag image.....
    image = cv2.rectangle(image, (box[0], box[1]), (box[2], box[3]), (0, 255, 0), 2)
    detection_out.write(image)
    detection_out_avi.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)

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

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

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

It's worth mentioning that in the process of code running, there are always a lot of warning before the crash.

Example:

image description

edit retag flag offensive close merge delete

Comments

Can you post snippet code of cv2.VideoWriter and out.write?

supra56 gravatar imagesupra56 ( 2019-09-17 05:51:18 -0500 )edit

thank you, content has been commit, awaiting moderation

bob mu gravatar imagebob mu ( 2019-09-17 07:41:48 -0500 )edit