简体   繁体   English

将面部识别数据传递到另一个python脚本

[英]Pass facial recognition data to another python script

I am going to start on a python project where I'm making a voice assistant using pyttsx and I'm thinking it would be a cool feature to integrate facial recognition into the assistant. 我将开始一个Python项目,在该项目中,我将使用pyttsx制作语音助手,并且我认为将面部识别集成到助手中将是一个很酷的功能。 I just wanted to ask that if I make a python script for face recognition (opencv), can I pass the result to the voice assistant so it can spell it out loud? 我只是想问一下,如果我制作一个用于人脸识别(opencv)的python脚本,可以将结果传递给语音助手,以便将其大声地拼出来吗? Any help appreciated. 任何帮助表示赞赏。

1) install face_recognition 1)安装face_recognition

pip install face_recognition

2)load faces using cv2 imread. 2)使用cv2 imread加载面孔。

import cv2  

#faces that look like obama
obama_face1 = cv2.imread("obama_1.png")  
obama_face2 = cv2.imread("obama_2.png")  

#faces that look like trump
trump_face = cv2.imread("trump.png")  

3) Create a face encoding. 3)创建人脸编码。 Face recognition will create something called a face encoding for a face. 人脸识别将为人脸创建一种称为人脸编码的东西。

import facial_recognition  

obama_vector1 = face_recognition.face_encodings(obama_face1)[0]
obama_vector2 = face_recognition.face_encodings(obama_face2)[0]

trump_vector1 = face_recognition.face_encodings(trump_face)[0]

4) create a similarity function. 4)创建相似度函数。 A similarity function tells us if two face encodings are similar. 相似度函数告诉我们两个面部编码是否相似。

import scipy.spatial.distance
def cosine_similarity(u:np.array,b:np.array) -> np.float:
    return scipy.spatial.distance.cosine(u,b)  

5) run the similarity function between faces 5)运行面孔之间的相似度函数


trump_X_obama1  = cosine_similarity(obama_vector1, trump_vector)  
trump_X_obama2  = cosine_similarity(obama_vector2, trump_vector)  
obama1_X_obama2 = cosine_similarity(obama_vector1, obama_vector2)  

6) When you run cosine_similarity on two encodings, and the number is small then those two encodings are similar. 6)当您对两种编码运行cosine_similarity且数量较小时,则这两种编码相似。 If the number is large they are less similar. 如果数字较大,则它们不太相似。

print("Obama1 is more like Obama2 than it is Trump", obama1_X_obama2 < trump_X_obama1)  

7) To create something that recognizes you. 7)创造一些能认出你的东西。 Save a lot of encodings of yourself. 节省很多编码。 Try encoding other faces as well. 尝试编码其他面孔。 Determine what similarity is good enough. 确定什么相似性足够好。 The encoding is what you need to determine if you're similar. 您需要确定编码是否相似。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM