【发布时间】:2017-02-03 09:31:59
【问题描述】:
有没有什么方法可以让我自己的训练集在 python 中进行人脸识别?更具体地说,我想制作一个像 AT&T Face 数据库一样的火车。我希望我的相机为每个人拍摄 20 张图像(最多 30 张),并按每个人的姓名将其存储在单独的文件夹中。
import cv2, sys, numpy, os
size = 4
fn_haar = 'haarcascade_frontalface_default.xml'
fn_dir = 'att_faces'
fn_name = sys.argv[1]
path = os.path.join(fn_dir, fn_name)
if not os.path.isdir(path):
os.mkdir(path)
(im_width, im_height) = (112, 92)
haar_cascade = cv2.CascadeClassifier(fn_haar)
webcam = cv2.VideoCapture(0)
# The program loops until it has 20 images of the face.
count = 0
while count < 20:
(rval, im) = webcam.read()
im = cv2.flip(im, 1, 0)
gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
mini = cv2.resize(gray, (gray.shape[1] / size, gray.shape[0] / size))
faces = haar_cascade.detectMultiScale(mini)
faces = sorted(faces, key=lambda x: x[3])
if faces:
face_i = faces[0]
(x, y, w, h) = [v * size for v in face_i]
face = gray[y:y + h, x:x + w]
face_resize = cv2.resize(face, (im_width, im_height))
pin=sorted([int(n[:n.find('.')]) for n in os.listdir(path)
if n[0]!='.' ]+[0])[-1] + 1
cv2.imwrite('%s/%s.png' % (path, pin), face_resize)
cv2.rectangle(im, (x, y), (x + w, y + h), (0, 255, 0), 3)
cv2.putText(im, fn_name, (x - 10, y - 10), cv2.FONT_HERSHEY_PLAIN,
1,(0, 255, 0))
count += 1
cv2.imshow('OpenCV', im)
key = cv2.waitKey(10)
if key == 27:
break
【问题讨论】:
-
我对你想要做什么感到困惑。你想让opencv为你拍一个人的30张照片吗?或者你有图片,你想训练一个检测器吗?
-
您能更具体地说明您在哪里遇到问题吗?分享一下你目前写的那段代码?
-
此代码正在拍摄 20 张检测到的面部照片并将其保存在文件夹中,然后终止。我想编辑它。它应该为每张被检测到的脸拍摄 20 张照片,并将每张脸保存在单独的文件夹中。它不应自行终止。
-
代码已添加..
-
@user3543300 此代码正在拍摄 20 张检测到的面部照片并将其保存在文件夹中,然后终止。我想编辑它。它应该为每张被检测到的脸拍摄 20 张照片,并将每张脸保存在单独的文件夹中。它不应该自行终止
标签: python opencv face-detection face-recognition training-data