-
Notifications
You must be signed in to change notification settings - Fork 674
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot use the exported model #349
Comments
Hello @Lostdawn95, I'm glad to hear that you're interested in using your TensorFlow Lite model on your Raspberry Pi 4. If you're encountering issues with the script, it would be helpful if you could provide more details about the specific error you're facing. Nonetheless, here's the code i used to run mine classify.py # Copyright 2021 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Main script to run image classification."""
import argparse
import sys
import time
import cv2
from tflite_support.task import core
from tflite_support.task import processor
from tflite_support.task import vision
# Visualization parameters
_ROW_SIZE = 20 # pixels
_LEFT_MARGIN = 24 # pixels
_TEXT_COLOR = (0, 0, 255) # red
_FONT_SIZE = 1
_FONT_THICKNESS = 1
_FPS_AVERAGE_FRAME_COUNT = 10
def run(model: str, max_results: int, score_threshold: float, num_threads: int,
enable_edgetpu: bool, camera_id: int, width: int, height: int) -> None:
"""Continuously run inference on images acquired from the camera.
Args:
model: Name of the TFLite image classification model.
max_results: Max of classification results.
score_threshold: The score threshold of classification results.
num_threads: Number of CPU threads to run the model.
enable_edgetpu: Whether to run the model on EdgeTPU.
camera_id: The camera id to be passed to OpenCV.
width: The width of the frame captured from the camera.
height: The height of the frame captured from the camera.
"""
# Initialize the image classification model
base_options = core.BaseOptions(
file_name=model, use_coral=enable_edgetpu, num_threads=num_threads)
# Enable Coral by this setting
classification_options = processor.ClassificationOptions(
max_results=max_results, score_threshold=score_threshold)
options = vision.ImageClassifierOptions(
base_options=base_options, classification_options=classification_options)
classifier = vision.ImageClassifier.create_from_options(options)
# Variables to calculate FPS
counter, fps = 0, 0
start_time = time.time()
# Start capturing video input from the camera
cap = cv2.VideoCapture(camera_id)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
# Continuously capture images from the camera and run inference
while cap.isOpened():
success, image = cap.read()
if not success:
sys.exit(
'ERROR: Unable to read from webcam. Please verify your webcam settings.'
)
counter += 1
image = cv2.flip(image, 1)
# Convert the image from BGR to RGB as required by the TFLite model.
rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Create TensorImage from the RGB image
tensor_image = vision.TensorImage.create_from_array(rgb_image)
# List classification results
categories = classifier.classify(tensor_image)
# Show classification results on the image
for idx, category in enumerate(categories.classifications[0].categories):
category_name = category.category_name
score = round(category.score, 2)
result_text = category_name + ' (' + str(score) + ')'
text_location = (_LEFT_MARGIN, (idx + 2) * _ROW_SIZE)
cv2.putText(image, result_text, text_location, cv2.FONT_HERSHEY_PLAIN,
_FONT_SIZE, _TEXT_COLOR, _FONT_THICKNESS)
# Calculate the FPS
if counter % _FPS_AVERAGE_FRAME_COUNT == 0:
end_time = time.time()
fps = _FPS_AVERAGE_FRAME_COUNT / (end_time - start_time)
start_time = time.time()
# Show the FPS
fps_text = 'FPS = ' + str(int(fps))
text_location = (_LEFT_MARGIN, _ROW_SIZE)
cv2.putText(image, fps_text, text_location, cv2.FONT_HERSHEY_PLAIN,
_FONT_SIZE, _TEXT_COLOR, _FONT_THICKNESS)
# Stop the program if the ESC key is pressed.
if cv2.waitKey(1) == 27:
break
cv2.imshow('image_classification', image)
cap.release()
cv2.destroyAllWindows()
def main():
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument(
'--model',
help='Name of image classification model.',
required=False,
default='efficientnet_lite0.tflite')
parser.add_argument(
'--maxResults',
help='Max of classification results.',
required=False,
default=3)
parser.add_argument(
'--scoreThreshold',
help='The score threshold of classification results.',
required=False,
type=float,
default=0.0)
parser.add_argument(
'--numThreads',
help='Number of CPU threads to run the model.',
required=False,
default=4)
parser.add_argument(
'--enableEdgeTPU',
help='Whether to run the model on EdgeTPU.',
action='store_true',
required=False,
default=False)
parser.add_argument(
'--cameraId', help='Id of camera.', required=False, default=0)
parser.add_argument(
'--frameWidth',
help='Width of frame to capture from camera.',
required=False,
default=640)
parser.add_argument(
'--frameHeight',
help='Height of frame to capture from camera.',
required=False,
default=480)
args = parser.parse_args()
run(args.model, int(args.maxResults),
args.scoreThreshold, int(args.numThreads), bool(args.enableEdgeTPU),
int(args.cameraId), args.frameWidth, args.frameHeight)
if __name__ == '__main__':
main() Run the script using the following command, replacing <model_path> with the actual path to your .tflite model file: python classify.py --model=<model_path> --maxResults=3 --scoreThreshold=0.0 --numThreads=4 --enableEdgeTPU=False --cameraId=0 --frameWidth=640 --frameHeight=480 Adjust the arguments ( The script will use the provided model file and start processing the frames from the camera. Make sure that your Raspberry Pi is properly set up with the camera and the necessary libraries are installed before running the script. If you encounter any issues or need further assistance, feel free to reach out. thank you |
Hey!
First of all I have to say I just love how simple and easy to use your website is
It's absolutely amazing!
I made a model, checked it using the webcam, and was really happy with the results
I'd love to use it on my Raspberry Pi 4
So far i installed tensorflow lite there and ran tflite files with a guide I have:
source tflite1-env/bin/activate
python TFLite_detection_webcam.py --modeldir=<dir_name>
but I do get an error in this .py file
so I was wondering if there is a guide here how to use the exported model, it would mean the world to me right now to get this working on my Raspberry+camera
ty in advance xo
The text was updated successfully, but these errors were encountered: