栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python+flask+多线程

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python+flask+多线程

通过url打开使用opencv访问摄像头,如果没有调用close函数就一直正常识别,否则关掉。

from flask import Flask,request
import cv2
from concurrent.futures import ThreadPoolExecutor

app = Flask(__name__)

closeState = False

@app.route('/open',methods=["GET","POST"])
def hello_world():
   winName = request.args.get("winName")
   name = request.args.get("name")
   print(winName,name)
   future = pool.submit(taskOpen,winName,name)
   future.add_done_callback(openDone)
   return future.result()

def taskOpen(winName,name):
   cap = cv2.VideoCapture(0)
   while True:
      ret, img = cap.read()
      if ret:
         cv2.imshow(winName,img)
         # if cv2.waitKey(1) & 0xFF == ord('q'):
         global closeState
         print(name)
         if cv2.waitKey(1) and closeState==True: # 中间停止
            cv2.destroyWindow(winName)
            closeState=False
            return "1"
         elif name=="hblee": # 正常识别
            cv2.waitKey(3000)
            cv2.destroyWindow(winName)
            return name


def openDone(response):
   print('in openDone',response.result())

@app.route('/close')
def helloClose():
   future = pool.submit(taskClose)
   future.add_done_callback(closeDone)
   return future.result()

def taskClose():
   global closeState
   closeState = True
   return str(closeState)

def closeDone(response):
   print('in close done',response.result())

if __name__ == '__main__':
   pool = ThreadPoolExecutor(10)
   app.run()

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/331258.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号