<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Matt</title>
    <description>The latest articles on Forem by Matt (@mattcohard).</description>
    <link>https://forem.com/mattcohard</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1365213%2F8c83d389-e660-4e48-8bff-a61a36ea5ce1.PNG</url>
      <title>Forem: Matt</title>
      <link>https://forem.com/mattcohard</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/mattcohard"/>
    <language>en</language>
    <item>
      <title>How I Implemented Facial Recognition using Python</title>
      <dc:creator>Matt</dc:creator>
      <pubDate>Wed, 20 Mar 2024 10:25:55 +0000</pubDate>
      <link>https://forem.com/mattcohard/how-i-implemented-facial-recognition-using-python-3826</link>
      <guid>https://forem.com/mattcohard/how-i-implemented-facial-recognition-using-python-3826</guid>
      <description>&lt;p&gt;I recently implemented Luxand.cloud's face recognition API into my project, and it's been working great. Below you'll find the steps how to integrate it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting up the environment
&lt;/h2&gt;

&lt;p&gt;Install the required libraries by running the following command in your terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip3 install requests
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Adding people to the database
&lt;/h2&gt;

&lt;p&gt;Create a Python file and import the necessary libraries:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/usr/bin/env python3

import requests
import json

API_TOKEN = "c9d85df66aba4f1085682a53b00a0cbf"
Define a function to the person to the database:

def add_person(name, image_path, collections = ""):
    if image_path.startswith("https://"):
        files = {"photos": image_path}
    else:
        files = {"photos": open(image_path, "rb")}

    response = requests.post(
        url="https://api.luxand.cloud/v2/person",
        headers={"token": API_TOKEN},
        data={"name": name, "store": "1", "collections": collections},
        files=files,
    )

    if response.status_code == 200:
        person = response.json()

        print("Added person", name, "with UUID", person["uuid"])
        return person["uuid"]
    else:
        print("Can't add person", name, ":", response.text)
        return None
Now you can add people to the database one by one:

person_name = "person name"

# path to image can be local file name or URL
path_to_image = "path_to_image"

# enter the collection name to create the collection and add person to it
collection_name = ""

person_uuid = add_person(person_name, path_to_image, collection_name)
Improving accuracy of recognition
If you upload more than one image of a person, the face recognition engine will be able to recognize people with better accuracy. To do that, create a function that can add faces to a person.

def add_face(person_uuid, image_path):
    if image_path.startswith("https://"):
        files = {"photo": image_path}
    else:
        files = {"photo": open(image_path, "rb")}


    response = requests.post(
        url="https://api.luxand.cloud/v2/person/%s" % person_uuid,
        headers={"token": API_TOKEN},
        data={"store": "1"},
        files=files
    )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, add more photos to this person to improve face recognition accuracy. While having a single image of a person is acceptable, adding 3-5 more images will significantly improve face recognition accuracy.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;add_face(person_uuid, "path_to_another_image")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Recognizing faces
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def recognize_face(image_path):
    url = "https://api.luxand.cloud/photo/search/v2"
    headers = {"token": API_TOKEN}

    if image_path.startswith("https://"):
        files = {"photo": image_path}
    else:
        files = {"photo": open(image_path, "rb")}

    response = requests.post(url, headers=headers, files=files)
    result = json.loads(response.text)

    if response.status_code == 200:
        return response.json()
    else:
        print("Can't recognize people:", response.text)
        return None


image_path_recognition = "path_to_image_for_recognition"
recognize_face(image_path_recognition)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Replace the path_to_image_for_recognition with the actual image file path for recognition.&lt;/p&gt;

&lt;h2&gt;
  
  
  Complete code
&lt;/h2&gt;

&lt;p&gt;Here you can find the complete version of the code I used above. You can just copy and paste it into your file, replace parameters, and it will work.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/usr/bin/env python3
import requests
import json

API_TOKEN = "c9d85df66aba4f1085682a53b00a0cbf"

# This function adds a person to the database
def add_person(name, image_path, collections = ""):
    if image_path.startswith("https://"):
        files = {"photos": image_path}
    else:
        files = {"photos": open(image_path, "rb")}

    response = requests.post(
        url="https://api.luxand.cloud/v2/person",
        headers={"token": API_TOKEN},
        data={"name": name, "store": "1", "collections": collections},
        files=files,
    )

    if response.status_code == 200:
        person = response.json()

        print("Added person", name, "with UUID", person["uuid"])
        return person["uuid"]
    else:
        print("Can't add person", name, ":", response.text)
        return None


# This function recognizes a face in an image
def recognize_face(image_path):
    url = "https://api.luxand.cloud/photo/search/v2"
    headers = {"token": API_TOKEN}

    if image_path.startswith("https://"):
        files = {"photo": image_path}
    else:
        files = {"photo": open(image_path, "rb")}

    response = requests.post(url, headers=headers, files=files)
    result = json.loads(response.text)

    if response.status_code == 200:
        return response.json()
    else:
        print("Can't recognize people:", response.text)
        return None


if __name__ == "__main__":
    # Adding a person
    person_uuid = add_person("John Smith", "face1.jpg", "Employees")
    if person_uuid == None:
        exit(1)

    # Recognizing a face
    recognized_people = recognize_face("face2.jpg")

    print("Recognized people:", recognized_people)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>webdev</category>
      <category>python</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
