|
|
import gradio as gr |
|
|
import tensorflow as tf |
|
|
import numpy as np |
|
|
from PIL import Image as PILImage |
|
|
|
|
|
|
|
|
interpreter = tf.lite.Interpreter(model_path="model_unquant.tflite") |
|
|
interpreter.allocate_tensors() |
|
|
|
|
|
|
|
|
input_details = interpreter.get_input_details() |
|
|
output_details = interpreter.get_output_details() |
|
|
|
|
|
|
|
|
classes = ['Bastonete', 'Basófilo'] |
|
|
|
|
|
def recognize_image(image): |
|
|
|
|
|
image = PILImage.fromarray(image).resize((224, 224)) |
|
|
|
|
|
|
|
|
image_array = np.array(image).astype(np.float32) |
|
|
image_array /= 255.0 |
|
|
|
|
|
|
|
|
image_input = np.expand_dims(image_array, axis=0) |
|
|
|
|
|
|
|
|
interpreter.set_tensor(input_details[0]['index'], image_input) |
|
|
interpreter.invoke() |
|
|
|
|
|
|
|
|
output_data = interpreter.get_tensor(output_details[0]['index']) |
|
|
|
|
|
|
|
|
predicted_class_index = np.argmax(output_data) |
|
|
predicted_class_name = classes[predicted_class_index] |
|
|
predicted_confidence = output_data[0][predicted_class_index] * 100 |
|
|
|
|
|
return f"Classe: {predicted_class_name} (Confiança: {predicted_confidence:.2f}%)" |
|
|
|
|
|
|
|
|
interface = gr.Interface( |
|
|
fn=recognize_image, |
|
|
inputs=gr.Image(), |
|
|
outputs="text" |
|
|
) |
|
|
|
|
|
|
|
|
interface.launch(share=True) |