import sensor, image, time
import json
from pyb import UART
For color tracking to work really well you should ideally be in a very, very,
very, controlled enviroment where the lighting is constant...
red_threshold = (30, 100, 15, 127, 15, 127)
You may need to tweak the above settings for tracking green things...
Select an area in the Framebuffer to copy the color settings.
sensor.reset() # Initialize the camera sensor.
sensor.set_pixformat(sensor.RGB565) # use RGB565.
sensor.set_framesize(sensor.QQVGA) # use QQVGA for speed.分辨率为120*160。
sensor.skip_frames(10) # Let new settings take affect.
sensor.set_auto_whitebal(False) # turn this off.
clock = time.clock() # Tracks FPS.
uart = UART(3, 115200)
def find_max(blobs):
max_size=0
for blob in blobs:
if blob[2]*blob[3]>max_size:
max_blob=blob
max_size=blob[2]*blob[3]
return max_blob
while(True):
clock.tick() # Track elapsed milliseconds between snapshots().
img = sensor.snapshot() # Take a picture and return the image.
img.draw_cross(int(img.width()/2),int(img.height()/2))
blobs = img.find_blobs([red_threshold])
if blobs:
max_blob = find_max(blobs)
#print('sum : %d'% len(blobs))
# Draw a rect around the blob.
img.draw_rectangle(max_blob[0:4]) # rect
img.draw_cross(max_blob[5], max_blob[6]) # cx, cy
#{(1,22),(-3,33),(22222,0),(9999,12),(0,0)}
uart.writechar((max_blob[5]))
# print('you send:',data_out)
print(max_blob[5], max_blob[6])
print(img.width()/2,img.height()/2)#宽度80,高度60
else:
print("not found!")
Arduino
#include <SoftwareSerial.h>
SoftwareSerial softSerial(10, 11); // RX, TX
char c;
String comdata = "";
void setup() {
// put your setup code here, to run once:
softSerial.begin(115200);
Serial.begin(115200);
}
void loop() {
if(softSerial.available())
{comdata += char(softSerial.read());
delay(2);
Serial.println(comdata);
}
}