对于一个根据模型识别物体的脚本,见到了好几种写法,差点把我弄晕了
比如有以下两种写法:
if __name__ == "__main__":
(# if name == ‘main’: 下的代码只有在第一种情况下(即文件作为脚本直接执行)才会被执行,而 import 到其他脚本中是不会被执行的。)中调用main,在main函数中封装物体识别代码的,还加了一堆try,expect非常健壮所以,这里简单整理一下识别的基本调用函数,主要是围绕kpu函数进行的
task = kpu.load(0x500000)
,使用之前需要先使用kflash_gui软件将smodel文件或kfpkg文件烧录到指定地址上task = kpu.load("/sd/m.kmodel")
,使用前需要先将模型拷贝到sd卡中_ = kpu.init_yolo2(task, 0.5, 0.3, 5, anchor)
,task在上一步生成,anchor是每一个模型唯一的数值数组,在训练模型下载下来的boot代码中有这个数组信息img = sensor.snapshot()
objects = kpu.run_yolo2(task, img)
在相机拍到的图像上跑模型,如果相似放到objectsif objects:
,整个方框把物体框起来for obj in objects:
每个obj含有三个参数:置信度confidence = float(obj.value())
范围itemROL = obj.rect()
对应物体的label名所在位置(比如classes = [‘unmask’,‘masks’]中0或1)classID = int(obj.classid())
,可以根据置信度调整识别结果,范围画框框,根据id显示判断结果最后附上一个没用if __name__ == 'main':
写法的识别口罩的例子:
import sensor, image, lcd, time
import KPU as kpu
color_R = (255, 0, 0)
color_G = (0, 255, 0)
color_B = (0, 0, 255)
class_IDs = ['no_mask', 'mask']
def drawConfidenceText(image, rol, classid, value):
text = ""
_confidence = int(value * 100)
if classid == 1:
text = 'mask: ' + str(_confidence) + '%'
else:
text = 'no_mask: ' + str(_confidence) + '%'
image.draw_string(rol[0], rol[1], text, color=color_R, scale=2.5)
lcd.init()
sensor.reset(dual_buff=True)
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.set_hmirror(0)
sensor.run(1)
task = kpu.load(0x500000)
anchor = (0.1606, 0.3562, 0.4712, 0.9568, 0.9877, 1.9108, 1.8761, 3.5310, 3.4423, 5.6823)
_ = kpu.init_yolo2(task, 0.5, 0.3, 5, anchor)
img_lcd = image.Image()
clock = time.clock()
while (True):
clock.tick()
img = sensor.snapshot()
code = kpu.run_yolo2(task, img)
if code:
totalRes = len(code)
for item in code:
confidence = float(item.value())
itemROL = item.rect()
classID = int(item.classid())
if confidence < 0.52:
_ = img.draw_rectangle(itemROL, color=color_B, tickness=5)
continue
if classID == 1 and confidence > 0.65:
_ = img.draw_rectangle(itemROL, color_G, tickness=5)
if totalRes == 1:
drawConfidenceText(img, (0, 0), 1, confidence)
else:
_ = img.draw_rectangle(itemROL, color=color_R, tickness=5)
if totalRes == 1:
drawConfidenceText(img, (0, 0), 0, confidence)
_ = lcd.display(img)
print(clock.fps())
_ = kpu.deinit(task)