Update src/streamlit_app.py
Browse files- src/streamlit_app.py +85 -51
src/streamlit_app.py
CHANGED
|
@@ -1,71 +1,105 @@
|
|
| 1 |
-
import
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
from PIL import Image
|
| 4 |
-
import
|
| 5 |
-
import
|
| 6 |
-
import os
|
| 7 |
|
| 8 |
-
# ==========================
|
| 9 |
-
#
|
| 10 |
-
# ==========================
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
| 14 |
|
|
|
|
| 15 |
# Carregar labels
|
|
|
|
| 16 |
with open(LABELS_PATH, "r", encoding="utf-8") as f:
|
| 17 |
-
class_names = [
|
| 18 |
|
| 19 |
-
#
|
|
|
|
|
|
|
| 20 |
with open(META_PATH, "r", encoding="utf-8") as f:
|
| 21 |
meta = json.load(f)
|
| 22 |
|
| 23 |
-
IMG_SIZE = meta["input_shape"]["height"]
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
|
|
|
|
|
|
|
| 27 |
interpreter.allocate_tensors()
|
|
|
|
| 28 |
input_details = interpreter.get_input_details()
|
| 29 |
output_details = interpreter.get_output_details()
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
#
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
def predict(img: Image.Image):
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
interpreter.set_tensor(input_details[0]['index'], img)
|
| 42 |
-
interpreter.invoke()
|
| 43 |
|
| 44 |
-
output = interpreter.get_tensor(output_details[0]['index'])[0]
|
| 45 |
-
return output
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
for label, prob in zip(class_names, preds):
|
| 71 |
-
st.write(f"- **{label}**
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
+
import streamlit as st
|
| 6 |
+
import tensorflow as tf
|
|
|
|
| 7 |
|
| 8 |
+
# ==========================
|
| 9 |
+
# Caminhos dos arquivos
|
| 10 |
+
# ==========================
|
| 11 |
+
BASE_DIR = os.path.dirname(__file__)
|
| 12 |
+
MODEL_PATH = os.path.join(BASE_DIR, "model.tflite")
|
| 13 |
+
LABELS_PATH = os.path.join(BASE_DIR, "labels.txt")
|
| 14 |
+
META_PATH = os.path.join(BASE_DIR, "meta.json")
|
| 15 |
|
| 16 |
+
# ==========================
|
| 17 |
# Carregar labels
|
| 18 |
+
# ==========================
|
| 19 |
with open(LABELS_PATH, "r", encoding="utf-8") as f:
|
| 20 |
+
class_names = [line.strip() for line in f if line.strip()]
|
| 21 |
|
| 22 |
+
# ==========================
|
| 23 |
+
# Carregar metadados
|
| 24 |
+
# ==========================
|
| 25 |
with open(META_PATH, "r", encoding="utf-8") as f:
|
| 26 |
meta = json.load(f)
|
| 27 |
|
| 28 |
+
IMG_SIZE = int(meta["input_shape"]["height"])
|
| 29 |
|
| 30 |
+
# ==========================
|
| 31 |
+
# Carregar modelo TFLite com TensorFlow
|
| 32 |
+
# ==========================
|
| 33 |
+
interpreter = tf.lite.Interpreter(model_path=MODEL_PATH)
|
| 34 |
interpreter.allocate_tensors()
|
| 35 |
+
|
| 36 |
input_details = interpreter.get_input_details()
|
| 37 |
output_details = interpreter.get_output_details()
|
| 38 |
|
| 39 |
+
# ==========================
|
| 40 |
+
# Funções auxiliares
|
| 41 |
+
# ==========================
|
| 42 |
+
def preprocess_image(image: Image.Image) -> np.ndarray:
|
| 43 |
+
# redimensiona
|
| 44 |
+
image = image.resize((IMG_SIZE, IMG_SIZE))
|
| 45 |
+
arr = np.array(image).astype("float32") / 255.0
|
| 46 |
|
| 47 |
+
# garante 3 canais (RGB)
|
| 48 |
+
if arr.ndim == 2: # imagem em escala de cinza
|
| 49 |
+
arr = np.stack([arr] * 3, axis=-1)
|
|
|
|
| 50 |
|
| 51 |
+
# adiciona batch dimension
|
| 52 |
+
arr = np.expand_dims(arr, axis=0)
|
| 53 |
+
return arr
|
|
|
|
|
|
|
|
|
|
| 54 |
|
|
|
|
|
|
|
| 55 |
|
| 56 |
+
def predict(image: Image.Image) -> np.ndarray:
|
| 57 |
+
x = preprocess_image(image)
|
| 58 |
+
interpreter.set_tensor(input_details[0]["index"], x)
|
| 59 |
+
interpreter.invoke()
|
| 60 |
+
preds = interpreter.get_tensor(output_details[0]["index"])[0]
|
| 61 |
+
return preds
|
| 62 |
+
|
| 63 |
+
# ==========================
|
| 64 |
+
# Interface Streamlit
|
| 65 |
+
# ==========================
|
| 66 |
+
st.set_page_config(page_title="ArqueoIA", page_icon="🗿", layout="centered")
|
| 67 |
+
|
| 68 |
+
st.markdown(
|
| 69 |
+
"<h1 style='text-align:center; color:#1E88E5;'>🗿 ArqueoIA</h1>",
|
| 70 |
+
unsafe_allow_html=True,
|
| 71 |
+
)
|
| 72 |
+
st.markdown(
|
| 73 |
+
"<h3 style='text-align:center;'>Classificador educativo de artefatos arqueológicos</h3>",
|
| 74 |
+
unsafe_allow_html=True,
|
| 75 |
+
)
|
| 76 |
+
st.write(
|
| 77 |
+
"<p style='text-align:center;'>Envie uma foto de <b>osso</b>, "
|
| 78 |
+
"<b>cerâmica</b>, <b>moeda</b> ou <b>flecha</b>.</p>",
|
| 79 |
+
unsafe_allow_html=True,
|
| 80 |
+
)
|
| 81 |
+
st.write("---")
|
| 82 |
+
|
| 83 |
+
uploaded_file = st.file_uploader(
|
| 84 |
+
"Envie uma imagem do artefato:",
|
| 85 |
+
type=["jpg", "jpeg", "png"],
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
if uploaded_file is not None:
|
| 89 |
+
image = Image.open(uploaded_file).convert("RGB")
|
| 90 |
+
st.image(image, caption="Imagem enviada", use_column_width=True)
|
| 91 |
+
|
| 92 |
+
st.write("### 🔍 Analisando imagem...")
|
| 93 |
+
preds = predict(image)
|
| 94 |
+
|
| 95 |
+
top_idx = int(np.argmax(preds))
|
| 96 |
+
top_label = class_names[top_idx]
|
| 97 |
+
top_conf = float(preds[top_idx]) * 100.0
|
| 98 |
+
|
| 99 |
+
st.success(f"Sugestão da IA: **{top_label}** ({top_conf:.1f}% de confiança)")
|
| 100 |
+
|
| 101 |
+
st.write("### 📊 Detalhamento das classes")
|
| 102 |
for label, prob in zip(class_names, preds):
|
| 103 |
+
st.write(f"- **{label}**: {prob * 100:.1f}%")
|
| 104 |
+
else:
|
| 105 |
+
st.info("Carregue uma imagem para começar.")
|