I want to run some mp4
videos inside a process but only camera feed works. The software gets stuck with no error message. I already tried both and I found this doesn't run. The print is where the code gets stuck.
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()
this runs.
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()
I need an example of mp4 file run in a multiprocessing way.
CodePudding user response:
Here's a minimal working example with optional FPS control. If you need to extract frames from your process back to the main program, you can use multiprocessing.Queue()
to transfer frames since multiprocesses have an independent memory stack.
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()
Related camera/IP/RTSP/streaming, FPS, video, threading, and multiprocessing posts