Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +75 -42
- requirements.txt +5 -4
app.py
CHANGED
|
@@ -1,42 +1,75 @@
|
|
| 1 |
-
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
-
import
|
| 6 |
-
|
| 7 |
-
import
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
st.
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import torch
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import numpy as np
|
| 6 |
+
import tempfile
|
| 7 |
+
import os
|
| 8 |
+
import requests
|
| 9 |
+
from io import BytesIO
|
| 10 |
+
from pathlib import Path
|
| 11 |
+
|
| 12 |
+
st.set_page_config(page_title="Ignition Point Detector", layout="centered")
|
| 13 |
+
|
| 14 |
+
st.image("logoall.jpg", use_container_width=True)
|
| 15 |
+
|
| 16 |
+
st.title("Ignition Point Detector ๐ฅ")
|
| 17 |
+
|
| 18 |
+
st.markdown("AI ๊ธฐ๋ฐ ํ์ฌ ์ด๋ฏธ์ง ๋ถ์ ์น์ฑ์
๋๋ค. ์๋์์ ๋ชจ๋ธ ํ์ผ(.pt)๊ณผ ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ์ฌ ๋ฐํ์ง์ ์ ์์ธกํด๋ณด์ธ์.")
|
| 19 |
+
|
| 20 |
+
# ๋ชจ๋ธ URL ์
๋ ฅ
|
| 21 |
+
model_url = st.text_input("โ .pt ๋ชจ๋ธ ํ์ผ URL์ ์
๋ ฅํ์ธ์ (์: Google Drive ๊ณต์ ๋งํฌ):")
|
| 22 |
+
|
| 23 |
+
# ์ด๋ฏธ์ง ์
๋ก๋
|
| 24 |
+
uploaded_images = st.file_uploader("โก ๋ถ์ํ ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ์ธ์ (์ฌ๋ฌ ์ฅ ๊ฐ๋ฅ)", type=["jpg", "jpeg", "png"], accept_multiple_files=True)
|
| 25 |
+
|
| 26 |
+
# ์์ธก ๋ฒํผ
|
| 27 |
+
predict_btn = st.button("๐ฅ ์์ธก ์์")
|
| 28 |
+
|
| 29 |
+
def load_model_from_url(url):
|
| 30 |
+
response = requests.get(url)
|
| 31 |
+
if response.status_code == 200:
|
| 32 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pt") as tmp_file:
|
| 33 |
+
tmp_file.write(response.content)
|
| 34 |
+
tmp_path = tmp_file.name
|
| 35 |
+
try:
|
| 36 |
+
model = torch.load(tmp_path, map_location=torch.device('cpu'))
|
| 37 |
+
model.eval()
|
| 38 |
+
return model
|
| 39 |
+
except Exception as e:
|
| 40 |
+
st.error(f"๋ชจ๋ธ ๋ก๋ ์คํจ: {e}")
|
| 41 |
+
return None
|
| 42 |
+
else:
|
| 43 |
+
st.error("๋ชจ๋ธ ๋ค์ด๋ก๋ ์คํจ: URL์ ํ์ธํด์ฃผ์ธ์.")
|
| 44 |
+
return None
|
| 45 |
+
|
| 46 |
+
def run_prediction(model, image):
|
| 47 |
+
try:
|
| 48 |
+
img = Image.open(image).convert('RGB')
|
| 49 |
+
img_resized = img.resize((640, 640))
|
| 50 |
+
img_array = np.array(img_resized) / 255.0
|
| 51 |
+
img_tensor = torch.tensor(img_array).permute(2, 0, 1).unsqueeze(0).float()
|
| 52 |
+
results = model(img_tensor)
|
| 53 |
+
if isinstance(results, dict) and 'pred' in results:
|
| 54 |
+
pred_boxes = results['pred'][0]
|
| 55 |
+
for box in pred_boxes:
|
| 56 |
+
x1, y1, x2, y2, conf, cls = box.tolist()
|
| 57 |
+
st.write(f"๐ฅ ์์ธก ๋ฐ์ค: ์ข์๋จ ({x1:.0f}, {y1:.0f}), ์ฐํ๋จ ({x2:.0f}, {y2:.0f}), ์ ๋ขฐ๋: {conf:.2f}")
|
| 58 |
+
st.image(img, caption="์
๋ก๋ํ ์ด๋ฏธ์ง", use_container_width=True)
|
| 59 |
+
else:
|
| 60 |
+
st.warning("๋ชจ๋ธ ์์ธก ๊ฒฐ๊ณผ๊ฐ ์ฌ๋ฐ๋ฅด์ง ์์ต๋๋ค.")
|
| 61 |
+
except Exception as e:
|
| 62 |
+
st.error(f"์์ธก ์คํจ: {e}")
|
| 63 |
+
|
| 64 |
+
if predict_btn:
|
| 65 |
+
if not model_url:
|
| 66 |
+
st.warning("๋ชจ๋ธ URL์ ์
๋ ฅํด์ฃผ์ธ์.")
|
| 67 |
+
elif not uploaded_images:
|
| 68 |
+
st.warning("์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํด์ฃผ์ธ์.")
|
| 69 |
+
else:
|
| 70 |
+
with st.spinner("๋ชจ๋ธ์ ๋ค์ด๋ก๋ํ๊ณ ๋ก๋ ์ค์
๋๋ค..."):
|
| 71 |
+
model = load_model_from_url(model_url)
|
| 72 |
+
if model:
|
| 73 |
+
for img in uploaded_images:
|
| 74 |
+
st.subheader(f"๐ท {img.name}")
|
| 75 |
+
run_prediction(model, img)
|
requirements.txt
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
numpy
|
| 5 |
Pillow
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
streamlit==1.29.0
|
| 3 |
+
torch>=2.0.0
|
|
|
|
| 4 |
Pillow
|
| 5 |
+
numpy
|
| 6 |
+
requests
|