Daebin's picture
Upload 2 files
ee4f7de verified
import streamlit as st
import torch
from PIL import Image
import numpy as np
import tempfile
import os
import requests
from io import BytesIO
from pathlib import Path
st.set_page_config(page_title="Ignition Point Detector", layout="centered")
st.image("logoall.jpg", use_container_width=True)
st.title("Ignition Point Detector ๐Ÿ”ฅ")
st.markdown("AI ๊ธฐ๋ฐ˜ ํ™”์žฌ ์ด๋ฏธ์ง€ ๋ถ„์„ ์›น์•ฑ์ž…๋‹ˆ๋‹ค. ์•„๋ž˜์—์„œ ๋ชจ๋ธ ํŒŒ์ผ(.pt)๊ณผ ์ด๋ฏธ์ง€๋ฅผ ์—…๋กœ๋“œํ•˜์—ฌ ๋ฐœํ™”์ง€์ ์„ ์˜ˆ์ธกํ•ด๋ณด์„ธ์š”.")
# ๋ชจ๋ธ URL ์ž…๋ ฅ
model_url = st.text_input("โ‘  .pt ๋ชจ๋ธ ํŒŒ์ผ URL์„ ์ž…๋ ฅํ•˜์„ธ์š” (์˜ˆ: Google Drive ๊ณต์œ  ๋งํฌ):")
# ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ
uploaded_images = st.file_uploader("โ‘ก ๋ถ„์„ํ•  ์ด๋ฏธ์ง€๋ฅผ ์—…๋กœ๋“œํ•˜์„ธ์š” (์—ฌ๋Ÿฌ ์žฅ ๊ฐ€๋Šฅ)", type=["jpg", "jpeg", "png"], accept_multiple_files=True)
# ์˜ˆ์ธก ๋ฒ„ํŠผ
predict_btn = st.button("๐Ÿ”ฅ ์˜ˆ์ธก ์‹œ์ž‘")
def load_model_from_url(url):
response = requests.get(url)
if response.status_code == 200:
with tempfile.NamedTemporaryFile(delete=False, suffix=".pt") as tmp_file:
tmp_file.write(response.content)
tmp_path = tmp_file.name
try:
model = torch.load(tmp_path, map_location=torch.device('cpu'))
model.eval()
return model
except Exception as e:
st.error(f"๋ชจ๋ธ ๋กœ๋“œ ์‹คํŒจ: {e}")
return None
else:
st.error("๋ชจ๋ธ ๋‹ค์šด๋กœ๋“œ ์‹คํŒจ: URL์„ ํ™•์ธํ•ด์ฃผ์„ธ์š”.")
return None
def run_prediction(model, image):
try:
img = Image.open(image).convert('RGB')
img_resized = img.resize((640, 640))
img_array = np.array(img_resized) / 255.0
img_tensor = torch.tensor(img_array).permute(2, 0, 1).unsqueeze(0).float()
results = model(img_tensor)
if isinstance(results, dict) and 'pred' in results:
pred_boxes = results['pred'][0]
for box in pred_boxes:
x1, y1, x2, y2, conf, cls = box.tolist()
st.write(f"๐Ÿ”ฅ ์˜ˆ์ธก ๋ฐ•์Šค: ์ขŒ์ƒ๋‹จ ({x1:.0f}, {y1:.0f}), ์šฐํ•˜๋‹จ ({x2:.0f}, {y2:.0f}), ์‹ ๋ขฐ๋„: {conf:.2f}")
st.image(img, caption="์—…๋กœ๋“œํ•œ ์ด๋ฏธ์ง€", use_container_width=True)
else:
st.warning("๋ชจ๋ธ ์˜ˆ์ธก ๊ฒฐ๊ณผ๊ฐ€ ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์Šต๋‹ˆ๋‹ค.")
except Exception as e:
st.error(f"์˜ˆ์ธก ์‹คํŒจ: {e}")
if predict_btn:
if not model_url:
st.warning("๋ชจ๋ธ URL์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.")
elif not uploaded_images:
st.warning("์ด๋ฏธ์ง€๋ฅผ ์—…๋กœ๋“œํ•ด์ฃผ์„ธ์š”.")
else:
with st.spinner("๋ชจ๋ธ์„ ๋‹ค์šด๋กœ๋“œํ•˜๊ณ  ๋กœ๋“œ ์ค‘์ž…๋‹ˆ๋‹ค..."):
model = load_model_from_url(model_url)
if model:
for img in uploaded_images:
st.subheader(f"๐Ÿ“ท {img.name}")
run_prediction(model, img)