import sensor, image, time,pyb,math, struct
from pyb import LED,Timer
from pyb import UART
uart = UART(3,500000)#初始化串口 波特率 500000
BlackPole=(0, 17, -26, 39, -11, 40)
sensor.reset() # Initialize the camera sensor.
sensor.set_pixformat(sensor.RGB565) # use RGB565.
sensor.set_framesize(sensor.QQVGA) # use QQVGA for speed.
sensor.set_windowing((70,0,40,120))
sensor.skip_frames(10) # Let new settings take affect.
sensor.set_auto_whitebal(False) # turn this off.
clock = time.clock() # Tracks FPS.
led = pyb.LED(3) # Red LED = 1, Green LED = 2, Blue LED = 3, IR LEDs = 4.
class Dot(object):
flag = 0 #杆检测标志 1代表红色 2代表绿色
black_cnt = 0
Dot=Dot()
class Receive(object):
uart_buf = []
mode = 0
R=Receive()
#读取串口缓存
def UartReadBuffer():
i = 0
Buffer_size = uart.any()
while i<Buffer_size:
R.uart_buf.append(uart.readchar())
print("found: Modeflag=",R.uart_buf[0]) #从单片机发给openmv的数据是50
if (R.uart_buf[0]==32):
R.mode=1
if (R.uart_buf[0]==30):
Dot.black_cnt=1
i = i + 1
#串口发送函数
def UartSendData(Data):
uart.write(Data)
#杆检测数据打包
def DotDataPack(flag):
#print("found: flag=",flag)
pack_data=bytearray([flag])
return pack_data
#色块识别函数
#定义函数:找到画面中最大的指定色块
def FindMax(blobs):
max_size=1
if blobs:
max_blob = 0
for blob in blobs:
blob_size = blob.w()*blob.h()
if ( (blob_size > max_size) & (blob_size > 100) ) :#& (blob.density()<1.2*math.pi/4) & (blob.density()>0.8*math.pi/4)
if ( math.fabs( blob.w() / blob.h() - 1 ) < 2.0 ) :
max_blob=blob
max_size = blob.w()*blob.h()
return max_blob
def LineFilter(src, dst):
for i in range(0, len(dst), 1):
dst[i] = src[i<<1]
while(True):
UartReadBuffer()
clock.tick() # Track elapsed milliseconds between snapshots().
img = sensor.snapshot(line_filter = LineFilter)#拍一张图像
Black_blobs = img.find_blobs([BlackPole], pixels_threshold=3, area_threshold=3, merge=True, margin=5)#识别红色物体
max_blob = FindMax(Black_blobs)#找到最大的黑色色块那个
if (max_blob) and (R.mode==1):
img.draw_cross(max_blob.cx(), max_blob.cy())#物体中心画十字
img.draw_rectangle(max_blob.rect())#画圈
LED(3).toggle()
if(Dot.black_cnt==0):#第一次黑色色块识别
Dot.flag =33
if(Dot.black_cnt==1):#第二次黑色色块识别
Dot.flag =34
#print(Dot.flag)
UartSendData(DotDataPack(Dot.flag))从单片机发给openmv的数据是50
报错:MemoryError: memory allocation failed, allocating 131072 bytes
你用的是什么硬件?
@kidswong999 smt32f407
@ghbd 使用OpenMV4 H7
我们只解决官方正版的OpenMV的问题(STM32),其他的分支有很多兼容问题,我们无法解决。