保存阵列中的不识别面



我正在处理一个"人脸识别";项目我已经做了识别部分,我的项目是基于3个代码文件

  1. 人脸检测(采集大约50人的样本(
  2. 面部训练(将训练拍摄的图像(
  3. 人脸识别(将能够识别真实的训练图像时间(

现在我要做的是,如果一个不在数据集中的未知人物在相机中被检测到超过3次,人脸识别程序将等待一段时间,人脸检测将运行,它将对该人物进行采样和训练,以便下次该人物出现在相机上时,他/她将被识别。这是我的人脸识别的代码

import cv2
from picamera.array import PiRGBArray
from picamera import PiCamera
import picamera 
import numpy as np 
import pickle
import RPi.GPIO as GPIO
from time import sleep
from subprocess import call
import time
import datetime
import boto3
from botocore.client import Config
import serial
# port = serial.Serial('/dev/ttyUSB0',9600)
now = datetime.datetime.now()
currentDate = str(now.month) + "_" + str(now.day) + "_" + str(now.year)+ "_" + str(now.hour)
cloudpath ='Videos/cctvfootage'+currentDate+'.mp4'
with open('labels', 'rb') as f:
dict= pickle.load(f)
f.close()
#setup the camera
camera =PiCamera()
camera.resolution = (600,500)
camera.framerate = 30
rawCapture = PiRGBArray(camera, size=(600, 500))

# Load prebuilt model for Frontal Face detection
faceCascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
# Create Local Binary Patterns Histograms for face recognization
recognizer = cv2.face.createLBPHFaceRecognizer()
# Load the trained mode
recognizer.load("trainer.yml")
font = cv2.FONT_HERSHEY_SIMPLEX
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'X264')
out = cv2.VideoWriter(cloudpath, fourcc, 2.0, (camera.resolution))
lastRes=''
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
frame = frame.array
cv2.rectangle(frame, (0, 0), (455, 30), (0,0,0), thickness=-1)
cv2.putText(frame, time.asctime(), (10, 25), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 255, 0), lineType=cv2.LINE_AA, thickness=2)  
data = open(cloudpath, 'rb') 
# Convert the captured frame into grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(gray, scaleFactor = 1.5, minNeighbors = 5)
# For each face in faces
for (x, y, w, h) in faces:
# Create rectangle around the face
roiGray = gray[y:y+h, x:x+w]
# Recognize the face belongs to which ID
id_, conf = recognizer.predict(roiGray)

for name, value in dict.items():
if value == id_:
break
print(name)
print(conf)
#agar confidence <=70 hoga toh door open hoga wrna nhi
# Put text describe who is in the picture
if conf <= 70:

cv2.rectangle(frame,(x-20,y-20), (x+w+20,y+h+20), (0,255,0), 4)
cv2.putText(frame,name,(x,y-40), font, 1, (255,255,255), 3)


else:
cv2.rectangle(frame,(x-20,y-20), (x+w+20,y+h+20), (0,255,0), 4)
cv2.putText(frame,"Unknown", (x,y-40), font, 1, (255,255,255),3)
cv2.imshow('frame', frame)
out.write(frame)
key = cv2.waitKey(1)
rawCapture.truncate(0)  
#if cross button is pressed close the cam
if key == 27:
print("Video Saved Successfully..")
break               
cv2.destroyAllWindows()

你尝试过deepface吗?它的流功能可以访问你的网络摄像头,并应用实时人脸识别和面部属性分析(年龄、性别和情绪预测(。您也可以将网络摄像头流媒体内容切换为视频。

#!pip install deepface
from deepface import DeepFace
DeepFace.stream("my_db")

在这里,my_db是一个存储我的面部数据库的文件夹。

最新更新