opencv练习_人脸识别比对查看相似度

import cv2
import numpy as np
import os
import time

photos =list()
lables =list()

#郭德纲的脸
for root, dirs, files in os.walk(r".\image\gdg_lian"):
for file in files:
print(‘file’,"./image/gdg_lian/"+file)
img = cv2.imread("./image/gdg_lian/"+ file,0 )
imga = cv2.resize(img, (194, 194))
photos.append(imga)
print(‘photos’,len(photos))

for i in range(len(files)):
lables.append(1)
print(‘lables’,len(lables))

#于谦的脸
for root, dirs, files in os.walk(r".\image\yq_lian"):
for file in files:
print(‘file’,"./image/yq_lian/"+file)
img = cv2.imread("./image/yq_lian/"+ file,0 )
imga = cv2.resize(img, (194, 194))
photos.append(imga)
print(‘photos’,len(photos))

for i in range(len(files)):
lables.append(2)
print(‘lables’,len(lables))

#金星的脸
for root, dirs, files in os.walk(r".\image\jx_lian"):
for file in files:
print(‘file’,"./image/jx_lian/"+file)
img = cv2.imread("./image/jx_lian/"+ file,0 )
imga = cv2.resize(img, (194, 194))
photos.append(imga)
print(‘photos’,len(photos))

for i in range(len(files)):
lables.append(3)
print(‘lables’,len(lables))

#李若彤的脸
for root, dirs, files in os.walk(r".\image\lrt_lian"):
for file in files:
print(‘file’,"./image/lrt_lian/"+file)
img = cv2.imread("./image/lrt_lian/"+ file,0 )
imga = cv2.resize(img, (194, 194))
photos.append(imga)
print(‘photos’,len(photos))

for i in range(len(files)):
lables.append(4)
print(‘lables’,len(lables))

#岳云鹏的脸
for root, dirs, files in os.walk(r".\image\yyp_lian"):
for file in files:
print(‘file’,"./image/yyp_lian/"+file)
img = cv2.imread("./image/yyp_lian/"+ file,0 )
imga = cv2.resize(img, (194, 194))
photos.append(imga)
print(‘photos’,len(photos))

for i in range(len(files)):
lables.append(5)
print(‘lables’,len(lables))

names ={‘1’:‘郭德纲’,‘2’:‘于谦’,‘3’:‘金星’,‘4’:‘李若彤’,‘5’:‘岳云鹏’}

#model=cv2.face.EigenFaceRecognizer_create()
#model=cv2.face.FisherFaceRecognizer_create()
model=cv2.face.LBPHFaceRecognizer_create()
model.train(photos,np.array(lables))

#文件下循环比对
for root, dirs, files in os.walk(r".\image\ceshi_lian"):
for file in files:
img = cv2.imread("./image/ceshi_lian/" + file, 0)
imga = cv2.resize(img, (194, 194))
label, confidence = model.predict(imga)
print(names[str(label)], str(file), confidence)

你可能感兴趣的:(python)