Flask Web 框架中的 OpenCV 人臉檢測部署
介紹
本文,我們將介紹使用 Flask API 部署進行人臉檢測。OpenCV 是一個 Python 庫,旨在解決計算機視覺問題。它用于各種應(yīng)用,例如人臉檢測、視頻捕獲、跟蹤移動對象和對象披露。
Flask API
Flask 是一個廣泛使用的微型 Web 框架,用于在 Python 中創(chuàng)建 API。它是一個簡單而強大的 Web 框架,旨在快速輕松地啟動,并能夠擴展到復(fù)雜的應(yīng)用程序。
什么是 Haarcascade
它是一種對象檢測算法,用于識別圖像或?qū)崟r視頻中的人臉。該算法使用邊緣或線檢測特征。
現(xiàn)在讓我們進入項目實現(xiàn)。
項目結(jié)構(gòu)
index.html
<!DOCTYPE html>
<html>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<title>Dharmaraj - Face Detection</title>
<style>
h2
{
padding-bottom:20px;
font-weight: 600;
font-size: 3.2em
}
</style>
<body>
<div class="container"><center><h2>Face Detection</h2></center>
<div class="col-lg-offset-2 col-lg-8">
<center><form class="form-inline" action = "/stop" method = "post" enctype="multipart/form-data">
<input type = "submit" class="btn btn-danger btn-md btn-block" value="Stop">
</form></center>
<center><form class="form-inline" action = "/start" method = "post" enctype="multipart/form-data">
<input type = "submit" class="btn btn-success btn-md btn-block" value="Start">
</form></center><br></div>
<div class="col-lg-offset-2 col-lg-8">
<img src="{{ url_for('video_capture') }}" width="100%">
</div></div>
</body>
</html>
stop.html
<!DOCTYPE html>
<html>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<title>Dharmaraj - Face Detection</title>
<style>
h2
{
padding-bottom:20px;
font-weight: 600;
font-size: 3.2em
}
</style>
<body>
<div class="container">
<center><h2>Face Detection</h2></center>
<div class="col-lg-offset-2 col-lg-8">
<center><form class="form-inline" action = "/stop" method = "post" enctype="multipart/form-data">
<input type = "submit" class="btn btn-danger btn-md btn-block" value="Stop">
</form></center>
<center><form class="form-inline" action = "/start" method = "post" enctype="multipart/form-data">
<input type = "submit" class="btn btn-success btn-md btn-block" value="Start">
</form></center><br>
</div></div>
</body>
</html>
app.py
from flask import Flask, render_template, Response
import cv2
app=Flask(__name__)
def capture_by_frames():
global camera
camera = cv2.VideoCapture(0)
while True:
success, frame = camera.read() # read the camera frame
detector=cv2.CascadeClassifier('Haarcascades/haarcascade_frontalface_default.xml')
faces=detector.detectMultiScale(frame,1.2,6)
#Draw the rectangle around each face
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 3)
ret, buffer = cv2.imencode('.jpg', frame)
frame = buffer.tobytes()
yield (b'--frame'
b'Content-Type: image/jpeg' + frame + b'')
@app.route('/')
def index():
return render_template('index.html')
@app.route('/start',methods=['POST'])
def start():
return render_template('index.html')
@app.route('/stop',methods=['POST'])
def stop():
if camera.isOpened():
camera.release()
return render_template('stop.html')
@app.route('/video_capture')
def video_capture():
return Response(capture_by_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__=='__main__':
app.run(debug=True,use_reloader=False, port=8000)
結(jié)果
運行此代碼后,你將看到以下屏幕。
復(fù)制此 URL http://127.0.0.1:8000/ 并將其粘貼到瀏覽器中以獲取結(jié)果。
原文標題 : Flask Web 框架中的 OpenCV 人臉檢測部署

請輸入評論內(nèi)容...
請輸入評論/評論長度6~500個字
最新活動更多
推薦專題
-
10 大模型的盡頭是開源
- 1 特斯拉工人被故障機器人打成重傷,索賠3.6億
- 2 【行業(yè)深度研究】退居幕后四年后,張一鳴終于把算法公司變成AI公司?
- 3 人形機器人廠商,正在批量復(fù)刻宇樹G1
- 4 AI 時代,阿里云想當“安卓” ,那誰是“蘋果”?
- 5 華為公布昇騰芯片三年計劃,自研HBM曝光
- 6 硬剛英偉達!華為發(fā)布全球最強算力超節(jié)點和集群
- 7 機器人9月大事件|3家國產(chǎn)機器人沖刺IPO,行業(yè)交付與融資再創(chuàng)新高!
- 8 谷歌“香蕉”爆火啟示:國產(chǎn)垂類AI的危機還是轉(zhuǎn)機?
- 9 00后華裔女生靠兩部AI電影狂賺7.8億人民幣,AI正式進軍好萊塢
- 10 美光:AI Capex瘋投不止,終于要拉起存儲超級周期了?