My TFLite running on Desktop

I have TFlite Model and i want to run checking image on Desktop.

Preparation

Python

If you don’t have Python on your PC then you can install it from: Getting Start with Python.

TFLite Model

You can use my sample TFLite Model. Inside my TFLite model there are 4 labels. indosiar_iklan, indosiar, sctv and sctv_iklan. If you confused with TFLite or Label you can read this.

Execution

The Script

Use following script to run the TFLite

import tensorflow as tf
import cv2.cv2 as cv
import pathlib
import sys
# Load TFLite model and allocate tensors.
interpreter = tf.lite.Interpreter(model_path="model.tflite")
# Get input and output tensors.
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
# input details
print(input_details)
# output details
print(output_details)
interpreter.allocate_tensors()for file in pathlib.Path(sys.argv[1]).iterdir():
# read and resize the image
img = cv.imread(r"{}".format(file.resolve()))
new_img = cv.resize(img, (224, 224))

interpreter.set_tensor(input_details[0]['index'], [new_img])
interpreter.invoke()
output_data = interpreter.get_tensor(output_details[0]['index'])
if (output_data[0][0] > 200):
print("file {} -> indosiar_ads".format(file.stem))
elif (output_data[0][1] > 200):
print("file {} -> indosiar".format(file.stem))
elif (output_data[0][2] > 200):
print("file {} -> sctv".format(file.stem))
elif (output_data[0][3] > 200):
print("file {} -> sctv_ads".format(file.stem))
else:
print("file {} -> unknown".format(file.stem))

My TFLite model file name is model.tflite. So i give that on model_path when creating interpreter object.

On looping section there is sys.argv[1] that the value get from arg given when run the script. The argument is folder test path.

The images for test you can download from this link.

Install Requirement Libraries

You can use Conda or Pip for this.

pip3 install tensorflow
pip3 install pathlib
pip3 install opencv-python

Run the Script

Run the script with following argument as testing images folder.

python3 main.py test

--

--

--

Still believe, can change the world with code..

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Considerations when Developing for the Cloud

Configuring Hadoop Services Using Ansible Playbook

Building a WebGL Project in Unity

Livin’ La Vida Low Code

I think back to that morning in May 1995 when I made that fateful call to the medical school.

[Leet Code] Validate Binary Search Tree

[Leet Code] Word Pattern

Terraform; GCP Service Account with Role and json keys.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
ade sueb

ade sueb

Still believe, can change the world with code..

More from Medium

Deploying Machine Learning Models on Live User Webcam Feeds using TensorFlow.js

TensorFlow js Logo

Why is “import module” preferred over “from module import *”?

Step by step guide to setup Tensorflow with GPU support on windows 10

A Simple Introduction To Data Parallelism In JAX