Reputation: 61
I have an pretrained model. Model trained with 20000 "gray" sample. İt is working with "gray" test samples. But I want to test this model with webcam. Here my code:
#Load the saved model
model = keras.models.load_model('C:\keras\handrecognition_model.h5')
video = cv2.VideoCapture(0)
while True:
_, frame = video.read()
im = Image.fromarray(frame, 'RGB')
im = im.resize((128, 128))
img_array = np.array(im)
img_array = np.expand_dims(img_array, axis=0)
prediction = int(model.predict(img_array)[0][0])
# if prediction is 0, which means I am missing on the image, then show the frame in gray color.
if prediction == 0:
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow("Capturing", frame)
key = cv2.waitKey(1)
if key == ord('q'):
break
video.release()
cv2.destroyAllWindows()
There is an error : ValueError: Error when checking input: expected conv2d_1_input to have shape (120, 320, 1) but got array with shape (128, 128, 3).
Here output of gray test images:
Model trained:
# Construction of model
model = Sequential()
model.add(Conv2D(32, (5, 5), activation='relu', input_shape=(120, 320, 1)))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(10, activation='softmax'))
Edit: I update code like this:
_, frame = video.read()
frame = cv2.resize(frame, (120, 360))
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
img_array = np.array(gray)
ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (1, 360, 120)
Edit 2: Here train article: https://towardsdatascience.com/tutorial-using-deep-learning-and-cnns-to-make-a-hand-gesture-recognition-model-371770b63a51
Edit 3: I guess, it is working. Now I will send frame to predict and I will found hand gest. I will share if I can do. Thank you.
_, frame = video.read()
frameCopy=frame.copy()
frameCopy = cv2.resize(frameCopy, (120, 320))
gray = cv2.cvtColor(frameCopy, cv2.COLOR_BGR2GRAY)
img_array = np.array(gray)
img_array = img_array.reshape(120, 320, 1)
img_array = np.expand_dims(img_array, axis=0)
Upvotes: 3
Views: 1195
Reputation: 3496
Answer for you question after your edit: You need 4 dimensions not three: (batch-size, channels, width, height). So try the following:
img_array = np.array(gray)
img_array = img_array.reshape(1, 1, 360, 120)
Upvotes: 1
Reputation: 1056
im = Image.fromarray(frame, 'RGB')
im = im.resize((128, 128))
im = im.convert('RGB')
This will convert your grayscale image to RGB. It will just copy the values from the one dimension you currently have to 3 dimensions. If it throws an error, try this instead
im = im.convert('L')
im = im.convert('RGB')
'L'
converts it to black and white first in case your input is sometimes not just 1D
Upvotes: 0