0 ratings0% found this document useful (0 votes) 67 views8 pagesWeb API For Face Recognition
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
2a/t0/2021 16:59 \Web API for Face Recogniion - CodeProject
009) CODE
PROJECT
Web API for Face Recognition
¥ ‘Sergey L. Gladkiy
3OJul2021 CPOL
In this article, we'll wrap the face identification model in a simple Web API, create a client application on the Raspberry Pi, and run
the client-server system
Here well develop Python code for sending detected face images to the recognition server, wrap the Al face recognition with a
simple web AP! to receive the face images from an edge device, and show how it works together.
Download [Link] - 80.9 MB
Introduction
Face recognition is one area of artificial intelligence (Al) where the modern approaches of deep learning (DL) have had great success
‘during the last decade. The best face recognition systems can recognize people in images and video with the same precision
humans can—or even better.
‘Our series of articles on this topic is divided into two parts:
+ Face detection, where the client-side application detects human faces in images or in a video feed, aligns the detected face
pictures, and submits them to the server.
‘+ Face recognition (this part), where the server-side application performs face recognition.
We assume that you are familiar with DN, Python, Keras, and TensorFlow. You are welcome to download this project code to follow
along,
In the previous articles, we learned how to detect faces with the MTCNN library on a Raspberry Pi device and how to identify faces
with the FaceNet model. In this article, well see how these components can be used together in a simple web client-server system,
Client-side Application
Let's start with the client part running on an edge device. First, we write code for a simple class that will send face images to the
Python
class Imgsend:
def _init_(self, host, port, debug_nod
[Link] = host
self-port = port
self-url = host+":"+str(port)+" /api/faceing”
self.dbg_mode = debug node
alse):
def send(self, ing):
(, encoded) = [Link](" prj
» img)
data = [Link]()
headers = { "content-type": "image/png" }
[Link]/Artcles/530570 1MWeb-API-or-Face-Recogntion?display=Print 1823/10/2021 16:69 \Web API for Face Recognition - CodeProject
if self.dog_node:
print("Sending request
‘print (data)
ta = [Link]()
response = [Link]([Link], datasdata, header
t2 = time. tine()
dt = t2-t1
if self.dbg_mode:
print ("Request processed:
+str(dt)+" sec")
result = [Link](response. text)
return result
‘The constructor receives the Host and port parameters and forms the final URL with the special path /api/faceimg to route the
request tothe face identification method. In the Send method, we encode the image to the PN format, convert itto a string, and
send the string to the server with the requests . post function
We must also modify the face detector described in this (reference to part ‘Face Detection on Raspberry Pi) article
Python
class VideowFR:
def _init (self, detector, sender)
[Link] = detector
[Link] = sender
def process(self, video, alignFalse, save_pat!
detection_nun = 0;
rec_nun = @
capture = [Link] (video)
img = None
dname = ‘AI face recognition’
[Link](dname, cv2.WINDOW_NORMAL)
[Link](dnane, 96, 720)
frame_count = @
dt=@
if align:
fa = Face_Align Mouth(160)
# Capture all frames
while (True):
(ret, frane) = [Link]()
3 frame is None
break
frane_count = frane_count+1
1 = [Link]()
faces = [Link] (frame)
f_count = len(faces)
detection_nun += f_count
names = None
Lf (f_count>@) and (not ([Link] is None)):
names = [None]*F_count
for (i, face) in enumerate(faces):
if align’
(cropped,
else:
(4cropped, f_img) = [Link] (frame, face)
mg) = [Link](frame, face)
if (not (fing is"None)) and (not f_ing.size==0):
response = self. [Link](f_ing)
is_recognized = response[ “nessage” ]=="RECOGNIZED"
print (response[ "nessage"])
if is_recognized:
print(response[ "name" ]+":
‘+response["percent"])
hitps:[Link]/Artcles/530570 1Web-API-or-Face-Recogntion?display=Print 2823/10/2021 16:69 \Web API for Face Recognition - CodeProject
if is recognized:
réc_num += 1
name = response[ "name"]
percent = int(response( "percent"])
conf = percent*@.@1
names[i] = (name, conf)
if not (save_path is None):
ps = ("RO3d" % rec_num)+"_
ps = [Link](save_path, ps)
[Link](ps, f_ime)
‘"03d" % percent)+" png’
t2 = [Link]()
dt = dt + (t2-t1)
if Len(Faces)>0:
Utils.draw_faces(faces, (0, 0, 255), frame, True, True, names)
# Display the resulting frame
v2. imshow(dnane, frame)
Af [Link](1) & @xFF
break
ord('a"):
capture. release()
v2. destroyAl Windows ()
if dbo:
fps = detection_nun/dt
else
fps = 0
return (detection_num, rec_num, ps)
Now we have a face recognizer, not just a face detector. It includes an internal MTCNN detector and an image sender. When a face
is detected, itis sent to the server. When the response from the server is received, its parsed and saved to the specified folder.
Server-Side Application
Let’s move to the server application. We use the Flask microframework to wrap our face identification code with a web APL
Python
import flask
from flask import Flask, request, Response
print (flask.__version_)
# Initialize the Flask application
app = Flask(_name_)
rec = None
db = None
rec_data = None
save_path = None
@[Link]("/api/faceimg”, methods=[ 'POST'])
cof test():
response = ()
r_status = 220
= request
print ("Processing recognition request.
t1 = [Link]()
nparr = [Link]([Link], np.uint8)
img = [Link](nparr, cv2. INREAD_COLOR)
[Link]/Artcles/530570 1MWeb-API-or-Face-Recogntion?display=Print 3823/10/2021 16:69 \Web API for Face Recognition - CodeProject
embds = [Link](img)
data = [Link](enbds, f_db)
12 = [Link]()
dt = t2-t1
print("Recognition request processed: "+str(dt)+" sec")
rec_data.count()
ps =
info
if not (data is None):
(name, dist, p_photo) = data
conf = 1.0 * dist
percent = int(con#*100)
info = “Recognized: "sname+" "“+str(conf)
ps = ("x03d" % rec_data.get_count())+"_"#names"_"+("Xe3d" % percent)+" .png”
response = { "RECOGNIZED",
"percent": str(percent) }
else:
info
ps
response = { "message":
“UNRECOGNIZED
'Ke3d" % rec_data.get_count())+"_unrecognized"+" png"
"UNRECOGNIZED" }
print (info)
if not (save_path is None):
Ps = [Link]. join(save_path, ps)
[Link](ps, ing)
# encode response using jsonpickle
response_pickled = [Link](response)
return Response(response=response_pickled, statu
"_status, mimetype="application/json")
‘After initializing the Flask application, we apply the route decorator to trigger the test method with the specified URL (the
same one the client application uses) n this method, we decode the received PNG image of a face, get the embeddings, recognize
the face, and send the response back to the client.
Running the System in a Container
Finally, here is the code for running our web application’
Python
if __name. _main_’
host = str(sys-argv[1])
port = int([Link][2])
# FaceNet recognizer
m_file = r"/hone/pi_fr/net/facenet_keras.h5
rec = FaceNetRec(n File, 0.5)
rec_data = Recbata()
print ("Recognizer loaded.")
print (rec.get_nodel() inputs)
print (rec.get_nodel() -outputs)
# Face 0B
save_path = r"/hone/pi_fr/rec
db_path = r"/none/pi_fr/ab"
£ db = FaceDB()
#idb.1oad(db_path, rec)
db_f_count = 1en(#_db. get_data())
print ("face D8 loaded: "“+str(db_# count))
print("Face recognition running")
thost = "[Link]"
hitps:[Link]/Artcles/530570 1Web-API-or-Face-Recogntion?display=Print
4823/10/2021 16:69 \Web API for Face Recognition - CodeProject
tport = 50
app. run(hos
ost, port=port, threaded=False)
‘As we're going to run the web application in the created Docker container (reference to the previous part), we need to start this,
container with the appropriate network settings. Use the following commands to create a new container from the image:
\odocker network create my-net
\odocker create --name FR_2 --network my-net --publish 5050:50 sergeylgladkiy/fr:v1
When the FR_2 container starts it forwards port 5050 of the host machine to the internal port 50 of the container.
Now we can run the application in the container (note that because
inside the container, we specify internal port 50)
# python /home/pi_fr/pi_fr_facenet.run_align_dock_flask.[Link] [Link] 52
‘When the server starts, we can run the client on a Raspberry Pi device, Here is the code we used to launch the application
Python
if _name__ == "_main_":
Av file = str([Link][2])
‘#host = str([Link][2])
#port = int([Link][3])
v_file = r"/hone/pi/Desktop/P1_FR/video/5_2.mp4"
host = "[Link]"
port = 5050
# Video Web recognition
save_path = r"/hone/pi/Desktop/PI_FR/rec"
d = ATCNN_Detector(5@, 0.95)
sender = IngSend(host, port, True)
vr = VideowFR(d, sender)
(f_count, rec_count, fps) = [Link](v_file, True, save_path)
print("Face detections
print("Face recognitions:
print("FPS: “+str(fps))
'+ste(f_count))
“sstr(rec_count))
[Note that the client uses the IP and port of the host machine ($050), net the IP of the container and internal port number (50),
‘The following two videos show how our client-server systemworks:
hitps:[Link]/Artcles/530570 1Web-API-or-Face-Recogntion?dsplay=Print
5823/10/2021 16:69 \Web API for Face Recogniion - CodeProject
video 2 recognize web
video 4 recognize web
[Link]/Artcles/530570 1Web-API-or-Face-Recogntion?display=Print 382a/t0/2021 16:59 \Web API for Face Recogniion - CodeProject
‘As you can see, the recognition request was processed very quickly; it took only about 0.07 seconds. This is the proof of the correct
system architecture. The client sends to the Server only the cropped and aligned detected face images, thus decreasing the network
load, while the identification algorithm runs on a powerful server computer.
Next Steps
In the next article of the series, well show how to run the face recognition servers on Kubermetes, Stay tuned!
License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)
About the Author
Sergey L. Gladkiy
‘Team Leader VIPAKS
Russian Federation sal
EDUCATION:
Master's degree in Mechanics.
PhD degree in Mathematics and Physics.
PROFESSIONAL EXPERIENCE:
15 years’ experience in developing scientific programs
(C#, C++, Delphi Java, Fortran)
SCIENTIFIC INTERESTS:
Mathematical modeling, symbolic computer algebra, numerical methods, 3D geometry modeling, artificial intelligence,
differential equations, boundary value problems,
Comments and Discussions
1 message has been posted for th
Recogr
antcle Visit [Link]
jon to post and view comments on this article, or click here to get a print view with messages,
Permalink ‘Axticle Copyright 2021 by Sergey L Glackiy
Advertise Everything else Copyright © CodeProject, 1999-
Prvacy 2021
hitps:[Link]/Artcles/530570 1Web-API-or-Face-Recogntion?dsplay=Print 71823/10/2021 16:69 \Web API for Face Recognition - CodeProject
Cookies
Terms of Use Web04 2620211019,
hitps:[Link]/Artcles/530570 1Web-API-or-Face-Recognion?display=Print oo