select python 中来自 Haar-cascade 的图像中检测到的多张人脸中的特定人脸
select specific face from multiple detected faces inside image from Haar-cascade in python
我有一些包含单张或多张脸的图片,但如果图片中有多张脸,我只想 select 一张脸。我使用 OpenCV python 通过 haar-cascade 检测人脸,这是完美的,但我不能 select 从具有多个人脸检测器的图像中提取特定人脸。我的代码如下:
cascPath = "Python35\Lib\site-packages\cv\data\haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascPath)
listing = os.listdir(path\of\images)
print("Detection face of new individual images")
for file in listing:
im = (path1 + '\' + imagePath + '\' + file)
imag = cv2.imread(im)
imag = imutils.resize(imag, width=500)
gray = cv2.cvtColor(imag, cv2.COLOR_BGR2GRAY)
# Detect faces in the image
faces = faceCascade.detectMultiScale(gray)
print("Founded face is {} faces which are {}".format(len(faces), faces))
if len(faces)>1:
i = 0
for (x, y, w, h) in faces:
cv2.rectangle(imag, (x, y), (x + w, y + h), (255, 0, 0), 2)
cv2.putText(imag, "Face #{}".format(i), (x - 10, y - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
i = i + 1
cv2.imshow("im", imag)
cv2.waitKey(0)
cv2.destroyAllWindows()
var = int(input("Which face you want to detect it"))
faces = faces[var]
print("Selected face is", faces)
print("type of selected face",type(faces))
print("the drawing face is", faces)
# Draw a rectangle around the face
for (x, y, w, h) in faces:
cv2.rectangle(imag, (x, y), (x + w, y + h), (255, 0, 0), 2)
roi_gray = gray[y:y + h, x:x + w]
roi_color = imag[y:y + h, x:x + w]
cv2.imshow("face", roi_color)
cv2.waitKey(0)
cv2.destroyAllWindows()
如果图像只包含一张脸,此代码可以成功运行,但是当有多张脸并且我想通过输入它的索引来select其中一张时,我收到以下错误。
for (x, y, w, h) in faces:
TypeError: 'numpy.int32' object is not iterable
谁能帮我解决问题,我select 已经创建好的矩形,为什么拒绝它。
你能否在迭代之前打印 faces 对象,运行 你的代码,并告诉我们输出是什么?到底是哪一行出错了?
我如下解决了与面迭代器相关的问题并成功工作。
if len(faces) > 1:
i = 0
for f in faces:
face = faces[i]
(x, y, w, h) = face
cv2.rectangle(imag, (x, y), (x + w, y + h), (255, 0, 0), 2)
cv2.putText(imag, "Face #{}".format(i), (x - 10, y - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
i = i + 1
我有一些包含单张或多张脸的图片,但如果图片中有多张脸,我只想 select 一张脸。我使用 OpenCV python 通过 haar-cascade 检测人脸,这是完美的,但我不能 select 从具有多个人脸检测器的图像中提取特定人脸。我的代码如下:
cascPath = "Python35\Lib\site-packages\cv\data\haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascPath)
listing = os.listdir(path\of\images)
print("Detection face of new individual images")
for file in listing:
im = (path1 + '\' + imagePath + '\' + file)
imag = cv2.imread(im)
imag = imutils.resize(imag, width=500)
gray = cv2.cvtColor(imag, cv2.COLOR_BGR2GRAY)
# Detect faces in the image
faces = faceCascade.detectMultiScale(gray)
print("Founded face is {} faces which are {}".format(len(faces), faces))
if len(faces)>1:
i = 0
for (x, y, w, h) in faces:
cv2.rectangle(imag, (x, y), (x + w, y + h), (255, 0, 0), 2)
cv2.putText(imag, "Face #{}".format(i), (x - 10, y - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
i = i + 1
cv2.imshow("im", imag)
cv2.waitKey(0)
cv2.destroyAllWindows()
var = int(input("Which face you want to detect it"))
faces = faces[var]
print("Selected face is", faces)
print("type of selected face",type(faces))
print("the drawing face is", faces)
# Draw a rectangle around the face
for (x, y, w, h) in faces:
cv2.rectangle(imag, (x, y), (x + w, y + h), (255, 0, 0), 2)
roi_gray = gray[y:y + h, x:x + w]
roi_color = imag[y:y + h, x:x + w]
cv2.imshow("face", roi_color)
cv2.waitKey(0)
cv2.destroyAllWindows()
如果图像只包含一张脸,此代码可以成功运行,但是当有多张脸并且我想通过输入它的索引来select其中一张时,我收到以下错误。
for (x, y, w, h) in faces:
TypeError: 'numpy.int32' object is not iterable
谁能帮我解决问题,我select 已经创建好的矩形,为什么拒绝它。
你能否在迭代之前打印 faces 对象,运行 你的代码,并告诉我们输出是什么?到底是哪一行出错了?
我如下解决了与面迭代器相关的问题并成功工作。
if len(faces) > 1:
i = 0
for f in faces:
face = faces[i]
(x, y, w, h) = face
cv2.rectangle(imag, (x, y), (x + w, y + h), (255, 0, 0), 2)
cv2.putText(imag, "Face #{}".format(i), (x - 10, y - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
i = i + 1