【手把手教你做一个工作辅助机器人】--- 人脸识别模块_服务器

 欢迎关注我👆,收藏下次不迷路┗|`O′|┛ 嗷~~

 

目录

一.模块介绍

二.功能演示案例

三.完整代码


一.模块介绍

    人脸识别模块可以离线识别出画面中的人脸,并于主人人脸进行比对。此外,该模块还具有认证信息反馈,人脸位置信息反馈(用于GUI页面的眼睛方向)等功能。

二.功能演示案例

    这部分代码可以比对两张图片是否是同一个人。

import face_recognition
import numpy as np
import sys
import cv2


def theSamePerson(one_pic,two_pic):
    '''
    给定两张图片,判断是否是同一个人
    '''
    chenglong = face_recognition.load_image_file(one_pic)
    unknown_image = face_recognition.load_image_file(two_pic)
    
    biden_encoding = face_recognition.face_encodings(chenglong)[0]
    unknown_encoding = face_recognition.face_encodings(unknown_image)[0]
    results = face_recognition.compare_faces([biden_encoding], unknown_encoding,tolerance=0.55)
    print('results: ',results)
    return results[0]

def main():
    src1,src2 = sys.argv[1],sys.argv[2]
    theSamePerson(src1,src2)


main()
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.

三.完整代码

import face_recognition
import numpy as np
import sys
import cv2,os
import requests,time
from datetime import datetime


anan_pic=face_recognition.load_image_file('../settings/anan.jpg')
face_loc = face_recognition.face_locations(anan_pic)[0]
anan_encoding=face_recognition.face_encodings(np.array(anan_pic[face_loc[0]:face_loc[2],face_loc[3]:face_loc[1],:]))[0]

# 创建视频捕获对象,参数0表示默认相机  
cap = cv2.VideoCapture(0)  
  
# 检查相机是否成功打开  
if not cap.isOpened():  
    print("无法打开相机")  
else:  
    # 循环读取视频帧  
    while True:  
        # 读取一帧  
        for i in range(50):
            ret, frame = cap.read()  
  
        # 如果正确读取帧,ret为True  
        if not ret:  
            print("无法读取视频帧")  
            break  
        loc_x='NONE'
        loc_y='NONE'


  
        # 查找视频帧中的人脸和人脸编码  
        try:
            face_locations = face_recognition.face_locations(frame)
            face_location=face_locations[0]
            loc_x=(((face_location[1]+face_location[3])//2)/frame.shape[1])-0.5
            loc_y=(((face_location[0]+face_location[2])//2)/frame.shape[0])-0.5
            loc_x=loc_x*1.5
            loc_y=loc_y*1.5
            url = 'http://127.0.0.1:6999/set_face_pose' 
            response = requests.post(url, json={'loc_x':str(loc_x),'loc_y':str(loc_y)})   
            current_timestamp = str(datetime.now()).replace(":","-")+"---"+str(time.time())
            current_timestamp=current_timestamp.replace(" ","-").replace(".","-")
            cv2.imwrite('face_pic'+current_timestamp+'.jpg',frame[face_location[0]:face_location[2],face_location[3]:face_location[1],:])
            url = 'http://127.0.0.1:6999/upload_face_pics_file'  
            files = {'file': open('face_pic'+current_timestamp+'.jpg', 'rb')}  
            response = requests.post(url, files=files)
            os.system('rm -rf face_pic'+current_timestamp+'.jpg')
        except:
            url = 'http://127.0.0.1:6999/set_face_pose' 
            response = requests.post(url, json={'loc_x':str(loc_x),'loc_y':str(loc_y)})   

        for face_loc in face_locations:
            try:
                face_encoding = face_recognition.face_encodings(np.array(frame[face_loc[0]:face_loc[2],face_loc[3]:face_loc[1],:]))[0]
            
                (top, right, bottom, left)=face_loc
                # 比较人脸编码  
                results = face_recognition.compare_faces([anan_encoding], face_encoding,tolerance=0.55)  
  
                # 如果找到匹配的人脸  
                if True in results:  
                    # 获取匹配索引  
                    match_index = results.index(True)  
                    url = 'http://127.0.0.1:6999/set_master_status_YES'  
                    # 发送POST请求  
                    response = requests.post(url, json={})  
                    print(f"Found a match! Similarity: {np.linalg.norm(anan_encoding - face_encoding):.2f}")  
  
                # 在人脸周围画一个框  
                cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2)  
            except:
                print("error")
        # 显示帧  
        #cv2.imshow('cam', frame)  
        # 按下'q'键退出循环  
        #if cv2.waitKey(1) == ord('q'):  
        #    break  
  
# 释放视频捕获对象  
cap.release()  
# 关闭所有OpenCV窗口  
cv2.destroyAllWindows()
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.

 非常感谢您花时间阅读我的博客,希望这些分享能为您带来启发和帮助。期待您的反馈与交流,让我们共同成长,再次感谢!

👇个人网站👇

 安城安的云世界

 

【手把手教你做一个工作辅助机器人】--- 人脸识别模块_容器_02