Skip to content

Commit

Permalink
Merge pull request #308 from sryu1/master
Browse files Browse the repository at this point in the history
Updated keras and opencv keras snippets attempt
  • Loading branch information
HalfdanJ authored Jan 27, 2023
2 parents 090986f + cb5204e commit b6e50f0
Show file tree
Hide file tree
Showing 6 changed files with 373 additions and 301 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,12 @@ This repository contains two components of [Teachable Machine](https://teachable

You have a few options:

- [Use this form](https://forms.gle/uthe2C4tZNPA11GX7).
- Share your projects using [#teachablemachine](https://twitter.com/hashtag/teachablemachine) on Twitter or on the [Experiments with Google](https://experiments.withgoogle.com/submit) page.
- Open an issue in this repository.

## Community Contributions and Projects

- [Teachable Machine Node Library for image models](https://github.com/tr7zw/teachablemachine-node-example)
- [Teachable Machine Node Library for image models](https://github.com/tr7zw/teachablemachine-node-example) (Archived and now continued [here](https://github.com/drinkspiller/teachablemachine-node-example/))
- [Teachable Machine Mobile for image models](https://github.com/mstale007/Teachable_Machine_Mobile/tree/master)

## Disclaimer
Expand Down
4 changes: 2 additions & 2 deletions libraries/README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Teachable Machine Libraries

This section contains support libraries for the new version of Teachable Machine. For more info go to: [Teachable Machine](https://teachablemachine.withgoogle.com/io19).
This section contains support libraries for the new version of Teachable Machine.

## Model Libraries

| Library | Based on model | Details | Install | CDN |
|---------|-----------------|---------------------------------------------------------|---------|-----|
| [Image](./image/) | [MobileNet](https://github.com/tensorflow/tfjs-models/tree/master/mobilenet) | Use a model trained to classify your own images | `npm i @teachablemachine/image` | [![](https://data.jsdelivr.com/v1/package/npm/@teachablemachine/image/badge)](https://www.jsdelivr.com/package/npm/@teachablemachine/image) |
| [Audio](./audio/) | [Speech Commands](https://github.com/tensorflow/tfjs-models/tree/master/speech-commands) | Use a model trained to classify your own audio snippets | npm i @tensorflow-models/speech-commands | [![](https://data.jsdelivr.com/v1/package/npm/@tensorflow-models/speech-commands/badge)](https://github.com/tensorflow/tfjs-models/tree/master/speech-commands) |
| [Audio](./audio/) | [Speech Commands](https://github.com/tensorflow/tfjs-models/tree/master/speech-commands) | Use a model trained to classify your own audio snippets | `npm i @tensorflow-models/speech-commands` | [![](https://data.jsdelivr.com/v1/package/npm/@tensorflow-models/speech-commands/badge)](https://github.com/tensorflow/tfjs-models/tree/master/speech-commands) |
| [Pose](./pose/) | [PoseNet](https://github.com/tensorflow/tfjs-models/tree/master/posenet) | Use a model trained to classify body poses | `npm i @teachablemachine/pose` | [![](https://data.jsdelivr.com/v1/package/npm/@teachablemachine/pose/badge)](https://www.jsdelivr.com/package/npm/@teachablemachine/pose) |

## Development
Expand Down
26 changes: 13 additions & 13 deletions snippets/markdown/image/tensorflow/keras.md
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
```python
from keras.models import load_model
from PIL import Image, ImageOps #Install pillow instead of PIL
from keras.models import load_model # TensorFlow is required for Keras to work
from PIL import Image, ImageOps # Install pillow instead of PIL
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)
model = load_model("keras_Model.h5", compile=False)

# Load the labels
class_names = open('labels.txt', 'r').readlines()
class_names = open("labels.txt", "r").readlines()

# Create the array of the right shape to feed into the keras model
# The 'length' or number of images you can put into the array is
# determined by the first position in the shape tuple, in this case 1.
# determined by the first position in the shape tuple, in this case 1
data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)

# Replace this with the path to your image
image = Image.open('<IMAGE_PATH>').convert('RGB')
image = Image.open("<IMAGE_PATH>").convert("RGB")

#resize the image to a 224x224 with the same strategy as in TM2:
#resizing the image to be at least 224x224 and then cropping from the center
# resizing the image to be at least 224x224 and then cropping from the center
size = (224, 224)
image = ImageOps.fit(image, size, Image.Resampling.LANCZOS)

#turn the image into a numpy array
# turn the image into a numpy array
image_array = np.asarray(image)

# Normalize the image
normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1
normalized_image_array = (image_array.astype(np.float32) / 127.5) - 1

# Load the image into the array
data[0] = normalized_image_array

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

print('Class:', class_name, end='')
print('Confidence score:', confidence_score)
# Print prediction and confidence score
print("Class:", class_name[2:], end="")
print("Confidence Score:", confidence_score)
```
47 changes: 30 additions & 17 deletions snippets/markdown/image/tensorflow/opencv-keras.md
Original file line number Diff line number Diff line change
@@ -1,36 +1,49 @@
```Python
import cv2
```python
from keras.models import load_model # TensorFlow is required for Keras to work
import cv2 # Install opencv-python
import numpy as np
from keras.models import load_model

# Disable scientific notation for clarity
np.set_printoptions(suppress=True)

# Load the model
model = load_model('keras_model.h5')
model = load_model("keras_Model.h5", compile=False)

# CAMERA can be 0 or 1 based on default camera of your computer.
camera = cv2.VideoCapture(0)
# Load the labels
class_names = open("labels.txt", "r").readlines()

# Grab the labels from the labels.txt file. This will be used later.
labels = open('labels.txt', 'r').readlines()
# CAMERA can be 0 or 1 based on default camera of your computer
camera = cv2.VideoCapture(0)

while True:
# Grab the webcameras image.
# Grab the webcamera's image.
ret, image = camera.read()
# Resize the raw image into (224-height,224-width) pixels.

# 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)
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
# Have the model predict what the current image is. Model.predict
# returns an array of percentages. Example:[0.2,0.8] meaning its 20% sure
# it is the first label and 80% sure its the second label.
probabilities = model.predict(image)
# Print what the highest value probabilitie label
print(labels[np.argmax(probabilities)])

# 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
Expand Down
4 changes: 2 additions & 2 deletions snippets/package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"dependencies": {
"http-server": "^0.11.1"
"http-server": "^14.1.1"
},
"scripts": {
"start": "http-server --cors snippets",
"lint": "markdownlint ./markdown ./converter"
},
"devDependencies": {
"markdownlint-cli": "^0.18.0"
"markdownlint-cli": "^0.32.2"
}
}
Loading

0 comments on commit b6e50f0

Please sign in to comment.