logo

我的代码放入openmv运行后镜头不开启,如果是开着的状态画面也会突然不动

2024-04-22 20:08
F
qgwi
import sensor, image, time, math
from pyb import UART
import json
import ustruct
red_threshold_01 = ((6, 62, 25, 71, 15, 47));
green_threshold_01 = ((31, 99, -22, -61, 12, 59));
blue_threshold_01 = ((73, 43, -49, 57, -82, -13));
threshold = [50, 50, 0, 0, 0, 0]
rec=5;
sign=0;
uart = UART(3,115200)
uart.init(115200, bits=8, parity=None, stop=1)
def sending_data_qr(code1,code2):
    global uart;
    data = ustruct.pack("<bbhhb",
                   0x2C,
                   0x12,
                   int(code1),
                   int(code2),
                   0x5B)
    uart.write(data);
def sending_data_sign(sign):
    global uart;
    data = ustruct.pack("<bbhb",
                   0x2C,
                   0x12,
                   int(sign),
                   0x5B)
    uart.write(data);
def sending_data(cx):
    global uart;
    data = ustruct.pack("<bbhb",
                   0x2C,
                   0x12,
                   int(cx),
                   0x5B)
    uart.write(data);
def recive_data():
    global uart
    global rec
    if uart.any():
        rec = uart.readline();
        print( rec)
def find_max(blobs):
    max_size=0
    for blob in blobs:
        if blob.pixels() > max_size:
            max_blob=blob
            max_size = blob.pixels()
    return max_blob
while(1):
    if(rec==b'\x01'):
        sensor.reset()
        sensor.set_pixformat(sensor.RGB565)
        sensor.set_framesize(sensor.QVGA)
        sensor.skip_frames(30)
        sensor.set_auto_gain(False)
        while(rec==b'\x01'):
            img = sensor.snapshot()
            img.lens_corr(1.8)
            for code in img.find_qrcodes():
                if( len ( list (code.payload()))==7):
                    tem = list (code.payload());
                    print(tem);
                    qr1=tem[0]+tem[1]+tem[2];
                    qr2=tem[4]+tem[5]+tem[6];
                    print(qr1);
                    print(qr2);
                    sending_data_qr(qr1,qr2);
            recive_data();
    elif(rec==b'\x05'):
        sensor.reset()
        sensor.set_pixformat(sensor.RGB565)
        sensor.set_framesize(sensor.QVGA)
        sensor.skip_frames(30)
        sensor.set_auto_gain(False)
        while(rec==b'\x05'):
            img = sensor.snapshot()
            img.lens_corr(1.8)
            for code in img.find_qrcodes():
                if( len ( list (code.payload()))==4):#判断扫码得到的是不是四位字符
                    tem = list (code.payload());#扫码得到的四位字符列表
                    #print(tem);
                    if(tem[0]=='红'):
                        if(tem[2]=='绿'):
                            qr1=12;
                        elif(tem[2]=='蓝'):
                            qr1=13;
                    if(tem[0]=='绿'):
                        if(tem[2]=='红'):
                            qr1=21;
                        elif(tem[2]=='蓝'):
                            qr1=23;
                    if(tem[0]=='蓝'):
                        if(tem[2]=='红'):
                            qr1=31;
                        elif(tem[2]=='绿'):
                            qr1=32;
                    print(qr1);
                    sending_data_qr(qr1,qr1);
            recive_data();
    elif(rec==b'\x04'):
        sensor.reset()
        sensor.set_pixformat(sensor.RGB565)
        sensor.set_framesize(sensor.QVGA)
        sensor.skip_frames(time = 2000)
        sensor.set_auto_gain(False)
        sensor.set_auto_whitebal(False)
        clock = time.clock()
        while(rec==b'\x04'):
            clock.tick()
            img = sensor.snapshot()
            blobs = img.find_blobs([red_threshold_01], pixels_threshold=100, area_threshold=100, merge=True, margin=10);
            blobs1 = img.find_blobs([green_threshold_01], pixels_threshold=100, area_threshold=100, merge=True, margin=10);
            blobs2 = img.find_blobs([blue_threshold_01], pixels_threshold=100, area_threshold=100, merge=True, margin=10);
            cx=0;cy=0;cx1=0;cy1=0;cx2=0;cy2=0;
            if blobs:
                max_b = find_max(blobs);
                img.draw_rectangle(max_b[0:4])
                img.draw_cross(max_b[5], max_b[6])
                img.draw_cross(160, 120)
                cx=max_b[5];
                cy=max_b[6];
                img.draw_line((160,120,cx,cy), color=(127));
                img.draw_string(cx, cy, "(%d, %d)"%(cx,cy), color=(127));
                sign=sign+1;
            elif blobs1:
                    max_b = find_max(blobs1);
                    img.draw_rectangle(max_b[0:4])
                    img.draw_cross(max_b[5], max_b[6])
                    img.draw_cross(160, 120)
                    cx1=max_b[5];
                    cy1=max_b[6];
                    img.draw_line((160,120,cx1,cy1), color=(127));
                    img.draw_string(cx1, cy1, "(%d, %d)"%(cx1,cy1), color=(127));
                    sign=sign+2;
            elif blobs2:
                    max_b = find_max(blobs2);
                    img.draw_rectangle(max_b[0:4])
                    img.draw_cross(max_b[5], max_b[6])
                    img.draw_cross(160, 120)
                    cx2=max_b[5];
                    cy2=max_b[6];
                    img.draw_line((160,120,cx2,cy2), color=(127));
                    img.draw_string(cx2, cy2, "(%d, %d)"%(cx2,cy2), color=(127));
                    sign=sign+3;
            print(sign);
            sending_data_sign(sign);
            sign=0;
            recive_data();
    else :
        recive_data();
2ae4a116-6646-4ce6-911f-b259639e58ec.png