Open adamgan919191 opened 5 years ago
We are using this way to handle images from Flask:
@self.api.route('/api/v1/demo/face', methods = ['POST'])
def route_face():
img = self.get_img(request)
res = self.module_face.do_process(img)
return self.get_res(res)
def get_img(self, request):
data = request.files['file'].read()
npimg = np.frombuffer(data, np.uint8)
img = cv2.imdecode(npimg, cv2.IMREAD_COLOR)
return img
def get_res(self, res):
resp = Response(response = res, status = 200, mimetype = "application/json")
resp.headers['Content-Type'] = 'application/json; charset=utf-8'
resp.headers['Access-Control-Allow-Origin'] = '*'
return resp
PS: After that, you can use the img
as you like.
Post method with cURL:
curl -F "file=@./tests/barbara_palvin.jpg" http://localhost:5000/api/v1/demo/face
I hope it helps, if you are looking for like that, @adamgan919191
Good Luck!
Hye Dentrax, Thank you help and reply :)
Basically I have three file which is app.py camera.py and gallery.html. I attach my code for you 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 image in save folder changing. can you help me on this matter?
Thank you
@adamgan919191 I think that way is a bit slow way. Because you are doing I/O
operations. Instead, my advice is to use socket for real-time operations. If you want to get least 60 FPS, do not face recognition in the main loop. Instead, you can run face recognition operations in async
, on different thread
, each X
frame. This can be the fastest solution.
@Dentrax I agree 👍🏽
you can run face recognition operations in
async
does face recognition lib support concurrency via async?
face_recognition version:1.2.3 Python version:3.7.2 Operating System: Window 64bits
I have a question. currently I am working with face recognition project by using face_recognition library provided by Python, flask and OpenCV. Currently my face recognition works well in flask server.
Currently I am working on face capture in flask. What I mean here is when the webcam detect the face it will capture the face and display in flask.
My question is how I can send the capture face in webcam and display in flask? It's great if someone can share the code to me or maybe related tutorial on this.