Daebin commited on
Commit
ee4f7de
ยท
verified ยท
1 Parent(s): 21ef916

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +75 -42
  2. requirements.txt +5 -4
app.py CHANGED
@@ -1,42 +1,75 @@
1
- import streamlit as st
2
- import torch
3
- import tempfile
4
- import os
5
- import cv2
6
- from PIL import Image
7
- import numpy as np
8
-
9
- st.set_page_config(page_title="Fire Ignition Detector", layout="centered")
10
-
11
- # ์ƒ๋‹จ ๋กœ๊ณ ์™€ ์ œ๋ชฉ
12
- st.image("logoall.jpg", use_column_width=True)
13
- st.markdown("<h2 style='text-align: center;'>ํ™”์žฌ ๋ฐœํ™”์ง€์  ํƒ์ง€๊ธฐ</h2>", unsafe_allow_html=True)
14
-
15
- # ๋ชจ๋ธ ๋กœ๋“œ
16
- @st.cache_resource
17
- def load_model(model_path):
18
- model = torch.load(model_path, map_location=torch.device('cpu'))
19
- model.eval()
20
- return model
21
-
22
- uploaded_model = st.file_uploader("๐Ÿ”ฅ YOLOv5 ๋ชจ๋ธ(.pt) ํŒŒ์ผ ์—…๋กœ๋“œ", type=["pt"])
23
- if uploaded_model is not None:
24
- with tempfile.NamedTemporaryFile(delete=False, suffix=".pt") as tmp_model:
25
- tmp_model.write(uploaded_model.read())
26
- model_path = tmp_model.name
27
- model = load_model(model_path)
28
- st.success("๋ชจ๋ธ์ด ์„ฑ๊ณต์ ์œผ๋กœ ์—…๋กœ๋“œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.")
29
-
30
- # ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ
31
- uploaded_images = st.file_uploader("๐Ÿ–ผ๏ธ ๋ถ„์„ํ•  ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ (์—ฌ๋Ÿฌ ์žฅ ๊ฐ€๋Šฅ)", type=["jpg", "jpeg", "png"], accept_multiple_files=True)
32
-
33
- if uploaded_images:
34
- for img_file in uploaded_images:
35
- file_bytes = np.asarray(bytearray(img_file.read()), dtype=np.uint8)
36
- image = cv2.imdecode(file_bytes, 1)
37
- image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
38
-
39
- results = model(image_rgb)
40
-
41
- result_img = results.render()[0]
42
- st.image(result_img, caption=f"๐Ÿ” ๋ถ„์„ ๊ฒฐ๊ณผ: {img_file.name}", use_column_width=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- streamlit
2
- torch
3
- opencv-python
4
- numpy
5
  Pillow
 
 
 
1
+
2
+ streamlit==1.29.0
3
+ torch>=2.0.0
 
4
  Pillow
5
+ numpy
6
+ requests