opencv练习

opencv练习,第1张

opencv练习

import cv2
import numpy as np
import os
import time

photos =list()
lables =list()

#郭德纲的脸
for root, dirs, files in os.walk(r".imagegdg_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".imageyq_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".imagejx_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".imagelrt_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".imageyyp_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".imageceshi_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)

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5636217.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存