Python OpenCV 多处理 cv2.VideoCapture mp4

Python OpenCV multiprocessing cv2.VideoCapture mp4

我想在进程中 运行 一些 mp4 视频,但只有摄像头源有效。该软件卡住,没有错误消息。我已经尝试了两者,但我发现这不会 运行。打印是代码卡住的地方。

import cv2
import multiprocessing

dispW=640
dispH=480

# Camera inputs 

cap=cv2.VideoCapture('/home/kc/Downloads/darknet/1.mp4')
cap.set(cv2.CAP_PROP_FRAME_WIDTH, dispW)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, dispH)

#cv2.namedWindow("Window")

def c1():
    global cap
    while True:
        print('here')
        success, img = cap.read()
        #ret, frame1 = cap1.read()
        #frame2 = numpy.hstack((frame,frame1))
        print('here')
        cv2.imshow("Window2", img)

        #This breaks on 'q' key
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()

tCap1 = multiprocessing.Process(target=c1)
tCap1.start()

这个运行s.

import cv2
import multiprocessing

dispW=640
dispH=480

# Camera inputs 

cap=cv2.VideoCapture('/dev/video0')
cap.set(cv2.CAP_PROP_FRAME_WIDTH, dispW)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, dispH)


#cv2.namedWindow("Window")
def c1():
    global cap
    while True:
        success, img = cap.read()
        #ret, frame1 = cap1.read()
        #frame2 = numpy.hstack((frame,frame1))
        print('here')
        cv2.imshow("Window2", img)

        #This breaks on 'q' key
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()

tCap1 = multiprocessing.Process(target=c1)
tCap1.start()

我需要一个多处理方式的 mp4 文件示例 运行。

这是一个带有可选 FPS 控制的最小工作示例。如果你需要从你的进程中提取帧返回到主程序,你可以使用 multiprocessing.Queue() 来传输帧,因为多进程有一个独立的内存堆栈。

import multiprocessing as mp
import cv2, time

def capture_frames():
    src = 'test.mp4'
    capture = cv2.VideoCapture(src)
    capture.set(cv2.CAP_PROP_BUFFERSIZE, 2)

    # FPS = 1/X, X = desired FPS
    FPS = 1/120
    FPS_MS = int(FPS * 1000)

    while True:
        # Ensure camera is connected
        if capture.isOpened():
            (status, frame) = capture.read()
            
            # Ensure valid frame
            if status:
                cv2.imshow('frame', frame)
            else:
                break
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
            time.sleep(FPS)

    capture.release()
    cv2.destroyAllWindows()

if __name__ == '__main__':
    print('Starting video stream')
    capture_process = mp.Process(target=capture_frames, args=())
    capture_process.start()

相关 camera/IP/RTSP/streaming、FPS、视频、线程和多处理帖子

  1. Video Streaming from IP Camera in Python Using OpenCV cv2.VideoCapture