DHEIVER commited on
Commit
f9f0754
1 Parent(s): 8c1323b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -24
app.py CHANGED
@@ -1,8 +1,9 @@
1
  import gradio as gr
2
  import torch
3
  from transformers import AutoFeatureExtractor, AutoModelForImageClassification
4
- from PIL import Image, ImageDraw, ImageFont
5
  import numpy as np
 
6
 
7
  class ThyroidTumorClassifierApp:
8
  def __init__(self):
@@ -32,46 +33,44 @@ class ThyroidTumorClassifierApp:
32
  # Predicted class label
33
  predicted_label = class_labels[predicted_class]
34
 
35
- # Add information to the output image
36
  output_image_with_info = self.add_info_to_image(image, predicted_label, probabilities)
37
 
38
- # Convert the output image to PIL format
39
- output_pil_image = Image.fromarray(output_image_with_info)
40
-
41
- # Return the modified output image as PIL image
42
- return output_pil_image
43
 
44
  def add_info_to_image(self, image, predicted_label, probabilities):
45
- # Convert the image to Pillow image format
46
- image_pil = Image.fromarray(image)
47
-
48
- # Create a drawing object to add text to the image
49
- draw = ImageDraw.Draw(image_pil)
50
 
51
- # Choose the font and text size
52
- font = ImageFont.truetype("arial.ttf", 20)
 
 
 
 
53
 
54
- # Add the predicted class label to the image
55
- draw.text((10, 10), f"Classe Prevista: {predicted_label}", fill="white", font=font)
56
 
57
- # Add the probabilities to the image
58
- for i, prob in enumerate(probabilities):
59
- y_offset = 40 + i * 30
60
  class_name = f"Classe {i}:"
61
  probability = f"{prob:.2f}"
62
- draw.text((10, y_offset), f"{class_name} {probability}", fill="white", font=font)
63
 
64
- # Convert back to numpy format
65
- image_with_info = np.array(image_pil)
66
 
67
- return image_with_info
68
 
69
  def run_interface(self):
70
  # Create a Gradio interface
71
  input_interface = gr.Interface(
72
  fn=self.classify_image,
73
  inputs=gr.inputs.Image(),
74
- outputs=gr.outputs.Image(type="pil"), # Use type="pil" for Pillow image format
75
  title="Tumor da Tireoide Classificação",
76
  description="Faça o upload de uma imagem de um tumor da tireoide para classificação. A saída inclui o rótulo da classe prevista e as probabilidades com informações adicionais.",
77
  )
 
1
  import gradio as gr
2
  import torch
3
  from transformers import AutoFeatureExtractor, AutoModelForImageClassification
4
+ from PIL import Image
5
  import numpy as np
6
+ import cv2
7
 
8
  class ThyroidTumorClassifierApp:
9
  def __init__(self):
 
33
  # Predicted class label
34
  predicted_label = class_labels[predicted_class]
35
 
36
+ # Add information to the output image using OpenCV
37
  output_image_with_info = self.add_info_to_image(image, predicted_label, probabilities)
38
 
39
+ # Return the modified output image as an array
40
+ return output_image_with_info
 
 
 
41
 
42
  def add_info_to_image(self, image, predicted_label, probabilities):
43
+ # Convert the image to RGB format
44
+ image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 
 
 
45
 
46
+ # Add the predicted class label and probabilities to the image using OpenCV
47
+ font = cv2.FONT_HERSHEY_SIMPLEX
48
+ font_scale = 0.6
49
+ font_thickness = 1
50
+ text_color = (255, 255, 255)
51
+ text_position = (10, 30)
52
 
53
+ # Add predicted class label
54
+ cv2.putText(image_rgb, f"Classe Prevista: {predicted_label}", text_position, font, font_scale, text_color, font_thickness)
55
 
56
+ # Add class probabilities
57
+ for i, prob in enumerate(probabilities[0]):
58
+ y_offset = 60 + i * 30
59
  class_name = f"Classe {i}:"
60
  probability = f"{prob:.2f}"
61
+ cv2.putText(image_rgb, f"{class_name} {probability}", (10, text_position[1] + y_offset), font, font_scale, text_color, font_thickness)
62
 
63
+ # Convert back to BGR format for display
64
+ output_image = cv2.cvtColor(image_rgb, cv2.COLOR_RGB2BGR)
65
 
66
+ return output_image
67
 
68
  def run_interface(self):
69
  # Create a Gradio interface
70
  input_interface = gr.Interface(
71
  fn=self.classify_image,
72
  inputs=gr.inputs.Image(),
73
+ outputs=gr.outputs.Image(),
74
  title="Tumor da Tireoide Classificação",
75
  description="Faça o upload de uma imagem de um tumor da tireoide para classificação. A saída inclui o rótulo da classe prevista e as probabilidades com informações adicionais.",
76
  )