1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > python中使用flask实现人脸实时检测

python中使用flask实现人脸实时检测

时间:2020-09-14 17:14:18

相关推荐

python中使用flask实现人脸实时检测

flask:参考博客:/m0_51004308/article/details/122440065

代码结构:

video.html:

<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Video_Real_Time_Detect</title></head><body><h1 style="text-align:center">Video_Real_Time_Detect</h1><div style="text-align: center;width=800px;height=600px;border:'#708090' solid 2px"><img src="{{url_for('video_index')}}"></div></body></html>

test.py

import cv2import dlibfrom PIL import Image,ImageTkfrom flask import Flask,render_template,Responseapp=Flask(__name__)detect=dlib.get_frontal_face_detector()font=cv2.FONT_HERSHEY_SIMPLEXmargin=0.2img_w,img_h=128,128@app.route('/',methods=['POST','GET'])def index():return render_template('video.html')def Video():cap = cv2.VideoCapture(0)while cap.isOpened():OK,frame=cap.read()if not OK:breakdetects=detect(frame)if len(detects)>0:for i,locate in enumerate(detects):x1,y1,x2,y2,w,h=locate.left(),locate.right()+1,locate.bottom()+1,locate.width(),locate.width(),locate.height()xw1=max(int(x1-margin*w),0)yw1=max(int(y1-margin*h),0)xw2=min(int(x2+margin*w),img_w-1)yw2=min(int(y2+margin*h),img_h-1)cv2.rectangle(frame,(x1,y1),(x2,y2),(0,255,0),2)cv2.putText(frame,'person',(locate.left(),locate.top()-10),font,1.2,(255,0,0),3)# print(type(frame))image=cv2.imencode('.jpg',frame)[1].tobytes()## 使用generator函数输出视频流, 每次请求输出的content类型是image/jpegyield (b'--frame\r\n'b'Content-Type: image/jpeg\r\n\r\n'+image+b'\r\n')cap.release()cv2.destroyAllWindows()@app.route('/video_indexs',methods=['POST','GET'])def video_index():## 这个地址返回视频流响应return Response(Video(),mimetype='multipart/x-mixed-replace; boundary=frame')if __name__=='__main__':print('Pycharm')app.run(debug=True)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。