如何在 Python 程式中使用 Google Teachable Machine 建立的 model
# Python program to control mouse based on head position # Import necessary modules
import numpy as np
import cv2
import tensorflow as tf
# Using laptop’s webcam as the source of video
cap = cv2.VideoCapture(0)
cap.set(3, 480) # ID 3 = width
cap.set(4, 320) # ID 4 = height
# Labels — The various outcome possibilities
labels = ["Up","Down","Left","Right","Neutral"]
# Loading the model weigths we just downloaded
model = tf.keras.models.load_model("keras_model.h5", compile = False)
while True:
success, image = cap.read()
if success == False:
break
# Necessary to avoid conflict between left and right
image = cv2.flip(image, 1)
cv2.imshow("Frame", image)
# The model takes an image of dimensions (224,224) as input so let’s
# reshape our image to the same.
img = cv2.resize(image, (224, 224))
# Convert the image to a numpy array
img = np.array(img, dtype=np.float32)
img = np.expand_dims(img, axis=0)
# Normalizing input image
img = img / 255
# Predict the class
prediction = model.predict(img)
# Map the prediction to the labels
# Rnp.argmax returns the indices of the maximum values along an axis.
predicted_labels = labels[np.argmax(prediction[0], axis=-1)]
# print(predicted_labels)
print(predicted_labels, np.argmax(prediction[0], axis=-1), prediction[0])
# Close all windows if one second has passed and ‘q’ is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release open connections
cap.release()
cv2.destroyAllWindows()
Other Videos By 夏sir網上教室
2021-11-15 | Diving game 2021 (3C Vivian) |
2021-11-15 | Shooting game (3C Lui) |
2021-11-15 | Two's complement secrets 二補碼 有妙法 |
2021-11-13 | Shooting game (3C Miu Miu) |
2021-11-13 | Shooting game (3C Yi Sum) |
2021-11-13 | Shooting game (3C Vickie) |
2021-11-13 | Shooting game (3C Sze Lam) |
2021-11-13 | Shooting game (3C Sabrina) |
2021-11-13 | Shooting game (3C Hazel) |
2021-11-13 | Diving game 2021 (3C Lisa) |
2021-11-06 | 如何在 Python 程式中使用 Google Teachable Machine 建立的 model |
2021-11-04 | Python Face Recognition Demo |
2021-11-03 | Python Face Recognition |
2021-11-02 | Python 五行 codes下載 YouTube 影片 |
2021-10-25 | 詩歌 - Love was when |
2021-10-23 | 詩歌 - Let us send the light |
2021-10-22 | 詩歌 - Come Holy Spirit |
2021-10-22 | 詩歌 - The Lord is my shepherd |
2021-10-22 | 詩歌 - 恩賜 (絕版) |
2021-10-18 | Python Google Voice Assistance Setup on MacOS - with trouble-shooting on installing pyaudio (廣東話) |
2021-10-13 | 如何利用 Python 建立點陣圖 (廣東話) |