簡體   English   中英

cv2.error:OpenCV(4.6.0):-1:錯誤:(-5:錯誤參數)

[英]cv2.error: OpenCV(4.6.0) :-1: error: (-5:Bad argument)

我現在正在上一門課程,我正在將導師的代碼輸入到 Python 文件中。 但是,我得到了一個錯誤。 我想說,導師沒有任何錯誤。 視頻的鏈接是這個(轉到“使用 OpenCV 的內置識別器進行人臉識別”部分)。 我是 OpenCV 的新手,找不到出現此錯誤的原因。

我得到的錯誤是:

Traceback (most recent call last):
  File "c:/Users/Ad/Desktop/OpenCV/faces_train.py", line 33, in <module>
    create_train()
  File "c:/Users/Ad/Desktop/OpenCV/faces_train.py", line 24, in create_train
    gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
cv2.error: OpenCV(4.6.0) :-1: error: (-5:Bad argument) in function 'cvtColor'
 Overload resolution failed:
  > - src is not a numpy array, neither a scalar
  > - Expected Ptr<cv::UMat> for argument 'src'

我寫的代碼是:

import os
import cv2 as cv
import numpy as np

people = ['Ben Affleck', 'Elton John', 'Jerry Seinfield', 'Madonna', 'Mindy Kaling']
DIR = r'C:\Users\Ad\Desktop\OpenCV\Faces'

haar_cascade = cv.CascadeClassifier('haatcascade_frontalface_default.xml')

features = []
labels = []

def create_train():
    for person in people:
        path = os.path.join(DIR, person)

        label = people.index(person)

        for img in os.listdir(path):

            img_path = os.path.join(path, img)
            img_array = cv.imread(img_path)

            gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)

            faces_rect = cv.detectMultiScale(gray, scaleFactor = 1.1, minNeighbours = 4)

            for (x,y,w,h) in faces_rect:
                faces_roi = gray[y:y+h, x:x+w]
                features.append(faces_roi)
                labels.append(label)

create_train()
print('Training done------------------------')

features = np.array(features, dtype = 'object')
labels = np.array(labels)

face_recognizer = cv.face.LBPHFaceRecognizer_create()


face_recognizer.train(features, labels)

np.save('features.npy', features)
np.save('labels.npy', labels)

您沒有將圖像的 numpy 數組傳遞給cv2.cvt_color()

只需傳遞 numpy 數組,它應該可以與您一起使用:

gray = cv.cvtColor(img_array, cv.COLOR_BGR2GRAY)

暫無
暫無

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

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