簡體   English   中英

為什么我在 python 中使用 detectMultiScale 時出現打開 cv2 錯誤

[英]Why am I getting open cv2 error when I use detectMultiScale in python

我正在為圖像中的人臉識別編寫此代碼

  import cv2

face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xlm") # this is haarcaacade for frontalfcae

img=cv2.imread("E:\\python\\opencv\\photo.jpg")
# here we are loading colour images. But it is better to use grey scale image when searching for face because it give high accuracy. 


gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #This converts colour image to grey image

faces = face_cascade.detectMultiScale(gray, scaleFactor=1.05, minNeighbors=5)


cv2.imshow("GREY",gray)
cv2.waitKey(0)
cv2.destroyAllWindows

我在 output 中得到這個

(venv2) PS E:\python\opencv> & e:/python/venv2/Scripts/python.exe e:/python/opencv/face_recognition.py
Traceback (most recent call last):
  File "e:/python/opencv/face_recognition.py", line 10, in <module>
    faces = face_cascade.detectMultiScale(gray, scaleFactor=1.05, minNeighbors=5)
cv2.error: OpenCV(4.5.4) D:\a\opencv-python\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'

錯誤在第 5 行

faces = face_cascade.detectMultiScale(gray, scaleFactor=1.05, minNeighbors=5)

請向我解釋為什么我會收到此錯誤

您如何下載.xml文件並將您的cv2.CascadeClassifier function 重定向到它? 似乎 function 找不到它,默認情況下它沒有安裝/下載。
這是鏈接: OpenCV haarcascades

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM