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

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

一、现象

 

上面这张图是 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)

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

# vision distance - By: SingTown - Wed Jul 30 2025 import sensor import time # 设置VGA画面,并裁剪中间的画面 sensor.reset() sensor.set_pixformat(sensor.GRAYSCALE) sensor.set_framesize(sensor.VGA) sensor.set_windowing((200,240)) sensor.skip_frames(time=2000) clock = time.clock() # Tracks FPS. CENTER_X = 200/2 CENTER_Y = 240//2 # 边框的真实长度,单位mm FRAME_WIDTH_MM = 176 FRAME_HIGHT_MM = 265 # 校准的数据 # 例如: 当距离是1400mm时,边框宽度为82像素 # DISTANCE_MM_1 = 1100 # FRAME_WIDTH_PIXEL_1 = 104 DISTANCE_MM_2 = 1400 FRAME_WIDTH_PIXEL_2 = 80 FRAME_HEIGHT_PIXEL_2 = 120 # DISTANCE_MM_3 = 1800 # FRAME_WIDTH_PIXEL_3 = 64 def find_center_min_blob(blobs): # 找中间最小的色块 blob = None min_area = 100000 for b in blobs: if abs(b.cx()-CENTER_X)+ abs(b.cy()-CENTER_Y) > 50: continue if b.area() > min_area: continue blob = b min_area = b.area() return blob def find_center_max_blob(blobs): # 找中间最大的色块 blob = None max_area = 0 for b in blobs: if abs(b.cx()-CENTER_X)+ abs(b.cy()-CENTER_Y) > 50: continue if b.area() < max_area: continue blob = b max_area = b.area() return blob while True: clock.tick() img = sensor.snapshot() # 找白色色块,黑色边框内部 frames = img.find_blobs([(150,256)]) frame_blob = find_center_min_blob(frames) if not frame_blob: print("NO FRAME") continue # 计算距离 distance = DISTANCE_MM_2*FRAME_HEIGHT_PIXEL_2/frame_blob.h() # 缩小roi,避免黑框的黑边 frame_roi = (frame_blob.x()+5, frame_blob.y()+5, frame_blob.w()-10, frame_blob.h()-10) if frame_roi[2] <= 0 or frame_roi[3] <= 0: print("ROI ERROR") continue print(frame_roi) # 找黑色色块,目标物体 objs = img.find_blobs([(0,150)],roi=frame_roi) obj_blob = find_center_max_blob(objs) if not obj_blob: print("NO OBJS") continue # 计算物体边长 obj_w_mm = obj_blob.w()/frame_blob.w()*FRAME_WIDTH_MM print(frame_blob.w(), frame_blob.h()) print(obj_blob.density()) if 0.9 < obj_blob.density() : print("矩形") elif 0.6 < obj_blob.density(): print("圆形") elif 0.4 < obj_blob.density(): print("三角形形") else: print("无法识别到形状") img.draw_string(10,10, "length:"+str(obj_w_mm)+"mm") img.draw_string(10,20, "distance:"+str(distance)+"mm") img.draw_rectangle(frame_blob.rect()) img.draw_rectangle(obj_blob.rect())这是openmv识别图形的代码,能不能改成k230能正常运行其功能的代码
最新发布
08-02
# 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
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值