I am using a pre-trained model on a video dataset. Below is my code.
from keras.applications.vgg19 import decode_predictions
from keras.applications.vgg19 import VGG19, preprocess_input
import threading,cv2
import numpy as np
label = ''
frame = None
class MyThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
global label
self.model = VGG19(weights="imagenet")
while (~(frame is None)):
(inID, label) = self.predict(frame)
def predict(self, frame):
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB).astype(np.float32)
image = image.transpose((2, 0, 1))
image = image.reshape((1,) + image.shape)
image = preprocess_input(image)
preds = self.model.predict(image)
return decode_predictions(preds)[0]
videoFile ="D:/lostpanda.mp4"
cap = cv2.VideoCapture(videoFile)
while(cap.isOpened()):
keras_thread = MyThread()
keras_thread.start()
while (True):
ret, original = cap.read()
frame = cv2.resize(original, (224, 224))
cv2.putText(original, "Label: {}".format(label), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
cv2.imshow("Classification", original)
cap.release()
frame = None
I am retrieving the below error
ValueError: Error when checking input: expected input_1 to have a shape (224, 224, 3) but got array
with shape (3, 224, 224)
Traceback (most recent call last):
File "C:/Usersvideo-classification.py", line 35, in <module>
frame = cv2.resize(original, (224, 224))
cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\imgproc\src\resize.cpp:4045: error:
(-215:Assertion failed) !ssize.empty() in function 'cv::resize'
Thanks, help is highly appreciated.
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB).astype(np.float32)keras.backend.set_image_data_format('channels_last')