Update app.py
Browse files
app.py
CHANGED
|
@@ -3,15 +3,12 @@ import roboflow
|
|
| 3 |
import pandas as pd
|
| 4 |
import numpy as np
|
| 5 |
import matplotlib.pyplot as plt
|
| 6 |
-
import re
|
| 7 |
import zipfile
|
| 8 |
import tempfile
|
| 9 |
import json
|
| 10 |
from shapely.geometry import Polygon
|
| 11 |
from PIL import Image
|
| 12 |
-
from concurrent.futures import ThreadPoolExecutor
|
| 13 |
from io import BytesIO
|
| 14 |
-
from fpdf import FPDF
|
| 15 |
|
| 16 |
# 🔥 Inicializar o modelo do Roboflow
|
| 17 |
API_KEY = "mGkz7QhkhD90YfeiaOxV"
|
|
@@ -22,11 +19,11 @@ model = project.version("8").model
|
|
| 22 |
# 📌 Configurações fixas do modelo
|
| 23 |
model.confidence = 80 # Confiança do modelo
|
| 24 |
model.overlap = 25 # Sobreposição fixada em 25%
|
|
|
|
| 25 |
|
| 26 |
# 📌 Expander para Configurações Avançadas
|
| 27 |
with st.expander("⚙️ Configurações Avançadas", expanded=True):
|
| 28 |
model.confidence = st.slider("Confiança do Modelo (%)", 50, 100, 80)
|
| 29 |
-
resolution_option = st.radio("Escolha a Resolução da Imagem Exportada:", ["Baixa", "Média", "Alta"])
|
| 30 |
|
| 31 |
# 📌 Função para calcular a área do polígono segmentado
|
| 32 |
def calculate_polygon_area(points):
|
|
@@ -53,13 +50,9 @@ def process_image(image, image_name, progress_value):
|
|
| 53 |
x_coords = [p['x'] for p in points] + [points[0]['x']]
|
| 54 |
y_coords = [p['y'] for p in points] + [points[0]['y']]
|
| 55 |
|
| 56 |
-
# Ajustar DPI conforme escolha da resolução
|
| 57 |
-
dpi_options = {"Baixa": 72, "Média": 150, "Alta": 300}
|
| 58 |
-
selected_dpi = dpi_options[resolution_option]
|
| 59 |
-
|
| 60 |
# Criar imagem segmentada
|
| 61 |
segmented_buffer = BytesIO()
|
| 62 |
-
fig, ax = plt.subplots(figsize=(6, 6), dpi=
|
| 63 |
ax.imshow(image)
|
| 64 |
ax.plot(x_coords, y_coords, color='red', linewidth=2)
|
| 65 |
ax.set_title(f"Segmentação: {image_name}")
|
|
@@ -68,7 +61,7 @@ def process_image(image, image_name, progress_value):
|
|
| 68 |
|
| 69 |
# Criar imagem do polígono
|
| 70 |
polygon_buffer = BytesIO()
|
| 71 |
-
fig_polygon, ax_polygon = plt.subplots(figsize=(6, 6), dpi=
|
| 72 |
ax_polygon.plot(x_coords, y_coords, 'r-', linewidth=2)
|
| 73 |
ax_polygon.scatter(x_coords, y_coords, color='red', s=5)
|
| 74 |
ax_polygon.set_title("Contorno do Polígono")
|
|
@@ -124,36 +117,43 @@ elif upload_option == "Pasta de imagens":
|
|
| 124 |
|
| 125 |
if uploaded_files:
|
| 126 |
results_data = []
|
|
|
|
| 127 |
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
|
|
|
| 131 |
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
progress_text = st.text(f"Progresso: {progress_value}%")
|
| 136 |
|
| 137 |
-
|
| 138 |
|
| 139 |
-
|
| 140 |
-
|
| 141 |
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
if results:
|
| 147 |
results_df = pd.DataFrame(results)
|
| 148 |
st.write("📊 **Tabela de Resultados**")
|
| 149 |
st.dataframe(results_df)
|
| 150 |
|
| 151 |
-
# Criar
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
zip_file.writestr(f"segmentada_{uploaded_file.name}.png", segmented_buffer.getvalue())
|
| 156 |
-
zip_file.writestr(f"poligono_{uploaded_file.name}.png", polygon_buffer.getvalue())
|
| 157 |
|
| 158 |
-
|
| 159 |
-
st.download_button("📥 Baixar
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import pandas as pd
|
| 4 |
import numpy as np
|
| 5 |
import matplotlib.pyplot as plt
|
|
|
|
| 6 |
import zipfile
|
| 7 |
import tempfile
|
| 8 |
import json
|
| 9 |
from shapely.geometry import Polygon
|
| 10 |
from PIL import Image
|
|
|
|
| 11 |
from io import BytesIO
|
|
|
|
| 12 |
|
| 13 |
# 🔥 Inicializar o modelo do Roboflow
|
| 14 |
API_KEY = "mGkz7QhkhD90YfeiaOxV"
|
|
|
|
| 19 |
# 📌 Configurações fixas do modelo
|
| 20 |
model.confidence = 80 # Confiança do modelo
|
| 21 |
model.overlap = 25 # Sobreposição fixada em 25%
|
| 22 |
+
dpi_value = 300 # Resolução sempre em Alta (300 DPI)
|
| 23 |
|
| 24 |
# 📌 Expander para Configurações Avançadas
|
| 25 |
with st.expander("⚙️ Configurações Avançadas", expanded=True):
|
| 26 |
model.confidence = st.slider("Confiança do Modelo (%)", 50, 100, 80)
|
|
|
|
| 27 |
|
| 28 |
# 📌 Função para calcular a área do polígono segmentado
|
| 29 |
def calculate_polygon_area(points):
|
|
|
|
| 50 |
x_coords = [p['x'] for p in points] + [points[0]['x']]
|
| 51 |
y_coords = [p['y'] for p in points] + [points[0]['y']]
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
# Criar imagem segmentada
|
| 54 |
segmented_buffer = BytesIO()
|
| 55 |
+
fig, ax = plt.subplots(figsize=(6, 6), dpi=dpi_value)
|
| 56 |
ax.imshow(image)
|
| 57 |
ax.plot(x_coords, y_coords, color='red', linewidth=2)
|
| 58 |
ax.set_title(f"Segmentação: {image_name}")
|
|
|
|
| 61 |
|
| 62 |
# Criar imagem do polígono
|
| 63 |
polygon_buffer = BytesIO()
|
| 64 |
+
fig_polygon, ax_polygon = plt.subplots(figsize=(6, 6), dpi=dpi_value)
|
| 65 |
ax_polygon.plot(x_coords, y_coords, 'r-', linewidth=2)
|
| 66 |
ax_polygon.scatter(x_coords, y_coords, color='red', s=5)
|
| 67 |
ax_polygon.set_title("Contorno do Polígono")
|
|
|
|
| 117 |
|
| 118 |
if uploaded_files:
|
| 119 |
results_data = []
|
| 120 |
+
zip_images_buffer = BytesIO()
|
| 121 |
|
| 122 |
+
with zipfile.ZipFile(zip_images_buffer, "w") as zip_file:
|
| 123 |
+
for i, uploaded_file in enumerate(uploaded_files):
|
| 124 |
+
image = Image.open(uploaded_file)
|
| 125 |
+
st.image(image, caption=f"Imagem Original - {uploaded_file.name}", use_container_width=True)
|
| 126 |
|
| 127 |
+
progress_value = int(((i + 1) / len(uploaded_files)) * 100)
|
| 128 |
+
progress_bar = st.progress(progress_value)
|
| 129 |
+
progress_text = st.text(f"Progresso: {progress_value}%")
|
|
|
|
| 130 |
|
| 131 |
+
area, segmented_buffer, polygon_buffer, original_image = process_image(image, uploaded_file.name, progress_value)
|
| 132 |
|
| 133 |
+
if area is not None:
|
| 134 |
+
results.append({"Imagem": uploaded_file.name, "Área Segmentada (px²)": area})
|
| 135 |
|
| 136 |
+
# Adicionar imagens ao ZIP
|
| 137 |
+
zip_file.writestr(f"segmentada_{uploaded_file.name}.png", segmented_buffer.getvalue())
|
| 138 |
+
zip_file.writestr(f"poligono_{uploaded_file.name}.png", polygon_buffer.getvalue())
|
| 139 |
+
|
| 140 |
+
# Mostrar as imagens processadas
|
| 141 |
+
st.image(segmented_buffer, caption="Segmentação", use_container_width=True)
|
| 142 |
+
st.image(polygon_buffer, caption="Polígono", use_container_width=True)
|
| 143 |
|
| 144 |
if results:
|
| 145 |
results_df = pd.DataFrame(results)
|
| 146 |
st.write("📊 **Tabela de Resultados**")
|
| 147 |
st.dataframe(results_df)
|
| 148 |
|
| 149 |
+
# Criar arquivo Excel
|
| 150 |
+
excel_buffer = BytesIO()
|
| 151 |
+
results_df.to_excel(excel_buffer, index=False)
|
| 152 |
+
excel_buffer.seek(0)
|
|
|
|
|
|
|
| 153 |
|
| 154 |
+
# Botão para baixar a tabela em Excel
|
| 155 |
+
st.download_button("📥 Baixar Tabela (Excel)", excel_buffer, file_name="resultados_segmentacao.xlsx", mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
|
| 156 |
+
|
| 157 |
+
# Botão para baixar as imagens segmentadas e polígonos (ZIP)
|
| 158 |
+
zip_images_buffer.seek(0)
|
| 159 |
+
st.download_button("📥 Baixar Todas as Imagens Segmentadas e Polígonos", zip_images_buffer, file_name="imagens_segmentadas.zip", mime="application/zip")
|