Simple Running A Complicated Python Script

Let’s say you got a complicated script that need some libraries to make it Run well

import tensorflow as tf
import cv2.cv2 as cv
import pathlib
# 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("folder").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))

install python3

sudo apt-get install python3

install pip

sudo apt install python3-pip

Don’t forget to update

pip install --upgrade pip

noted : if you just want to experiment the script, better you use virtual environment or jupyter notebook

install the required dependencies

pip3 install tensorflow
pip3 install pathlib
pip3 install opencv-python

Running the script

python3 main.py

--

--

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..