使用线程Python的HTTP请求

2024-09-24 00:23:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用flask把我的视频流到我的网络浏览器。所以我遵循this link中的说明

在我的程序中,流式传输视频时,我必须向另一个服务发出HTTP请求。 当我发出请求时,我的视频流正在停止

我的程序基于这些链接, 请帮助如何使用锁和HTTP请求线程

我的示例代码:(其余与源代码相同)

def generate():
    global outputFrame, lock, isRecognized, cnt
    while True:
        # wait until the lock is acquired
        with lock:
            # check if the output frame is available, otherwise skip
            # the iteration of the loop
            cnt+=1
            if cnt%50 == 0:
                isRecognized = True
            if outputFrame is None:
                continue
            (flag, encodedImage) = cv2.imencode(".jpg", outputFrame)
            # ensure the frame was successfully encoded
            if not flag:
                continue
        yield(b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + bytearray(encodedImage) + b'\r\n')

def generate_car():
    # grab global references to the output frame and lock variables
    global isRecognized, outputFrame
    url = 'myurl.com'
    while True:
        # wait until the lock is acquired
        with lock:
            if isRecognized ==True:
                isRecognized = False
                # encode the frame in JPEG format
                cv2.imwrite("temp.jpg", outputFrame)
                with open("temp.jpg", "rb") as c:
                    b64string = base64.b64encode(c.read())
                    b64string=str(b64string)[2:-1]

                r = requests.post(url, data=b64string) # <-- this line is the problem

                r = json.loads(r.text)
                print('\n\n', r ,'\n\n')
                (__, encodedImage) = cv2.imencode(".jpg", outputFrame)
                # yield the output frame in the byte format
                yield(b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + 
                    bytearray(encodedImage) + b'\r\n')  

Tags: thetruelockoutputifiswithcv2
1条回答
网友
1楼 · 发布于 2024-09-24 00:23:07

您必须在nginx这样的web服务器上运行flask应用程序,或者使用uwsgi让它同时处理多个请求

相关问题 更多 >