diff options
-rw-r--r-- | nicla/road.py | 13 | ||||
-rw-r--r-- | nicla/traffic_light.py | 9 |
2 files changed, 14 insertions, 8 deletions
diff --git a/nicla/road.py b/nicla/road.py index c5ffae3..8d60dff 100644 --- a/nicla/road.py +++ b/nicla/road.py @@ -1,5 +1,6 @@ import sensor, image, time, math import uart +import traffic_light from consts import * sensor.reset() @@ -23,8 +24,8 @@ points = [(STRETCH, HORIZON), (WIDTH-1+SQUEEZE, HEIGHT-1), (-SQUEEZE, HEIGHT-1)] -def main(): - img = sensor.snapshot() +def drive(driveImg): + img = driveImg.copy() img.to_grayscale() img.replace(vflip=True, hmirror=True) img.rotation_corr(corners=points) @@ -49,8 +50,14 @@ def main(): steerByte = int((avg + 1.0) * (DUI_CMD_STEER_END - DUI_CMD_STEER_START) / 2 + DUI_CMD_STEER_START) uart.uart_buffer(steerByte) + sensor.dealloc_extra_fb() while(True): - main() + img = sensor.snapshot() + data = traffic_light.traf_lights(img) + if data is not None: + print(data) + uart.uart_buffer(data) + drive(img) uart.uart_buffer(DUI_CMD_SPEED_END) clock.tick() diff --git a/nicla/traffic_light.py b/nicla/traffic_light.py index 3d81139..517e2c3 100644 --- a/nicla/traffic_light.py +++ b/nicla/traffic_light.py @@ -30,11 +30,9 @@ def rgb2hsv(rgb): h = (h/6.0) % 1.0 return (h, s, v) -while(True): - clock.tick() - img = sensor.snapshot() - ## todo: downsample img - original = img.copy(copy_to_fb=True) + +def traf_lights(img) + original = img.copy() img = img.to_grayscale() for blob in img.find_blobs([(0, 60)], pixels_threshold=100): aspect = blob.h() / blob.w() @@ -63,3 +61,4 @@ while(True): img.draw_rectangle(blob.rect()) img.draw_circle(lights[light_status-1][0], lights[light_status-1][1], 2) print(("", "rood", "geel", "groen")[light_status]) + sensor.dealloc_extra_fb() |