-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.py
279 lines (240 loc) · 8.62 KB
/
functions.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# -*- coding: utf-8 -*-
"""
Created on Sat May 25 10:11:25 2024
@author: ASUS
"""
import numpy as np
import logging
import pandas as pd
def get_price(prices_data_frame: pd.DataFrame(), product_name="small bottle") -> int:
product_price = prices_data_frame[prices_data_frame["product"] == product_name][
"price"
].iloc[0]
return product_price
def update_balance(balance_data_frame: pd.DataFrame(), customer_id, amount=0):
new_balance = (
balance_data_frame[balance_data_frame["person_id"] == customer_id].balance
+ amount
)
balance_data_frame[balance_data_frame["person_id"] == customer_id].iloc[
0
].balance = new_balance
balance_data_frame.loc[
balance_data_frame["person_id"] == customer_id, "balance"
] = new_balance
log_info = f"{amount} was added tu your balance"
return balance_data_frame, log_info
def logger_init():
logger = logging.getLogger()
logger.setLevel(logging.INFO)
formatter = logging.Formatter("%(asctime)s | %(levelname)s | %(message)s")
file_handler = logging.FileHandler("logs.log")
file_handler.setLevel(logging.INFO)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
return logger
# detect move in region of interest
def mov_detection_roi(
last_init_frame, current_frame_gray, bot_shelf_border, up_shelf_border, thresh
)->bool:
last_init_frame_crop = last_init_frame[bot_shelf_border:up_shelf_border, :]
current_frame_shelf = current_frame_gray[bot_shelf_border:up_shelf_border, :]
diff = abs(
np.array(current_frame_shelf, dtype=float)
- np.array(last_init_frame_crop, dtype=float)
)
if np.max(diff) > 100:
return True
else:
return False
def process_all_cases(
final_current_lists,
curr_heights,
items_list,
state,
new_state,
customer_id,
balance_data_frame,
prices_data_frame,
log_updater,
):
log_info = ""
if len(final_current_lists) < 5:
return sorted(items_list), state, balance_data_frame, log_info
final_items = get_final_items_list(final_current_lists)
if len(curr_heights) < len(items_list):
items_list, state, balance_data_frame, log_info = update_after_taking(
items_list,
final_items,
state,
new_state,
customer_id,
balance_data_frame,
prices_data_frame,
log_updater,
)
elif len(curr_heights) > len(items_list):
items_list, state, balance_data_frame, log_info = update_after_returning(
items_list,
final_items,
state,
new_state,
customer_id,
balance_data_frame,
prices_data_frame,
log_updater,
)
# multiple events action, return and take
elif sorted(items_list) != sorted(final_items):
items_list, state, balance_data_frame, log_info = update_after_returning(
items_list,
final_items,
state,
new_state,
customer_id,
balance_data_frame,
prices_data_frame,
log_updater,
)
items_list, state, balance_data_frame, log_info = update_after_taking(
items_list,
final_items,
state,
new_state,
customer_id,
balance_data_frame,
prices_data_frame,
log_updater,
)
return sorted(items_list), state, balance_data_frame, log_info
def get_heights(result_boxes)->list:
curr_heights = list()
for object_box in result_boxes:
for ii in range(0, len(object_box.id)):
current_obj_boundary = object_box.xywh.numpy()[ii]
curr_heights.append(current_obj_boundary[3])
return curr_heights
def get_final_items_list(final_current_lists)->list:
final_items = list()
for final_item in final_current_lists:
for ii in final_item:
final_items.append(ii)
final_items = sorted(list(np.unique(final_items)))
return final_items
def update_after_returning(
init_items: list,
current_items: list,
state: bool,
new_state: bool,
customer_id: int,
balance_data_frame,
prices_data_frame,
log_updater,
):
log_info = ""
return_list = [x for x in current_items if (x not in init_items)]
if return_list:
for returned_item in return_list:
new_state = str("The customer returns the " + returned_item)
price = get_price(prices_data_frame, product_name=returned_item)
balance_data_frame, log_info = update_balance(
balance_data_frame, customer_id, price
)
init_items.append(returned_item)
if update_state(new_state, state):
print(new_state)
state = new_state
log_info = (
"you have returned "
+ returned_item
+ " "
+ str(price)
+ " was added to your balance"
)
log_updater.info(log_info)
return sorted(init_items), state, balance_data_frame, log_info
def update_after_taking(
init_items: list,
current_items: list,
state: bool,
new_state: bool,
customer_id,
balance_data_frame,
prices_data_frame,
log_updater,
):
log_info = ""
buy_factor = -1
gone_list = [x for x in init_items if (x not in current_items)]
if gone_list:
for taken_item in gone_list:
new_state = str("The customer takes the " + taken_item)
price = get_price(prices_data_frame, product_name=taken_item)
balance_data_frame, log_info = update_balance(
balance_data_frame, customer_id, buy_factor * price
)
init_items.remove(taken_item)
if update_state(new_state, state):
print(new_state)
state = new_state
log_info = (
"you have bought "
+ taken_item
+ " "
+ str(price)
+ " was discounted from your balance"
)
log_updater.info(log_info)
return sorted(init_items), state, balance_data_frame, log_info
# get objects in initial state
def get_ids(init_boxes: list)->list:
tracked_id_list = list([])
for object_box in init_boxes:
if object_box.id != None:
for ii in object_box.id.numpy():
tracked_id_list.append(ii)
tracked_id_list = list(np.unique(tracked_id_list))
return tracked_id_list
def shelf_boundary(tracked_id_list, init_boxes):
tracked_boxes = list([])
tofind_list = tracked_id_list
for object_box in init_boxes:
for ii in range(0, len(object_box.id)):
if object_box.id[ii].numpy() in tofind_list:
tracked_boxes.append(object_box.xywh.numpy()[ii])
tofind_list.remove(object_box.id[ii].numpy())
obj_count = len(tracked_boxes)
x_list, y_list, width_list, height_list = list(), list(), list(), list()
for ii in range(0, obj_count):
x_list.append(tracked_boxes[ii][0])
y_list.append(tracked_boxes[ii][1])
width_list.append(tracked_boxes[ii][2])
height_list.append(tracked_boxes[ii][3])
bot_shelf_border = int(min(np.array(y_list)) - int(0.5 * max(height_list)))
up_shelf_border = int(max(np.array(y_list)) + int(0.5 * max(height_list)))
return bot_shelf_border, up_shelf_border, height_list
def update_state(newstate: str, state: str):
if newstate != state:
return True
else:
return False
def describe_objects(height_list, height_thresholds):
item_list = list()
for height in height_list:
if height < height_thresholds[0]:
item_list.append("small bottle")
elif height > height_thresholds[len(height_thresholds) - 1]:
item_list.append("large bottle")
else:
item_list.append("medium bottle")
return sorted(item_list)
def height_classification(height_list: list):
height_thresholds = list([])
if len(height_list) == 2:
height_thresholds.append(int(np.mean([height_list[0], height_list[1]])))
elif len(height_list) == 3:
height_thresholds.append(int(np.mean([height_list[0], height_list[1]])))
height_thresholds.append(int(np.mean([height_list[1], height_list[2]])))
else:
raise ValueError("only one, or more than 3 objects detected !!")
return height_thresholds, len(height_thresholds)