logo

色块可以成功识别,但print不出我想要的数据,请问一下下面这段代码哪里有问题呢?

2019-07-03 11:08
M
isdz
# Untitled - By: HP - 周五 6月 7 2019

import sensor,time,pyb,math,image
import json
from pyb import Pin,Timer,LED,UART


uart_buf =bytearray([0x55,0x00,0x00,0x00,0x00,0xAA])
#设置发送串口3
uart = UART(3,9600)
uart.init(9600,bits=8,parity=None,stop=1)

x_error =0
y_error =0
x_Sign = 0
y_Sign = 0
def find_max(blobs):    #寻找最大色块函数定义
    max_pixels=0         #通过像素比较
    for blob in blobs:     #blob[0,1,2,3,4]=x,y,w,h,pixels (int)
         if blob[4]>max_pixels:
             max_blob=blob
             max_pixels=blob[4]
    return max_blob

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA) # we run out of memory if the resolution is much bigger...  图像像素大小QQVGA是160✖120
sensor.skip_frames(30)
sensor.set_auto_gain(False)  # must turn this off to prevent image washout...
sensor.set_auto_whitebal(False)  # must turn this off to prevent image washout...
sensor.set_auto_exposure(True)
clock = time.clock()

black = (0, 31, -34, 41, 24, -21)


while(True):

   clock.tick()

   img=sensor.snapshot()

   blobs = img.find_blobs([black])

   if blobs:
      max_blob=find_max(blobs)
      img.draw_rectangle(max_blob.rect())
      img.draw_cross(max_blob.cx(),max_blob.cy())
      x_error = int(max_blob.cx()-img.width()/2)
      y_error = int(max_blob.cy()-img.height()/2)
   else:
      x_error = 0
      y_error = 0

   if x_error >= 0:
      x_Sign = 0
   if x_error <=0:
      x_Sign = 1
   if y_error >= 0:
      y_Sign = 0
   if y_error <= 0:
      y_Sign = 1

x_abs=abs(x_error)
y_abs=abs(y_error)
print(x_Sign,x_abs,y_Sign,y_abs)
2019-07-03 11:46
I
网红小智智 星瞳科技

缩进不对,导致逻辑不对。

2019-07-03 15:49
M
isdz

嗯嗯嗯嗯解决啦,感谢!