Basically I have three file which is app.py camera.py and gallery.html. I attach my code for your reference.
app.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()
camera.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()
gallery.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>
This what i have done so far, the webcam will capture the face in webcam and save in folder. Then send the image to gallery.html. Currently, i am want to display the image real time in html templete without refresh when the face is captured it will automatically display in html gallery.html dynamically or real time.For your information i am using flask,python and openCV
My question is how i can display the face capture real time without refresh. When new face captured it will automatically display in gallery.html?
Hope someone can regarding on this matter.Thank you