correcao final
Browse files
app.py
CHANGED
|
@@ -1,51 +1,29 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import tensorflow as tf
|
| 3 |
import numpy as np
|
| 4 |
-
from PIL import Image
|
| 5 |
|
| 6 |
# Carregar o modelo .tflite
|
| 7 |
interpreter = tf.lite.Interpreter(model_path="model_unquant.tflite")
|
| 8 |
interpreter.allocate_tensors()
|
| 9 |
|
| 10 |
-
#
|
| 11 |
input_details = interpreter.get_input_details()
|
| 12 |
output_details = interpreter.get_output_details()
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
image = PILImage.fromarray(image).resize((224, 224))
|
| 20 |
-
|
| 21 |
-
# Converter para array NumPy e normalizar os valores
|
| 22 |
-
image_array = np.array(image).astype(np.float32)
|
| 23 |
-
image_array /= 255.0 # Normalizar para o intervalo [0, 1]
|
| 24 |
-
|
| 25 |
-
# Adicionar uma dimensão extra para o batch size (necessário para o modelo)
|
| 26 |
-
image_input = np.expand_dims(image_array, axis=0)
|
| 27 |
-
|
| 28 |
-
# Fazer a inferência no modelo
|
| 29 |
-
interpreter.set_tensor(input_details[0]['index'], image_input)
|
| 30 |
interpreter.invoke()
|
|
|
|
| 31 |
|
| 32 |
-
#
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
# Obter o índice da classe com maior probabilidade
|
| 36 |
-
predicted_class_index = np.argmax(output_data)
|
| 37 |
-
predicted_class_name = classes[predicted_class_index]
|
| 38 |
-
predicted_confidence = output_data[0][predicted_class_index] * 100 # Converter para %
|
| 39 |
-
|
| 40 |
-
return f"Classe: {predicted_class_name} (Confiança: {predicted_confidence:.2f}%)"
|
| 41 |
|
| 42 |
# Configurando a interface Gradio
|
| 43 |
-
interface = gr.Interface(
|
| 44 |
-
fn=recognize_image,
|
| 45 |
-
inputs=gr.Image(), # Removemos o shape
|
| 46 |
-
outputs="text", # Saída: texto com a classe e confiança
|
| 47 |
-
allow_flagging="never",
|
| 48 |
-
queue=False # Agora suportado pela versão atualizada do Gradio
|
| 49 |
-
)
|
| 50 |
-
|
| 51 |
interface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import tensorflow as tf
|
| 3 |
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
|
| 6 |
# Carregar o modelo .tflite
|
| 7 |
interpreter = tf.lite.Interpreter(model_path="model_unquant.tflite")
|
| 8 |
interpreter.allocate_tensors()
|
| 9 |
|
| 10 |
+
# Detalhes da entrada e saída do modelo
|
| 11 |
input_details = interpreter.get_input_details()
|
| 12 |
output_details = interpreter.get_output_details()
|
| 13 |
|
| 14 |
+
def predict(image):
|
| 15 |
+
# Redimensiona a imagem para o tamanho necessário pelo modelo
|
| 16 |
+
image = image.resize((224, 224)) # Ajuste para o tamanho que seu modelo exige
|
| 17 |
+
image = np.expand_dims(np.array(image), axis=0).astype(np.float32)
|
| 18 |
|
| 19 |
+
# Realiza a inferência
|
| 20 |
+
interpreter.set_tensor(input_details[0]['index'], image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
interpreter.invoke()
|
| 22 |
+
output = interpreter.get_tensor(output_details[0]['index'])
|
| 23 |
|
| 24 |
+
# O resultado pode precisar ser ajustado conforme a saída do modelo
|
| 25 |
+
return output.tolist() # Retorna o resultado da inferência
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
# Configurando a interface Gradio
|
| 28 |
+
interface = gr.Interface(fn=predict, inputs="image", outputs="label")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
interface.launch()
|