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

Python TypeError:UMat()缺少必需的参数';范围';(位置2),python,python-3.x,numpy,opencv,facial-identification,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) Fi

我正在写一个面部识别程序,我不断地遇到这个错误,我只是很困惑,我在网上没有看到其他例子,人们在转换到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)
如果不使用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'
回溯(最近一次呼叫最后一次):
文件“test.py”,第48行,在
测试照片()
文件“test.py”,第40行,在test_照片中
面,矩形=检测面(测试照片)
文件“test.py”,第16行,在detect_face中
灰色=cv2.CVT颜色(imgUMat,cv2.COLOR\U BGR2GRAY)
TypeError:参数“src”应为cv::UMat

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

我认为这与作为cv2函数输入的数组的数据类型有关。我也得到了错误,当我执行
arr.dtype
时,它显示为float16,当转换为float32时,错误得到了解决。

而不是使用
cv2.UMat()
转换为
UMat
,只需将它传入
np.float32()
。两者在所有意图和目的上都是相同的

您的代码如下所示:

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

如果img是PIL图像,则imgUMat=np.array(img)
def detect_face(img):   
    imgUMat = np.float32(img)
    gray = cv2.cvtColor(imgUMat, cv2.COLOR_BGR2GRAY)