Download as pdf
Download as pdf
You are on page 1of 8
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 net.zip - 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 Self.host = host self-port = port self-url = host+":"+str(port)+" /api/faceing” self.dbg_mode = debug node alse): def send(self, ing): (, encoded) = cv2.imencode(" prj » img) data = encoded.tostring() headers = { "content-type": "image/png" } hitpsslwwn.codeproject.com/Artcles/530570 1MWeb-API-or-Face-Recogntion?display=Print 18 23/10/2021 16:69 \Web API for Face Recognition - CodeProject if self.dog_node: print("Sending request ‘print (data) ta = tine.tine() response = requests.post(self.url, datasdata, header t2 = time. tine() dt = t2-t1 if self.dbg_mode: print ("Request processed: +str(dt)+" sec") result = json.loads(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) Self.detector = detector self.sender = sender def process(self, video, alignFalse, save_pat! detection_nun = 0; rec_nun = @ capture = cv2.VideoCapture (video) img = None dname = ‘AI face recognition’ cv2.namedWindow(dname, cv2.WINDOW_NORMAL) cv2.resizeWindow(dnane, 96, 720) frame_count = @ dt=@ if align: fa = Face_Align Mouth(160) # Capture all frames while (True): (ret, frane) = capture.read() 3 frame is None break frane_count = frane_count+1 1 = time.tine() faces = self.detector.detect (frame) f_count = len(faces) detection_nun += f_count names = None Lf (f_count>@) and (not (self.sender is None)): names = [None]*F_count for (i, face) in enumerate(faces): if align’ (cropped, else: (4cropped, f_img) = self.detector.extract (frame, face) mg) = fa.align(frame, face) if (not (fing is"None)) and (not f_ing.size==0): response = self. sender.send(f_ing) is_recognized = response[ “nessage” ]=="RECOGNIZED" print (response[ "nessage"]) if is_recognized: print(response[ "name" ]+": ‘+response["percent"]) hitps:slwwn.codeproject.com/Artcles/530570 1Web-API-or-Face-Recogntion?display=Print 28 23/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 = os.path.join(save_path, ps) cv2.inwrite(ps, f_ime) ‘"03d" % percent)+" png’ t2 = time.tine() 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 cv2.waitkey(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 @app.route("/api/faceimg”, methods=[ 'POST']) cof test(): response = () r_status = 220 = request print ("Processing recognition request. t1 = time.time() nparr = np.fromstring(r.data, np.uint8) img = cv2.indecode(nparr, cv2. INREAD_COLOR) hitpsslwwn.codeproject.com/Artcles/530570 1MWeb-API-or-Face-Recogntion?display=Print 38 23/10/2021 16:69 \Web API for Face Recognition - CodeProject embds = rec.embeddings(img) data = rec.recognize(enbds, f_db) 12 = time.time() 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 = 05.path. join(save_path, ps) cv2.imwrite(ps, ing) # encode response using jsonpickle response_pickled = jsonpickle.encode(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(sys.argv[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 = "0.0.0.0" hitps:slwwn.codeproject.com/Artcles/530570 1Web-API-or-Face-Recogntion?display=Print 48 23/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.Inx.py 0.0.0.0 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(sys.argv[2]) ‘#host = str(sys.argv[2]) #port = int(sys.argv[3]) v_file = r"/hone/pi/Desktop/P1_FR/video/5_2.mp4" host = "http://192.168.2.135" 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) = vr.process(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:lww.codeproject.com/Artcles/530570 1Web-API-or-Face-Recogntion?dsplay=Print 58 23/10/2021 16:69 \Web API for Face Recogniion - CodeProject video 2 recognize web video 4 recognize web hitpsslww.codeproject.com/Artcles/530570 1Web-API-or-Face-Recogntion?display=Print 38 2a/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 https://www.codeproject.com/Articles/S305701/Web-API-for-Face- 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:lwwu.codeproject.com/Artcles/530570 1Web-API-or-Face-Recogntion?dsplay=Print 718 23/10/2021 16:69 \Web API for Face Recognition - CodeProject Cookies Terms of Use Web04 2620211019, hitps:lww.codeproject.com/Artcles/530570 1Web-API-or-Face-Recognion?display=Print oo

You might also like