#๐Ÿ”’ Error while loading a keras Model (.h5) exported by teachable machine.

7 messages ยท Page 1 of 1 (latest)

sterile latchBOT
#

@cloud mist

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

cloud mist
#

So i was working on a computer vision project and i decided to use teachable machine to train my model, my model worked like a charm in the teachable machine and i was able to export it as well, but when i tried to use it in my project i was not able to load the model for some reason (i am use the example code that teachable machine provides)

#

my code:
from keras.models import load_model # TensorFlow is required for Keras to work
import cv2 # Install opencv-python
import numpy as np

Disable scientific notation for clarity

np.set_printoptions(suppress=True)

Load the model

model = load_model("keras_Model.h5", compile=False)

Load the labels

class_names = open("labels.txt", "r").readlines()

CAMERA can be 0 or 1 based on default camera of your computer

camera = cv2.VideoCapture(1)

while True:
# Grab the webcamera's image.
ret, image = camera.read()

# Resize the raw image into (224-height,224-width) pixels
image = cv2.resize(image, (224, 224), interpolation=cv2.INTER_AREA)

# Show the image in a window
cv2.imshow("Webcam Image", image)

# Make the image a numpy array and reshape it to the models input shape.
image = np.asarray(image, dtype=np.float32).reshape(1, 224, 224, 3)

# Normalize the image array
image = (image / 127.5) - 1

# Predicts the model
prediction = model.predict(image)
index = np.argmax(prediction)
class_name = class_names[index]
confidence_score = prediction[0][index]

# Print prediction and confidence score
print("Class:", class_name[2:], end="")
print("Confidence Score:", str(np.round(confidence_score * 100))[:-2], "%")

# Listen to the keyboard for presses.
keyboard_input = cv2.waitKey(1)

# 27 is the ASCII for the esc key on your keyboard.
if keyboard_input == 27:
    break

camera.release()
cv2.destroyAllWindows()

#

my file structure:

sterile latchBOT
#

@cloud mist

Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.