googlecreativelab / teachablemachine-community

Example code snippets and machine learning code for Teachable Machine
https://g.co/teachablemachine
Apache License 2.0
1.52k stars 686 forks source link

using tflite on raspberry pi 3 file from teachable machine exported tflite file #105

Open aloooooha opened 4 years ago

aloooooha commented 4 years ago

hello

i am trying to use teachable machine export result image trained file *.tflite file on the tensorflow lite, raspberry pi 3

but i got error which is

Traceback (most recent call last): File "/home/pi/tensorflow2/examples/lite/examples/object_detection/raspberry_pi/detect_picamera.py", line 164, in <module> main() File "/home/pi/tensorflow2/examples/lite/examples/object_detection/raspberry_pi/detect_picamera.py", line 148, in main results = detect_objects(interpreter, image, args.threshold) File "/home/pi/tensorflow2/examples/lite/examples/object_detection/raspberry_pi/detect_picamera.py", line 79, in detect_objects count = int(get_output_tensor(interpreter, 3)) TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'

i want to know included file in tensorflow lite example .tflite file with teachable machine exported .tflite are not same?

or is there am i missing something?

iamroot-01 commented 4 years ago

I am using tflite on rpi4 with the edgetpu accelerator. I downloaded the edgetpu compatible file for my custom model from teachable and when trying to run in my local , I get index out of range error . I will post the error shortly .

MichielBontenbal commented 4 years ago

Same issue here. The tflite file i have exported contains an error. Line 24: image = Image.open(image_path)

But in the script no variable 'image_path' is defined.

See exported script below: from edgetpu.classification.engine import ClassificationEngine from PIL import Image import cv2 import re import os

the TFLite converted to be used with edgetpu

modelPath = 'model_label/model_edgetpu.tflite'

The path to labels.txt that was downloaded with your model

labelPath = 'model_label/labels.txt'

This function parses the labels.txt and puts it in a python dictionary

def loadLabels(labelPath): p = re.compile(r'\s*(\d+)(.+)') with open(labelPath, 'r', encoding='utf-8') as labelFile: lines = (p.match(line).groups() for line in labelFile.readlines()) return {int(num): text.strip() for num, text in lines}

This function takes in a PIL Image from any source or path you choose

def classifyImage(image_path, engine): Load and format your image for use with TM2 model image is reformated to a square to match training image = Image.open(image_path) image.resize((224, 224))

# Classify and ouptut inference
classifications = engine.ClassifyWithImage(image)
return classifications

def main():

Load your model onto your Coral Edgetpu

engine = ClassificationEngine(modelPath)
labels = loadLabels(labelPath)

cap = cv2.VideoCapture(0)
while cap.isOpened():
    ret, frame = cap.read()
    if not ret:
        break

    # Format the image into a PIL Image so its compatable with Edge TPU
    cv2_im = frame
    pil_im = Image.fromarray(cv2_im)

    Resize and flip image so its a square and matches training
    pil_im.resize((224, 224))
    pil_im.transpose(Image.FLIP_LEFT_RIGHT)

    # Classify and display image
    results = classifyImage(pil_im, engine)
    cv2.imshow('frame', cv2_im)
    print(results)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

if name == 'main': main()

main

GautamBose commented 4 years ago

Hi there! you should set the image_path variable to a string thats a path to the image you wish to classify.