Python 多进程 OpenCV 网络摄像头 &获取请求 [英] Python Multiprocess OpenCV Webcam & Get Request

查看:141
本文介绍了Python 多进程 OpenCV 网络摄像头 &获取请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是多处理的新手,但我花了一整天的时间试图让以下内容正常工作.

I'm new to multiprocessing but have spent a whole day trying to get the following to work properly.

我正在尝试使用 OpenCV (func_run_forever) 与单个 requests.get (func_run_once) 并行运行我的网络摄像头.所以预期的结果是我的网络摄像头在一个窗口中运行 (imshow),并且在我的网络摄像头运行时来自 requests.get 的单个响应 200.但是,在我退出 OpenCV 窗口之前 request.get 不会运行.

I'm trying to run my Webcam using OpenCV (func_run_forever) in parallel with a single requests.get (func_run_once). So the expected result is my webcam running in a window (imshow), and a single Response 200 from requests.get while my webcam is running. However, the request.get won't run until I quit the OpenCV window.

任何帮助、提示、线索……甚至答案都将不胜感激!

Any help, tips, clues...or even answers would be much appreciated!

from multiprocessing import Process
import cv2
import requests

def func_run_forever():
    cap = cv2.VideoCapture(0)
    while(True):
        ret, frame = cap.read()
        cv2.imshow('frame',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    cap.release()
    cv2.destroyAllWindows()

def func_run_once():
    res = requests.get('https://www.google.com.au')
    print(res)

if __name__ == '__main__':
    p1 = Process(target=func_run_forever())
    p2 = Process(target=func_run_once())
    p1.start()
    p2.start()
    p1.join()
    p2.join()

推荐答案

伙伴,你的代码需要 1% 的修改才能按预期工作.改变这个:

Mate, your code needs like 1% modification to work as intended. Change this:

<代码> P1 =进程(目标= func_run_forever())p2 = 进程(目标=func_run_once())

到此:

p1 = Process(target=func_run_forever)
p2 = Process(target=func_run_once)

这篇关于Python 多进程 OpenCV 网络摄像头 &amp;获取请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