Python - 无法检测面部和眼睛?

问题描述 投票:0回答:2

我正在尝试使用 OpenCV 库创建面部和眼睛检测。这是我一直在使用的代码。它运行顺利,没有错误,但唯一的问题是没有显示任何结果,使用此代码找不到面孔和眼睛

import cv2
import sys
import numpy as np
import os

# Get user supplied values
imagePath = sys.argv[1]


# Create the haar cascade
faceCascade = cv2.CascadeClassifier('C:\Users\Karthik\Downloads\Programs\opencv\sources\data\haarcascades\haarcascad_frontalface_default.xml')
eyeCascade= cv2.CascadeClassifier('C:\Users\Karthik\Downloads\Programs\opencv\sources\data\haarcascades\haarcascade_eye.xml')

# Read the image
image = cv2.imread(imagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Detect faces in the image
faces = faceCascade.detectMultiScale(
    gray,
    scaleFactor=1.2,
    minNeighbors=5,
    minSize=(30, 30),
    flags = cv2.cv.CV_HAAR_SCALE_IMAGE
)

print "Found {0} faces!".format(len(faces))

# Draw a rectangle around the faces
for (x, y, w, h) in faces:
    cv2.rectangle(image, (x, y), (x+w, y+h), (255, 0, 0), 2)
    roi_gray = gray[y:y+h, x:x+w]
    roi_color = image[y:y+h, x:x+w]

    eyes = eyeCascade.detectMultiscale(roi_gray)
    for (ex,ey,ew,eh) in eyes:
            cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0, 255, 0), 2)


cv2.imshow("Faces found", image)
print image.shape
cv2.waitKey(0)
python opencv numpy face-detection eye-detection
2个回答
2
投票

对我来说,它可以在我的 Ubuntu 15.10 上的 jupyter 笔记本上使用 OpenCV 3.1.0-dev 和 python 3.4

难道你有一个简单的错字吗?

haarcascad_frontalface_default.xml
=>
haarcascade_frontalface_default.xml

这里:

eyes = eyeCascade.detectMultiscale(roi_gray)
=>
eyeCascade.detectMultiScale(roi_gray)

这是我的工作代码:

%matplotlib inline

import matplotlib
import matplotlib.pyplot as plt

import cv2
import sys
import numpy as np
import os

# Create the haar cascade
faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eyeCascade= cv2.CascadeClassifier('haarcascade_eye.xml')

# Read the image
image = cv2.imread('lena.png', 0)

if image is None:
    raise ValueError('Image not found')

# Detect faces in the image
faces = faceCascade.detectMultiScale(image)

print('Found {} faces!'.format(len(faces)))

# Draw a rectangle around the faces
for (x, y, w, h) in faces:
    cv2.rectangle(image, (x, y), (x+w, y+h), 255, 2)
    roi = image[y:y+h, x:x+w]

    eyes = eyeCascade.detectMultiScale(roi)
    for (ex,ey,ew,eh) in eyes:
            cv2.rectangle(roi,(ex,ey),(ex+ew,ey+eh), 255, 2)


plt.figure()
plt.imshow(image, cmap='gray')
plt.show()  


0
投票
I am facing some issue if you guyz can help me out with this im trying to achieve the same but having some error:

Code:

pip install opencv-python
import numpy as np
import cv2
import matplotlib
from matplotlib import pyplot as plt
%matplotlib inline


img = cv2.imread('./test_images/sharapova1.jpg')
img.shape

from IPython.display import Image
Image(filename='./test_images/sharapova1.jpg') 

plt.imshow(img)


gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray.shape
gray


plt.imshow(gray, cmap='gray')


face_cascade = cv2.CascadeClassifier('C:/Users/dipes/SportsPersonClassifier/model/opencv/haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('C:/Users/dipes/SportsPersonClassifier/model/opencv/haarcascades/haarcascade_eye.xml')

faces = face_cascade.detectMultiScale(gray, 1.3, 5)
faces

(x,y,w,h) = faces[0]
x,y,w,h

face_img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
plt.imshow(face_img)


cv2.destroyAllWindows()
for (x,y,w,h) in faces:
    face_img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
    roi_gray = gray[y:y+h, x:x+w]
    roi_color = face_img[y:y+h, x:x+w]
    eyes = eye_cascade.detectMultiScale(roi_gray)
    for (ex,ey,ew,eh) in eyes:
        cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
        

plt.figure()
plt.imshow(face_img, cmap='gray')
plt.show()


After this code chunk error:
error                                     Traceback (most recent call last)
<ipython-input-25-0838b463c64c> in <module>
      4     roi_gray = gray[y:y+h, x:x+w]
      5     roi_color = face_img[y:y+h, x:x+w]
----> 6     eyes = eye_cascade.detectMultiScale(roi_gray)
      7     for (ex,ey,ew,eh) in eyes:
      8         cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)

error: OpenCV(4.9.0) D:\a\opencv-python\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'
© www.soinside.com 2019 - 2024. All rights reserved.