-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
48 lines (38 loc) · 1.48 KB
/
Copy pathmain.py
File metadata and controls
48 lines (38 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import streamlit as st
from PIL import Image
import tempfile
import cv2
from ultralytics import YOLO
import os
from dotenv import load_dotenv
# Carica variabili d'ambiente
load_dotenv()
YOLO_MODEL_PATH = os.getenv("YOLO_MODEL_PATH")
# Caricamento modello
model = YOLO(YOLO_MODEL_PATH)
st.title("🔍 Traffic Sign Detection and Recognition")
supported_types = ["jpg", "jpeg", "png", "bmp", "tiff", "webp"]
uploaded_file = st.file_uploader("Carica un'immagine", type=supported_types)
if uploaded_file is not None:
try:
image = Image.open(uploaded_file)
st.image(image, caption="🖼️ Immagine caricata", use_container_width=True)
with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as temp:
image.convert("RGB").save(temp.name)
temp_path = temp.name
if st.button("Esegui predizione"):
with st.spinner("🧠 In esecuzione YOLO..."):
results = model.predict(
source=temp_path, save=False, save_txt=False
)
annotated_bgr = results[0].plot()
annotated_rgb = cv2.cvtColor(annotated_bgr, cv2.COLOR_BGR2RGB)
st.image(
annotated_rgb,
caption="📍 Risultato YOLO",
use_container_width=True,
)
if os.path.exists(temp_path):
os.remove(temp_path)
except Exception as e:
st.error(f"Errore nell'apertura dell'immagine: {e}")