import sensor, image, time
import json
from pyb import UART
uart = UART(3, 9600)
a=3
B=3
C=[(47, 100, 61, -27, 40, 109)]
D=[]
E=[]
F=3
thresholds = [(6, 47, 121, 6, 93, 6), # generic_red_thresholds
(47, 100, 61, -27, 40, 109), # generic_yellow_thresholds
(23, 69, 89, -12, -7, -63),
(20, 37, 20, 60, -1, 45),
(24, 36, -1, 20, -55, -25),
(30, 44, -46, -9, 7, 44),
(21, 100, 118, 19, 40, -116)] # generic_blue_thresholds
def find_max(blobs):
max_size=0
for blob in blobs:
if blob.pixels() > max_size:# blob.pixels()返回从属于色块(int)一部分的像素数量。
max_blob=blob
max_size = blob.pixels()
return max_blob
def x(D,B):
while(B>2):
clock.tick() # Track elapsed milliseconds between snapshots().
img = sensor.snapshot() # Take a picture and return the image.
blobs = img.find_blobs(D)
if blobs:
print('视野中的色块数sum : %d'% len(blobs))
data=[]
# 寻找视野中最大的色块
max_blob=find_max(blobs)
# 框出最大的色块,并在中心画十字
img.draw_rectangle(max_blob.rect()) # rect
img.draw_cross(max_blob.cx(), max_blob.cy()) # cx, cy
# 将最大色块的中心坐标加在传输数组data[]后
data.append( (max_blob.cx(),max_blob.cy()) )
#{(1,22),(-3,33),(22222,0),(9999,12),(0,0)}
data_out = json.dumps(set(data)) # ujson.dumps(obj):返回表示为JSON字符串的 obj 。
B=1 # set() 函数创建一个无序不重复元素集
return max_blob.cx()
#uart.write(data_out +'\n')
def y(D,B):
while(B>2):
clock.tick() # Track elapsed milliseconds between snapshots().
img = sensor.snapshot() # Take a picture and return the image.
blobs = img.find_blobs([yellow_threshold])
if blobs:
print('视野中的色块数sum : %d'% len(blobs))
data=[]
# 寻找视野中最大的色块
max_blob=find_max(blobs)
# 框出最大的色块,并在中心画十字
img.draw_rectangle(max_blob.rect()) # rect
img.draw_cross(max_blob.cx(), max_blob.cy()) # cx, cy
# 将最大色块的中心坐标加在传输数组data[]后
data.append( (max_blob.cx(),max_blob.cy()) )
#{(1,22),(-3,33),(22222,0),(9999,12),(0,0)}
data_out = json.dumps(set(data)) # ujson.dumps(obj):返回表示为JSON字符串的 obj 。
B=1
# set() 函数创建一个无序不重复元素集
return max_blob.cy()
def zhua(): #机器臂抓取
uart.write('h')
if uart.any():
D=uart.readline()
if D:
uart.write('j')
if uart.any():
D=uart.readline()
if D:
uart.write('e')
return
def qi(): #机器臂起
uart.write('g')
if uart.any():
D=uart.readline()
if D:
uart.write('y')
if uart.any():
D=uart.readline()
if D:
uart.write('e')
return
def fang(): #放物体
uart.write('h')
if uart.any():
D=uart.readline()
if D:
uart.write('j')
if uart.any():
D=uart.readline()
if D:
uart.write('f')
return
def chu(): #机器臂初始定义
uart.write('g')
if uart.any():
D=uart.readline()
if D:
uart.write('y')
if uart.any():
D=uart.readline()
if D:
uart.write('f')
return
def go(B): #寻找到要找的物块后
C=B
uart.write('s')
if x(B,3)<45: #右转
uart.write('b')
if x(B,3)>45 and x(B,3)<55:
uart.write('s')
if x(B,3)>55: #左转
uart.write('c')
if x(B,3)>45 and x(B,3)<55:
uart.write('s')
if y(B,3)<75: #前进
uart.write('a')
if y(B,3)>75 and y(B,3)<95:
uart.write('s')
if y(B,3)>95: #后退
uart.write('d')
if y(B,3)>75 and y(B,3)<95:
uart.write('s')
if x(B,3)>45 and x(B,3)<55:
if y(B,3)<75 and y(B,3)>95:
zhua()
else:
go(C)
return
while(a>2): #不断拍照
clock.tick()
img = sensor.snapshot().lens_corr(1.8) #拍摄一张照片,lens_corr函数用于非鱼眼畸变矫正,默认设置参数为1.8,
for blob in img.find_blobs(thresholds,pixels_threshold=200,roi = (100,80,600,440),area_threshold=200):
#openmv自带的寻找色块函数。
#pixels_threshold是像素阈值,面积小于这个值的色块就忽略
#roi是感兴趣区域,只在这个区域内寻找色块
#are_threshold是面积阈值,如果色块被框起来的面积小于这个值,会被过滤掉
print('该形状占空比为',blob.density())
uart.write('b')
if blob.density()>0.705:
uart = UART(3, 9600)
a=1
sensor.reset() #初始化设置
sensor.set_pixformat(sensor.RGB565) #设置为彩色
sensor.set_framesize(sensor.SVGA) #设置清晰度
sensor.skip_frames(time = 2000) #跳过前2000ms的图像
sensor.set_auto_gain(False) # must be turned off for color tracking
sensor.set_auto_whitebal(False) # must be turned off for color tracking
clock = time.clock() #创建一个clock便于计算FPS,看看到底卡不卡
sensor.set_auto_gain(False) # 关闭自动自动增益。默认开启的。
sensor.set_auto_whitebal(False) #关闭白平衡。在颜色识别中,一定要关闭白平衡。
# 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.
sensor.skip_frames(10) # Let new settings take affect.
sensor.set_auto_whitebal(False) # turn this off.
clock = time.clock() # Tracks FPS.
a=3
while(a>2): #不断拍照
clock.tick()
img = sensor.snapshot().lens_corr(1.8) #拍摄一张照片,lens_corr函数用于非鱼眼畸变矫正,默认设置参数为1.8,
for blob in img.find_blobs(thresholds,pixels_threshold=200,roi = (100,80,600,440),area_threshold=200):
#openmv自带的寻找色块函数。
#pixels_threshold是像素阈值,面积小于这个值的色块就忽略
#roi是感兴趣区域,只在这个区域内寻找色块
#are_threshold是面积阈值,如果色块被框起来的面积小于这个值,会被过滤掉
print('该形状占空比为',blob.density())
uart.write('b')
if blob.density()>0.805:
uart = UART(3, 9600)
a=1
while(a>2): #不断拍照
clock.tick()
img = sensor.snapshot().lens_corr(1.8) #拍摄一张照片,lens_corr函数用于非鱼眼畸变矫正,默认设置参数为1.8,
for blob in img.find_blobs(thresholds,pixels_threshold=200,roi = (100,80,600,440),area_threshold=200):
#openmv自带的寻找色块函数。
#pixels_threshold是像素阈值,面积小于这个值的色块就忽略
#roi是感兴趣区域,只在这个区域内寻找色块
#are_threshold是面积阈值,如果色块被框起来的面积小于这个值,会被过滤掉
print('该形状占空比为',blob.density())
uart.write('b')
if blob.density()>0.705:
uart = UART(3, 9600)
a=1
go(C)143 行,加一句
global uart
@kidswong999 在 [local variable referenced before assignment\.](/post/27162) 中说:
> global uart
好的,十分感谢。