用户使用 OpenMV H7 Plus 运行基于 Edge Impulse 训练的 FOMO 目标检测模型,已成功识别草莓并输出其中心坐标(x, y)。当前目标是实现机械臂自动采摘,需精确定位草莓梗(果柄)位置,而非仅草莓果实中心。
for x, y, w, h, score 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])import sensor, image, time, ml, math, uos, gc
sensor.reset() # Reset and initialize the sensor. sensor.set_pixformat(sensor.RGB565) # 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
try: # load the model, alloc the model file on the heap if we have at least 64K free after loading net = ml.Model("trained.tflite", load_to_fb=uos.stat('trained.tflite')[6] > (gc.mem_free() - (64*1024))) 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('\n') for line in open("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):
# 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, ml, math, uos, gc
sensor.reset() # Reset and initialize the sensor.
sensor.set_pixformat(sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.QQVGA) # 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
# 列出文件目录及文件信息
print("\nFiles on SD card:")
for f in uos.listdir():
temp = uos.stat(f)
print("file: %s, size: %u bytes" % (f, temp[6]))
print("")
gc.collect()
labels = [line.rstrip('\n') for line in open("labels.txt")]
print(labels)
net = ml.Model("trained.tflite", load_to_fb=True)
print(net)
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):
# Slice the output and multiply by 255.0 to ensure float arithmetic
# OpenMV image.Image() from ndarray expects simple memory layout and float32 usually
img_data = outputs[0][0, :, :, i] * 255.0
img = image.Image(img_data)
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.predict([img], callback=fomo_post_process)):
if i == 0: continue # background class
if len(detection_list) == 0: continue # no detections for this class?
print("********** %s **********" % labels[i])
for x, y, w, h, score 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")
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 lclock = time.clock() while(True): clock.tick()
img = sensor.snapshot()
for i, detection_list in enumerate(net.predict([img], callback=fomo_post_process)):
if i == 0: continue # background class
if len(detection_list) == 0: continue # no detections for this class?
print("********** %s **********" % labels[i])
for x, y, w, h, score 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")
你最终需要的是机械臂抓取点的三维空间坐标。具体可以参考:OpenMV机械臂-项目实例 · OpenMV中文入门教程
我现在得到了草莓x y的坐标,有没有教程得到z轴坐标
最大的问题是怎么识别草莓梗,得到草莓梗的坐标

还得是识别成熟草莓的梗
有木有办法🌹🌹
Z坐标得你自己解决,如加传感器什么的,OpenMV仅能反馈X,Y
识别成熟加入成熟和不成熟的数据集即可
识别草莓梗有什么办法吗😵💫
如果成熟草莓和未成熟草莓以及草莓柄都识别,那怎么确保我得到成熟草莓柄的坐标

怎么区分成熟草莓的柄和未未成熟草莓的柄🌹🌹
为什么要识别柄呢?得到了草莓的位置不就等于得到了柄的坐标吗?
我要根据坐标控制机械臂上的剪刀去剪草莓柄,只识别草莓不就直接怼草莓上了?得到根据草莓位置得到柄的坐标?😵💫🌹🌹
我要根据坐标控制机械臂上的剪刀去剪草莓柄,只识别草莓不就直接怼草莓上了?得到根据草莓位置得到柄的坐标?😵💫🌹🌹
得到草莓坐标后,然后再此坐标上做一些偏移不就可以了?柄的样子都一模一样,神经网络也分辨不出来,只能依靠草莓的坐标去做处理啊