logo

TypeError:can't convert tuple to int ?

2021-04-22 17:17
8
rphe
import sensor, image, time, math, pyb

from pyb import UART
from pyb import LED

red_led = LED(1)
green_led = LED(2)
blue_led = LED(3)

p = pyb.Pin("P0", pyb.Pin.IN)

global left,middle,right,VAL,flag_color
global color
global flag_color


left=0
middle=0
right=0

flag_color = 0

red_threshold = (15, 52, 38, 74, 3, 59), # generic_red_thresholds
green_threshold = (22, 48, -47, 22, 49, 13), # generic_green_thresholds
blue_threshold = (73, 28, -51, 0, -58, -15) # generic_blue_thresholds

red_color_code = 1
green_color_code = 2
blue_color_code = 4
black_color_code = 8


sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(10)
sensor.set_auto_whitebal(False)

clock = time.clock()

uart = UART(3, 115200)

#LED(4).on()                    #当光线弱的时候,用于补光
def Light():
    red_led.on()
    green_led.on()
    blue_led.on()

while(flag_color == 0):
    VAL = p.value()
    blue_led.on()
    print(VAL)

    if VAL == 0:
        flag_color = 1

    if flag_color == 1:
        blue_led.off()
        break
###############################################################################################
while(flag_color == 1):
    clock.tick()
    img = sensor.snapshot()
    img.draw_rectangle((5,55,50,73),color=(0x00,0x00,0x00))
    img.draw_rectangle((105,55,50,73),color=(0x00,0x00,0x00))
    img.draw_rectangle((205,55,50,73),color=(0x00,0x00,0x00))   #三个识别区位置(根据储物位置和摄像头位置调整)

    blob_left = img.find_blobs([red_threshold,green_threshold,blue_threshold],area_threshold=200, merge=True,roi=(5,55,50,73))
    blob_middle = img.find_blobs([red_threshold,green_threshold,blue_threshold],area_threshold=200, merge=True,roi=(105,55,50,73))
    blob_right = img.find_blobs([red_threshold,green_threshold,blue_threshold],area_threshold=200, merge=True,roi=(205,55,50,73))

    left=0
    middle=0
    right=0
    #####################   判断左边物块颜色
    if blob_left:
        for blob in blob_left:
            x = blob[0]
            y = blob[1]
            width = blob[2]
            height = blob[3]
            center_x = blob[5]
            center_y = blob[6]
            color_code = blob[8]
        # 添加颜色说明
            if color_code == red_color_code:
                img.draw_string(x, y - 10, "red", color = (0xFF, 0x00, 0x00))
                left = 1
            elif color_code == green_color_code:
                img.draw_string(x, y - 10, "green", color = (0x00, 0xFF, 0x00))
                left = 2
            elif color_code == blue_color_code:
                img.draw_string(x, y - 10, "blue", color = (0x00, 0x00, 0xFF))
                left = 3
            color=color_code
            img.draw_rectangle([x, y, width, height])
            img.draw_cross(center_x, center_y)
            #print("%d" % left)
            #uart.write("%d" % left)#串口通信
######################  中间物块颜色检测
    if blob_middle:
        for blob in blob_middle:
            x = blob[0]
            y = blob[1]
            width = blob[2]
            height = blob[3]
            center_x = blob[5]
            center_y = blob[6]
            color_code = blob[8]
            if color_code == red_color_code:
                img.draw_string(x, y - 10, "red", color = (0xFF, 0x00, 0x00))
                middle = 1
            elif color_code == green_color_code:
                img.draw_string(x, y - 10, "green", color = (0x00, 0xFF, 0x00))
                middle = 2
            elif color_code == blue_color_code:
                img.draw_string(x, y - 10, "blue", color = (0x00, 0x00, 0xFF))
                middle = 3
            color=color_code
            #length = 6943*math.sin(0.0001625*height+3.089)
            img.draw_rectangle([x, y, width, height])
            img.draw_cross(center_x, center_y)
            #print(length)
            #print("%d" % middle)
            #uart.write("%d" % middle)#串口通信
######################  右侧物块颜色检测
    if blob_right:
        for blob in blob_right:
            x = blob[0]
            y = blob[1]
            width = blob[2]
            height = blob[3]
            center_x = blob[5]
            center_y = blob[6]
            color_code = blob[8]
