程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python practice_ Opencv camera face recognition filling material library

編輯:Python

Call the external camera , Face recognition on images , Compare the internal features of the material library , Find one with high similarity , If it is less than 30 Save the data to the material library

import cv2
import numpy as np
import os
import time

face_catalogue Facial catalogue

class Face_comparison():

def __init__(self,img,model,file_name):
# Face recognition
self.faceCascade = cv2.CascadeClassifier("./venv/Lib/site-packages/cv2/data/haarcascade_frontalface_default.xml")
self.faceCascade_eye = cv2.CascadeClassifier("./venv/Lib/site-packages/cv2/data/haarcascade_eye.xml")
self.img = img
self.shape = img.shape
self.model = model
self.file_name = file_name
# Time calculation
def get_current_time(self):
ct = time.time()
local_time = time.localtime(ct)
data_head = time.strftime("%Y%m%d%H%M%S", local_time)
data_secs = abs(ct - round(ct)) * 1000
time_stamp = "%s%03d" % (data_head, data_secs)
return time_stamp
def Similarity(self):
# Intercept the face area of the picture , As an independent picture , For saving or other judgments
cropped = self.img[self.y:self.y + self.w, self.x:self.x + self.h]
# Image graying
cropped = cv2.cvtColor(cropped, cv2.COLOR_BGR2GRAY)
# The pixel expansion of the picture is consistent with the material
cropped = cv2.resize(cropped, (400, 400))
# Similarity comparison
label, confidence = self.model.predict(cropped)
# If the similarity is less than 30, Save the material pictures , Expand the portrait Library
if round(int(confidence), 4) <= 30:
print(' Similarity comparison ', str(label) + '_' + self.file_name[str(label)],round(int(confidence), 4))
in_time = self.get_current_time()
# As the material library fills , The score will be lower and lower , Continue to expand storage too much
cv2.imwrite('./image/face_catalogue/'+str(label)+'_'+self.file_name[str(label)]+'/'+in_time+'.jpeg', cropped)
#print(str((self.x, self.y, self.w, self.h)), str(label), self.file_name[str(label)], round(int(confidence), 4))
# Font style
font = cv2.FONT_HERSHEY_TRIPLEX
# Font factor
self.font_xs = round(self.shape[0] / 768, 2)
# Words mark
cv2.putText(self.img, self.file_name[str(label)] +'_'+str(round(int(confidence),4)) , (self.x, self.y-5), font, round(0.6*self.font_xs,1), (0, 0, 255))
def face_sign(self):
faces = self.faceCascade.detectMultiScale(self.img, 1.15,5)
print(faces)
if len(faces)>0:
for (self.x, self.y, self.w, self.h) in faces:
# Face recognition similarity in image
self.Similarity()
# # Add eye recognition , If there are eyes in storage
# eye = self.faceCascade_eye.detectMultiScale(cropped, 1.15)
# if len(eye) > 0:
cv2.rectangle(self.img, (self.x, self.y), (self.x + self.w, self.y + self.h), (0, 0, 200), 2)
return self.img
else:
return self.img

def face_catalogue(catalogue):
# Face directory loading , Loading pictures every time is too wasteful
photos = list()
lables = list()
file_name = {}
# Traverse the upper directory
for root, dirs, files in os.walk(catalogue):
# Check how many subdirectories exist
if files == []:
#print(‘root’, root, ‘dirs’, dirs, ‘files’, files)
for dir in dirs:
for root_son, dirs_son, files_son in os.walk(catalogue + “/%s” % dir):
#print(‘dir’, dir, ‘files’, files_son)
dir_id = dir[0:7]
dir_name = dir[8:]
#print(‘dir_id’, dir_id, ‘dir_name’, dir_name)
# Add dictionaries
file_name[dir_id]=dir_name
# Attachment count
file_num = 0
for file_son in files_son:
#print(‘file’, catalogue + “/%s/%s” % (dir, file_son))
img = cv2.imread(catalogue + “/%s/%s” % (dir, file_son), 0)
imga = cv2.resize(img, (400, 400))
photos.append(imga)
lables.append(int(dir_id))
file_num += 1
#print(‘ Serial number ’,dir,‘ The amount of material :’,file_num)
#print(‘ Number of files ’,set(lables),file_name)
model = cv2.face.LBPHFaceRecognizer_create()
model.train(photos, np.array(lables))
return model,file_name

if name == ‘main’:
# Face material library loading
model,file_name = face_catalogue(‘./image/face_catalogue’)
# The test image
#img = cv2.imread(“ceshi10.jpeg”)
capture = cv2.VideoCapture(1, cv2.CAP_DSHOW)
print(‘capture111’)
while (capture.isOpened()):
print(‘capture222’)
retval, image = capture.read()
# Generating classes
face_sign_user = Face_comparison(image,model,file_name)
# Call function
img = face_sign_user.face_sign()
cv2.imshow(‘img’, img)
key = cv2.waitKey(1)
cv2.waitKey()
cv2.destroyAllWindows()


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved