K210_set_windowing(roi)_ROI列表无法设置x和y的问题

文章讲述了使用MaixbitK210摄像头时遇到的问题,即set_windowing函数中的ROI设置无效。解决方法包括物理调整摄像头位置和编程方式,如使用copy_to_fb和调整内存管理以减少图像变量。作者还讨论了预处理图像和不同图像格式对内存的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、现象

 

上面这张图是 320*240的原图数据

摄像头得到的QVGA/或其他更大的像素格式 的图像

 

上面这张是 set_windowing((224,224))

set_windowing((224,224)) 会导致 取到的是 左上角224*224的图像

ROI = (x,y,w,h)=(0,0,224,224)

根据set_windowing函数的声明,这时候通过设置ROI的x和y值 使 感兴趣区域 变为摄像头得到1的图像的中央

然而实际没有效果

结果搜索和技术支持的询问——maixbit自带的摄像头 的驱动 不支持 set_windowing函数中对ROI的x,y进行设置

二、解决办法

(一)物理解决办法,也是我在网上看到的大部分办法

将 k210摄像头的位置放在小车/云台中线偏右的地方 , 使模型的输入为小车正对着的数字

但是显然 误差很大

而且这样子能看到的范围很小,只能镜头的左上角看小车的正前方,而镜头右边的视野完全被set_window切割了

导致 小车在识别4个数字上 k210性能是足够识别4个,但是还是得一左一右两个k210来识别

(二)代码解决,网上没有,严重怀疑是我初创( )让我慰藉一下电赛前1.5个月都花在这个k210上面

(1)一开始的思路——除了set_windowing(roi),还有什么函数可以用

原则——我们是为了 预处理摄像头的图像 从而使模型的输入图像 是224*224 且是摄像头正对的图像,以摄像头为中线

img.copy(roi = (48,8,224,224))也可以设置输入图像的在摄像头源图像上的ROI

但是两者差别在哪呢?

set_windowing是sensor模块下的函数——即与摄像头有关,处理的是摄像头的图像

copy函数是ide带的对图像进行处理的函数——然而这里图像是由摄像头处理后的得到的

这有什么区别呢?

涉及到k210的存储系统

分为RGB565和888一个是pix格式,一个是给kpu处理的图像数据格式

然而如果是这样子写,会发现程序会在此处终止,结束执行,甚至k210会与ide断链

直接原因是 内存爆了

根本原因是 在图像处理的路上面 多了一个图片变量 而k210的内存本来就小,才8M吧

摄像头的照片 没试过能不能装在SD卡的区域内

但是没装sd卡也可以 处理摄像头照片且跑模型

解决办法就是 让这个路上只要有一个图像变量就行

    img = sensor.snapshot()
    img.copy(roi = (48,8,224,224),copy_to_fb = img)
    img.pix_to_ai()

#然后在输入图像给模型
    objects = kpu.run_yolo2(task, img)

后续还可以 通过 灰度图 来增快帧率

