Commit
·
b0369fe
1
Parent(s):
26dfb56
go3
Browse files- app.py +70 -26
- requirements.txt +8 -1
app.py
CHANGED
@@ -3,46 +3,90 @@ import numpy as np
|
|
3 |
from PIL import Image
|
4 |
import torch
|
5 |
import matplotlib.pyplot as plt
|
|
|
6 |
|
7 |
-
|
8 |
-
|
|
|
|
|
9 |
|
10 |
def detect(img):
|
|
|
|
|
11 |
|
|
|
12 |
img_arr = np.array(img)
|
13 |
|
|
|
14 |
results = model(img_arr)
|
15 |
-
|
16 |
|
17 |
-
|
|
|
18 |
ax.imshow(img_arr)
|
19 |
-
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
fig.canvas.draw()
|
31 |
-
pil_img = Image.fromarray(np.array(fig.canvas.renderer._renderer))
|
32 |
-
plt.close(fig)
|
33 |
-
|
34 |
return pil_img
|
35 |
|
36 |
-
|
37 |
-
#Gradiooo
|
38 |
iface = gr.Interface(
|
39 |
fn=detect,
|
40 |
-
inputs=gr.Image(type="pil"),
|
41 |
-
outputs=gr.Image(type="pil"),
|
42 |
-
title="YOLOv5 Sun Spot Hunter",
|
43 |
-
description="
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
)
|
46 |
|
47 |
-
|
48 |
-
iface.launch(debug=True)
|
|
|
3 |
from PIL import Image
|
4 |
import torch
|
5 |
import matplotlib.pyplot as plt
|
6 |
+
import io
|
7 |
|
8 |
+
# Carregar modelo
|
9 |
+
print("Carregando modelo YOLOv5...")
|
10 |
+
model = torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt', trust_repo=True)
|
11 |
+
print("Modelo carregado com sucesso!")
|
12 |
|
13 |
def detect(img):
|
14 |
+
if img is None:
|
15 |
+
return None
|
16 |
|
17 |
+
# Converter para array numpy
|
18 |
img_arr = np.array(img)
|
19 |
|
20 |
+
# Fazer predição
|
21 |
results = model(img_arr)
|
|
|
22 |
|
23 |
+
# Criar figura
|
24 |
+
fig, ax = plt.subplots(figsize=(12, 8))
|
25 |
ax.imshow(img_arr)
|
|
|
26 |
|
27 |
+
# Contador de manchas solares
|
28 |
+
sunspot_count = 0
|
29 |
+
|
30 |
+
# Processar detecções
|
31 |
+
detections = results.xyxy[0].cpu().numpy()
|
32 |
+
|
33 |
+
for *xyxy, conf, cls in detections:
|
34 |
+
if conf > 0.25: # Filtrar detecções com baixa confiança
|
35 |
+
x1, y1, x2, y2 = map(int, xyxy)
|
36 |
+
label = model.names[int(cls)]
|
37 |
+
sunspot_count += 1
|
38 |
+
|
39 |
+
# Desenhar bounding box
|
40 |
+
ax.add_patch(plt.Rectangle((x1, y1), x2-x1, y2-y1,
|
41 |
+
fill=False, color='red', linewidth=2))
|
42 |
+
|
43 |
+
# Adicionar label com confiança
|
44 |
+
ax.text(x1, y1-10, f'{label} {conf:.2f}',
|
45 |
+
color='white', fontsize=10,
|
46 |
+
bbox={'facecolor': 'red', 'alpha': 0.8})
|
47 |
+
|
48 |
+
# Título com contagem
|
49 |
+
ax.set_title(f'Sun Spots Detectados: {sunspot_count}',
|
50 |
+
fontsize=16, fontweight='bold', color='white',
|
51 |
+
bbox={'facecolor': 'black', 'alpha': 0.7})
|
52 |
+
|
53 |
+
plt.axis('off')
|
54 |
+
plt.tight_layout()
|
55 |
+
|
56 |
+
# Converter para PIL Image
|
57 |
+
buf = io.BytesIO()
|
58 |
+
plt.savefig(buf, format='png', bbox_inches='tight',
|
59 |
+
dpi=150, facecolor='black')
|
60 |
+
buf.seek(0)
|
61 |
+
pil_img = Image.open(buf)
|
62 |
+
plt.close(fig)
|
63 |
|
|
|
|
|
|
|
|
|
64 |
return pil_img
|
65 |
|
66 |
+
# Interface Gradio
|
|
|
67 |
iface = gr.Interface(
|
68 |
fn=detect,
|
69 |
+
inputs=gr.Image(type="pil", label="📷 Upload da Imagem Solar"),
|
70 |
+
outputs=gr.Image(type="pil", label="🔍 Detecções de Manchas Solares"),
|
71 |
+
title="☀️ YOLOv5 Sun Spot Hunter",
|
72 |
+
description="""
|
73 |
+
**Detector de Manchas Solares** 🌞
|
74 |
+
|
75 |
+
Este detector foi treinado usando YOLOv5 e rotulado no Makesense.ai para identificar manchas solares em imagens.
|
76 |
+
|
77 |
+
📋 **Como usar:**
|
78 |
+
1. Faça upload de uma imagem solar
|
79 |
+
2. O modelo detectará automaticamente as manchas solares
|
80 |
+
3. As detecções serão destacadas com caixas vermelhas
|
81 |
+
""",
|
82 |
+
examples=[["example1.jpg"]] if False else None, # Descomente se tiver imagens de exemplo
|
83 |
+
theme=gr.themes.Soft(),
|
84 |
+
css="""
|
85 |
+
.gradio-container {
|
86 |
+
background: linear-gradient(45deg, #FDB813, #FF8C00);
|
87 |
+
}
|
88 |
+
"""
|
89 |
)
|
90 |
|
91 |
+
if __name__ == "__main__":
|
92 |
+
iface.launch(debug=True, share=True)
|
requirements.txt
CHANGED
@@ -1,8 +1,15 @@
|
|
1 |
-
gradio==4.29.0
|
2 |
torch
|
|
|
3 |
numpy
|
4 |
Pillow
|
5 |
matplotlib
|
6 |
opencv-python
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
|
|
|
1 |
+
gradio==4.29.0
|
2 |
torch
|
3 |
+
torchvision
|
4 |
numpy
|
5 |
Pillow
|
6 |
matplotlib
|
7 |
opencv-python
|
8 |
+
seaborn
|
9 |
+
ultralytics
|
10 |
+
PyYAML
|
11 |
+
requests
|
12 |
+
tqdm
|
13 |
+
scipy
|
14 |
|
15 |
|