yolo2_20class_awnn.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. from maix import nn, camera, image, display
  2. from maix.nn import decoder
  3. import time
  4. model = {
  5. "param": "/home/model/yolo2_20class_awnn.param",
  6. "bin": "/home/model/yolo2_20class_awnn.bin"
  7. }
  8. options = {
  9. "model_type": "awnn",
  10. "inputs": {
  11. "input0": (224, 224, 3)
  12. },
  13. "outputs": {
  14. "output0": (7, 7, (1+4+20)*5)
  15. },
  16. "mean": [127.5, 127.5, 127.5],
  17. "norm": [0.0078125, 0.0078125, 0.0078125],
  18. }
  19. labels = ["aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"]
  20. anchors = [5.4, 5.38, 1.65, 2.09, 0.8, 1.83, 2.45, 4.14, 0.46, 0.8]
  21. m = nn.load(model, opt=options)
  22. yolo2_decoder = decoder.Yolo2(len(labels), anchors, net_in_size=(options["inputs"]["input0"][0], options["inputs"]["input0"][1]), net_out_size=(7, 7))
  23. while True:
  24. img = camera.capture()
  25. AI_img = img.copy().resize(224, 224)
  26. out = m.forward(AI_img.tobytes(), quantize=True, layout="hwc")
  27. boxes, probs = yolo2_decoder.run(out, nms=0.3, threshold=0.3, img_size=(options["inputs"]["input0"][0], options["inputs"]["input0"][1]))
  28. if len(boxes):
  29. for i, box in enumerate(boxes):
  30. class_id = probs[i][0]
  31. prob = probs[i][1][class_id]
  32. disp_str = "{}:{:.2f}%".format(labels[class_id], prob*100)
  33. img.draw_rectangle(box[0], box[1], box[0] + box[2], box[1] + box[3], color = (0, 255, 0), thickness=2)
  34. x = box[0]
  35. y = box[1] - 20
  36. if y < 0:
  37. y = 0
  38. img.draw_string(x, y, disp_str, 1.5, color = (255, 0, 0), thickness=2)
  39. display.show(img)