# Edge Impulse - OpenMV FOMO Object Detection Example # # This work is licensed under the MIT license. # Copyright (c) 2013-2024 OpenMV LLC. All rights reserved. # https://github.com/openmv/openmv/blob/master/LICENSE import sensor, image, time, tf, math, uos, gc, os sensor.reset() # Reset and initialize the sensor. sensor.set_pixformat(sensor.GRAYSCALE) # Set pixel format to RGB565 (or GRAYSCALE) sensor.set_framesize(sensor.QVGA) # Set frame size to QVGA (320x240) sensor.set_windowing((240, 240)) # Set 240x240 window. sensor.skip_frames(time=2000) # Let the camera adjust. net = None labels = None min_confidence = 0.5 numfind_net_pathtp = "trained.tflite" try: # load the model, alloc the model file on the heap if we have at least 64K free after loading net = tf.load(numfind_net_pathtp, load_to_fb=True) except Exception as e: raise Exception('Failed to load "trained.tflite", did you copy the .tflite and labels.txt file onto the mass-storage device? (' + str(e) + ')') try: labels = [line.rstrip() for line in open("/sd/labels.txt")] except Exception as e: raise Exception('Failed to load "labels.txt", did you copy the .tflite and labels.txt file onto the mass-storage device? (' + str(e) + ')') colors = [ # Add more colors if you are detecting more than 7 types of classes at once. (255, 0, 0), ( 0, 255, 0), (255, 255, 0), ( 0, 0, 255), (255, 0, 255), ( 0, 255, 255), (255, 255, 255), ] threshold_list = [(math.ceil(min_confidence * 255), 255)] def fomo_post_process(model, inputs, outputs): ob, oh, ow, oc = model.output_shape[0] x_scale = inputs[0].roi[2] / ow y_scale = inputs[0].roi[3] / oh scale = min(x_scale, y_scale) x_offset = ((inputs[0].roi[2] - (ow * scale)) / 2) + inputs[0].roi[0] y_offset = ((inputs[0].roi[3] - (ow * scale)) / 2) + inputs[0].roi[1] l = [[] for i in range(oc)] for i in range(oc): img = image.Image(outputs[0][0, :, :, i] * 255) blobs = img.find_blobs( threshold_list, x_stride=1, y_stride=1, area_threshold=1, pixels_threshold=1 ) for b in blobs: rect = b.rect() x, y, w, h = rect score = ( img.get_statistics(thresholds=threshold_list, roi=rect).l_mean() / 255.0 ) x = int((x * scale) + x_offset) y = int((y * scale) + y_offset) w = int(w * scale) h = int(h * scale) l[i].append((x, y, w, h, score)) return l clock = time.clock() while(True): clock.tick() img = sensor.snapshot() for i, detection_list in enumerate(net.detect(img,thresholds[(math.ceil(min_confidence*255),255)])): if i == 0: continue # background class if len(detection_list) == 0: continue # no detections for this class? print("********** %s **********" % labels[i]) for d in detection_list: center_x = math.floor(x + (w / 2)) center_y = math.floor(y + (h / 2)) #print(f"x {center_x}\ty {center_y}\tscore {score}") img.draw_circle((center_x, center_y, 12), color=colors[i]) print(clock.fps(), "fps", end="\n\n") AttributeError: 'tf_model' object has no attribute 'detect'
07-06
THRESHOLD = (0, 23, -128, 127, -128, 127) # Grayscale threshold for dark things... import sensor, image, time from pyb import LED from machine import UART import struct sensor.reset() sensor.set_vflip(False) # 设置OpenMV图像“水平方向进行翻转” sensor.set_hmirror(False) # 设置OpenMV图像“竖直方向进行翻转” sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QQQVGA) # 80x60 (4,800 pixels) - O(N^2) max = 2,3040,000. # 线性回归算法的运算量大,越小的分辨率识别的效果越好,运算速度越快 #sensor.set_windowing([0,20,80,40]) sensor.skip_frames(time = 2000) # WARNING: If you use QQVGA it may take seconds clock = time.clock() # to process a frame sometimes. myuart = UART(1, 115200) # UART(1)是P0-RX P1-TX myuart.init(115200, bits=8, parity=None, stop=1) #8位数据位,无校验位,1位停止位 def send_data_packet(x, y): temp = struct.pack(">bbii", #格式为小端模式俩个字符俩个整型 0xAA, #帧头1 0xBB, #帧头2 int(x), # up sample by 4 #数据1 int(y)) # up sample by 4 #数据2 myuart.write(temp) #串口发送 while(True): clock.tick() img = sensor.snapshot().binary([THRESHOLD]) ''' 截取一张图片,进行 “阈值分割” 阈值分割函数image.binary()对图像进行二值化(binary:二元的;由两部分组成的) 得到的效果是:将阈值颜色变成白色,非阈值颜色变成黑色''' line = img.get_regression([(100,100)], robust = True)#调用线性回归函数 # 对所有的阈值像素进行线性回归 # 线性回归的效果就是将我们视野中“二值化”分割后的图像回归成一条直线 if (line): ####### rho_err = abs(line.rho())-img.width()/2 # 计算我们的直线相对于中央位置偏移的距离(偏移的像素) # abs()函数:返回数字的绝对值 line.rho():返回霍夫变换后的直线p值。 if line.theta()>90: theta_err = line.theta()-180 else: theta_err = line.theta() ######### # 进行坐标的变换:y轴方向为0°,x轴正方向为90°,x轴负方向为-90° img.draw_line(line.line(), color = 127) print(rho_err,line.magnitude(),theta_err) #line.magnitude()返回一个表示“线性回归效果”的值,这个值越大,线性回归效果越好; # 如果越接近于0,说明我们的线性回归效果越接近于一个圆,效果越差 if line.magnitude()>8: send_data_packet(rho_err,theta_err) LED(1).off() else: LED(1).on() LED(2).off() else: LED(2).on() pass #print(clock.fps())(这个是openmv的巡线代码,如果是用k210,应该对代码做哪些改动?)
最新发布
07-31
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值