一:关于Facenet
Facenet 是Florian Schroff等人2015年提出的一种人脸识别的模型,论文地址
FaceNet: A Unified Embedding for Face Recognition and Clustering
Facenet 的基本流程是首先在给出的图片中选出人脸的区域,然后计算人脸的特征embings,输入一张图片,就可以将所有包含在图片中的所有人脸特征,每个人脸特曾可以使用128维的向量表示。要做人脸识别,那么一个简单的思路就是,计算待检测样本与数据库中的人脸特征数据对比距离,距离越小那么待检测的样本就越有可能是这个人。为了避免数据库中无事前录入的人脸信息,而导致的错误,我们可以设置一个最小的距离阈值。
二:开发pepper机器人
pepper机器人只支持python2.7版本的,而我们平时使用的是python3.x,模型中涉及到各种依赖,两个版本之间的兼容性难以处理。博主采取的方式是机器人采集照片通过网络传回服务器,服务器部署人脸识别模型,服务器处理完成之后,把识别结果返回给机器人,机器人做相关的处理。
三 :代码
1机器人采集照片,并传送给服务器
import naoqi
from naoqi import ALProxy
import socket
import time
address = ('192.168.100.22', 2567)
photoCaptureProxy = ALProxy("ALPhotoCapture", "192.168.100.108", 9559)
tts=ALProxy("ALTextToSpeech", "192.168.100.108", 9559)
photoCaptureProxy.setResolution(2)
photoCaptureProxy.setPictureFormat("jpg")
def takephoto(): #机器人以每秒1张速度采集照片
while True:
photos = photoCaptureProxy.takePictures(1, "/home/nao/recordings/cameras/", "image")
send(photos)
print('即将发送{}'.format(photos))
time.sleep(1)
def getfaceInfor():
pass
def send(photos): #将照片信息传回服务器,接受服务器的处理结果,并作出相关的操作
for photo in photos[0]:
print('sending {}'.format(photo))
data = file_deal(photo)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(address)
sock.send('{}|{}'.format(len(data), file).encode())
reply = sock.recv(1024)
if 'ok' == reply.decode():
go = 0
total = len(data)
while go < total:
data_to_send = data[go:go + 1024]
sock.send(data_to_send)
go += len(data_to_send)
reply = sock.recv(1024)
if 'copy' == reply.decode():
print('{} send successfully'.format(photo))
sock.send(b'infor')
person_infor = sock.recv(1024)
person_name = person_infor
if 'no' != person_name:
sayHello(person_name)
sock.close()
def sayHello(person_name): #说出人名
tts.say('你好{}'.format(person_name))
def file_deal(file_path):
mes = b''
try:
file = open(file_path,'rb')
mes = file.read()
except:
print('error{}'.format(file_path))
else:
file.close()
return mes
if __name__ == '__main__':
takephoto()
2,服务器端代码
LOCAL_IP = '192.168.100.22'
PORT = 2567
def init_sourceData(path):
image_paths = []
try:
image_dir = os.listdir(path)
for file in image_dir:
image_path = os.path.join(path,file)
if os.path.isfile(image_path):
image_paths.append(image_path)
except FileNotFoundError as e:
print(e)
return image_paths
def generate_dataBase(image_paths):
minsize = 20 # minimum size of face
threshold = [0.6, 0.7, 0.7] # three steps's threshold
factor = 0.709 # scale factor
margin = 44
image_size = 160
controller = None
sessD = None
data_h5 = None
try:
data_h5 = h5py.File('people_infor.h5','w')
except FileNotFoundError as e:
print(e)
with tf.Graph().as_default():
print