DHEIVER commited on
Commit
8d9c7cb
1 Parent(s): da33e50

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -33
app.py CHANGED
@@ -2,40 +2,54 @@ import gradio as gr
2
  import tensorflow as tf
3
  import numpy as np
4
  import cv2
5
- import datetime
6
 
7
- # Carregue o modelo treinado
8
- model_path = 'modelo_treinado.h5'
9
- model = tf.keras.models.load_model(model_path)
 
 
 
 
 
 
 
 
 
10
 
11
  class_labels = ["Normal", "Cataract"]
12
 
13
- def classify_image(input_image):
14
- # Redimensione a imagem de entrada para o tamanho esperado pelo modelo
15
- input_image = tf.image.resize(input_image, (192, 256))
16
- input_image = (input_image / 255.0)
17
- input_image = np.expand_dims(input_image, axis=0)
18
-
19
- # Faça a previsão
20
- prediction = model.predict(input_image)
21
- class_index = np.argmax(prediction)
22
- predicted_class = class_labels[class_index]
23
-
24
- # Crie uma cópia da imagem de entrada para exibir
25
- output_image = input_image[0].copy()
26
- output_image = (output_image * 255).astype('uint8')
27
-
28
- # Desenhe uma caixa de detecção na imagem
29
- cv2.putText(output_image, predicted_class, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
30
- timestamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
31
- cv2.putText(output_image, timestamp, (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
32
-
33
- return output_image
34
-
35
- # Configurar a interface Gradio
36
- gr.Interface(
37
- fn=classify_image,
38
- inputs="image",
39
- outputs="image",
40
- live=True
41
- ).launch()
 
 
 
 
 
 
 
2
  import tensorflow as tf
3
  import numpy as np
4
  import cv2
 
5
 
6
+ # Load the pre-trained ResNet-18 model from PyTorch
7
+ import torch
8
+ import requests
9
+ from torchvision import transforms
10
+
11
+ resnet_model = torch.hub.load('pytorch/vision:v0.6.0', 'resnet18', pretrained=True).eval()
12
+ response = requests.get("https://git.io/JJkYN")
13
+ labels = response.text.split("\n")
14
+
15
+ # Load the TensorFlow model
16
+ tf_model_path = 'modelo_treinado.h5'
17
+ tf_model = tf.keras.models.load_model(tf_model_path)
18
 
19
  class_labels = ["Normal", "Cataract"]
20
 
21
+ def predict(inp):
22
+ # First, use the ResNet-18 model to predict labels
23
+ inp_resized = transforms.ToTensor()(inp).unsqueeze(0)
24
+ with torch.no_grad():
25
+ prediction_resnet = torch.nn.functional.softmax(resnet_model(inp_resized)[0], dim=0)
26
+ confidences_resnet = {labels[i]: float(prediction_resnet[i]) for i in range(1000)}
27
+
28
+ # Then, use the TensorFlow model to predict Normal or Cataract
29
+ img_array = cv2.cvtColor(np.array(inp), cv2.COLOR_RGB2BGR)
30
+ img_array = cv2.resize(img_array, (224, 224)) # Resize to match the input size expected by the TF model
31
+ img_array = img_array / 255.0 # Normalize pixel values
32
+ img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
33
+
34
+ prediction_tf = tf_model.predict(img_array)
35
+ label_index = np.argmax(prediction_tf)
36
+ confidence_tf = float(prediction_tf[0, label_index])
37
+
38
+ # Combine the ResNet-18 and TensorFlow predictions
39
+ resnet_label = max(confidences_resnet, key=confidences_resnet.get)
40
+ if confidence_tf >= 0.5:
41
+ final_label = class_labels[label_index]
42
+ confidence = confidence_tf
43
+ else:
44
+ final_label = resnet_label
45
+ confidence = confidences_resnet[resnet_label]
46
+
47
+ return final_label, confidence
48
+
49
+ demo = gr.Interface(
50
+ fn=predict,
51
+ inputs=gr.inputs.Image(type="pil"),
52
+ outputs=["label", "number"]
53
+ )
54
+
55
+ demo.launch()