Update app.py
Browse files
app.py
CHANGED
@@ -1,61 +1,29 @@
|
|
1 |
-
from transformers import ViTFeatureExtractor, ViTForImageClassification
|
2 |
-
from hugsvision.inference.VisionClassifierInference import VisionClassifierInference
|
3 |
import gradio as gr
|
4 |
-
import
|
5 |
-
import numpy as np
|
6 |
-
from PIL import Image
|
7 |
|
8 |
-
#
|
9 |
-
|
10 |
-
feature_extractor = ViTFeatureExtractor.from_pretrained(path)
|
11 |
-
model = ViTForImageClassification.from_pretrained(path)
|
12 |
|
13 |
-
#
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
# Define a function to classify and overlay the label on the image
|
20 |
-
def classify_image_with_overlay(img):
|
21 |
-
# Convert the numpy array image to a PIL image
|
22 |
-
img_pil = Image.fromarray(img)
|
23 |
-
|
24 |
-
# Predict the label and probability
|
25 |
-
prediction = classifier.predict_image(img=img_pil)
|
26 |
-
|
27 |
-
# Load the image using OpenCV
|
28 |
-
image = cv2.cvtColor(np.array(img_pil), cv2.COLOR_RGB2BGR)
|
29 |
-
|
30 |
-
# Add a white rectangle for the label
|
31 |
-
font = cv2.FONT_HERSHEY_SIMPLEX
|
32 |
-
org = (10, 50) # Adjust the position of the label
|
33 |
-
font_scale = 1.5 # Increase the font size
|
34 |
-
color = (255, 255, 255) # White color
|
35 |
-
thickness = 2
|
36 |
-
text = f"{prediction['label']}: {prediction['probability']:.2f}"
|
37 |
-
|
38 |
-
text_size = cv2.getTextSize(text, font, font_scale, thickness)[0]
|
39 |
-
cv2.rectangle(image, (org[0] - 10, org[1] - text_size[1] - 10), (org[0] + text_size[0], org[1]), color, cv2.FILLED)
|
40 |
-
|
41 |
-
# Put the label text on the white rectangle
|
42 |
-
cv2.putText(image, text, org, font, font_scale, (0, 0, 0), thickness, cv2.LINE_AA)
|
43 |
-
|
44 |
-
# Resize the image to a larger size
|
45 |
-
image = cv2.resize(image, (800, 800)) # Defina o tamanho desejado aqui
|
46 |
-
|
47 |
-
return image
|
48 |
-
|
49 |
-
# ...
|
50 |
|
|
|
|
|
|
|
|
|
51 |
|
|
|
52 |
iface = gr.Interface(
|
53 |
-
fn=
|
54 |
inputs=gr.inputs.Image(),
|
55 |
-
outputs=gr.outputs.
|
56 |
live=True,
|
57 |
-
title="ViT Image Classifier
|
58 |
-
description="
|
59 |
)
|
60 |
|
61 |
if __name__ == "__main__":
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import pipeline
|
|
|
|
|
3 |
|
4 |
+
# Carregue a pipeline de classificação de imagem ViT
|
5 |
+
image_classifier = pipeline("image-classification", model="mrm8488/vit-base-patch16-224_finetuned-kvasirv2-colonoscopy")
|
|
|
|
|
6 |
|
7 |
+
# Função para classificar a imagem
|
8 |
+
def classify_image(img):
|
9 |
+
# Execute a classificação da imagem usando a pipeline
|
10 |
+
result = image_classifier(img)
|
11 |
+
label = result[0]['label']
|
12 |
+
probability = result[0]['score']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
+
# Formate o resultado para exibição
|
15 |
+
output_text = f"Classe: {label}, Probabilidade: {probability:.2f}"
|
16 |
+
|
17 |
+
return output_text
|
18 |
|
19 |
+
# Interface Gradio
|
20 |
iface = gr.Interface(
|
21 |
+
fn=classify_image,
|
22 |
inputs=gr.inputs.Image(),
|
23 |
+
outputs=gr.outputs.Textbox(),
|
24 |
live=True,
|
25 |
+
title="ViT Image Classifier",
|
26 |
+
description="Carregue uma imagem para classificação.",
|
27 |
)
|
28 |
|
29 |
if __name__ == "__main__":
|