A real time image classification model using Convolutional Neural Nets + open CV, here a Link to code
I am trying to run the following file camera_test.py
which implements multithreading to improve fps of the program. The while loop prepares the frame, while the thread processes it to predict the label of the image within the frame
from imagenet_utils import decode_predictions
from imagenet_utils import preprocess_input
from keras.applications.vgg16 import VGG16
import cv2
import numpy as np
import sys
import threading
label = ''
frame = None
class MyThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
global label
# Load the VGG16 network
print("[INFO] loading network...")
self.model = VGG16(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]
cap = cv2.VideoCapture(0)
if (cap.isOpened()):
print("Camera OK")
else:
cap.open()
keras_thread = MyThread()
keras_thread.start()
while (True):
ret, original = cap.read()
frame = cv2.resize(original, (224, 224))
# Display the predictions
# print("ImageNet ID: {}, Label: {}".format(inID, label))
cv2.putText(original, "Label: {}".format(label), (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
cv2.imshow("Classification", original)
if (cv2.waitKey(1) & 0xFF == ord('q')):
break;
cap.release()
frame = None
cv2.destroyAllWindows()
sys.exit()
But, the performance is unpredictable. The code works perfectly fine sometimes, and another time throws off a random error.Error I commonly face when I run this code: ValueError: Tensor Tensor("Placeholder:0", shape=(3, 3, 3, 64), dtype=float32) is not an element of this graph
.
I have tried implementing multiprocessing.pool
instead of threads, but the program hangs up a lot and freezes. Are there any other alternatives? Or is there a way to fix this code?