如何在Python-OpenCV中并行化帧处理?

时间:2018-12-17 23:04:24

标签: python opencv

我阅读了有关它的其他问题,但似乎无法理解。 在Python中,我正在使用OpenCV进行车道检测脚本,我想在串行处理和并行处理之间进行比较。

考虑以下功能,我对两种不同的方法进行了说明:
注意: ApplyHoughLines只是从openCV调用cv2.HoughLines并返回lines

  1. 并行化整个FrameProcessing()函数。
  2. 仅并行应用ApplyHoughLines()函数,因为它是我使用的其他函数中计算量最大的函数(至少我认为是这样)。

什么是最佳解决方案?

到目前为止,我尝试的是使用Pool中的multiprocessing类,如下面的代码所示,还添加了if __name__ == "__main__":,就像其他人在运气不好时所说的那样,我仍然可以:RuntimeError: An attempt has been made to start a new process before the current process has finished its bootstrapping phase.
我迷路了很多,我想念什么?

    #some import statement
    video = cv2.VideoCapture("SourceVideo/Driving1.mp4")
    pool = mp.Pool(processes=4)

    def FrameProcessing(clip):

        while clip.isOpened():
            ret, frame = clip.read()

        if frame is None:
            break

        # Transform current frame into grayscale
        grayscale = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        # Apply Canny Edge Detection to current frame the two value are detection thresholds
        canny = cv2.Canny(grayscale, 100, 200)

        # Crop the frame, it returns the frame containing only the region of interest
        canny = CropFrame(canny)

        # Trying to parallelize
        lines = [pool.apply(ApplyHoughLines, args=(canny,))]

        if lines is None:
            continue

        for x1, y1, x2, y2 in lines[0]:
            cv2.line(lines, (x1, y1), (x2, y2), (0, 255, 0), 2)

        frame = LinesDrawer(frame, lines)

        canny = cv2.cvtColor(canny, cv2.COLOR_GRAY2BGR)
        merged_frame = np.concatenate((frame, canny), axis=0)

        merged_frame = cv2.resize(merged_frame, (1280, 720))

        cv2.imshow('merged_frame', merged_frame)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    def main():
        FrameProcessing(video)

    if __name__ == "__main__":
        main()

0 个答案:

没有答案