fcernafukuzaki commited on
Commit
ddca66a
·
verified ·
1 Parent(s): 77112d8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +140 -0
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  from ultralytics import YOLO
2
  import streamlit as st
3
 
@@ -24,6 +25,114 @@ def yolo_tracking(frame):
24
 
25
  return rgb_frame
26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  # Setting page layout
28
  st.set_page_config(
29
  page_title="Object Detection using YOLOv8",
@@ -35,3 +144,34 @@ st.set_page_config(
35
  # Main page heading
36
  st.title("Object Detection And Tracking using YOLOv8")
37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
  from ultralytics import YOLO
3
  import streamlit as st
4
 
 
25
 
26
  return rgb_frame
27
 
28
+ def _display_detected_frames(conf, model, st_count, st_frame, image):
29
+ """
30
+ Display the detected objects on a video frame using the YOLOv8 model.
31
+ :param conf (float): Confidence threshold for object detection.
32
+ :param model (YOLOv8): An instance of the `YOLOv8` class containing the YOLOv8 model.
33
+ :param st_frame (Streamlit object): A Streamlit object to display the detected video.
34
+ :param image (numpy array): A numpy array representing the video frame.
35
+ :return: None
36
+ """
37
+ # Resize the image to a standard size
38
+ #image = cv2.resize(image, (720, int(720 * (9 / 16))))
39
+
40
+ # Predict the objects in the image using YOLOv8 model
41
+ res = model.predict(image, conf=conf)
42
+
43
+ inText = 'Vehicle In'
44
+ outText = 'Vehicle Out'
45
+ if config.OBJECT_COUNTER1 != None:
46
+ for _, (key, value) in enumerate(config.OBJECT_COUNTER1.items()):
47
+ inText += ' - ' + str(key) + ": " +str(value)
48
+ if config.OBJECT_COUNTER != None:
49
+ for _, (key, value) in enumerate(config.OBJECT_COUNTER.items()):
50
+ outText += ' - ' + str(key) + ": " +str(value)
51
+
52
+ # Plot the detected objects on the video frame
53
+ st_count.write(inText + '\n\n' + outText)
54
+ res_plotted = res[0].plot()
55
+ st_frame.image(res_plotted,
56
+ caption='Detected Video',
57
+ channels="BGR",
58
+ use_column_width=True
59
+ )
60
+
61
+
62
+
63
+
64
+ def infer_uploaded_video(conf, model):
65
+ """
66
+ Execute inference for uploaded video
67
+ :param conf: Confidence of YOLOv8 model
68
+ :param model: An instance of the `YOLOv8` class containing the YOLOv8 model.
69
+ :return: None
70
+ """
71
+ source_video = st.sidebar.file_uploader(
72
+ label="Choose a video..."
73
+ )
74
+
75
+ if source_video:
76
+ st.video(source_video)
77
+
78
+ if source_video:
79
+ if st.button("Execution"):
80
+ with st.spinner("Running..."):
81
+ try:
82
+ config.OBJECT_COUNTER1 = None
83
+ config.OBJECT_COUNTER = None
84
+ tfile = tempfile.NamedTemporaryFile()
85
+ tfile.write(source_video.read())
86
+ vid_cap = cv2.VideoCapture(
87
+ tfile.name)
88
+ st_count = st.empty()
89
+ st_frame = st.empty()
90
+ while (vid_cap.isOpened()):
91
+ success, image = vid_cap.read()
92
+ if success:
93
+ _display_detected_frames(conf,
94
+ model,
95
+ st_count,
96
+ st_frame,
97
+ image
98
+ )
99
+ else:
100
+ vid_cap.release()
101
+ break
102
+ except Exception as e:
103
+ st.error(f"Error loading video: {e}")
104
+
105
+
106
+ def infer_uploaded_webcam(conf, model):
107
+ """
108
+ Execute inference for webcam.
109
+ :param conf: Confidence of YOLOv8 model
110
+ :param model: An instance of the `YOLOv8` class containing the YOLOv8 model.
111
+ :return: None
112
+ """
113
+ try:
114
+ flag = st.button(
115
+ label="Stop running"
116
+ )
117
+ vid_cap = cv2.VideoCapture(0) # local camera
118
+ st_count = st.empty()
119
+ st_frame = st.empty()
120
+ while not flag:
121
+ success, image = vid_cap.read()
122
+ if success:
123
+ _display_detected_frames(
124
+ conf,
125
+ model,
126
+ st_count,
127
+ st_frame,
128
+ image
129
+ )
130
+ else:
131
+ vid_cap.release()
132
+ break
133
+ except Exception as e:
134
+ st.error(f"Error loading video: {str(e)}")
135
+
136
  # Setting page layout
137
  st.set_page_config(
138
  page_title="Object Detection using YOLOv8",
 
144
  # Main page heading
145
  st.title("Object Detection And Tracking using YOLOv8")
146
 
147
+ # sidebar
148
+ st.sidebar.header("DL Model Config")
149
+
150
+ # model options
151
+ task_type = st.sidebar.selectbox(
152
+ "Select Task",
153
+ ["Detection"]
154
+ )
155
+
156
+ confidence = float(st.sidebar.slider(
157
+ "Select Model Confidence", 30, 100, 50)) / 100
158
+
159
+ # image/video options
160
+ st.sidebar.header("Image/Video Config")
161
+ source_selectbox = st.sidebar.selectbox(
162
+ "Select Source",
163
+ config.SOURCES_LIST
164
+ )
165
+
166
+ SOURCES_LIST = ["Video", "Webcam"]
167
+
168
+ source_img = None
169
+ #if source_selectbox == config.SOURCES_LIST[0]: # Image
170
+ # infer_uploaded_image(confidence, model)
171
+ #elif source_selectbox == config.SOURCES_LIST[1]: # Video
172
+ if source_selectbox == SOURCES_LIST[0]: # Video
173
+ infer_uploaded_video(confidence, model)
174
+ elif source_selectbox == SOURCES_LIST[1]: # Webcam
175
+ infer_uploaded_webcam(confidence, model)
176
+ else:
177
+ st.error("Currently only 'Image' and 'Video' source are implemented")