GDDO commited on
Commit
87220f9
·
verified ·
1 Parent(s): d7c5d62

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -22
app.py CHANGED
@@ -1,36 +1,46 @@
1
  import gradio as gr
2
- import tensorflow as tf
3
- import numpy as np
4
- from PIL import Image as PILImage
5
  import base64
6
  from io import BytesIO
 
 
 
7
 
 
8
  interpreter = tf.lite.Interpreter(model_path="model_unquant.tflite")
9
  interpreter.allocate_tensors()
 
10
  input_details = interpreter.get_input_details()
11
  output_details = interpreter.get_output_details()
12
  classes = ['Bastonete', 'Basófilo']
13
 
14
- def recognize_image(base64_image):
15
- image_data = base64.b64decode(base64_image)
16
- image = PILImage.open(BytesIO(image_data)).convert("RGB")
17
- # REMOVER O REDIMENSIONAMENTO
18
- # image = image.resize((224, 224)) # Não é mais necessário redimensionar aqui
19
- image_array = np.array(image).astype(np.float32)
20
- image_array /= 255.0
21
- image_input = np.expand_dims(image_array, axis=0)
22
- interpreter.set_tensor(input_details[0]['index'], image_input)
23
- interpreter.invoke()
24
- output_data = interpreter.get_tensor(output_details[0]['index'])
25
- predicted_class_index = np.argmax(output_data)
26
- predicted_class_name = classes[predicted_class_index]
27
- predicted_confidence = output_data[0][predicted_class_index] * 100
28
 
29
- return f"Classe: {predicted_class_name} (Confiança: {predicted_confidence:.2f}%)"
 
 
 
 
 
 
30
 
 
 
 
 
31
 
 
32
  interface = gr.Interface(
33
- fn=recognize_image,
34
- inputs="text",
35
- outputs="text"
36
- )
 
 
 
 
 
1
  import gradio as gr
 
 
 
2
  import base64
3
  from io import BytesIO
4
+ from PIL import Image
5
+ import tensorflow as tf
6
+ import numpy as np
7
 
8
+ # Carregar o modelo TensorFlow Lite
9
  interpreter = tf.lite.Interpreter(model_path="model_unquant.tflite")
10
  interpreter.allocate_tensors()
11
+
12
  input_details = interpreter.get_input_details()
13
  output_details = interpreter.get_output_details()
14
  classes = ['Bastonete', 'Basófilo']
15
 
16
+ def predict_base64_image(base64_image):
17
+ try:
18
+ # Decodificar Base64 para imagem
19
+ image_data = base64.b64decode(base64_image)
20
+ image = Image.open(BytesIO(image_data)).convert("RGB")
21
+ image_array = np.array(image).astype(np.float32) / 255.0
22
+ image_array = np.expand_dims(image_array, axis=0)
 
 
 
 
 
 
 
23
 
24
+ # Realizar a inferência
25
+ interpreter.set_tensor(input_details[0]['index'], image_array)
26
+ interpreter.invoke()
27
+ output_data = interpreter.get_tensor(output_details[0]['index'])
28
+ predicted_class_index = np.argmax(output_data)
29
+ predicted_class_name = classes[predicted_class_index]
30
+ predicted_confidence = output_data[0][predicted_class_index] * 100
31
 
32
+ # Retornar o resultado
33
+ return {"class": predicted_class_name, "confidence": f"{predicted_confidence:.2f}%"}
34
+ except Exception as e:
35
+ return {"error": str(e)}
36
 
37
+ # Configuração do Gradio
38
  interface = gr.Interface(
39
+ fn=predict_base64_image,
40
+ inputs="text", # Base64 será enviado como texto
41
+ outputs="json", # Retorna um JSON com a classe e a confiança
42
+ api_name="/predict"
43
+ )
44
+
45
+ # Iniciar o servidor
46
+ interface.launch()