From 76df689d48df0b5056769b9c8ca968ac4a0eb261 Mon Sep 17 00:00:00 2001 From: UnavailableDev <69792062+UnavailableDev@users.noreply.github.com> Date: Mon, 15 May 2023 13:56:58 +0200 Subject: optimization, reduced false positive blobs in final list --- openMV/POC_signs_red.py | 55 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 40 insertions(+), 15 deletions(-) diff --git a/openMV/POC_signs_red.py b/openMV/POC_signs_red.py index cb521b9..3279467 100644 --- a/openMV/POC_signs_red.py +++ b/openMV/POC_signs_red.py @@ -22,27 +22,52 @@ while(True): clock.tick() # Update the FPS clock. img = sensor.snapshot() # Take a picture and return the image. - #lines = img.find_lines() - #for i in lines: - #img.draw_line(i.line(), 255, 8) + ThR = 0 + ThG = 255 + ThB = 128 + threshold_r = [(ThR,255,0,255,255,ThG)] - #gray = img - #gray.to_grayscale() - #img.find_edges(0) + #Red + #if(R >= ThR and G <= thG) + #Blue + #if(B >= thB) - blobs = img.find_blobs(threshold_rgb) + blobs_r = img.find_blobs([(0, 100, 25, 63, -128, 127)]) + blobs_b = img.find_blobs([(0, 29, 11, -128, -31, -5)]) #blobs.count() #print(blobs) ##kpts = img.find_keypoints() - for index, b in enumerate(blobs, 1): - convex = b.convexity() - if convex < 0.8: - img.draw_rectangle(b.rect(),int((512+256)*convex),2) - print(b.convexity()) - #img.draw_line(12,12,200,200,255,8) + print(f"old: { len(blobs_r) + len(blobs_b) }") - print(clock.fps()) # Note: OpenMV Cam runs about half as fast when connected - # to the IDE. The FPS should increase once disconnected. + blobs_r[:] = [b for b in blobs_r if (b.convexity() < 0.7 and b.area() > 64)] + blobs_b[:] = [b for b in blobs_b if (b.convexity() < 0.7 and b.area() > 64)] + + print(f"new: { len(blobs_r) + len(blobs_b) }") + + #for index, b in enumerate(blobs_r): + #convex = b.convexity() + #roundn = b.roundness() + #if convex < 0.8: + #img.draw_rectangle(b.rect(),[255,int((256)*roundn),0],2) + #print(index) + #else: + #del blobs_r[index] + #img.draw_rectangle(b.rect(),[128,128,128],4) + + for index, b in enumerate(blobs_r): + roundn = b.roundness() + img.draw_rectangle(b.rect(),[255,int((256)*roundn),0],2) + #print(index) + + for index, b in enumerate(blobs_b): + roundn = b.roundness() + img.draw_rectangle(b.rect(),[0,int((256)*roundn),255],2) + + + # Note: OpenMV Cam runs about half as fast when connected + # to the IDE. The FPS should increase once disconnected. + + print("EOC") -- cgit v1.2.3 From f2335307a26a8ab3e18d8990d2640a8de4cbd0e4 Mon Sep 17 00:00:00 2001 From: UnavailableDev <69792062+UnavailableDev@users.noreply.github.com> Date: Tue, 6 Jun 2023 11:57:48 +0200 Subject: keypoint sign recognition --- openMV/POC_signs_red.py | 108 +++++++++++++++++++++++++++++------------------- 1 file changed, 66 insertions(+), 42 deletions(-) diff --git a/openMV/POC_signs_red.py b/openMV/POC_signs_red.py index 3279467..c996a88 100644 --- a/openMV/POC_signs_red.py +++ b/openMV/POC_signs_red.py @@ -4,70 +4,94 @@ import sensor, image, time -# Color Tracking Thresholds (Grayscale Min, Grayscale Max) -min_rgb = 128 -max_rgb = 255 -threshold_list = [(min_rgb, max_rgb)]# only bright grey colours will get tracked. -threshold_rgb = [(0, 100, 75, 32, 2, 127)] #only find red -#threshold_rgb = [(18, 78, -8, 127, 24, 127)] - sensor.reset() # Reset and initialize the sensor. sensor.set_pixformat(sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE) #sensor.set_pixformat(sensor.GRAYSCALE) -sensor.set_framesize(sensor.QVGA) # Set frame size to QVGA (320x240) +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. -while(True): - clock.tick() # Update the FPS clock. - img = sensor.snapshot() # Take a picture and return the image. - ThR = 0 - ThG = 255 - ThB = 128 - threshold_r = [(ThR,255,0,255,255,ThG)] +def init_kpts(str): + 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 + +def match_kpts(kpts0, kpts1): + if kpts0 is not None and kpts1 is not None: + + 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() > 1 + + else: + return 0 + +def read_red_sign(val, img, kpts): + + #img.draw_keypoints(kpts,255) + + if match_kpts(kpts, stop): + img.draw_rectangle(val.rect()) + #img.draw_cross(match.cx(), match.cy(), size=10) + print("stop") - #Red - #if(R >= ThR and G <= thG) + if match_kpts(kpts, speed): + img.draw_rectangle(val.rect()) + print("speed") - #Blue - #if(B >= thB) + if match_kpts(kpts, car): + img.draw_rectangle(val.rect()) + print("car") +#def read_red_sign(val, img, kpts): + + +kpts_threshold = 20 +kpts_corner = image.CORNER_FAST + +speed = init_kpts("speed") +stop = init_kpts("stop") +car = init_kpts("image") + + +while(True): + clock.tick() # Update the FPS clock. + img = sensor.snapshot() # Take a picture and return the image. + + ######## Detect signs blobs_r = img.find_blobs([(0, 100, 25, 63, -128, 127)]) blobs_b = img.find_blobs([(0, 29, 11, -128, -31, -5)]) - #blobs.count() - #print(blobs) - ##kpts = img.find_keypoints() - - print(f"old: { len(blobs_r) + len(blobs_b) }") + #print(f"old: { len(blobs_r) + len(blobs_b) }") blobs_r[:] = [b for b in blobs_r if (b.convexity() < 0.7 and b.area() > 64)] blobs_b[:] = [b for b in blobs_b if (b.convexity() < 0.7 and b.area() > 64)] + #print(f"new: { len(blobs_r) + len(blobs_b) }") + + + ######## Read signs + img = img.to_grayscale() - print(f"new: { len(blobs_r) + len(blobs_b) }") + if(len(blobs_r) > 0 or len(blobs_b) > 0): - #for index, b in enumerate(blobs_r): - #convex = b.convexity() - #roundn = b.roundness() - #if convex < 0.8: - #img.draw_rectangle(b.rect(),[255,int((256)*roundn),0],2) - #print(index) - #else: - #del blobs_r[index] - #img.draw_rectangle(b.rect(),[128,128,128],4) + kpts_img = img.find_keypoints(max_keypoints=255, threshold=kpts_threshold, corner_detector=kpts_corner) - for index, b in enumerate(blobs_r): - roundn = b.roundness() - img.draw_rectangle(b.rect(),[255,int((256)*roundn),0],2) - #print(index) + for index, b in enumerate(blobs_r): + read_red_sign(b, img, kpts_img) - for index, b in enumerate(blobs_b): - roundn = b.roundness() - img.draw_rectangle(b.rect(),[0,int((256)*roundn),255],2) + for index, b in enumerate(blobs_b): + read_blu_sign(b, img, kpts_img) # Note: OpenMV Cam runs about half as fast when connected # to the IDE. The FPS should increase once disconnected. + #img = gr - print("EOC") + #print("EOC") -- cgit v1.2.3 From 46acbbb6fd27cc00dadcc9118d774ae674f8eef5 Mon Sep 17 00:00:00 2001 From: UnavailableDev <69792062+UnavailableDev@users.noreply.github.com> Date: Tue, 6 Jun 2023 12:04:29 +0200 Subject: mv file --- nicla/signs_detect.py | 97 +++++++++++++++++++++++++++++++++++++++++++++++++ openMV/POC_signs_red.py | 97 ------------------------------------------------- 2 files changed, 97 insertions(+), 97 deletions(-) create mode 100644 nicla/signs_detect.py delete mode 100644 openMV/POC_signs_red.py diff --git a/nicla/signs_detect.py b/nicla/signs_detect.py new file mode 100644 index 0000000..c996a88 --- /dev/null +++ b/nicla/signs_detect.py @@ -0,0 +1,97 @@ +# Hello World Example +# +# Welcome to the OpenMV IDE! Click on the green run arrow button below to run the script! + +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_pixformat(sensor.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. + + +def init_kpts(str): + 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 + +def match_kpts(kpts0, kpts1): + if kpts0 is not None and kpts1 is not None: + + 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() > 1 + + else: + return 0 + +def read_red_sign(val, img, kpts): + + #img.draw_keypoints(kpts,255) + + if match_kpts(kpts, stop): + img.draw_rectangle(val.rect()) + #img.draw_cross(match.cx(), match.cy(), size=10) + print("stop") + + if match_kpts(kpts, speed): + img.draw_rectangle(val.rect()) + print("speed") + + if match_kpts(kpts, car): + img.draw_rectangle(val.rect()) + print("car") + +#def read_red_sign(val, img, kpts): + + +kpts_threshold = 20 +kpts_corner = image.CORNER_FAST + +speed = init_kpts("speed") +stop = init_kpts("stop") +car = init_kpts("image") + + +while(True): + clock.tick() # Update the FPS clock. + img = sensor.snapshot() # Take a picture and return the image. + + ######## 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) }") + + blobs_r[:] = [b for b in blobs_r if (b.convexity() < 0.7 and b.area() > 64)] + blobs_b[:] = [b for b in blobs_b if (b.convexity() < 0.7 and b.area() > 64)] + #print(f"new: { len(blobs_r) + len(blobs_b) }") + + + ######## Read signs + img = img.to_grayscale() + + 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): + read_red_sign(b, img, kpts_img) + + for index, b in enumerate(blobs_b): + read_blu_sign(b, img, kpts_img) + + + # Note: OpenMV Cam runs about half as fast when connected + # to the IDE. The FPS should increase once disconnected. + #img = gr + + #print("EOC") diff --git a/openMV/POC_signs_red.py b/openMV/POC_signs_red.py deleted file mode 100644 index c996a88..0000000 --- a/openMV/POC_signs_red.py +++ /dev/null @@ -1,97 +0,0 @@ -# Hello World Example -# -# Welcome to the OpenMV IDE! Click on the green run arrow button below to run the script! - -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_pixformat(sensor.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. - - -def init_kpts(str): - 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 - -def match_kpts(kpts0, kpts1): - if kpts0 is not None and kpts1 is not None: - - 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() > 1 - - else: - return 0 - -def read_red_sign(val, img, kpts): - - #img.draw_keypoints(kpts,255) - - if match_kpts(kpts, stop): - img.draw_rectangle(val.rect()) - #img.draw_cross(match.cx(), match.cy(), size=10) - print("stop") - - if match_kpts(kpts, speed): - img.draw_rectangle(val.rect()) - print("speed") - - if match_kpts(kpts, car): - img.draw_rectangle(val.rect()) - print("car") - -#def read_red_sign(val, img, kpts): - - -kpts_threshold = 20 -kpts_corner = image.CORNER_FAST - -speed = init_kpts("speed") -stop = init_kpts("stop") -car = init_kpts("image") - - -while(True): - clock.tick() # Update the FPS clock. - img = sensor.snapshot() # Take a picture and return the image. - - ######## 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) }") - - blobs_r[:] = [b for b in blobs_r if (b.convexity() < 0.7 and b.area() > 64)] - blobs_b[:] = [b for b in blobs_b if (b.convexity() < 0.7 and b.area() > 64)] - #print(f"new: { len(blobs_r) + len(blobs_b) }") - - - ######## Read signs - img = img.to_grayscale() - - 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): - read_red_sign(b, img, kpts_img) - - for index, b in enumerate(blobs_b): - read_blu_sign(b, img, kpts_img) - - - # Note: OpenMV Cam runs about half as fast when connected - # to the IDE. The FPS should increase once disconnected. - #img = gr - - #print("EOC") -- cgit v1.2.3 From e239429c8885a7c152194a9f25401204258a0fbd Mon Sep 17 00:00:00 2001 From: UnavailableDev <69792062+UnavailableDev@users.noreply.github.com> Date: Tue, 6 Jun 2023 12:08:31 +0200 Subject: cleanup --- nicla/signs_detect.py | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/nicla/signs_detect.py b/nicla/signs_detect.py index c996a88..baf62e8 100644 --- a/nicla/signs_detect.py +++ b/nicla/signs_detect.py @@ -1,12 +1,7 @@ -# Hello World Example -# -# Welcome to the OpenMV IDE! Click on the green run arrow button below to run the script! - 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_pixformat(sensor.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. @@ -15,28 +10,20 @@ clock = time.clock() # Create a clock object to track the FPS. def init_kpts(str): 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 def match_kpts(kpts0, kpts1): if kpts0 is not None and kpts1 is not None: - 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() > 1 - else: return 0 def read_red_sign(val, img, kpts): - - #img.draw_keypoints(kpts,255) - if match_kpts(kpts, stop): img.draw_rectangle(val.rect()) #img.draw_cross(match.cx(), match.cy(), size=10) @@ -80,7 +67,6 @@ while(True): img = img.to_grayscale() 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): @@ -88,10 +74,3 @@ while(True): for index, b in enumerate(blobs_b): read_blu_sign(b, img, kpts_img) - - - # Note: OpenMV Cam runs about half as fast when connected - # to the IDE. The FPS should increase once disconnected. - #img = gr - - #print("EOC") -- cgit v1.2.3