Python 类型错误 : UMat() missing required argument 'ranges' (pos 2)

标签 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() 内使用范围了。任何帮助将不胜感激。

最佳答案

无需使用 cv2.Umat() 转换为 UMat,只需将其传递到 np.float32() 即可。两者在所有意图和目的上都是相同的。

您的代码将如下所示:

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

关于Python 类型错误 : UMat() missing required argument 'ranges' (pos 2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54284937/

相关文章:

python - einsum 和距离计算

python - 无法使用 __exit__ 方法创建mockito模拟

python - 在 Python 3 中使用氢

访问数组某些部分的 Pythonic 方式

Python:对体素组件进行网格划分以计算表面积

python - Tkinter 文本小部件比任何其他带有网格的小部件更宽

Python CURL 删除请求函数

python - 同一文件中多个 <html><body> </html></body>

python - ProgrammingError : (psycopg2. ProgrammingError) 无法适配类型 'numpy.ndarray'

Python3.7 ImportError : No module named 'django'