Marco Rossi commited on
Commit
adfb30d
1 Parent(s): 9d81809

add streamlit app

Browse files
app.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import io
2
+
3
+ from PIL import Image
4
+ import streamlit as st
5
+ from ultralytics import YOLO
6
+
7
+
8
+ # config
9
+ options_to_yolo_checkpoint = {
10
+ "segment": "yolo/models/yolov8n-seg.pt",
11
+ "detect": "yolo/models/yolov8n.pt",
12
+ "finetuned": "yolo/models/yolov8n-vehicles-counting-finetuned.pt"
13
+ }
14
+
15
+ st.set_page_config(layout="wide")
16
+
17
+ st.session_state["image"] = None
18
+
19
+ st.session_state["yolo_checkpoint"] = None
20
+ st.session_state["yolo_model"] = None
21
+
22
+ st.title("Object detection with YOLO v8")
23
+
24
+ device_selection = st.radio("Aquire image : from", ["file", "camera"])
25
+
26
+
27
+ def save_img_in_memory(img: io.BytesIO):
28
+ if img is not None:
29
+ st.session_state["image"] = Image.open(img)
30
+
31
+
32
+ def select_from_file():
33
+ with st.form("Load form", clear_on_submit=True):
34
+ uploaded_file = st.file_uploader("Choose a file", type="jpg")
35
+ submitted = st.form_submit_button("Load")
36
+ if submitted:
37
+ save_img_in_memory(uploaded_file)
38
+ uploaded_file = None
39
+
40
+
41
+ def select_from_device():
42
+ screenshot = st.camera_input("camera input")
43
+ save_img_in_memory(screenshot)
44
+
45
+
46
+ def load_yolo(force: bool = False):
47
+ if st.session_state["yolo_model"] is None or force:
48
+ st.session_state["yolo_model"] = YOLO(st.session_state["yolo_checkpoint"])
49
+
50
+
51
+ def detect_with_yolo(img):
52
+ load_yolo()
53
+ result = st.session_state["yolo_model"](img)
54
+ im_array = result[0].plot()
55
+ im = Image.fromarray(im_array[..., ::-1])
56
+ st.image(im)
57
+
58
+
59
+ col1, col2 = st.columns(2)
60
+ with col1:
61
+ selected_model = st.selectbox(
62
+ "Select model", options_to_yolo_checkpoint.keys()
63
+ )
64
+ st.session_state["yolo_checkpoint"] = options_to_yolo_checkpoint[selected_model]
65
+
66
+ match device_selection:
67
+ case "file":
68
+ select_from_file()
69
+ case "camera":
70
+ select_from_device()
71
+ case _:
72
+ raise ValueError("We should not be here")
73
+
74
+
75
+ with col2:
76
+ if st.session_state["image"] is not None:
77
+ with st.spinner("Detecting ..."):
78
+ detect_with_yolo(st.session_state["image"])
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ torch==^2.1.1+cp
2
+ torchvision==^0.16.1+cpu
3
+ ultralytics==^8.0.221
4
+ streamlit==^1.29.0
yolo/models/yolov8n-seg.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d39e867b2c3a5dbc1aa764411544b475cb14727bf6af1ec46c238f8bb1351ab9
3
+ size 7054355
yolo/models/yolov8n-vehicles-counting-finetuned.pt.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:fe0c1aad24e0c14802a95ed22ce039e8b5d413c133e43707203078a48932e402
3
+ size 6252121
yolo/models/yolov8n.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:31e20dde3def09e2cf938c7be6fe23d9150bbbe503982af13345706515f2ef95
3
+ size 6534387