Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
from keras.models import load_model
|
| 4 |
+
|
| 5 |
+
# Load the model
|
| 6 |
+
model = load_model('keras_model.h5')
|
| 7 |
+
|
| 8 |
+
# CAMERA can be 0 or 1 based on default camera of your computer.
|
| 9 |
+
camera = cv2.VideoCapture(0)
|
| 10 |
+
|
| 11 |
+
# Grab the labels from the labels.txt file. This will be used later.
|
| 12 |
+
labels = open('labels.txt', 'r').readlines()
|
| 13 |
+
|
| 14 |
+
while True:
|
| 15 |
+
# Grab the webcameras image.
|
| 16 |
+
ret, image = camera.read()
|
| 17 |
+
# Resize the raw image into (224-height,224-width) pixels.
|
| 18 |
+
image = cv2.resize(image, (224, 224), interpolation=cv2.INTER_AREA)
|
| 19 |
+
# Show the image in a window
|
| 20 |
+
cv2.imshow('Webcam Image', image)
|
| 21 |
+
# Make the image a numpy array and reshape it to the models input shape.
|
| 22 |
+
image = np.asarray(image, dtype=np.float32).reshape(1, 224, 224, 3)
|
| 23 |
+
# Normalize the image array
|
| 24 |
+
image = (image / 127.5) - 1
|
| 25 |
+
# Have the model predict what the current image is. Model.predict
|
| 26 |
+
# returns an array of percentages. Example:[0.2,0.8] meaning its 20% sure
|
| 27 |
+
# it is the first label and 80% sure its the second label.
|
| 28 |
+
probabilities = model.predict(image)
|
| 29 |
+
# Print what the highest value probabilitie label
|
| 30 |
+
print(labels[np.argmax(probabilities)])
|
| 31 |
+
# Listen to the keyboard for presses.
|
| 32 |
+
keyboard_input = cv2.waitKey(1)
|
| 33 |
+
# 27 is the ASCII for the esc key on your keyboard.
|
| 34 |
+
if keyboard_input == 27:
|
| 35 |
+
break
|
| 36 |
+
|
| 37 |
+
camera.release()
|
| 38 |
+
cv2.destroyAllWindows()
|