embaras 2023-07-24 11:39 采纳率: 0%
浏览 684
已结题

opencv中resize代码报错

问题遇到的现象和发生背景

想做一个opencv识别特定人脸的代码,用的是cvzone.ClassificationModule 中的 Classifier函数实现的,但是总显示resize代码有错,想了好久还是不会。

代码如下:
from cvzone.ClassificationModule import Classifier
import cv2

cap = cv2.VideoCapture("G:/python37/people/photo1.jpg")
# cap = cv2.VideoCapture(0)
maskClassifier = Classifier('model/keras_model.h5', 'model/labels.txt')
while True:
    _, img = cap.read()
    predection = maskClassifier.getPrediction(img)
    print(predection)
    cv2.imshow("Image", img)
    cv2.waitKey(1)

######代码报错

([0.9795129, 0.020487158], 0)
Traceback (most recent call last):
  File "G:\python37\detect.py", line 9, in <module>
    predection = maskClassifier.getPrediction(img)
  File "G:\anaconda\envs\py37\lib\site-packages\cvzone\ClassificationModule.py", line 38, in getPrediction
    imgS = cv2.resize(img, (224, 224))
cv2.error: OpenCV(4.1.0) C:\projects\opencv-python\opencv\modules\imgproc\src\resize.cpp:3718: error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize'

######cvzone.ClassificationModule.getPrediction代码如下:

    def getPrediction(self, img, draw= True, pos=(50, 50), scale=2, color = (0,255,0)):
        # resize the image to a 224x224 with the same strategy as in TM2:
        imgS = cv2.resize(img, (224, 224))#就是这里出了问题
        # turn the image into a numpy array
        image_array = np.asarray(imgS)
        # Normalize the image
        normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1

        # Load the image into the array
        self.data[0] = normalized_image_array

        # run the inference
        prediction = self.model.predict(self.data)
        indexVal = np.argmax(prediction)

        if draw and self.labels_path:
            cv2.putText(img, str(self.list_labels[indexVal]),
                        pos, cv2.FONT_HERSHEY_COMPLEX, scale, color, 2)

        return list(prediction[0]), indexVal

望指导

  • 写回答

13条回答 默认 最新

  • Bert_2022 2023-07-24 11:56
    关注
    获得0.30元问题酬金

    可能与getPrediction函数中的图像尺寸有关。该函数期望输入图像的尺寸为224x224像素,因为加载的模型是基于这个特定尺寸进行训练的。

    然而,使用cv2.resize函数时,如果输入图像的尺寸与期望的224x224不匹配,可能会导致问题。为了处理不同尺寸的图像,你可以在cv2.resize函数中使用cv2.INTER_AREA插值方法,该方法适用于缩小图像。

    以下是如何修改getPrediction函数以处理不同尺寸图像的方法:

    import cv2
    import numpy as np
    def getPrediction(self, img, draw=True, pos=(50, 50), scale=2, color=(0, 255, 0)):
        # 将图像调整为224x224,使用与TM2相同的策略:
        imgS = cv2.resize(img, (224, 224), interpolation=cv2.INTER_AREA)
        # 其余代码保持不变...
    

    通过在cv2.resize函数中指定cv2.INTER_AREA插值方法,该函数将使用像素面积关系来缩小图像,从而适用于在减小图像尺寸时保留重要细节。
    请确保相应地更新Classifier类定义中的getPrediction函数。经过这样的修改,该函数现在应该可以处理各种尺寸的图像,而不会出现你遇到的OpenCV错误。

    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 8月1日
  • 创建了问题 7月24日

悬赏问题

  • ¥200 csgo2的viewmatrix值是否还有别的获取方式
  • ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊
  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 pycharm运行main文件,显示没有conda环境
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件
  • ¥15 为什么eclipse不能再下载了?
  • ¥15 编辑cmake lists 明明写了project项目名,但是还是报错怎么回事
  • ¥15 关于#计算机视觉#的问题:求一份高质量桥梁多病害数据集
  • ¥15 特定网页无法访问,已排除网页问题
  • ¥50 如何将脑的图像投影到颅骨上