Update src/streamlit_app.py
Browse files- src/streamlit_app.py +36 -23
src/streamlit_app.py
CHANGED
|
@@ -2,72 +2,85 @@ import os
|
|
| 2 |
import streamlit as st
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
-
import tensorflow as tf
|
| 6 |
import json
|
|
|
|
| 7 |
|
| 8 |
# ================================
|
| 9 |
-
# Caminhos dos arquivos
|
| 10 |
# ================================
|
| 11 |
BASE_DIR = os.path.dirname(__file__)
|
| 12 |
-
LABELS_PATH = os.path.join(BASE_DIR, "labels.txt")
|
| 13 |
-
META_PATH = os.path.join(BASE_DIR, "meta.json")
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
|
|
|
| 17 |
|
| 18 |
# ================================
|
| 19 |
-
# Carregar labels
|
| 20 |
# ================================
|
| 21 |
with open(LABELS_PATH, "r", encoding="utf-8") as f:
|
| 22 |
class_names = [line.strip() for line in f.readlines()]
|
| 23 |
|
| 24 |
# ================================
|
| 25 |
-
# Carregar
|
| 26 |
# ================================
|
| 27 |
with open(META_PATH, "r", encoding="utf-8") as f:
|
| 28 |
meta = json.load(f)
|
| 29 |
|
| 30 |
-
IMG_SIZE = meta["input_shape"]["height"]
|
| 31 |
|
| 32 |
# ================================
|
| 33 |
-
# Carregar modelo
|
| 34 |
# ================================
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
# ================================
|
| 38 |
-
# Função de inferência
|
| 39 |
# ================================
|
| 40 |
def predict(image: Image.Image):
|
| 41 |
img = image.resize((IMG_SIZE, IMG_SIZE))
|
| 42 |
img = np.array(img).astype("float32") / 255.0
|
| 43 |
img = np.expand_dims(img, axis=0)
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
# ================================
|
| 49 |
-
#
|
| 50 |
# ================================
|
| 51 |
st.markdown("<h1 style='text-align:center; color:#1E88E5;'>🚀 Equipe Netuno</h1>", unsafe_allow_html=True)
|
| 52 |
st.markdown("<h2 style='text-align:center;'>🗿 ArqueoIA</h2>", unsafe_allow_html=True)
|
| 53 |
-
st.markdown("<p style='text-align:center;'>Sistema educacional de IA para
|
| 54 |
st.write("---")
|
| 55 |
|
| 56 |
-
uploaded_file = st.file_uploader(
|
|
|
|
|
|
|
|
|
|
| 57 |
|
|
|
|
|
|
|
|
|
|
| 58 |
if uploaded_file:
|
| 59 |
image = Image.open(uploaded_file).convert("RGB")
|
| 60 |
st.image(image, caption="Imagem enviada", use_column_width=True)
|
| 61 |
|
| 62 |
st.write("### 🔍 Analisando imagem...")
|
|
|
|
| 63 |
preds = predict(image)
|
| 64 |
|
| 65 |
top_idx = int(np.argmax(preds))
|
|
|
|
| 66 |
top_label = class_names[top_idx]
|
| 67 |
-
top_conf = preds[top_idx] * 100
|
| 68 |
|
| 69 |
-
st.success(f"**Sugestão
|
| 70 |
|
| 71 |
-
st.write("### 📊 Detalhamento
|
| 72 |
-
for label,
|
| 73 |
-
st.write(f"- **{label}:** {
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
|
|
|
| 5 |
import json
|
| 6 |
+
import tflite_runtime.interpreter as tflite
|
| 7 |
|
| 8 |
# ================================
|
| 9 |
+
# 1) Caminhos dos arquivos
|
| 10 |
# ================================
|
| 11 |
BASE_DIR = os.path.dirname(__file__)
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
LABELS_PATH = os.path.join(BASE_DIR, "labels.txt")
|
| 14 |
+
META_PATH = os.path.join(BASE_DIR, "meta.json")
|
| 15 |
+
MODEL_PATH = os.path.join(BASE_DIR, "model.tflite")
|
| 16 |
|
| 17 |
# ================================
|
| 18 |
+
# 2) Carregar labels
|
| 19 |
# ================================
|
| 20 |
with open(LABELS_PATH, "r", encoding="utf-8") as f:
|
| 21 |
class_names = [line.strip() for line in f.readlines()]
|
| 22 |
|
| 23 |
# ================================
|
| 24 |
+
# 3) Carregar meta.json
|
| 25 |
# ================================
|
| 26 |
with open(META_PATH, "r", encoding="utf-8") as f:
|
| 27 |
meta = json.load(f)
|
| 28 |
|
| 29 |
+
IMG_SIZE = meta["input_shape"]["height"] # usa altura como tamanho quadrado
|
| 30 |
|
| 31 |
# ================================
|
| 32 |
+
# 4) Carregar modelo TFLite
|
| 33 |
# ================================
|
| 34 |
+
interpreter = tflite.Interpreter(model_path=MODEL_PATH)
|
| 35 |
+
interpreter.allocate_tensors()
|
| 36 |
+
|
| 37 |
+
input_details = interpreter.get_input_details()
|
| 38 |
+
output_details = interpreter.get_output_details()
|
| 39 |
|
| 40 |
# ================================
|
| 41 |
+
# 5) Função de inferência
|
| 42 |
# ================================
|
| 43 |
def predict(image: Image.Image):
|
| 44 |
img = image.resize((IMG_SIZE, IMG_SIZE))
|
| 45 |
img = np.array(img).astype("float32") / 255.0
|
| 46 |
img = np.expand_dims(img, axis=0)
|
| 47 |
+
|
| 48 |
+
interpreter.set_tensor(input_details[0]["index"], img)
|
| 49 |
+
interpreter.invoke()
|
| 50 |
+
|
| 51 |
+
output = interpreter.get_tensor(output_details[0]["index"])[0]
|
| 52 |
+
return output
|
| 53 |
|
| 54 |
# ================================
|
| 55 |
+
# 6) Interface
|
| 56 |
# ================================
|
| 57 |
st.markdown("<h1 style='text-align:center; color:#1E88E5;'>🚀 Equipe Netuno</h1>", unsafe_allow_html=True)
|
| 58 |
st.markdown("<h2 style='text-align:center;'>🗿 ArqueoIA</h2>", unsafe_allow_html=True)
|
| 59 |
+
st.markdown("<p style='text-align:center;'>Sistema educacional de IA para classificar artefatos arqueológicos.</p>", unsafe_allow_html=True)
|
| 60 |
st.write("---")
|
| 61 |
|
| 62 |
+
uploaded_file = st.file_uploader(
|
| 63 |
+
"Envie uma imagem do artefato:",
|
| 64 |
+
type=["jpg", "jpeg", "png"]
|
| 65 |
+
)
|
| 66 |
|
| 67 |
+
# ================================
|
| 68 |
+
# 7) Execução
|
| 69 |
+
# ================================
|
| 70 |
if uploaded_file:
|
| 71 |
image = Image.open(uploaded_file).convert("RGB")
|
| 72 |
st.image(image, caption="Imagem enviada", use_column_width=True)
|
| 73 |
|
| 74 |
st.write("### 🔍 Analisando imagem...")
|
| 75 |
+
|
| 76 |
preds = predict(image)
|
| 77 |
|
| 78 |
top_idx = int(np.argmax(preds))
|
| 79 |
+
top_conf = float(preds[top_idx]) * 100
|
| 80 |
top_label = class_names[top_idx]
|
|
|
|
| 81 |
|
| 82 |
+
st.success(f"**Sugestão: {top_label} ({top_conf:.1f}% de confiança)**")
|
| 83 |
|
| 84 |
+
st.write("### 📊 Detalhamento completo")
|
| 85 |
+
for label, p in zip(class_names, preds):
|
| 86 |
+
st.write(f"- **{label}:** {p*100:.1f}%")
|