OpenCV、PyTorch、ValueError:平铺无法扩展外部图像

2024-09-25 04:19:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我收到以下错误:ValueError:tile无法扩展外部图像

在推断人脸识别软件的过程中,该软件会检查您是否佩戴了新冠病毒口罩

这是密码

    transformations = Compose([
        ToPILImage(),
        Resize((100, 100)),
        ToTensor(),
    ])
    

[...]

    for frame in vreader(str(videopath)):
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        faces = faceDetector.detect(frame)
        for face in faces:
            xStart, yStart, width, height = face
            
            # clamp coordinates that are outside of the image
            xStart, yStart = max(xStart, 0), max(yStart, 0)
            
            # predict mask label on extracted face
            faceImg = frame[yStart:yStart+height, xStart:xStart+width]
            output = model(transformations(faceImg).unsqueeze(0).to(device))
            _, predicted = torch.max(output.data, 1)
            
            # draw face frame
            cv2.rectangle(frame,
                          (xStart, yStart),
                          (xStart + width, yStart + height),
                          (126, 65, 64),
                          thickness=2)

主要问题源于这个片段

output = model(transformations(faceImg).unsqueeze(0).to(device))

可能是facedetector.py中的“detect”函数,它是一个单独的元素,仅用于查找图片中的面:

def detect(self, image):
    """ detect faces in image
    """
    net = self.classifier
    height, width = image.shape[:2]
    blob = blobFromImage(resize(image, (300, 300)), 1.0,
                         (300, 300), (104.0, 177.0, 123.0))
    net.setInput(blob)
    detections = net.forward()
    faces = []
    for i in range(0, detections.shape[2]):
        confidence = detections[0, 0, i, 2]
        if confidence < self.confidenceThreshold:
            continue
        box = detections[0, 0, i, 3:7] * np.array([width, height, width, height])
        startX, startY, endX, endY = box.astype("int")
        faces.append(np.array([startX, startY, endX-startX, endY-startY]))
    return faces

我试图在1280x720p视频上运行推断。不知道怎么了。它开始推理,从我收集的数据来看,模型工作了,但很快就出现了错误

你觉得怎么样

这是错误的完整堆栈

File "video.py", line 66, in tagVideo
output = model(transformations(faceImg).unsqueeze(0).to(device))

文件“C:\Users\User\Ana\anaconda3\envs\Venv\lib\site packages\torchvision\transforms\transforms.py”,第60行,调用 img=t(img) 文件“C:\Users\User\Ana\anaconda3\envs\Venv\lib\site packages\torchvision\transforms\transforms.py”,第179行,在调用中 返回F.to_pil_图像(图片,自我模式) 文件“C:\Users\User\Ana\anaconda3\envs\Venv\lib\site packages\torchvision\transforms\functional.py”,第292行,在to\u pil\u图像中 返回Image.fromarray(npimg,mode=mode) 文件“C:\Users\User\Ana\anaconda3\envs\Venv\lib\site packages\PIL\Image.py”,第2793行,在fromarray中 从缓冲区返回(模式,大小,对象,“原始”,原始模式,0,1) 文件“C:\Users\User\Ana\anaconda3\envs\Venv\lib\site packages\PIL\Image.py”,第2733行,在frombuffer中 从字节返回(模式、大小、数据、解码器名称、参数) 文件“C:\Users\User\Ana\anaconda3\envs\Venv\lib\site packages\PIL\Image.py”,第2679行,以frombytes为单位 im.frombytes(数据、解码器名称、参数) 文件“C:\Users\User\Ana\anaconda3\envs\Venv\lib\site packages\PIL\Image.py”,第796行,以frombytes为单位 d、 setimage(self.im)


Tags: 文件pyvenvlibpackagessiteframeusers
1条回答
网友
1楼 · 发布于 2024-09-25 04:19:46

当bboxes或数组(如果分段)的值无效,因此无法用于索引图像时,会导致此错误

例如,类似bbox的[10,20,30,40]可以正常工作,但类似bbox的 [10,-5,30,40]不会因为是负值

同样,传入诸如[]之类的bbox也会导致此错误

因此,我建议打印您的BBox,以查看是否会得到这样的意外数组

萨尔塔克贾因

相关问题 更多 >