Python Forum
android e python fece recognition
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
android e python fece recognition
#1
Hi, I'm making an app in Android Studio that communicates with Python via socket and I have to implement the recognition. I did it this way but when I insert two photos with the same face but different light it gives me different biometric keys, how can I solve it?
import math
import numpy as np
import face_recognition
import os
import sys
from socket import *

""" PARAMETRI INIZIALI """
zm_fc = 0.50
TCP_IP = "100.101.0.11"  # Indirizzo IP del server
T_PORT = 65535  # Porta del server

socket_tcp = socket(AF_INET, SOCK_STREAM)
socket_tcp.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
socket_tcp.bind((TCP_IP, T_PORT))
socket_tcp.listen(1)

def receiveImage(conn):
    try:
        with open('/Users/Bia/Desktop/ReceivedImage.png', 'wb') as img:
            # Ricevi la dimensione dell'array di byte all'inizio del flusso
            size_bytes = conn.recv(4)
            image_size = int.from_bytes(size_bytes, byteorder='big')

            # Ricevi l'immagine
            remaining_size = image_size
            while remaining_size > 0:
                data = conn.recv(min(1024, remaining_size))
                if not data:
                    # Se non ci sono più dati da ricevere, esci dal ciclo
                    break
                img.write(data)
                remaining_size -= len(data)

        print("Immagine ricevuta dal dispositivo Android")

    except Exception as e:
        print("Errore durante la ricezione dell'immagine:", str(e))

def sendKey(key, conn):
    try:
        if key is not None:
            conn.send(key.encode())
            print("Chiave biometrica inviata al dispositivo Android")
        else:
            # Se il volto non è stato rilevato, invia un messaggio di "NO_FACE_DETECTED"
            conn.send("NO_FACE_DETECTED".encode())
            print("Nessun volto rilevato. Invio del messaggio di 'NO_FACE_DETECTED'")
    except Exception as e:
        print("Errore durante l'invio della chiave biometrica:", str(e))
    finally:
        conn.close()

def get_frame():
    try:
        frame = face_recognition.load_image_file('/Users/Bia/Desktop/ReceivedImage.png')
        face_locations = face_recognition.face_locations(frame)

        if len(face_locations) > 0:
            print("Volto rilevato.")
            face_encodings = face_recognition.face_encodings(frame, face_locations)
            return True, face_encodings[0]
        else:
            print("Nessun volto rilevato.")
            return False, None

    except Exception as e:
        print("Errore in get_frame:", str(e))
        return False, None

def start():
    try:
        ret, face_encoding = get_frame()
        if ret and face_encoding is not None:
            print("Chiave biometrica generata:", str(face_encoding))
            return str(face_encoding)
        else:
            print("Nessun volto rilevato.")
            return None
    except Exception as e:
        print("Errore nella generazione della chiave biometrica:", str(e))
    return None

if __name__ == '__main__':
    print("In attesa del dispositivo Android ...")

    while True:
        conn, addr = socket_tcp.accept()

        print("Connessione con il dispositivo Android stabilita. Pronto per ricevere l'immagine")
        receiveImage(conn)
        print("Generazione della chiave biometrica in corso ...")

        chiave_biometrica = start()
        if chiave_biometrica is not None:
            print("La chiave biometrica è stata generata:", chiave_biometrica)
            print("Invio della chiave al dispositivo Android ...")
            sendKey(chiave_biometrica, conn)
            conn.close()  # Chiudo la connessione dopo aver inviato la chiave
            
        else:
            print("Nessun volto rilevato. Si prega di reinserire le informazioni dell'utente.")
            chiave_messaggio = "NO_FACE_DETECTED"

            # Chiudo la connessione prima di inviare il messaggio
            conn.close()

            # Creo una nuova connessione per inviare il messaggio
            conn, addr = socket_tcp.accept()
            conn.send(chiave_messaggio.encode())

            conn.close()
thanks **smile**
buran write Sep-10-2023, 05:52 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
Hello, territorial io I think instead of relying on a single face encoding, you can generate multiple encodings for each face and compare them to find a match. This approach allows for more robust matching by considering variations in lighting and other factors.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Running Python under Termux on Android tablet dfkettle 1 3,508 Jan-25-2023, 02:34 PM
Last Post: dfkettle
  Python-for-Android:p4a: syntax error in main.py while compiling apk jttolleson 2 1,870 Sep-17-2022, 04:09 AM
Last Post: jttolleson
  Python bot for ADB Android SCRCPY Bizzy_ 3 4,516 May-01-2021, 10:41 AM
Last Post: Larz60+
  Can Embedded Python run any shared library on Android ? sprotz 0 2,334 Nov-08-2020, 12:21 PM
Last Post: sprotz
  Python Speech recognition, word by word AceScottie 6 16,027 Apr-12-2020, 09:50 AM
Last Post: vinayakdhage
  Developing Python with OpenCV into an Android App AviationFreak 1 6,835 Sep-29-2019, 08:55 AM
Last Post: wavic
  Where is usually located the python interpreter on android devices? anddontyoucomebacknomore 1 2,265 Jul-03-2019, 06:32 PM
Last Post: metulburr
  Integrating Python Script for Android App everythingisenergy 0 6,286 Jan-15-2019, 07:39 PM
Last Post: everythingisenergy

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020