訂閱
糾錯
加入自媒體

Flask Web 框架中的 OpenCV 人臉檢測部署

介紹

本文,我們將介紹使用 Flask API 部署進(jìn)行人臉檢測。OpenCV 是一個 Python 庫,旨在解決計算機(jī)視覺問題。它用于各種應(yīng)用,例如人臉檢測、視頻捕獲、跟蹤移動對象和對象披露。

Flask API

Flask 是一個廣泛使用的微型 Web 框架,用于在 Python 中創(chuàng)建 API。它是一個簡單而強(qiáng)大的 Web 框架,旨在快速輕松地啟動,并能夠擴(kuò)展到復(fù)雜的應(yīng)用程序。

什么是 Haarcascade

它是一種對象檢測算法,用于識別圖像或?qū)崟r視頻中的人臉。該算法使用邊緣或線檢測特征。

現(xiàn)在讓我們進(jì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é)果

運(yùn)行此代碼后,你將看到以下屏幕。

復(fù)制此 URL http://127.0.0.1:8000/ 并將其粘貼到瀏覽器中以獲取結(jié)果。

image.png

       原文標(biāo)題 : Flask Web 框架中的 OpenCV 人臉檢測部署

聲明: 本文由入駐維科號的作者撰寫,觀點僅代表作者本人,不代表OFweek立場。如有侵權(quán)或其他問題,請聯(lián)系舉報。

發(fā)表評論

0條評論,0人參與

請輸入評論內(nèi)容...

請輸入評論/評論長度6~500個字

您提交的評論過于頻繁,請輸入驗證碼繼續(xù)

暫無評論

暫無評論

人工智能 獵頭職位 更多
掃碼關(guān)注公眾號
OFweek人工智能網(wǎng)
獲取更多精彩內(nèi)容
文章糾錯
x
*文字標(biāo)題:
*糾錯內(nèi)容:
聯(lián)系郵箱:
*驗 證 碼:

粵公網(wǎng)安備 44030502002758號