Python TypeError:UMat()缺少必需的参数“范围”(位置2)

时间:2019-01-21 07:06:10

标签: python python-3.x numpy opencv facial-identification

我正在编写一个面部识别程序,并且不断出现此错误,我非常困惑,我在网络上没有看到其他示例,人们在转换为UMat时会包含范围

    Traceback (most recent call last):
  File "test.py", line 48, in <module>
    test_photos()
  File "test.py", line 40, in test_photos
    face, rect = detect_face(test_photo)
  File "test.py", line 15, in detect_face
    imgUMat = cv2.UMat(img)
TypeError: UMat() missing required argument 'ranges' (pos 2)

我的代码是

def detect_face(img):   
    imgUMat = cv2.UMat(img)
    gray = cv2.cvtColor(imgUMat, cv2.COLOR_BGR2GRAY)
    face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
    faces = face_cascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5)
    if (len(faces)==0):
        return None, None
    (x, y, w, h) = faces[0]
    gray = gray.get()
    return gray[y:y+h,x:x+w], faces[0]

def prepare_training_data():
    faces = []
    labels = []
    for img in photo_name_list: #a collection of file locations as strings
        image = cv2.imread(img)
        face, rect = detect_face(image)
        if face is not None:
            faces.append(face)
            labels.append(me)
    return faces, labels

def test_photos():
    face_recognizer = cv2.face.LBPHFaceRecognizer_create()
    faces, labels = prepare_training_data()
    face_recognizer.train(np.array(faces), np.array(labels))
    face, rect = detect_face(test_photo)
    label = face_recognizer.predict(face)
    if label == me:
        print("it's me")
    else:
        print("it's not me")


test_photos()

如果我不使用UMat(),则会出现此错误:

Traceback (most recent call last):
  File "test.py", line 48, in <module>
    test_photos()
  File "test.py", line 40, in test_photos
    face, rect = detect_face(test_photo)
  File "test.py", line 16, in detect_face
    gray = cv2.cvtColor(imgUMat, cv2.COLOR_BGR2GRAY)
TypeError: Expected cv::UMat for argument 'src'

我使用的是OpenCV 4.0.0,老实说,我非常困惑,因为从我所看到的来看,没有其他人必须使用UMat来使用cvtColor(),更不用说在UMat()中使用范围了。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

我认为这与作为cv2函数输入的数组数据类型有关。我也遇到了错误,当我执行package main import ( "fmt" ) type user struct { userID int name string email string } func (t user) String() string { return fmt.Sprintf("{%v %v %v}", t.userID, t.name, t.email) } func main() { var users []*user addUsers(users) } func addUsers(users []*user) { users = append(users, &user{userID: 1, name: "cooluser1", email: "cool.user1@gmail.com"}) users = append(users, &user{userID: 2, name: "cooluser2", email: "cool.user2@gmail.com"}) printUsers(users) } func printUsers(users []*user) { fmt.Printf("users at slice %v \n", users) } 时,它显示为float16,并转换为float32时,错误得到解决。

答案 1 :(得分:0)

与其使用UMat转换为cv2.Umat(),不如将其传递到np.float32()中。两者在所有意图和目的上都是相同的。

您的代码如下:

def detect_face(img):   
    imgUMat = np.float32(img)
    gray = cv2.cvtColor(imgUMat, cv2.COLOR_BGR2GRAY)