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

Python practice_ Opencv facial similarity marker

編輯:Python

By traversing ‘./image/face_catalogue’ Picture materials under the directory , As a face template .
Read “ceshi7.jpeg” Picture file , face recognition , Compare the facial similarity of the areas that match the face , The one with the highest similarity is marked .

import cv2
import numpy as np
import os

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
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)
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
# Words mark
cv2.putText(self.img, self.file_name[str(label)] +'_'+str(round(int(confidence),4)) , (self.x, self.y-5), font, 0.4, (0, 0, 255))
def face_sign(self):
faces = self.faceCascade.detectMultiScale(self.img, 1.15,10)
#print(faces)
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)
# If the picture is larger than the screen , To scale , Keep it small
print('self.shape',self.shape,type(self.shape))
if self.shape[0] >= 768 :
self.shape_xs = round(self.shape[0]/768)
print('shape_xs',self.shape_xs)
self.img_new = cv2.resize(self.img, (round(self.shape[1]/self.shape_xs), round(self.shape[0]/self.shape_xs)))
elif self.shape[1] >= 1366:
self.shape_xs = round(self.shape[1] / 1366)
print('shape_xs', self.shape_xs)
self.img_new = cv2.resize(self.img, (round(self.shape[1] / self.shape_xs), round(self.shape[0] / self.shape_xs)))
else:
self.img_new = self.img
cv2.imshow('img', self.img_new)
cv2.waitKey()
cv2.destroyAllWindows()

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(“ceshi7.jpeg”)
# Generating classes
face_sign = Face_comparison(img,model,file_name)
# Call function
face_sign.face_sign()


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