Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
27 views
Web API For Face Recognition
Uploaded by
Gabriel Gomes
AI-enhanced title
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
Download now
Download
Save Web API for Face Recognition For Later
Download
Save
Save Web API for Face Recognition For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
0 ratings
0% found this document useful (0 votes)
27 views
Web API For Face Recognition
Uploaded by
Gabriel Gomes
AI-enhanced title
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
Download now
Download
Save Web API for Face Recognition For Later
Carousel Previous
Carousel Next
Save
Save Web API for Face Recognition For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
Download now
Download
You are on page 1
/ 8
Search
Fullscreen
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 1823/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 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 = 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 3823/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 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.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 5823/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 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 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 71823/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
The Subtle Art of Not Giving a F*ck: A Counterintuitive Approach to Living a Good Life
From Everand
The Subtle Art of Not Giving a F*ck: A Counterintuitive Approach to Living a Good Life
Mark Manson
4/5 (6386)
Principles: Life and Work
From Everand
Principles: Life and Work
Ray Dalio
4/5 (634)
The Gifts of Imperfection: Let Go of Who You Think You're Supposed to Be and Embrace Who You Are
From Everand
The Gifts of Imperfection: Let Go of Who You Think You're Supposed to Be and Embrace Who You Are
Brené Brown
4/5 (1160)
Never Split the Difference: Negotiating As If Your Life Depended On It
From Everand
Never Split the Difference: Negotiating As If Your Life Depended On It
Chris Voss
4.5/5 (983)
The Glass Castle: A Memoir
From Everand
The Glass Castle: A Memoir
Jeannette Walls
4/5 (8302)
Grit: The Power of Passion and Perseverance
From Everand
Grit: The Power of Passion and Perseverance
Angela Duckworth
4/5 (633)
Sing, Unburied, Sing: A Novel
From Everand
Sing, Unburied, Sing: A Novel
Jesmyn Ward
4/5 (1254)
Shoe Dog: A Memoir by the Creator of Nike
From Everand
Shoe Dog: A Memoir by the Creator of Nike
Phil Knight
4.5/5 (933)
The Perks of Being a Wallflower
From Everand
The Perks of Being a Wallflower
Stephen Chbosky
4/5 (10337)
Her Body and Other Parties: Stories
From Everand
Her Body and Other Parties: Stories
Carmen Maria Machado
4/5 (887)
The Hard Thing About Hard Things: Building a Business When There Are No Easy Answers
From Everand
The Hard Thing About Hard Things: Building a Business When There Are No Easy Answers
Ben Horowitz
4.5/5 (361)
Hidden Figures: The American Dream and the Untold Story of the Black Women Mathematicians Who Helped Win the Space Race
From Everand
Hidden Figures: The American Dream and the Untold Story of the Black Women Mathematicians Who Helped Win the Space Race
Margot Lee Shetterly
4/5 (1007)
Steve Jobs
From Everand
Steve Jobs
Walter Isaacson
4/5 (3237)
Elon Musk: Tesla, SpaceX, and the Quest for a Fantastic Future
From Everand
Elon Musk: Tesla, SpaceX, and the Quest for a Fantastic Future
Ashlee Vance
4.5/5 (581)
The Emperor of All Maladies: A Biography of Cancer
From Everand
The Emperor of All Maladies: A Biography of Cancer
Siddhartha Mukherjee
4.5/5 (297)
A Man Called Ove: A Novel
From Everand
A Man Called Ove: A Novel
Fredrik Backman
4.5/5 (5058)
Angela's Ashes: A Memoir
From Everand
Angela's Ashes: A Memoir
Frank McCourt
4.5/5 (943)
The Art of Racing in the Rain: A Novel
From Everand
The Art of Racing in the Rain: A Novel
Garth Stein
4/5 (4346)
The Yellow House: A Memoir (2019 National Book Award Winner)
From Everand
The Yellow House: A Memoir (2019 National Book Award Winner)
Sarah M. Broom
4/5 (100)
The Little Book of Hygge: Danish Secrets to Happy Living
From Everand
The Little Book of Hygge: Danish Secrets to Happy Living
Meik Wiking
3.5/5 (458)
Brooklyn: A Novel
From Everand
Brooklyn: A Novel
Colm Tóibín
3.5/5 (2091)
Yes Please
From Everand
Yes Please
Amy Poehler
4/5 (1993)
Devil in the Grove: Thurgood Marshall, the Groveland Boys, and the Dawn of a New America
From Everand
Devil in the Grove: Thurgood Marshall, the Groveland Boys, and the Dawn of a New America
Gilbert King
4.5/5 (278)
The World Is Flat 3.0: A Brief History of the Twenty-first Century
From Everand
The World Is Flat 3.0: A Brief History of the Twenty-first Century
Thomas L. Friedman
3.5/5 (2283)
Bad Feminist: Essays
From Everand
Bad Feminist: Essays
Roxane Gay
4/5 (1077)
The Woman in Cabin 10
From Everand
The Woman in Cabin 10
Ruth Ware
3.5/5 (2780)
A Tree Grows in Brooklyn
From Everand
A Tree Grows in Brooklyn
Betty Smith
4.5/5 (2032)
The Outsider: A Novel
From Everand
The Outsider: A Novel
Stephen King
4/5 (2838)
The Sympathizer: A Novel (Pulitzer Prize for Fiction)
From Everand
The Sympathizer: A Novel (Pulitzer Prize for Fiction)
Viet Thanh Nguyen
4.5/5 (141)
A Heartbreaking Work Of Staggering Genius: A Memoir Based on a True Story
From Everand
A Heartbreaking Work Of Staggering Genius: A Memoir Based on a True Story
Dave Eggers
3.5/5 (692)
Team of Rivals: The Political Genius of Abraham Lincoln
From Everand
Team of Rivals: The Political Genius of Abraham Lincoln
Doris Kearns Goodwin
4.5/5 (1912)
Wolf Hall: A Novel
From Everand
Wolf Hall: A Novel
Hilary Mantel
4/5 (4086)
On Fire: The (Burning) Case for a Green New Deal
From Everand
On Fire: The (Burning) Case for a Green New Deal
Naomi Klein
4/5 (76)
Fear: Trump in the White House
From Everand
Fear: Trump in the White House
Bob Woodward
3.5/5 (830)
Manhattan Beach: A Novel
From Everand
Manhattan Beach: A Novel
Jennifer Egan
3.5/5 (906)
Rise of ISIS: A Threat We Can't Ignore
From Everand
Rise of ISIS: A Threat We Can't Ignore
Jay Sekulow
3.5/5 (143)
John Adams
From Everand
John Adams
David McCullough
4.5/5 (2544)
The Light Between Oceans: A Novel
From Everand
The Light Between Oceans: A Novel
M L Stedman
4.5/5 (813)
News Track - News Aggregator
PDF
No ratings yet
News Track - News Aggregator
6 pages
Python Vs C Plus Plus Series - Polymorphism and Duck Typing
PDF
No ratings yet
Python Vs C Plus Plus Series - Polymorphism and Duck Typing
8 pages
Core With Dapper and Vs 2017 Using JWT Authentication WEB API and Consume It in Angular2 Client Application - CodeProject
PDF
No ratings yet
Core With Dapper and Vs 2017 Using JWT Authentication WEB API and Consume It in Angular2 Client Application - CodeProject
11 pages
Noisy Crypt
PDF
No ratings yet
Noisy Crypt
5 pages
Tetris On Canvas - CodeProject
PDF
No ratings yet
Tetris On Canvas - CodeProject
8 pages
Integration of Cake Build Script With TeamCity - CodeProject
PDF
No ratings yet
Integration of Cake Build Script With TeamCity - CodeProject
6 pages
The Intel Assembly Manual - CodeProject
PDF
No ratings yet
The Intel Assembly Manual - CodeProject
28 pages
Introducing SimpleSamSettings - CodeProject
PDF
No ratings yet
Introducing SimpleSamSettings - CodeProject
3 pages
NET Programming Using HP Vertica - CodeProject
PDF
No ratings yet
NET Programming Using HP Vertica - CodeProject
11 pages
Searching Music Incipits in Metric Space With Locality-Sensitive Hashing - CodeProject
PDF
No ratings yet
Searching Music Incipits in Metric Space With Locality-Sensitive Hashing - CodeProject
6 pages
Read Emirates ID in A Web Application - CodeProject
PDF
No ratings yet
Read Emirates ID in A Web Application - CodeProject
7 pages
Typemock vs. Google Mock - A Closer Look - CodeProject
PDF
No ratings yet
Typemock vs. Google Mock - A Closer Look - CodeProject
7 pages
Configuring A Build Pipeline On Azure DevOps For An ASP - Net Core API - CodeProject
PDF
No ratings yet
Configuring A Build Pipeline On Azure DevOps For An ASP - Net Core API - CodeProject
18 pages
Configuring A Build Pipeline On Azure DevOps For An ASP - Net Core API - CodeProject
PDF
No ratings yet
Configuring A Build Pipeline On Azure DevOps For An ASP - Net Core API - CodeProject
10 pages
Creating Web API in ASP - Net Core 2.0 - CodeProject
PDF
No ratings yet
Creating Web API in ASP - Net Core 2.0 - CodeProject
36 pages
Configuring A Build Pipeline On Azure DevOps For An ASP - Net Core API - CodeProject
PDF
No ratings yet
Configuring A Build Pipeline On Azure DevOps For An ASP - Net Core API - CodeProject
10 pages
Learning Entity Framework (Day 4) - Understanding Entity Framework Core and Code First Migrations in EF Core - CodeProject
PDF
No ratings yet
Learning Entity Framework (Day 4) - Understanding Entity Framework Core and Code First Migrations in EF Core - CodeProject
32 pages
Machine Learning With ML - Net and C# - VB - Net - CodeProject
PDF
No ratings yet
Machine Learning With ML - Net and C# - VB - Net - CodeProject
17 pages
Unit Test - Branch Level Code Coverage For .NET Core, XUnit, OpenCover, ReportGenerator and Visual Studio Integration - CodeProject
PDF
No ratings yet
Unit Test - Branch Level Code Coverage For .NET Core, XUnit, OpenCover, ReportGenerator and Visual Studio Integration - CodeProject
8 pages
Brute Force Password Search by Interop - Automation - CodeProject
PDF
No ratings yet
Brute Force Password Search by Interop - Automation - CodeProject
8 pages
The Unwinding: An Inner History of the New America
From Everand
The Unwinding: An Inner History of the New America
George Packer
4/5 (45)
Little Women
From Everand
Little Women
Louisa May Alcott
4.5/5 (2369)
The Constant Gardener: A Novel
From Everand
The Constant Gardener: A Novel
John le Carré
4/5 (277)