Files changed (1) hide show
  1. app.py +106 -0
app.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ !pip install -q transformers torch Pillow requests matplotlib gradio
2
+
3
+ import torch
4
+ import requests
5
+ from io import BytesIO
6
+ from PIL import Image
7
+ import matplotlib.pyplot as plt
8
+ import matplotlib.patches as patches
9
+ import gradio as gr
10
+
11
+ from transformers import AutoProcessor, OmDetTurboForObjectDetection
12
+
13
+ device = "cuda" if torch.cuda.is_available() else "cpu"
14
+ print(f"Iniciando no dispositivo: {device.upper()}")
15
+
16
+ processor = AutoProcessor.from_pretrained("omlab/omdet-turbo-swin-tiny-hf")
17
+ model = OmDetTurboForObjectDetection.from_pretrained(
18
+ "omlab/omdet-turbo-swin-tiny-hf"
19
+ ).to(device)
20
+
21
+ def plot_results(image, results):
22
+ fig, ax = plt.subplots(1, figsize=(8, 6))
23
+ ax.imshow(image)
24
+ ax.axis("off")
25
+
26
+ labels = results.get("text_labels", results.get("classes", []))
27
+
28
+ for score, class_name, box in zip(results["scores"], labels, results["boxes"]):
29
+ xmin, ymin, xmax, ymax = box.tolist()
30
+ rect = patches.Rectangle(
31
+ (xmin, ymin),
32
+ xmax - xmin,
33
+ ymax - ymin,
34
+ linewidth=2,
35
+ edgecolor='red',
36
+ facecolor='none'
37
+ )
38
+ ax.add_patch(rect)
39
+
40
+ label = f"{class_name}: {score:.2f}"
41
+ ax.text(
42
+ xmin,
43
+ ymin - 5,
44
+ label,
45
+ color='white',
46
+ fontsize=10,
47
+ weight='bold',
48
+ backgroundcolor="red"
49
+ )
50
+
51
+ return fig
52
+
53
+ def detectar_objetos(url, classes_texto):
54
+ try:
55
+ image = Image.open(BytesIO(requests.get(url).content)).convert("RGB")
56
+
57
+ classes = [c.strip() for c in classes_texto.split(",")]
58
+ task = "Detect {}.".format(", ".join(classes))
59
+
60
+ inputs = processor(
61
+ images=[image],
62
+ text=[classes],
63
+ task=[task],
64
+ return_tensors="pt",
65
+ ).to(device)
66
+
67
+ with torch.no_grad():
68
+ outputs = model(**inputs)
69
+
70
+ results = processor.post_process_grounded_object_detection(
71
+ outputs,
72
+ text_labels=[classes],
73
+ target_sizes=[image.size[::-1]],
74
+ threshold=0.2,
75
+ nms_threshold=0.3,
76
+ )[0]
77
+
78
+ saida = ""
79
+ labels = results.get("text_labels", results.get("classes", []))
80
+
81
+ for score, class_name, box in zip(results["scores"], labels, results["boxes"]):
82
+ box_rounded = [round(b, 1) for b in box.tolist()]
83
+ saida += f"{class_name} ({round(score.item(),2)}) -> {box_rounded}\n"
84
+
85
+ fig = plot_results(image, results)
86
+
87
+ return fig, saida
88
+
89
+ except Exception as e:
90
+ return None, f"Erro: {str(e)}"
91
+
92
+ interface = gr.Interface(
93
+ fn=detectar_objetos,
94
+ inputs=[
95
+ gr.Textbox(label="URL da imagem"),
96
+ gr.Textbox(label="Classes (separadas por vírgula)", value="cat, dog")
97
+ ],
98
+ outputs=[
99
+ gr.Plot(label="Imagem com detecção"),
100
+ gr.Textbox(label="Resultados")
101
+ ],
102
+ title="Detecção de Objetos por URL",
103
+ description="Cole uma URL de imagem e informe os objetos que deseja detectar."
104
+ )
105
+
106
+ interface.launch()