import cv2
import smbus
import time
import _thread
import face_recognition
from voice import SetReader
from voice import Reader_Type
from voice import SetVolume
from voice import Speech_text
from voice import EncodingFormat_Type
from voice import GetChipStatus
from voice import ChipStatus_Type
'''
程序执行部分,以上为配置相关的函数和字典定义
'''
SetReader(Reader_Type["Reader_XiaoYan"])
SetVolume(10)
my_image = cv2.imread("/home/pi/Pictures/1.jpg")
my_image = cv2.cvtColor(my_image, cv2.COLOR_BGR2RGB)
my_face_encoding = face_recognition.face_encodings(my_image)
face_cascade = cv2.CascadeClassifier('/usr/share/opencv/haarcascades/haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(0)
def read_cap():
global flag
while cap.isOpened():
flag=0
(ret, frame) = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, minSize=(100, 100), flags=cv2.CASCADE_FIND_BIGGEST_OBJECT)
face_locations = []
for (left, top, width, height) in faces:
right = left+width
bottom = top+height
face_locations.append((int(top), int(right), int(bottom), int(left)))
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)
for face_encoding in face_encodings:
matches = face_recognition.compare_faces(my_face_encoding, face_encoding, tolerance=0.3)
if True in matches:
flag=1
else:
flag=2
cv2.imshow("Recognition", frame)
cv2.waitKey(5)
cap.release()
cv2.destroyAllWindows()
def speech():
global flag
time.sleep(0.3)
while True:
if flag==1:
Speech_text("门已开,请通行",EncodingFormat_Type["GB2312"])
while GetChipStatus() != ChipStatus_Type['ChipStatus_Idle']:
time.sleep(0.001)
elif flag==2:
Speech_text("识别失败",EncodingFormat_Type["GB2312"])
while GetChipStatus() != ChipStatus_Type['ChipStatus_Idle']:
time.sleep(0.001)
_thread.start_new_thread(read_cap,())
_thread.start_new_thread(speech,())
'''
cap=cv2.VideoCapture(0)
while cap.isOpened():
(ret, frame) = cap.read()
frame = cv2.flip(frame, 1)
cv2.imshow("Shot", frame)
k = cv2.waitKey(5)
if k == ord('s'):
cv2.imwrite("/home/pi/Pictures/5.jpg", frame)
'''
"""
Created on Tue May 4 01:10:21 2021
@author: pi
"""
import cv2
import face_recognition
my_image = cv2.imread("/home/pi/Pictures/1.jpg")
my_image = cv2.cvtColor(my_image, cv2.COLOR_BGR2RGB)
my_face_encoding = face_recognition.face_encodings(my_image