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