gpt4 book ai didi

python - 在网络摄像头中捕捉人脸并实时显示到 flask 中

转载 作者:行者123 更新时间:2023-12-02 17:30:25 25 4
gpt4 key购买 nike

基本上我有三个文件,即 app.py camera.py 和 gallery.html。我附上我的代码供您引用。

应用程序.py

from flask import Flask, Response, json, render_template
from werkzeug.utils import secure_filename
from flask import request
from os import path, getcwd
import time
import os

app = Flask(__name__)
import cv2
from camera import VideoCamera


app.config['file_allowed'] = ['image/png', 'image/jpeg']
app.config['train_img'] = path.join(getcwd(), 'train_img')


def gen(camera):
while True:
frame = camera.get_frame()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')

@app.route('/video_feed')
def video_feed():
return Response(gen(VideoCamera()),
mimetype='multipart/x-mixed-replace; boundary=frame')

@app.route('/')
def index():
return render_template('index.html')

@app.route('/gallery')
def get_gallery():
images = os.listdir(os.path.join(app.static_folder, "capture_image"))
return render_template('gallery.html', images=images)
app.run()

相机.py
import cv2
import face_recognition
from PIL import Image
import os
import time



dir_path = "C:/tutorial/face_recognition/venv/src4/capture_image"

class VideoCamera(object):
def __init__(self):
self.video = cv2.VideoCapture(0)

def get_frame(self):
success, frame = self.video.read()
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
rgb_small_frame = small_frame[:, :, ::-1]
face_locations = face_recognition.face_locations(rgb_small_frame,number_of_times_to_upsample=2)

for face_location in face_locations:
top, right, bottom, left = face_location

face_image = rgb_small_frame[top:bottom, left:right]
pil_image = Image.fromarray(face_image)
File_Formatted = ("%s" % (top)) + ".jpg"
file_path = os.path.join( dir_path, File_Formatted)
pil_image.save(file_path)



ret, jpeg = cv2.imencode('.jpg', frame)
return jpeg.tobytes()

画廊.html
<section class="row">
{% for image in images %}
<section class="col-md-4 col-sm-6" style="background-color: green;">
<img src="{{ url_for('static', filename='capture_image/' + image) }}">
</section>
{% endfor %}
</section>

这是我到目前为止所做的,网络摄像头将在网络摄像头中捕获面部并保存在文件夹中。然后将图像发送到gallery.html。目前,我想在 html 模板中实时显示图像而无需刷新,当面部被捕获时,它将自动动态或实时显示在 html gallery.html 中。供您引用,我正在使用 flask 、python 和 openCV

我的问题是如何在不刷新的情况下实时显示面部捕捉。当新人脸被捕获时,它会自动显示在gallery.html中吗?

希望有人可以在这件事上。谢谢

最佳答案

好的。首先是下载这个模块:webcamJS .这是允许您从客户端捕获照片的 javascript 模块。测试它以熟悉它(有很多替代方案,但在我看来,它是最简单的解决方案之一)。

但我很好,我仍然放了一个最少的代码来告诉你如何使用它:

您配置您的 HTML 页面并添加以下 div(不要因为代码的结构而责备我,我知道它并不漂亮,所有这些 html 和 javascript 之间的大杂烩,但它有效):

<div id="my_photo_booth">
<div id="my_camera"></div>

<script src="{{url_for('static',filename='js/webcam.min.js')}}"></script>
<script src="{{url_for('static',filename='audio/shutter.mp3')}}"></script>
<script src="{{url_for('static',filename='audio/shutter.ogg')}}"></script>

<!-- Configure a few settings and attach camera -->
<script language="JavaScript">
Webcam.set({
// live preview size
width: 320,
height: 240,

// device capture size
dest_width: 640,
dest_height: 480,

// final cropped size
crop_width: 480,
crop_height: 480,

// format and quality
image_format: 'jpeg',
jpeg_quality: 90,

// flip horizontal (mirror mode)
flip_horiz: true
});
Webcam.attach( '#my_camera' );
</script>

<br>
<div id="results" style="display:none">
<!-- Your captured image will appear here... -->
</div>

<!-- A button for taking snaps -->
<form>
<div id="pre_take_buttons">
<!-- This button is shown before the user takes a snapshot -->
<input type=button class="btn btn-success btn-squared" value="CAPTURE" onClick="preview_snapshot()">
</div>

<div id="post_take_buttons" style="display:none">
<!-- These buttons are shown after a snapshot is taken -->
<input type=button class="btn btn-danger btn-squared responsive-width" value="&lt; AGAIN" onClick="cancel_preview()">
<input type=button class="btn btn-success btn-squared responsive-width" value="SAVE &gt;" onClick="save_photo()" style="font-weight:bold;">
</div>
</form>

</div>

一些 javascript 来操作照片捕获并将照片发送到服务器:
<script language="JavaScript">
// preload shutter audio clip
var shutter = new Audio();
shutter.autoplay = false;
shutter.src = navigator.userAgent.match(/Firefox/) ? '/static/audio/shutter.ogg' : '/static/audio/shutter.mp3';

function preview_snapshot() {
// play sound effect
try { shutter.currentTime = 0; } catch(e) {;} // fails in IE
shutter.play();

// freeze camera so user can preview current frame
Webcam.freeze();

// swap button sets
document.getElementById('pre_take_buttons').style.display = 'none';
document.getElementById('post_take_buttons').style.display = '';
}

function cancel_preview() {
// cancel preview freeze and return to live camera view
Webcam.unfreeze();

// swap buttons back to first set
document.getElementById('pre_take_buttons').style.display = '';
document.getElementById('post_take_buttons').style.display = 'none';
}

function save_photo() {
// actually snap photo (from preview freeze).
Webcam.snap( function(data_uri) {
// display results in page
console.log(data_uri);

// shut down camera, stop capturing
Webcam.reset();

$.getJSON($SCRIPT_ROOT + '/_photo_cap', {
photo_cap: data_uri,
},function(data){
var response = data.response;
});

} );
}
</script>

显然,您将此代码添加到 html 代码的底部。

我希望你能应付这一切。但这里有趣的部分是 save_photo()功能。在这个函数中,我得到 data uri从照片中并通过 ajax ( Check this link to see how to use jquery / ajax to send data to flask) 将其发送到 flask 。

在 flask 一侧:
import base64

@bp.route('/photo')
def photo():
return render_template('photo.html')


@bp.route('/_photo_cap')
def photo_cap():
photo_base64 = request.args.get('photo_cap')
header, encoded = photo_base64.split(",", 1)
binary_data = base64.b64decode(encoded)
image_name = "photo.jpeg"

with open(os.path.join("app/static/images/captures",image_name), "wb") as f:
f.write(binary_data)
//facial recognition operations
response = 'your response'

return jsonify(response=response)

这里有两种路由,一种是渲染拍照页面,另一种是接收通过ajax发送的数据uri。

基本上,在第二条 route 发生的事情是我获取数据 uri,将其转换为 base64 并将其存储在我的磁盘上。那么这就是你干预的地方。您进行面部识别操作,然后将响应返回到您的页面。

关于python - 在网络摄像头中捕捉人脸并实时显示到 flask 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56327262/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com