diff options
Diffstat (limited to 'nicla')
-rw-r--r-- | nicla/garbage_filter.py | 40 | ||||
-rw-r--r-- | nicla/signs_detect.py | 42 |
2 files changed, 60 insertions, 22 deletions
diff --git a/nicla/garbage_filter.py b/nicla/garbage_filter.py new file mode 100644 index 0000000..3afd2c3 --- /dev/null +++ b/nicla/garbage_filter.py @@ -0,0 +1,40 @@ +traffic_light_garbage = list() + +""" +filter garbage + +arguments: +arr -- garbage filter memory list +val -- input value +sensitivity -- minimum amount of `val` in `arr` to return `val` +limit -- max length of `arr` + +return value: +if `arr` contains `sensitivity` or more of any item, that item will be +returned, else None is returned +""" +def garbage_filter(arr, val, sensitivity, limit): + if val == None: return None + arr[:] = [None]*(limit - len(arr)) + arr + arr.pop(0) + arr.append(val) + if len([x for x in arr if x == val]) >= sensitivity: + return val + return None + +if __name__ == "__main__": + inputs = [ + "red", + None, + "green", + "green", + None, + "red", + "green", + "red", + "red", + None, + None + ] + for x in inputs: + print(garbage_filter(traffic_light_garbage, x, 3, 4)) diff --git a/nicla/signs_detect.py b/nicla/signs_detect.py index 17d47c3..70bc633 100644 --- a/nicla/signs_detect.py +++ b/nicla/signs_detect.py @@ -1,10 +1,14 @@ -import sensor, image +import sensor, image, time + +sensor.reset() # Reset and initialize the sensor. +sensor.set_pixformat(sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE) +sensor.set_framesize(sensor.HVGA) # Set frame size to QVGA (320x240) +sensor.skip_frames(time = 2000) # Wait for settings take effect. +clock = time.clock() # Create a clock object to track the FPS. -kpts_threshold = 20 -kpts_corner = image.CORNER_FAST def init_kpts(str): - temp_img = image.Image(f"./{str}.jpg",copy_to_fb=True) + temp_img = image.Image(f"./{str}.jpg", copy_to_fb=True) temp_img.to_grayscale() kpts = temp_img.find_keypoints(max_keypoints=128, threshold=kpts_threshold, corner_detector=kpts_corner, scale_factor=1.2) return kpts @@ -18,8 +22,8 @@ def match_kpts(kpts0, kpts1): match = image.match_descriptor(kpts0, kpts1, threshold=70) #print("matched:%d dt:%d"%(match.count(), match.theta())) if match.count() > 0: - #print(match.count()) - return match.count() > 0 + print(match.count()) + return match.count() > 0 else: return 0 @@ -29,23 +33,21 @@ def read_red_sign(val, img, kpts): #img.draw_rectangle(val.rect()) #img.draw_cross(match.cx(), match.cy(), size=10) #print("stop") - data = 0x01 - elif match_kpts(kpts, speed): - #img.draw_rectangle(val.rect()) + return 0x01 + if match_kpts(kpts, speed): + img.draw_rectangle(val.rect()) #print("speed") - data = 0x02 - elif match_kpts(kpts, car): - #img.draw_rectangle(val.rect()) + return 0x02 + if match_kpts(kpts, car): + img.draw_rectangle(val.rect()) #print("car") - data = 0x03 + return 0x03 - return data - -def read_blu_sign(val, img, kpts): - return 0x02 +#def read_red_sign(val, img, kpts): def sign_detection(img): ######## Detect signs + blobs_r = img.find_blobs([(0, 100, 25, 63, -128, 127)]) blobs_b = img.find_blobs([(0, 29, 11, -128, -31, -5)]) #print(f"old: { len(blobs_r) + len(blobs_b) }") @@ -57,17 +59,13 @@ def sign_detection(img): ######## Read signs img = img.to_grayscale() - sign_buffer = 0x00 + if(len(blobs_r) > 0 or len(blobs_b) > 0): kpts_img = img.find_keypoints(max_keypoints=255, threshold=kpts_threshold, corner_detector=kpts_corner) for index, b in enumerate(blobs_r): sign_buffer = read_red_sign(b, img, kpts_img) - if sign_buffer != 0x00: - break #for index, b in enumerate(blobs_b): #sign_buffer = read_blu_sign(b, img, kpts_img) - #if sign_buffer != 0x00: - #break return sign_buffer |