-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblob_detection_1.py
47 lines (38 loc) · 1.74 KB
/
blob_detection_1.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
import pyb # Import module for board related functions
import sensor # Import the module for sensor related functions
import image # Import module containing machine vision algorithms
import time # Import module for tracking elapsed time
sensor.reset() # Resets the sensor
sensor.set_pixformat(sensor.RGB565) # Sets the sensor to RGB
sensor.set_framesize(sensor.QVGA) # Sets the resolution to 320x240 px
sensor.set_vflip(True) # Flips the image vertically
sensor.set_hmirror(True) # Mirrors the image horizontally
sensor.skip_frames(time = 2000) # Skip some frames to let the image stabilize
# Define the min/max LAB values we're looking for
thresholdsApple = (24, 60, 32, 54, 0, 42)
thresholdsBanana = (45, 75, 5, -10, 40, 12)
ledRed = pyb.LED(1) # Initiates the red led
ledGreen = pyb.LED(2) # Initiates the green led
clock = time.clock() # Instantiates a clock object
while(True):
clock.tick() # Advances the clock
img = sensor.snapshot() # Takes a snapshot and saves it in memory
# Find blobs with a minimal area of 50x50 = 2500 px
# Overlapping blobs will be merged
blobs = img.find_blobs([thresholdsApple, thresholdsBanana], area_threshold=2500, merge=True)
# Draw blobs
for blob in blobs:
# Draw a rectangle where the blob was found
img.draw_rectangle(blob.rect(), color=(0,255,0))
# Draw a cross in the middle of the blob
img.draw_cross(blob.cx(), blob.cy(), color=(0,255,0))
# Turn on green LED if a blob was found
if len(blobs) > 0:
ledGreen.on()
ledRed.off()
else:
# Turn the red LED on if no blob was found
ledGreen.off()
ledRed.on()
pyb.delay(50) # Pauses the execution for 50ms
print(clock.fps()) # Prints the framerate to the serial console