Table of contents
4 Combining the Flask framework
1 Introduction to insightface
Recently, machine learning projects such as face recognition are very popular. I accidentally discovered an open source face recognition open source project insightface.
Face recognition technology can accurately identify faces and identities in images, and has a wealth of application scenarios, such as face payment in financial scenarios, criminal identification in security scenarios, and new crown epidemiological investigations in medical scenarios, etc. The algorithm evolution of face recognition has gone through the early stage represented by PCA, and then to the stage of statistical learning method based on "artificial features + classifier". In recent years, with the explosion of big data and GPU computing power, human Face recognition has entered the stage where the deep learning algorithm is the absolute protagonist.
InsightFace is the industry's mainstream face recognition solution based on the MXNet framework. Compared with the MXNet implementation , the OneFlow-based implementation is even better in terms of performance. OneFlow is 2.82 times faster in data parallelism; 2.45 times faster in model parallelism; 1.38 times faster in hybrid parallel + Partial fc times. The code implemented based on OneFlow has been merged into insightface's official repository , which includes dataset creation tutorials, training and verification scripts, pre-trained models , and conversion tools for MXNet models.
2 install insightface
Installing insightface is very simple, just run the following code:
pip3 install -U insightface
During the installation process, you may need to install other dependencies, just follow the prompts to install them accordingly.
3 Use insightface
After installing insightface, you can create a new test.py and add the following code:
import cv2
import numpy as np
import insightface
from insightface.app import FaceAnalysis
from insightface.data import get_image as ins_get_image
app = FaceAnalysis(providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])
app.prepare(ctx_id=0, det_size=(640, 640))
img = ins_get_image('t1')
faces = app.get(img)
rimg = app.draw_on(img, faces)
cv2.imwrite("./t1_output.jpg", rimg)
After running, you will get the result:
Isn't it cool~~
4 Combining the Flask framework
Create a new server.py
import flask, os, sys, time
from flask import Flask, render_template, request, make_response
import func
app = Flask(__name__)
interface_path = os.path.dirname(__file__)
sys.path.insert(0, interface_path)
@app.route('/', methods=['get'])
def index():
return render_template('index.html')
@app.route('/upload', methods=['post'])
def upload():
fname = request.files['img']
print("@@@@@@@@@@@@@@@@@@@@@@")
print(fname.filename)
newName = r'static/upload/' + fname.filename
fname.save(newName)
func.getRet(newName)
#image_data = open("ldh_output.jpg", "rb").read()
#response = make_response(image_data)
#response.headers['Content-Type'] = 'image/jpg'
#return response
return render_template('result.html')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug='True')
new func.py
import cv2
import numpy as np
import insightface
from insightface.app import FaceAnalysis
from insightface.data import get_image as ins_get_image
app = FaceAnalysis(providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])
app.prepare(ctx_id=0, det_size=(640, 640))
#img = ins_get_image('t1')
def getRet(filename):
img = cv2.imdecode(np.fromfile(filename, dtype=np.uint8), -1)
faces = app.get(img)
rimg = app.draw_on(img, faces)
cv2.imwrite("./static/ldh_output.jpg", rimg)
New index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>人脸识别</title>
</head>
<img src="{
{ url_for('static', filename='title.webp') }}" width="500" height=auto>
<form action="upload" method="post" enctype="multipart/form-data">
<body class="light-theme">
<p>识别人脸</p>
<input type="file" id="img" name="img">
<button type="submit">检测</button>
</body>
</form>
</html>
New-result.html
<html>
<body>
<img src="{
{ url_for('static', filename='ldh_output.jpg') }}" width="500" height=auto>
</body>
</html>
New main.css
:root {
--green: #00FF00;
--white: #FFFFFF;
--black: #000000;
}
body {
background: var(--bg);
color: var(--fontColor);
font-family: helvetica;
}
.light-theme {
--bg: var(--green);
--fontColor: var(--black);
}
Run server.py, visit 127.0.0.0:5000
Although the web interface is ugly, it can perform file upload and recognition normally.
Finally, we detect the face of the king of heaven: