Stanford-Cars / app.py
javad-rezaie
Updated to run on cpu.
71c7733
raw
history blame
1.69 kB
import gradio as gr
from pathlib import Path
import openvino as ov
import mmcv
import numpy as np
with open("/model_config/car_classes.txt", "r") as f:
car_classes = f.readlines()
CAR_CLASSES = [c.replace(",\n", "").replace("'","") for c in car_classes]
def pred_class(image_path):
base_artifacts_dir = Path("./")
model_name = "end2end"
model_xml_name = f'{model_name}.xml'
model_bin_name = f'{model_name}.bin'
model_xml_path = base_artifacts_dir / model_xml_name
device="CPU"
core = ov.Core()
model = core.read_model(model=model_xml_path)
compiled_model = core.compile_model(model=model, device_name=device)
output_layer = compiled_model.output(0)
image = mmcv.imread(image_path)
# Resize to MobileNet image shape.
input_image = mmcv.imresize(image, (224, 224))
normalized_image = mmcv.imnormalize(input_image, mean=np.array([123.675, 116.28, 103.53]), std=np.array([58.395, 57.12, 57.375]))
# Reshape to model input shape.
input_image = np.expand_dims(np.transpose(normalized_image, (2,0,1)), 0)
result_infer = compiled_model([input_image])[output_layer]
result_index = np.argmax(result_infer)
return CAR_CLASSES[result_index]
inputs_image = [
gr.components.Image(type="filepath", label="Input Image"),
]
outputs_text = [
gr.components.Textbox(label="Output Text"),
]
interface_image = gr.Interface(
fn=pred_class,
inputs=inputs_image,
outputs=outputs_text,
title="Car Classification",
cache_examples=False,
)
def greet(name):
return "Hello " + name + "!!"
gr.TabbedInterface(
[interface_image],
tab_names=['Image inference']
).queue().launch()