# Teachable machine
###### tags: `AI` `python` `Teachable`
# My codes
fetch picture
https://colab.research.google.com/drive/1vH1lqbZVxH6AmHAGu2mT3GMkfYxC0Uwd
Ai identify
https://colab.research.google.com/drive/1mhlTO8Lx16J_X0bT8wHxglouCy2Q_vZ0#scrollTo=kGUvQWv-lNWg
# Data
## Fetch resource by using python
## library
```python=
import requests
url ="https://data.nhi.gov.tw/resource/Nhi_Fst/Fstdata.csv"
```
Remark: csv-> a file form of Excel
```python
response = requests.get(url)
response
```
# Model
## Teachable machine

---
Model
main()
```
from keras.models import load_model
from PIL import Image, ImageOps #Install pillow instead of PIL
import numpy as np
filename = take_photo()
# 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()
# 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.
data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
# Replace this with the path to your image
image = Image.open(filename).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
size = (224, 224)
image = ImageOps.fit(image, size, Image.LANCZOS)
#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
# Load the image into the array
data[0] = normalized_image_array
# run the inference
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)
```

Capture images
Include code of the take photo function


Function Construct
```python=
from IPython.display import display, Javascript
from google.colab.output import eval_js
from base64 import b64decode
def take_photo(filename='photo.jpg', quality=0.8):
js = Javascript('''
async function takePhoto(quality) {
const div = document.createElement('div');
const capture = document.createElement('button');
capture.textContent = 'Capture';
div.appendChild(capture);
const video = document.createElement('video');
video.style.display = 'block';
const stream = await navigator.mediaDevices.getUserMedia({video: true});
document.body.appendChild(div);
div.appendChild(video);
video.srcObject = stream;
await video.play();
// Resize the output to fit the video element.
google.colab.output.setIframeHeight(document.documentElement.scrollHeight, true);
// Wait for Capture to be clicked.
await new Promise((resolve) => capture.onclick = resolve);
const canvas = document.createElement('canvas');
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
canvas.getContext('2d').drawImage(video, 0, 0);
stream.getVideoTracks()[0].stop();
div.remove();
return canvas.toDataURL('image/jpeg', quality);
}
''')
display(js)
data = eval_js('takePhoto({})'.format(quality))
binary = b64decode(data.split(',')[1])
with open(filename, 'wb') as f:
f.write(binary)
return filename
```
Take a picture by using web camera
```python
from IPython.display import Image
try:
filename = take_photo()
print('Saved to {}'.format(filename))
# Show the image which was just taken.
display(Image(filename))
except Exception as err:
# Errors will be thrown if the user does not have a webcam or if they do not
# grant the page permission to access it.
print(str(err))
```
Result




---
<!--
 -->
<!-- 



-->
---
Sample
[https://drive.google.com/drive/folders/1bRf3qNji5bPKCFMfpPvAvRB5zmAGRudt](https://drive.google.com/drive/folders/1bRf3qNji5bPKCFMfpPvAvRB5zmAGRudt?authuser=2)
Col lab
https://colab.research.google.com/drive/1vH1lqbZVxH6AmHAGu2mT3GMkfYxC0Uwd#scrollTo=cPmKwKpzAJOt
Reference
https://docs.google.com/presentation/d/129JDRkz4k5FScpTzC0GX2hN-omfqOxmHOkSpwhkkvM0/edit#slide=id.g1922aa4ceba_0_361
programe
https://colab.research.google.com/drive/1RY8BEmNy_8UVJIOCoDPBfRe8DDh_lXL2?usp=sharing