検索

Pipe使うと子プロセスのimshowが正常に動かない問題【知恵袋】

うまく動くコード

Managerでプロセス間通信

from __future__ import print_function
from imutils.video import WebcamVideoStream
from imutils.video import FPS
import imutils,cv2,time
import numpy as np
from multiprocessing import Pipe, Process, Manager


def img_get(pipe):
    print("[INFO] sampling THREADED frames from webcam...")
    vs = WebcamVideoStream(src=0).start()
    fps = FPS().start()

    WIDTH = 1920

    while(True):
        frame = vs.read()
        pipe = [frame]
        frame = imutils.resize(frame, width=WIDTH)
        cv2.imshow("Frame", frame)
        key = cv2.waitKey(1) & 0xFF

        if key == ord('c'):
            cv2.imwrite('./data/test_fps.jpg', frame)
        elif key == ord('q'):
            break
        fps.update()

with Manager() as manager:
     l = manager.list()
     camera_p = Process(target=img_get, args=[l])
     camera_p.start()
     while(True):
            time.sleep(2)
            print(type(l))

うまく動かないコード

Pipeでプロセス間通信

from __future__ import print_function
from imutils.video import WebcamVideoStream
from imutils.video import FPS
import imutils,cv2,time
import numpy as np
from multiprocessing import Pipe, Process

def img_get(pipe):
   print("[INFO] sampling THREADED frames from webcam...")
   vs = WebcamVideoStream(src=0).start()
   fps = FPS().start()
   WIDTH = 1920
   while(True):
        frame = vs.read()
        pipe.send(frame)
	frame = imutils.resize(frame, width=WIDTH)
	cv2.imshow("Frame", frame)
	key = cv2.waitKey(1) & 0xFF
	if key == ord('c'):
	    cv2.imwrite('./data/test_fps.jpg', frame)
        elif key == ord('q'):
            break
        fps.update()

parent_conn, child_conn = Pipe(duplex=False)
camera_p = Process(target=img_get, args=[child_conn])
camera_p.start()
while(True):
    time.sleep(2)
    print(type(parent_conn.recv()))