1 Star 0 Fork 2

欧阳香/人脸识别

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
facerec_on_raspberry_pi.py 5.30 KB
一键复制 编辑 原始数据 按行查看 历史
adam1iu 提交于 2020-01-03 16:33 . init commit
# 这是一个在树莓派上运行人脸识别的案例
# 本案例会在命令行控制面板上输出识别出的人脸数量和身份结果。
# 你需要一个2代以上的树莓派,并在树莓派上安装face_recognition,并连接上picamera摄像头
# 并确保picamera这个模块已经安装(树莓派一般会内置安装)
# 你可以参考这个教程配制你的树莓派:
# https://gist.github.com/ageitgey/1ac8dbe8572f3f533df6269dab35df65
import time
import face_recognition
import picamera
import numpy as np
import cv2
from img_base_api import *
out_video_name = 'video'
# 你需要在sudo raspi-config中把camera功能打开
camera = picamera.PiCamera()
camera.resolution = (320, 240) # 设置图像的width和height
camera.framerate = 35
# camera.saturation = 80 # 设置图像视频的饱和度
camera.brightness = 45 # 设置图像的亮度(50表示白平衡的状态)
camera.shutter_speed = 6000000 # 相机快门速度
# camera.iso = 800 # ISO标准实际上就是来自胶片工业的标准称谓,ISO是衡量胶片对光线敏感程度的标准。如50 ISO, 64 ISO, 100 ISO表示在曝光感应速度上要比高数值的来得慢,高数值ISO是指超过200以上的标准,如200 ISO, 400 ISO
# camera.sharpness = 0 #设置图像的锐度值,默认是0,取值范围是-100~100之间
# camera.hflip = True # 是否进行水平翻转50
camera.vflip = True # 是否进行垂直翻转
# camera.rotation = 0 # 是否对图像进行旋转
# camera.led = True # 值为False那么led为关灯的状态,True为开灯的状态
output = np.empty((240, 320, 3), dtype=np.uint8)
# 载入样本图片(奥巴马和拜登)
print("Loading known face image(s)")
obama_image = face_recognition.load_image_file("obama_small.jpg")
obama_face_encoding = face_recognition.face_encodings(obama_image)[0]
# 初始化变量
face_locations = []
face_encodings = []
face_names = []
process_this_frame = True
# first face flag
first_face_flag = False
no_face_flag = False
# face blur value
face_blur_val = 0
# before face encode
before_face_encoding = None
last_face_exist_time = 0
same_face_interval_time = 5
blur_var_init = 50
# video property
height = 0
width = 0
while True:
print("Capturing image.")
# 以numpy array的数据结构从picamera摄像头中获取一帧图片
camera.capture(output, format="bgr")
height, width, _ = output.shape
# 获得所有人脸的位置以及它们的编码
# face_locations = face_recognition.face_locations(output)
# print("Found {} faces in image.".format(len(face_locations)))
# face_encodings = face_recognition.face_encodings(output, face_locations)
if process_this_frame:
# Find all the faces and face encodings in the current frame of video
face_locations = face_recognition.face_locations(output)
face_encodings = face_recognition.face_encodings(output, face_locations)
no_face_flag = True if len(face_locations) == 0 else False
process_this_frame = not process_this_frame
for index, ((top, right, bottom, left), face_encoding) in enumerate(zip(face_locations, face_encodings)):
# Scale back up face locations since the frame we detected in was scaled to 1/4 size
left -= int(width * 0.1)
top -= int(height * 0.168)
right += int(width * 0.1)
bottom += int(height * 0.1)
if left < 0 or top < 0 or right < 0 or right > width or bottom < 0 or bottom > height:
continue
# blur var calc
gray = cv2.cvtColor(output, cv2.COLOR_BGR2GRAY)
blur_var = cv2.Laplacian(gray, cv2.CV_64F).var()
if blur_var < blur_var_init:
print(f'face blur val: {blur_var}')
continue
print(left, top, right, bottom)
top *= 4
right *= 4
bottom *= 4
left *= 4
if before_face_encoding is None:
matches = [False]
else:
matches = face_recognition.compare_faces([before_face_encoding], face_encoding)
if all(matches):
cv2.rectangle(output, (left, top), (right, bottom), color=[0, 0, 255], thickness=2)
continue
# ------------------- face compare && face quality evaluate end -----------------------------
print(f'top: {top} bottom: {bottom} left: {left} right: {right} ')
image = cv2.rectangle(output, (left, top), (right, bottom), (0, 0, 255), 2)
try:
cv2.imwrite('.face_img.jpg', image)
img_base64_str = cv2_base64(image)
stat, res = send_img_to_server(img_base64_str)
print("send img to server")
# if not stat:
# continue
# search_img_to_server(img_base64_str)
last_face_exist_time = time.time()
first_face_flag = True
before_face_encoding = face_encoding
# break
except:
pass
no_face_time = time.time()
if no_face_flag and last_face_exist_time != 0 and no_face_time - last_face_exist_time > same_face_interval_time:
print('no face')
before_face_encoding = None
first_face_flag = False
# Display the resulting image
cv2.imshow(out_video_name, output)
# Hit 'q' on the keyboard to quit!
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release handle to the webcam
camera.release()
cv2.destroyAllWindows()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/ouyangxiang_01/face_recognition.git
[email protected]:ouyangxiang_01/face_recognition.git
ouyangxiang_01
face_recognition
人脸识别
master

搜索帮助