代码拉取完成,页面将自动刷新
同步操作将从 易安物联(集团)有限公司/人脸识别 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
# 这是一个在树莓派上运行人脸识别的案例
# 本案例会在命令行控制面板上输出识别出的人脸数量和身份结果。
# 你需要一个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()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。