working face identify function
This commit is contained in:
		
							parent
							
								
									5bd4b4c100
								
							
						
					
					
						commit
						3ee1e00519
					
				
					 18 changed files with 9516 additions and 9334 deletions
				
			
		| 
						 | 
				
			
			@ -4,17 +4,29 @@ import cv2
 | 
			
		|||
import base64
 | 
			
		||||
import numpy as np
 | 
			
		||||
from deepface import DeepFace
 | 
			
		||||
import face_recognition as face
 | 
			
		||||
import os
 | 
			
		||||
 | 
			
		||||
app = Flask(__name__)
 | 
			
		||||
 | 
			
		||||
face_encodings: list = []
 | 
			
		||||
face_names: list = []
 | 
			
		||||
 | 
			
		||||
def init_face() -> None:
 | 
			
		||||
    for file in os.scandir("faces"):
 | 
			
		||||
        face_name = file.name.split('.')
 | 
			
		||||
        face_name = '.'.join(face_name[0:len(face_name)-1])
 | 
			
		||||
        face_names.append(face_name)
 | 
			
		||||
        face_image = face.load_image_file(file.path)
 | 
			
		||||
        face_encodings.append(face.face_encodings(face_image)[0])
 | 
			
		||||
 | 
			
		||||
@app.route('/')
 | 
			
		||||
def home():
 | 
			
		||||
def home() -> str:
 | 
			
		||||
    return '<h1>Ching Chong Bing Bong Ding Dong!!</h1>'
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@app.route('/process_image', methods=['POST'])
 | 
			
		||||
def process_image():
 | 
			
		||||
def process_image() -> str:
 | 
			
		||||
    print(request.data)
 | 
			
		||||
    request_data = json.loads(request.data.decode("utf-8"))
 | 
			
		||||
    img_nparr = np.frombuffer(base64.b64decode(request_data['image']), np.uint8)
 | 
			
		||||
| 
						 | 
				
			
			@ -25,6 +37,30 @@ def process_image():
 | 
			
		|||
    except:
 | 
			
		||||
        return []
 | 
			
		||||
 | 
			
		||||
@app.route('/identify_face', methods=['POST'])
 | 
			
		||||
def identify_face() -> str:
 | 
			
		||||
    request_data = json.loads(request.data.decode("utf-8"))
 | 
			
		||||
    target_confidence: float = request_data['target_confidence']
 | 
			
		||||
    img_nparr = np.frombuffer(base64.b64decode(request_data['image']), np.uint8)
 | 
			
		||||
    img = cv2.imdecode(img_nparr,cv2.IMREAD_COLOR)
 | 
			
		||||
    img = cv2.resize(img, (0,0), fx=0.5,fy=0.5)
 | 
			
		||||
    img = np.ascontiguousarray(img[:, :, ::-1])
 | 
			
		||||
    face_locations = face.face_locations(img)
 | 
			
		||||
    face_encodings_img = face.face_encodings(img, face_locations)
 | 
			
		||||
    response: list = []
 | 
			
		||||
    for face_encoding in face_encodings_img:
 | 
			
		||||
        face_distances = face.face_distance(face_encodings, face_encoding)
 | 
			
		||||
        index = np.argmin(face_distances)
 | 
			
		||||
        confidence = 1-face_distances[index]
 | 
			
		||||
        if confidence >= target_confidence:
 | 
			
		||||
            response.append({'name':face_names[index],'confidence': confidence})
 | 
			
		||||
    return response
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
if __name__ == '__main__':
 | 
			
		||||
    init_face()
 | 
			
		||||
    print(face_names)
 | 
			
		||||
    print(face_encodings)
 | 
			
		||||
 | 
			
		||||
    app.run()
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										
											BIN
										
									
								
								access_control_python_server/faces/boom.jpg
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								access_control_python_server/faces/boom.jpg
									
										
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| 
		 After Width: | Height: | Size: 148 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								access_control_python_server/faces/skt.jpg
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								access_control_python_server/faces/skt.jpg
									
										
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| 
		 After Width: | Height: | Size: 124 KiB  | 
| 
						 | 
				
			
			@ -1,3 +1,4 @@
 | 
			
		|||
flask
 | 
			
		||||
opencv-python
 | 
			
		||||
deepface
 | 
			
		||||
deepface
 | 
			
		||||
face_recognition
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue