python+flask+多线程

python+flask+多线程,第1张

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()

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4655077.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-06
下一篇 2022-11-06

发表评论

登录后才能评论

评论列表(0条)

保存