0

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?

Dadep
  • 2,796
  • 5
  • 27
  • 40
  • 2
    you are updating a variable (frame) that you read which is a global and it is updated in another, use a lock to make it thread safe – api55 Sep 26 '17 at 13:00
  • Sounds to me like you need mutexes – GPPK Sep 26 '17 at 13:21
  • @GPPK Thanks for you reply.I read about mutex (I am fairly new to python) in the given link and I am trying to implement it: http://www.8bitavenue.com/2016/11/python-mutex-example/ It talks about locking between child threads. I am confused as to how to implement it between child thread and main thread. And also where to type my_mutex.acquire() and my_mutex.release() when while loops are present . – Jagiri Manjeera Sep 27 '17 at 11:10
  • @api55, Thanks fro your reply. I am trying to use mutex as I mentioned above for locking – Jagiri Manjeera Sep 27 '17 at 11:11
  • @JagiriManjeera take a look to [this post](https://stackoverflow.com/questions/35088139/how-to-make-a-thread-safe-global-counter-in-python). Look at the second answer from Michael Foukarakis. You are probably wanting the "counter" object here for the frame variable. with some modifications to suit your needs. (val will be the frame you use, and instead of increament function you need to do the set frame one) – api55 Sep 27 '17 at 13:10
  • @api55, Hey thanks a lot! I will look into it – Jagiri Manjeera Sep 28 '17 at 07:29

0 Answers0