-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind_stuff.py
More file actions
64 lines (48 loc) · 1.33 KB
/
find_stuff.py
File metadata and controls
64 lines (48 loc) · 1.33 KB
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
from machine import UART, Pin
import ujson as json
UART_BAUDRATE = 115200
uart = UART(
0,
baudrate=UART_BAUDRATE,
tx=Pin(0),
rx=Pin(1),
timeout=200,
)
uart_buffer = ""
def get_bboxes(uart):
global uart_buffer
line = uart.read()
if line is None:
return (None, None, None)
try:
uart_buffer += line.decode("utf-8")
except:
uart_buffer = ""
return (None, None, None)
if "}" not in uart_buffer:
return (None, None, None)
try:
json_str, uart_buffer = uart_buffer.split("}\r\n", 1)
json_str += "}"
except:
uart_buffer = ""
return (None, None, None)
try:
data = json.loads(json_str)
except:
uart_buffer = ""
return (None, None, None)
bbox_arrow = None
bbox_basket = None
bbox_pad = None
for bbox in data["bboxes"]:
if bbox["id"] in (2, 5):
if bbox_pad is None or bbox["score"] > bbox_pad["score"]:
bbox_pad = bbox
elif bbox["id"] in (1, 4):
if bbox_basket is None or bbox["score"] > bbox_basket["score"]:
bbox_basket = bbox
elif bbox["id"] == 3:
if bbox_arrow is None or bbox["score"] > bbox_arrow["score"]:
bbox_arrow = bbox
return (bbox_arrow, bbox_basket, bbox_pad)