Spaces:
Runtime error
Runtime error
File size: 2,849 Bytes
ee4f7de | 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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
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)
|