9 Object detection9.1 experimental goals9.2 Preparation before the experiment9.3 experimental procedure9.4 experimental results9.5 experiment summary
This lesson focuses on the object detection function, analyzes the picture captured by the camera, and frames and displays the object type name if it matches the object type in the model.
The reference code path for this experiment is :CanMV\05-AI\voc20_object_detect.py
Please first import the model file into the memory card, and then insert the memory card into the memory card slot of the K210 module.
The factory firmware of the module has integrated the AI vision algorithm module. If you have downloaded other firmware, please burn it back to the factory firmware before doing the experiment.
ximport sensor, image, time, lcd
from maix import KPU
lcd.init()
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 100)
clock = time.clock()
xxxxxxxxxx
obj_name = ("aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor")
xxxxxxxxxx
od_img = image.Image(size=(320,256))
anchor = (1.3221, 1.73145, 3.19275, 4.00944, 5.05587, 8.09892, 9.47112, 4.84053, 11.2364, 10.0071)
kpu = KPU()
print("ready load model")
kpu.load_kmodel("/sd/KPU/voc20_object_detect/voc20_detect.kmodel")
kpu.init_yolo2(anchor, anchor_num=5, img_w=320, img_h=240, net_w=320 , net_h=256 ,layer_w=10 ,layer_h=8, threshold=0.7, nms_value=0.2, classes=20)
xxxxxxxxxx
while True:
clock.tick()
img = sensor.snapshot()
od_img.draw_image(img, 0,0)
od_img.pix_to_ai()
kpu.run_with_output(od_img)
dect = kpu.regionlayer_yolo2()
fps = clock.fps()
if len(dect) > 0:
print("dect:",dect)
for l in dect :
img.draw_rectangle(l[0],l[1],l[2],l[3], color=(0, 255, 0))
img.draw_string(l[0],l[1], obj_name[l[4]], color=(0, 255, 0), scale=1.5)
img.draw_string(0, 0, "%2.1ffps" %(fps), color=(0, 60, 128), scale=2.0)
lcd.display(img)
Connect the K210 module to the computer through the microUSB data cable, CanMV IDE click the connect button, after the connection is completed click the Run button to run the routine code. You can also download the code as main.py and run it in the K210 module.
After the system initialization is completed, the LCD displays the camera screen, shoots the object with the camera, the screen displays the name of the detected object, and the serial terminal at the bottom of the IDE prints information about the detected object.
Object detection requires a memory card to load the model file, so you need to import the model file into the memory card in advance, and then insert the memory card into the memory card slot of the K 210 module, if the model file in the memory card cannot be read, an error will be reported.
At present, the threshold value of detecting objects is threshold=0.7, and if the detection needs to be more accurate, the threshold can be adjusted appropriately.
Detecting objects can only detect objects in the variable 'obj name', other objects cannot be detected for the time being.