Authored by 2014Vee
Contact me via [email protected]
博主最近在做和人脸识别相关的项目:
在网上查找代码测试的过程中,发现网上大部分Python代码(如下)都会在运行后报错如下:
#!/usr/bin/python
#coding=utf-8
''' face detect
https://github.com/seathiefwang/FaceRecognition-tensorflow
http://tumumu.cn/2017/05/02/deep-learning-face/
'''
# pylint: disable=invalid-name
import os
import random
import numpy as np
import cv2
def createdir(*args):
''' create dir'''
for item in args:
if not os.path.exists(item):
os.makedirs(item)
IMGSIZE = 64
def getpaddingSize(shape):
''' get size to make image to be a square rect '''
h, w = shape
longest = max(h, w)
result = (np.array([longest]*4, int) - np.array([h, h, w, w], int)) // 2
return result.tolist()
def dealwithimage(img, h=64, w=64):
''' dealwithimage '''
#img = cv2.imread(imgpath)
top, bottom, left, right = getpaddingSize(img.shape[0:2])
img = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=[0, 0, 0])
img = cv2.resize(img, (h, w))
return img
def relight(imgsrc, alpha=1, bias=0):
'''relight'''
imgsrc = imgsrc.astype(float)
imgsrc = imgsrc * alpha + bias
imgsrc[imgsrc < 0] = 0
imgsrc[imgsrc > 255] = 255
imgsrc = imgsrc.astype(np.uint8)
return imgsrc
def getfacefromcamera(outdir):
createdir(outdir)
camera = cv2.VideoCapture(0)
haar = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# haar.load('D:\opencv330\sources\data\haarcascades/haarcascade_frontalface_default.xml')
n = 1
while 1:
if (n <= 200):
print('It`s processing %s image.' % n)
# 读帧
success, img = camera.read()
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = haar.detectMultiScale(gray_img, 1.3, 5)
for f_x, f_y, f_w, f_h in faces:
face = img[f_y:f_y+f_h, f_x:f_x+f_w]
face = cv2.resize(face, (IMGSIZE, IMGSIZE))
#could deal with face to train
face = relight(face, random.uniform(0.5, 1.5), random.randint(-50, 50))
cv2.imwrite(os.path.join(outdir, str(n)+'.jpg'), face)
cv2.putText(img, 'fetch data', (f_x, f_y - 20), cv2.FONT_HERSHEY_SIMPLEX, 1, 255, 2) #显示名字
img = cv2.rectangle(img, (f_x, f_y), (f_x + f_w, f_y + f_h), (255, 0, 0), 2)
n+=1
cv2.imshow('FDR', img)
key = cv2.waitKey(30) & 0xff
if key == 27:
break
else:
break
camera.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
name = input('please input yourename: ')
getfacefromcamera(os.path.join(r'C:\Users\liuping\PycharmProjects\study_opencv/trainfaces', name))
OpenCV(3.4.1) Error: Assertion failed (!empty()) in cv::CascadeClassifier::detectMultiScale, file C:\projects\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp, line 1698
Traceback (most recent call last):
File "C:/Users/liuping/PycharmProjects/study_opencv/opencv.py", line 81, in
getfacefromcamera(os.path.join(r'C:\Users\liuping\PycharmProjects\study_opencv/trainfaces', name))
File "C:/Users/liuping/PycharmProjects/study_opencv/opencv.py", line 59, in getfacefromcamera
faces = haar.detectMultiScale(gray_img, 1.3, 5)
cv2.error: OpenCV(3.4.1) C:\projects\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp:1698: error: (-215) !empty() in function cv::CascadeClassifier::detectMultiScale
这个到底是哪里出的问题呢?
作者借鉴了一个相似问题的博客解决了这个问题,注意一下下面的代码:
haar = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
这个代码的下面缺少对opencv分类器部分的path,可以加上下面这句解决:
haar.load('D:\opencv330\sources\data\haarcascades/haarcascade_frontalface_default.xml')
这里注意一定要按照opencv路径找到自己电脑的分类器path。
参考引用
https://blog.csdn.net/serena9636/article/details/52530446
https://www.jianshu.com/p/f4344669c694