Flask Web 框架中的 OpenCV 人臉檢測(cè)部署
介紹
本文,我們將介紹使用 Flask API 部署進(jìn)行人臉檢測(cè)。OpenCV 是一個(gè) Python 庫(kù),旨在解決計(jì)算機(jī)視覺(jué)問(wèn)題。它用于各種應(yīng)用,例如人臉檢測(cè)、視頻捕獲、跟蹤移動(dòng)對(duì)象和對(duì)象披露。
Flask API
Flask 是一個(gè)廣泛使用的微型 Web 框架,用于在 Python 中創(chuàng)建 API。它是一個(gè)簡(jiǎn)單而強(qiáng)大的 Web 框架,旨在快速輕松地啟動(dòng),并能夠擴(kuò)展到復(fù)雜的應(yīng)用程序。
什么是 Haarcascade
它是一種對(duì)象檢測(cè)算法,用于識(shí)別圖像或?qū)崟r(shí)視頻中的人臉。該算法使用邊緣或線檢測(cè)特征。
現(xiàn)在讓我們進(jìn)入項(xiàng)目實(shí)現(xiàn)。
項(xiàng)目結(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é)果
運(yùn)行此代碼后,你將看到以下屏幕。
復(fù)制此 URL http://127.0.0.1:8000/ 并將其粘貼到瀏覽器中以獲取結(jié)果。
原文標(biāo)題 : Flask Web 框架中的 OpenCV 人臉檢測(cè)部署

發(fā)表評(píng)論
請(qǐng)輸入評(píng)論內(nèi)容...
請(qǐng)輸入評(píng)論/評(píng)論長(zhǎng)度6~500個(gè)字
圖片新聞
-
機(jī)器人奧運(yùn)會(huì)戰(zhàn)報(bào):宇樹(shù)機(jī)器人摘下首金,天工Ultra搶走首位“百米飛人”
-
存儲(chǔ)圈掐架!江波龍起訴佰維,索賠121萬(wàn)
-
長(zhǎng)安汽車(chē)母公司突然更名:從“中國(guó)長(zhǎng)安”到“辰致科技”
-
豆包前負(fù)責(zé)人喬木出軌BP后續(xù):均被辭退
-
字節(jié)AI Lab負(fù)責(zé)人李航卸任后返聘,Seed進(jìn)入調(diào)整期
-
員工持股爆雷?廣汽埃安緊急回應(yīng)
-
中國(guó)“智造”背后的「關(guān)鍵力量」
-
小米汽車(chē)研發(fā)中心重磅落地,寶馬家門(mén)口“搶人”
最新活動(dòng)更多
-
即日-9.16點(diǎn)擊進(jìn)入 >> 【限時(shí)福利】TE 2025國(guó)際物聯(lián)網(wǎng)展·深圳站
-
10月23日立即報(bào)名>> Works With 開(kāi)發(fā)者大會(huì)深圳站
-
10月24日立即參評(píng)>> 【評(píng)選】維科杯·OFweek 2025(第十屆)物聯(lián)網(wǎng)行業(yè)年度評(píng)選
-
11月27日立即報(bào)名>> 【工程師系列】汽車(chē)電子技術(shù)在線大會(huì)
-
12月18日立即報(bào)名>> 【線下會(huì)議】OFweek 2025(第十屆)物聯(lián)網(wǎng)產(chǎn)業(yè)大會(huì)
-
精彩回顧立即查看>> 【限時(shí)下載】ADI中國(guó)三十周年感恩回饋助力企業(yè)升級(jí)!
推薦專(zhuān)題
- 1 阿里首位程序員,“掃地僧”多隆已離職
- 2 先進(jìn)算力新選擇 | 2025華為算力場(chǎng)景發(fā)布會(huì)暨北京xPN伙伴大會(huì)成功舉辦
- 3 宇樹(shù)機(jī)器人撞人事件的深度剖析:六維力傳感器如何成為人機(jī)安全的關(guān)鍵屏障
- 4 清華跑出具身智能獨(dú)角獸:給機(jī)器人安上眼睛和大腦,融資近20億
- 5 踢館大廠和微軟,剖析WPS靈犀的AI實(shí)用主義
- 6 特朗普要求英特爾首位華人 CEO 辭職
- 7 騰訊 Q2 財(cái)報(bào)亮眼:AI 已成第二增長(zhǎng)曲線
- 8 AI版“四萬(wàn)億刺激”計(jì)劃來(lái)了
- 9 谷歌吹響AI沖鋒號(hào),AI還有哪些機(jī)會(huì)
- 10 蘋(píng)果把身家押在Siri上:一場(chǎng)輸不起的自我革命