-
Notifications
You must be signed in to change notification settings - Fork 14
/
app_camera.py
75 lines (64 loc) · 2.46 KB
/
app_camera.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
import lcd
from framework import BaseApp, NeedRebootException
import sensor
import KPU as kpu
class CameraApp(BaseApp):
def __init__(self, system):
super(CameraApp, self).__init__(system)
self.__initialized = False
def __lazy_init(self):
err_counter = 0
while 1:
try:
sensor.reset() # Reset sensor may failed, let's try sometimes
break
except Exception:
err_counter = err_counter + 1
if err_counter == 20:
lcd.draw_string(lcd.width() // 2 - 100, lcd.height() // 2 - 4,
"Error: Sensor Init Failed", lcd.WHITE, lcd.RED)
time.sleep(0.1)
continue
print("progress 1 OK!")
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA) # QVGA=320x240
sensor.run(1)
print("progress 2 OK!")
self.task = kpu.load(0x300000) # Load Model File from Flash
anchor = (1.889, 2.5245, 2.9465, 3.94056, 3.99987,
5.3658, 5.155437, 6.92275, 6.718375, 9.01025)
# Anchor data is for bbox, extracted from the training sets.
print("progress 3 OK!")
kpu.init_yolo2(self.task, 0.5, 0.3, 5, anchor)
self.but_stu = 1
self.__initialized = True
def on_back_pressed(self):
raise NeedRebootException()
def on_draw(self):
if not self.__initialized:
self.__lazy_init()
try:
while True:
img = sensor.snapshot() # Take an image from sensor
print("progress 4 OK!")
# Run the detection routine
bbox = kpu.run_yolo2(self.task, img)
if bbox:
for i in bbox:
print(i)
img.draw_rectangle(i.rect())
lcd.display(img)
home_button = self.get_system().home_button
# TODO
led_w = self.get_system().led_w
if home_button.value() == 0 and self.but_stu == 1:
if led_w.value() == 1:
led_w.value(0)
else:
led_w.value(1)
self.but_stu = 0
if home_button.value() == 1 and self.but_stu == 0:
self.but_stu = 1
except KeyboardInterrupt:
a = kpu.deinit(task)
sys.exit()