My TFLite running on Desktop

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



TFLite Model


The Script

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
# 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])
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))
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

pip3 install tensorflow
pip3 install pathlib
pip3 install opencv-python

Run the Script

python3 test

Still believe, can change the world with code..