1、代碼
from aip import AipFace
import cv2
import time
import base64
from PIL import Image
from io import BytesIO
import pyttsx3
# """ 你的 APPID AK SK """
APP_ID = '1965####'
API_KEY = 'YXL65ekIloykyjrT4kzc####'
SECRET_KEY = 'lFiapBoZ5eBwOFyxMbiwQDmClg1u####'
client = AipFace(APP_ID, API_KEY, SECRET_KEY)
# def frame2base64(frame):
# img = Image.fromarray(frame) #將每一幀轉為Image
# output_buffer = BytesIO() #創建一個BytesIO
# img.save(output_buffer, format='JPEG') #寫入output_buffer
# byte_data = output_buffer.getvalue() #在記憶體中讀取
# image = base64.b64encode(byte_data) #轉為BASE64
# return image #轉碼成功 回傳base64編碼
def generate():
camera = cv2.VideoCapture(0)
engine = pyttsx3.init()
try:
while True:
engine = pyttsx3.init()
ret, img = camera.read()
cv2.imwrite("E://Ana/face.png",img)
cv2.imshow("呼叫攝像頭", img)
imageType = "BASE64"
groupIdList = "1,2,3,4"
""" 如果有可選引數 """
options = {}
options["max_face_num"] = 4
options["match_threshold"] = 70
options["quality_control"] = "NORMAL"
options["liveness_control"] = "NONE"
# options["user_id"] = "233451"
options["max_user_num"] = 4
# """ 帶引數呼叫人臉搜索 """
with open("E://Ana/face.png", 'rb') as fp:
imageB = base64.b64encode(fp.read())
image = str(imageB, 'utf-8')
""" 呼叫人臉搜索 """
result = client.search(image, imageType, groupIdList, options)
engine.runAndWait()
print(2)
if result:
if not result['result']:
continue
name = result['result']['user_list'][0]['user_id']#獲取名字
score = result['result']['user_list'][0]['score']#獲取相似度
if name == 'cgh_1':
if score>80:
print(score)
print(name)
engine.say("華來了")
elif name == 'yjc_1':
if score > 80:
print(score)
print(name)
engine.say("楊來了")
elif name == 'cjy_1':
if score > 80:
print(score)
print(name)
engine.say("言來了")
elif name == 'hjy_1':
if score > 80:
print(score)
print(name)
engine.say("怡來了")
else:
print("匹配失敗")
else:
continue
except Exception as e:
print(e)
finally:
# 釋放資源
engine.runAndWait()
camera.release()
cv2.destroyAllWindows()
generate()
2、實作步驟
2.1、獲取百度ID和key
在百度API中可以申請到免費的API賬號,每天有免費的幾千次人臉識別,下圖為我申請的API賬號,然后在人臉庫中創建需要識別的人臉就行了


2.2、創建客戶端
APP_ID = '1965####'
API_KEY = 'YXL65ekIloykyjrT4kzc####'
SECRET_KEY = 'lFiapBoZ5eBwOFyxMbiwQDmClg1u####'
client = AipFace(APP_ID, API_KEY, SECRET_KEY)
2.3、實作人臉識別
主要內容就是引數需要自己修改,然后獲取視頻影像,然后通過對比看一下,對比出來的名字,然后再看一下分數即可,我在里面加了一個電腦播報的,所以只要識別出來,電腦就會播報,延遲不超過以秒,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/219012.html
標籤:其他
上一篇:pyqt5環境搭建
