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