opencv cv 2,错误:打开加密版本(4.5.3):-1:错误:(-5:参数错误)在函式'resize'中

0wi1tuuw  于 2022-11-15  发布在  其他
关注(0)|答案(2)|浏览(116)
import numpy as np
import cv2
import pickle

########### PARAMETERS ##############
width = 640
height = 480
threshold = 0.65 # MINIMUM PROBABILITY TO CLASSIFY
cameraNo = 1
#####################################

#### CREATE CAMERA OBJECT
cap = cv2.VideoCapture(cameraNo)
cap.set(3,width)
cap.set(4,height)

#### LOAD THE TRAINNED MODEL
pickle_in = open("model_trained.p","rb")
model = pickle.load(pickle_in)

print(cv2.__version__)
#### PREPORCESSING FUNCTION
def preProcessing(img):
    img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    img = cv2.equalizeHist(img)
    img = img/255
    return img

    while True:
        success, imgOriginal = cap.read()
        img = np.asarray(imgOriginal)
        img = cv2.resize(img,(32,32))   <------ ERROR HERE
        img = preProcessing(img)
        cv2.imshow("Processsed Image",img)
        img = img.reshape(1,32,32,1)
        #### PREDICT
        classIndex = int(model.predict_classes(img))
        #print(classIndex)
    predictions = model.predict(img)
    #print(predictions)
    probVal= np.amax(predictions)
    print(classIndex,probVal)

    if probVal> threshold:
        cv2.putText(imgOriginal,str(classIndex) + "   "+str(probVal),
                    (50,50),cv2.FONT_HERSHEY_COMPLEX,
                    1,(0,0,255),1)

    cv2.imshow("Original Image",imgOriginal)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

错误:

文件“E:/DATA_SCIENCE/MY_WORK/test.py“,第32行,位于
(32,32))cv 2.错误:OpenCV(4.5.3):-1:错误:函数“resize”中的(-5:参数错误)重载解析失败:

  • 不支持src数据类型= 17
  • 参数cv::UMat'src'需要Ptr
8hhllhi2

8hhllhi21#

你应该释放下面代码的“cap”结束,退出while循环。

if probVal> threshold:
        cv2.putText(imgOriginal,str(classIndex) + "   "+str(probVal),
                    (50,50),cv2.FONT_HERSHEY_COMPLEX,
                    1,(0,0,255),1)

    cv2.imshow("Original Image",imgOriginal)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()
6yt4nkrj

6yt4nkrj2#

将cameraNo设置为0。这通常是因为您的笔记本电脑或PC的默认网络摄像头的摄像头编号为0。

相关问题