# 添加颜色说明
            if color_code == red_color_code:
                img.draw_string(x, y - 10, "red", color = (0xFF, 0x00, 0x00))
                right = 1
            elif color_code == green_color_code:
                img.draw_string(x, y - 10, "green", color = (0x00, 0xFF, 0x00))
                right = 2
            elif color_code == blue_color_code:
                img.draw_string(x, y - 10, "blue", color = (0x00, 0x00, 0xFF))
                right = 3
            color=color_code
            img.draw_rectangle([x, y, width, height])
            img.draw_cross(center_x, center_y)
            #print("%d" % right)
            #uart.write("%d" % right)#串口通信
            #time.sleep(10)
################################             将检测到的颜色发送                  #######################


            #elif(color_code==4):
                # print("blue")
                # command="b"
                # uart.write(command)
                # time.sleep(10)
    #print("%d" % left)
    #print("%d" % middle)
    #print("%d" % right)
            if left != 0 and right != 0 and left != right and left != middle and right != middle :
                if left == 1 and middle == 2 and right == 3 :
                    print("%d%d%d" % (left,middle,right))
                    #uart.write ("%d%d%d" % (left,middle,right))
                    uart.write('z')
                if left == 1 and middle == 3 and right == 2 :
                    print("%d%d%d" % (left,middle,right))
                    #uart.write ("%d%d%d" % (left,middle,right))
                    uart.write('x')
                if left == 2 and middle == 1 and right == 3 :
                    print("%d%d%d" % (left,middle,right))
                    #uart.write ("%d%d%d" % (left,middle,right))
                    uart.write('c')
                if left == 2 and middle == 3 and right == 1 :
                    print("%d%d%d" % (left,middle,right))
                    #uart.write ("%d%d%d" % (left,middle,right))
                    uart.write('v')
                if left == 3 and middle == 1 and right == 2 :
                    print("%d%d%d" % (left,middle,right))
                    #uart.write ("%d%d%d" % (left,middle,right))
                    uart.write('b')
                if left == 3 and middle == 2 and right == 1 :
                    print("%d%d%d" % (left,middle,right))
                    #uart.write ("%d%d%d" % (left,middle,right))
                    uart.write('n')
            if uart.any():                         #判断是否跳出当前循环
                tmp_date = uart.readline()
                red_led.on()
                print(tmp_date)
                if tmp_date == b'O':               #检测物块是否被接受
                    flag_color = 2
                    red_led.off()
                    break                          #已被接受,此函数结束
##########################           颜色检测与串口        #################
while(flag_color == 2):
    clock.tick()
    #Light()
    img0 = sensor.snapshot()
    img0.lens_corr(1.8)
    for QR in img0.find_qrcodes():
        code = QR.payload()
        img0.draw_rectangle(QR.rect(),color=(255,48,48))
        if code == '123':
            uart.write('z')
            print('123')
            red_led.on()
            red_led.off()
            flag_color = 3
            break
        elif code == '132':
            uart.write('x')
            red_led.on()
            red_led.off()
            print('123')
            flag_color = 3
            break
        elif code == '213':
            uart.write('c')
            red_led.on()
            red_led.off()
            print('213')
            flag_color = 3
            break
        elif code == '231':
            uart.write('v')
            red_led.on()
            red_led.off()
            print('231')
            flag_color = 3
            break
        elif code == '312':
            uart.write('b')
            red_led.on()
            red_led.off()
            print('312')
            flag_color = 3
            break
        elif code == '321':
            uart.write('n')
            red_led.on()
            red_led.off()
            print('321')
            flag_color = 3
            break
#########################           二维码扫描与串口            ###############

while (flag_color == 3):
    green_led.on()


#########################           完成任务之后          ##############
076124aa-188c-429b-9dcd-5e64810160f9.png
b2169a86-ff18-42c4-afb4-84d5c5e6e7dc.png
2021-04-22 19:53
8
rphe

@rphe 怎么修改这个错误

2021-04-25 16:10
I
网红小智智 星瞳科技

第23行和24行有问题。最后面多了逗号。