-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathhelper.py
99 lines (82 loc) · 4.05 KB
/
helper.py
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
from ultralytics import YOLO
import time
import streamlit as st
import cv2
import settings
import threading
def sleep_and_clear_success():
time.sleep(3)
st.session_state['recyclable_placeholder'].empty()
st.session_state['non_recyclable_placeholder'].empty()
st.session_state['hazardous_placeholder'].empty()
def load_model(model_path):
model = YOLO(model_path)
return model
def classify_waste_type(detected_items):
recyclable_items = set(detected_items) & set(settings.RECYCLABLE)
non_recyclable_items = set(detected_items) & set(settings.NON_RECYCLABLE)
hazardous_items = set(detected_items) & set(settings.HAZARDOUS)
return recyclable_items, non_recyclable_items, hazardous_items
def remove_dash_from_class_name(class_name):
return class_name.replace("_", " ")
def _display_detected_frames(model, st_frame, image):
image = cv2.resize(image, (640, int(640*(9/16))))
if 'unique_classes' not in st.session_state:
st.session_state['unique_classes'] = set()
if 'recyclable_placeholder' not in st.session_state:
st.session_state['recyclable_placeholder'] = st.sidebar.empty()
if 'non_recyclable_placeholder' not in st.session_state:
st.session_state['non_recyclable_placeholder'] = st.sidebar.empty()
if 'hazardous_placeholder' not in st.session_state:
st.session_state['hazardous_placeholder'] = st.sidebar.empty()
if 'last_detection_time' not in st.session_state:
st.session_state['last_detection_time'] = 0
res = model.predict(image, conf=0.6)
names = model.names
detected_items = set()
for result in res:
new_classes = set([names[int(c)] for c in result.boxes.cls])
if new_classes != st.session_state['unique_classes']:
st.session_state['unique_classes'] = new_classes
st.session_state['recyclable_placeholder'].markdown('')
st.session_state['non_recyclable_placeholder'].markdown('')
st.session_state['hazardous_placeholder'].markdown('')
detected_items.update(st.session_state['unique_classes'])
recyclable_items, non_recyclable_items, hazardous_items = classify_waste_type(detected_items)
if recyclable_items:
detected_items_str = "\n- ".join(remove_dash_from_class_name(item) for item in recyclable_items)
st.session_state['recyclable_placeholder'].markdown(
f"<div class='stRecyclable'>Recyclable items:\n\n- {detected_items_str}</div>",
unsafe_allow_html=True
)
if non_recyclable_items:
detected_items_str = "\n- ".join(remove_dash_from_class_name(item) for item in non_recyclable_items)
st.session_state['non_recyclable_placeholder'].markdown(
f"<div class='stNonRecyclable'>Non-Recyclable items:\n\n- {detected_items_str}</div>",
unsafe_allow_html=True
)
if hazardous_items:
detected_items_str = "\n- ".join(remove_dash_from_class_name(item) for item in hazardous_items)
st.session_state['hazardous_placeholder'].markdown(
f"<div class='stHazardous'>Hazardous items:\n\n- {detected_items_str}</div>",
unsafe_allow_html=True
)
threading.Thread(target=sleep_and_clear_success).start()
st.session_state['last_detection_time'] = time.time()
res_plotted = res[0].plot()
st_frame.image(res_plotted, channels="BGR")
def play_webcam(model):
source_webcam = settings.WEBCAM_PATH
if st.button('Detect Objects'):
try:
vid_cap = cv2.VideoCapture(source_webcam)
st_frame = st.empty()
while (vid_cap.isOpened()):
success, image = vid_cap.read()
if success:
_display_detected_frames(model,st_frame,image)
else:
vid_cap.release()
break
except Exception as e:
st.sidebar.error("Error loading video: " + str(e))