问题遇到的现象和发生背景
想做一个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
望指导