野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 559|回复: 0

立创·梁山派开发板-21年电赛F题-送药小车-K210功能实现代码...

[复制链接]
发表于 2023-8-1 09:52:47 | 显示全部楼层 |阅读模式
送药小车代码仓库:https://gitee.com/lcsc/medical_car
更好的观看体验请去:https://lceda001.feishu.cn/wiki/ZDYbwqDfCiwVlckUEcScF0KSnRh
送药小车立创开源平台资料:https://oshwhub.com/li-chuang-kai-fa-ban/21-dian-sai-f-ti-zhi-neng-song-yao-xiao-che

K210功能实现代码讲解
具体实现代码如下(在2_Code->application->sensor->k210->pyconde->main.py):

  1. # Hello World Example
  2. #
  3. # Welcome to the CanMV IDE! Click on the green run arrow button below to run the script!

  4. import sensor, image, time, lcd, struct, ustruct
  5. import gc

  6. from machine import UART,Timer,PWM
  7. from board import board_info
  8. from fpioa_manager import fm
  9. import KPU as kpu
  10. from Maix import GPIO,utils
  11. import gc
  12. import machine

  13. lcd.init()                          # Init lcd display
  14. lcd.clear(lcd.RED)                  # Clear lcd screen.

  15. sensor.reset(freq=22000000, dual_buff=1)                # 设置摄像头频率 24M 开启双缓冲模式 会提高帧率 但内存占用增加
  16. sensor.set_auto_exposure(0)         # 设置自动曝光
  17. sensor.set_auto_gain(False) # 颜色跟踪必须关闭自动增益
  18. sensor.set_auto_whitebal(False) # 颜色跟踪必须关闭白平衡
  19. sensor.set_pixformat(sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE)
  20. sensor.set_framesize(sensor.QVGA)   # Set frame size to QVGA (320x240)

  21. #kpu = KPU()
  22. #kpu.load_kmodel("/sd/KPU/mnist/uint8_mnist_cnn_model.kmodel")
  23. sensor.skip_frames(time = 2000)     # Wait for settings take effect.
  24. clock = time.clock()                # Create a clock object to track the FPS.
  25. sensor.set_auto_exposure(0)         # 设置自动曝光

  26. sensor.set_vflip(1)
  27. #______________________________________________________________________________________________________________________________
  28. #打印内存分配情况
  29. print(utils.gc_heap_size())
  30. print("stack mem:"+str(gc.mem_free() / 1024)) # stack mem
  31. print("heap mem:"+str(utils.heap_free() / 1024)) # heap mem

  32. #第一次用执行一次下面这两个语句
  33. #utils.gc_heap_size(800*1024)
  34. #machine.reset()

  35. #______________________________________________________________________________________________________________________________
  36. #程序运行选择
  37. is_need_debug = 0
  38. is_patrol_line = 0
  39. is_upacker_debug = 0
  40. is_stack_heap_mem_debug = 0
  41. is_upacker_recive_debug = 1
  42. is_findflob_debug = 0

  43. #______________________________________________________________________________________________________________________________
  44. #程序运行状态
  45. work_mode = 0  #0是巡线模式,1是数字识别模式

  46. #______________________________________________________________________________________________________________________________
  47. #串口配置区

  48. fm.register(6, fm.fpioa.UART1_TX, force=True)
  49. fm.register(7, fm.fpioa.UART1_RX, force=True)

  50. k210_uart = UART(UART.UART1, 115200, 8, 0, 0, timeout=1000, read_buf_len=4096)

  51. uart_test_write_str = '1260808878'

  52. #______________________________________________________________________________________________________________________________
  53. #按键蜂鸣器配置区
  54. #注册IO,注意高速GPIO口才有中断
  55. fm.register(35, fm.fpioa.GPIO0)
  56. fm.register(16, fm.fpioa.GPIOHS0)
  57. #构建案件对象
  58. KEY=GPIO(GPIO.GPIOHS0, GPIO.IN, GPIO.PULL_UP)

  59. #PWM通过定时器配置,接到IO15引脚
  60. tim = Timer(Timer.TIMER1, Timer.CHANNEL0, mode=Timer.MODE_PWM)
  61. beep = PWM(tim, freq=1000, duty=0, pin=9)

  62. #按键标志位
  63. key_node = 0
  64. key_press_long = 0

  65. #中断回调函数
  66. def fun(KEY):
  67.     global work_mode,key_node,key_press_long
  68.     temp_count = 0

  69.     time.sleep_ms(10) #消除抖动
  70.     while KEY.value()== 0:
  71.         key_node = 1
  72.         time.sleep_ms(10) #长按延时
  73.         #长按检测计数
  74.         temp_count=temp_count+1
  75.         print(temp_count)
  76.     if temp_count >= 50:

  77.         beep.duty(50)
  78.         time.sleep_ms(500)
  79.         beep.duty(0)
  80.         time.sleep_ms(100)

  81.         print(temp_count)
  82.         key_node = 0
  83.         key_press_long = 1

  84. #开启中断,下降沿触发
  85. KEY.irq(fun, GPIO.IRQ_FALLING)

  86. #______________________________________________________________________________________________________________________________
  87. #要传给梁山派的数据

  88. #work_mode:0是巡线模式,1是数字识别模式
  89. #recognition:0是未知(巡线状态下),1是药房门口区域(停车线),十字路口和T字路口由

  90. class uart_send_data_t:
  91.    def __init__( self, work_mode=0, recognition=0,top_block_offset=0,center_block_offset=0,left_block_offset=0,right_block_offset=0,left_number = 0,right_number = 0):
  92.       self.work_mode = work_mode
  93.       self.recognition = recognition
  94.       self.top_block_offset = top_block_offset
  95.       self.center_block_offset = center_block_offset
  96.       self.left_block_offset = left_block_offset
  97.       self.right_block_offset = right_block_offset
  98.       self.left_number = left_number
  99.       self.right_number = right_number


  100. global_uart_send_data = uart_send_data_t()

  101. #______________________________________________________________________________________________________________________________
  102. #感兴趣区配置
  103. roi_area_width = 30
  104. roi_area_color = (0, 0, 200)
  105. roi_area_thickness = 2


  106. left_roi = [50,40,roi_area_width,sensor.height()-40]
  107. right_roi = [sensor.width()-roi_area_width-50,40,roi_area_width,sensor.height()-40]
  108. top_roi = [0,10,sensor.width(),roi_area_width]
  109. center_roi = [0,int(sensor.height()/2)-int(roi_area_width/2),sensor.width(),roi_area_width]
  110. #bottom_roi = [0,sensor.height()- roi_area_width,sensor.width(),roi_area_width]

  111. black_block_roi = [0,100,sensor.width(),100]

  112. def draw_roi(img):
  113.     img.draw_rectangle(left_roi, color = roi_area_color, thickness = roi_area_thickness, fill = False)
  114.     img.draw_rectangle(right_roi, color = roi_area_color, thickness = roi_area_thickness, fill = False)
  115.     img.draw_rectangle(top_roi, color = roi_area_color, thickness = roi_area_thickness, fill = False)
  116.     img.draw_rectangle(center_roi, color = roi_area_color, thickness = roi_area_thickness, fill = False)
  117.     img.draw_rectangle(black_block_roi, color = (0, 255, 0), thickness = roi_area_thickness, fill = False)
  118.     return

  119. #______________________________________________________________________________________________________________________________
  120. #寻找色块区配置
  121. red_threshold =[12, 68, 11, 63, 5, 81]
  122. black_threshold =[0, 50, -24,-1, -18, 6]

  123. class red_blob_location_t:
  124.    def __init__( self, x=0, y=0,area = 0):
  125.       self.x = x
  126.       self.y = y
  127.       self.area = area


  128. #______________
  129. #左部blob位置信息
  130. left_blob_location = red_blob_location_t()
  131. left_pixels_threshold = 20     #若一个色块的像素数小于 pixel_threshold ,则会被过滤掉。
  132. left_area_threshold = 20       #若一个色块的边界框区域小于 area_threshold ,则会被过滤掉。
  133. left_x_stride = 20               #是查找某色块时需要跳过的x像素的数量。找到色块后,直线填充算法将精确像素。 若已知色块较大,可增加 x_stride 来提高查找色块的速度。
  134. left_y_stride = 20               #是查找某色块时需要跳过的y像素的数量。找到色块后,直线填充算法将精确像素。 若已知色块较大,可增加 y_stride 来提高查找色块的速度。

  135. #______________
  136. #右部blob位置信息
  137. right_blob_location = red_blob_location_t()
  138. right_pixels_threshold = 20     #若一个色块的像素数小于 pixel_threshold ,则会被过滤掉。
  139. right_area_threshold = 20       #若一个色块的边界框区域小于 area_threshold ,则会被过滤掉。
  140. right_x_stride = 20               #是查找某色块时需要跳过的x像素的数量。找到色块后,直线填充算法将精确像素。 若已知色块较大,可增加 x_stride 来提高查找色块的速度。
  141. right_y_stride = 20               #是查找某色块时需要跳过的y像素的数量。找到色块后,直线填充算法将精确像素。 若已知色块较大,可增加 y_stride 来提高查找色块的速度。

  142. #______________
  143. #顶部blob位置信息
  144. top_blob_location = red_blob_location_t()
  145. top_pixels_threshold = 20     #若一个色块的像素数小于 pixel_threshold ,则会被过滤掉。
  146. top_area_threshold = 20       #若一个色块的边界框区域小于 area_threshold ,则会被过滤掉。
  147. top_x_stride = 15               #是查找某色块时需要跳过的x像素的数量。找到色块后,直线填充算法将精确像素。 若已知色块较大,可增加 x_stride 来提高查找色块的速度。
  148. top_y_stride = 20               #是查找某色块时需要跳过的y像素的数量。找到色块后,直线填充算法将精确像素。 若已知色块较大,可增加 y_stride 来提高查找色块的速度。

  149. #______________
  150. #中部blob位置信息
  151. center_blob_location = red_blob_location_t()
  152. center_pixels_threshold = 20     #若一个色块的像素数小于 pixel_threshold ,则会被过滤掉。
  153. center_area_threshold = 20       #若一个色块的边界框区域小于 area_threshold ,则会被过滤掉。
  154. center_x_stride = 20               #是查找某色块时需要跳过的x像素的数量。找到色块后,直线填充算法将精确像素。 若已知色块较大,可增加 x_stride 来提高查找色块的速度。
  155. center_y_stride = 20               #是查找某色块时需要跳过的y像素的数量。找到色块后,直线填充算法将精确像素。 若已知色块较大,可增加 y_stride 来提高查找色块的速度。


  156. blob_location=[0,0]

  157. def find_blob(img):

  158.     global global_uart_send_data
  159.     #top_blob_location.x = -1
  160.     #top_blob_location.y = -1
  161.     #center_blob_location.x = -1
  162.     #center_blob_location.y = -1
  163.     left_blob_location.x = -1
  164.     left_blob_location.y = -1
  165.     right_blob_location.x = -1
  166.     right_blob_location.y = -1
  167.     center_blob_location.area = 0
  168.     for blob in img.find_blobs([red_threshold], roi = left_roi, x_stride = left_x_stride, y_stride = left_y_stride, pixels_threshold=left_pixels_threshold, area_threshold=left_area_threshold, merge=False, margin=10):
  169.         img.draw_rectangle(blob.rect())
  170.         img.draw_cross(blob.cx(), blob.cy())
  171.         left_blob_location.x = blob.cx()
  172.         left_blob_location.y = blob.cy()
  173.         left_blob_location.area = blob.area()

  174.     for blob in img.find_blobs([red_threshold], roi = right_roi,x_stride = right_x_stride, y_stride = right_y_stride, pixels_threshold=right_pixels_threshold, area_threshold=right_area_threshold, merge=True, margin=10):
  175.         img.draw_rectangle(blob.rect())
  176.         img.draw_cross(blob.cx(), blob.cy())
  177.         right_blob_location.x = blob.cx()
  178.         right_blob_location.y = blob.cy()
  179.         right_blob_location.area = blob.area()

  180.     for blob in img.find_blobs([red_threshold], roi = top_roi,x_stride = top_x_stride, y_stride = top_y_stride, pixels_threshold=top_pixels_threshold, area_threshold=top_area_threshold, merge=True, margin=10):
  181.         img.draw_rectangle(blob.rect())
  182.         img.draw_cross(blob.cx(), blob.cy())
  183.         #print("-------"+str(blob.area()))
  184.         top_blob_location.x = blob.cx()
  185.         top_blob_location.y = blob.cy()
  186.         top_blob_location.area = blob.area()

  187.     for blob in img.find_blobs([red_threshold], roi = center_roi,x_stride = center_x_stride, y_stride = center_y_stride, pixels_threshold=center_pixels_threshold, area_threshold=center_area_threshold, merge=True, margin=10):
  188.         img.draw_rectangle(blob.rect())
  189.         img.draw_cross(blob.cx(), blob.cy())
  190.         center_blob_location.x = blob.cx()
  191.         center_blob_location.y = blob.cy()
  192.         center_blob_location.area = blob.area()
  193.         #print(blob.pixels())


  194.     #for blob in img.find_blobs([black_threshold], roi = black_block_roi, pixels_threshold=30,merge=True,margin=50):
  195.         #img.draw_rectangle(blob.rect())
  196.         #img.draw_cross(blob.cx(), blob.cy())
  197.         ##print(blob.count())
  198.         #if(blob.count() >= 5):
  199.             #print("tinche"+str(blob.count()))
  200.             #global_uart_send_data.recognition = 1
  201.         #else:
  202.             #global_uart_send_data.recognition = 0

  203.     if(center_blob_location.area == 0):
  204.         print("tinche")
  205.         global_uart_send_data.recognition = 1
  206.     else:
  207.         global_uart_send_data.recognition = 0

  208.     return

  209. #______________________________________________________________________________________________________________________________
  210. #色块连线区配置
  211. lines_color = (0, 200, 0)
  212. lines_thickness = 1

  213. def draw_lines(img):
  214.     img.draw_line(top_blob_location.x, top_blob_location.y, center_blob_location.x, center_blob_location.y, color = lines_color, thickness = lines_thickness)
  215.     img.draw_line(left_blob_location.x, left_blob_location.y, right_blob_location.x, right_blob_location.y, color = lines_color, thickness = lines_thickness)
  216.     return


  217. #______________________________________________________________________________________________________________________________
  218. #upacker python实现代码 :https://github.com/aeo123/upacker/blob/master/python/upacker.py
  219. #介绍请看这里:https://github.com/aeo123/upacker

  220. class Upacker():
  221.     def __init__(self):
  222.         self._STX_L = 0x55
  223.         self._MAX_PACK_SIZE = 1024 + 4 + 128
  224.         self._calc = 0
  225.         self._check = 0
  226.         self._cnt = 0
  227.         self._flen = 0
  228.         self._state = 0
  229.         self._data = bytearray()

  230.     def _decode(self, d):
  231.         if (self._state == 0 and d == self._STX_L):
  232.             self._state = 1
  233.             self._calc = self._STX_L
  234.         elif self._state == 1:
  235.             self._flen = d & 0xff
  236.             self._calc ^= d & 0xff
  237.             self._state = 2
  238.         elif self._state == 2:
  239.             self._flen |= (d & 0xff) << 8
  240.             self._calc ^= d & 0x3F
  241.             # 数据包超长得情况下直接丢包
  242.             if ((self._flen & 0x3FFF) > self._MAX_PACK_SIZE):
  243.                 self._state = 0
  244.                 return -1
  245.             else:
  246.                 self._data = bytearray(self._flen & 0x3FFF)
  247.             self._state = 3
  248.             self._cnt = 0
  249.         elif self._state == 3:
  250.             header_crc = ((d & 0x03) << 4) | ((self._flen & 0xC000) >> 12)
  251.             self._check = d
  252.             if (header_crc != (self._calc & 0X3C)):
  253.                 self._state = 0
  254.                 return -1
  255.             self._state = 4
  256.             self._flen &= 0x3FFF
  257.         elif self._state == 4:
  258.             self._data[self._cnt] = d
  259.             self._cnt += 1
  260.             self._calc ^= d
  261.             if self._cnt == self._flen:
  262.                 self._state = 0
  263.                 #接收完,检查check
  264.                 if ((self._calc & 0xFC) == (self._check & 0XFC)):
  265.                     return 0
  266.                 else:
  267.                     return -1
  268.         else:
  269.             self._state = 0

  270.         return 1

  271.     # 解包
  272.     def unpack(self, bytes_data, callback):
  273.         ret = 0
  274.         for i in bytes_data:
  275.             ret = self._decode(i)
  276.             if ret == 0:
  277.                 callback(self._data)
  278.                 if(is_upacker_debug):
  279.                     print(self._data)
  280.             elif ret == -1:
  281.                 # callback(None)
  282.                 print("err")

  283.     # 打包
  284.     def enpack(self, data):
  285.         tmp = bytearray(4)
  286.         tmp[0] = 0x55
  287.         tmp[1] = len(data) & 0xff
  288.         tmp[2] = (len(data) >> 8) & 0xff
  289.         crc = tmp[0] ^ tmp[1] ^ tmp[2]
  290.         tmp[2] |= (crc & 0x0c) << 4
  291.         tmp[3] = 0x03 & (crc >> 4)

  292.         for i in range(len(data)):
  293.             crc ^= data[i]
  294.         tmp[3] |= (crc & 0xfc)

  295.         frame = struct.pack("BBBB%ds" % len(data), tmp[0], tmp[1], tmp[2],
  296.                             tmp[3], data)
  297.         #python3.5之前bytes数据没有hex()属性,所以修改了下面这个
  298.         #print(frame.hex())
  299.         if(is_upacker_debug):
  300.             print(''.join(map(lambda x:('' if len(hex(x))>=4 else '/x0')+hex(x)[2:],frame)))

  301.         return frame

  302. #____________
  303. #串口命令切换模式
  304. uart_cmd_need_change_mode = 0

  305. def print_hex(bytes):
  306.     global work_mode,uart_cmd_need_change_mode

  307.     hex_byte = [hex(i) for i in bytes]
  308.     if is_upacker_recive_debug:
  309.         print("-----"+" ".join(hex_byte))
  310.     if bytes[0] == 0x00:
  311.         work_mode = 0
  312.     if bytes[0] == 0x01:
  313.         work_mode = 1
  314.     uart_cmd_need_change_mode = 1

  315. #if __name__ == '__main__':
  316.     #buf = bytearray([0x00, 0x01, 0x02,0x03,0x77])
  317.     #pack = Upacker()
  318.     #pkt = pack.enpack(buf)
  319.     #pack.unpack(pkt, print_hex)

  320. #______________________________________________________________________________________________________________________________
  321. #upacker python实现代码结束



  322. def upacker_init():
  323.     pack = Upacker()

  324.     return pack

  325. yzh = 10

  326. #______________________________________________________________________________________________________________________________
  327. #发送数据到MCU,gd32是小端字节序
  328. #pack各字母对应类型
  329. #x   pad byte        no value            1
  330. #c   char            string of length 1  1
  331. #b   signed char     integer             1
  332. #B   unsigned char   integer             1
  333. #?   _Bool           bool                1
  334. #h   short           integer             2
  335. #H   unsigned short  integer             2
  336. #i   int             integer             4
  337. #I   unsigned int    integer or long     4
  338. #l   long            integer             4
  339. #L   unsigned long   long                4
  340. #q   long long       long                8
  341. #Q   unsilong long   long                8
  342. #f   float           float               4
  343. #d   double          float               8
  344. #s   char[]          string              1
  345. #p   char[]          string              1
  346. #P   void *          long

  347. def send_data_to_mcu(pack,global_uart_send_data):
  348.     hex_data = ustruct.pack("<bbhhhhbb",                        #小端字节序
  349.                    global_uart_send_data.work_mode,
  350.                    global_uart_send_data.recognition,
  351.                    global_uart_send_data.top_block_offset,          #以屏幕中线为0点
  352.                    global_uart_send_data.center_block_offset,
  353.                    global_uart_send_data.left_block_offset,          #以屏幕中线为0点
  354.                    global_uart_send_data.right_block_offset,
  355.                    global_uart_send_data.left_number,
  356.                    global_uart_send_data.right_number,)
  357.     pkg_data =  pack.enpack(hex_data)
  358.     k210_uart.write(pkg_data)

  359. #______________________________________________________________________________________________________________________________
  360. #pack解包后的回调函数
  361. #def data_callback(bytes):
  362.     #hex_byte = [hex(i) for i in bytes]
  363.     #print(" ".join(hex_byte))

  364. #______________________________________________________________________________________________________________________________
  365. #定时发送串口数据的定时器
  366. #构造函数
  367. # 定时器回调
  368. pack = upacker_init()

  369. def on_timer(timer):
  370.     global work_mode

  371.     global_uart_send_data.work_mode = work_mode
  372.     if(top_blob_location.x != -1):
  373.         global_uart_send_data.top_block_offset = top_blob_location.x-int(sensor.width()/2)
  374.     else:
  375.         global_uart_send_data.top_block_offset = 0

  376.     if(center_blob_location.x != -1):
  377.         global_uart_send_data.center_block_offset = center_blob_location.x-int(sensor.width()/2)
  378.     else:
  379.         global_uart_send_data.center_block_offset = 0

  380.     if(left_blob_location.y != -1):
  381.         global_uart_send_data.left_block_offset = left_blob_location.y-int(sensor.height()/2)
  382.     else:
  383.         global_uart_send_data.left_block_offset = 0

  384.     if(right_blob_location.y != -1):
  385.         global_uart_send_data.right_block_offset = right_blob_location.y-int(sensor.height()/2)
  386.     else:
  387.         global_uart_send_data.right_block_offset = 0



  388.     global_uart_send_data.top_block_offset = top_blob_location.x-int(sensor.width()/2)
  389.     global_uart_send_data.center_block_offset = center_blob_location.x-int(sensor.width()/2)


  390.     send_data_to_mcu(pack,global_uart_send_data)

  391. # 配置定时器0通道0
  392. tim = Timer(Timer.TIMER0, Timer.CHANNEL0, mode=Timer.MODE_PERIODIC, period=20, unit=Timer.UNIT_MS, callback=on_timer, arg=on_timer, start=False, priority=1, div=0)
  393. #定时发送串口数据
  394. tim.start()


  395. #______________________________________________________________________________________________________________________________
  396. #数字识别配置区域
  397. anchors = [1.40625, 1.875, 1.09375, 1.1875, 1.25, 1.46875, 1.53125, 2.15625, 1.1875, 1.8125000000000002]
  398. labels = ["1", "2", "3", "4", "5", "6", "7", "8"]

  399. actual_number = [1,2,3,4,5,6,7,8]


  400. KPU = kpu.load("/sd/mx.kmodel")
  401. xy = [0,0]
  402. kpu.init_yolo2(KPU,0.7,0.01,len(anchors)//2,anchors)

  403. #______________________________________________________________________________________________________________________________
  404. #主循环
  405. while (True):
  406.     gc.collect()
  407.     img = sensor.snapshot()  # Take a picture and return the image.
  408.     # img.draw_cross(sensor.width()//2, sensor.height()//2, color = (0, 255, 0), size = 10, thickness = 2)
  409.     clock.tick()
  410.     #手动切换
  411.     if key_press_long ==1:
  412.         key_press_long = 0
  413.         #长按后切换K210工作模式
  414.         if work_mode == 0:
  415.             work_mode =1
  416.             print("work mode to 1")
  417.             sensor.set_windowing((224, 224))
  418.         else:
  419.             work_mode =0
  420.             print("work mode to 0")
  421.             img = img.resize(320, 240)
  422.             sensor.set_windowing((320, 240))
  423.     #GD32控制切换
  424.     if uart_cmd_need_change_mode == 1:
  425.         uart_cmd_need_change_mode = 0
  426.         #串口命令来切换K210工作模式
  427.         if work_mode == 1:
  428.             print("work mode to 1")
  429.             sensor.set_windowing((224, 224))
  430.         else:
  431.             print("work mode to 0")
  432.             img = img.resize(320, 240)
  433.             sensor.set_windowing((320, 240))

  434.     if work_mode == 0:  # 巡线模式
  435.         find_blob(img)
  436.         draw_roi(img)
  437.         draw_lines(img)
  438.         if is_findflob_debug:
  439.             print(clock.fps(), top_blob_location.x,center_blob_location.x, left_blob_location.y, right_blob_location.y)  # Note: CanMV Cam runs about half as fast when connected,int(top.x),int(top.y)
  440.             print(clock.fps(), global_uart_send_data.top_block_offset,global_uart_send_data.center_block_offset, global_uart_send_data.left_block_offset, global_uart_send_data.right_block_offset)
  441.             print(global_uart_send_data.recognition)
  442.         fps = clock.fps()  # Update the FPS clock.
  443.         img.draw_string(2, 2, ("%2.1ffps" % (fps)), color=(0, 128, 0), scale=2)
  444.         lcd.display(img)

  445.     else:  # 数字识别模式

  446.         img = sensor.snapshot()  # Take a picture and return the image.
  447.         code = kpu.run_yolo2(KPU, img)
  448.         fps = clock.fps()  # Update the FPS clock.
  449.         img.draw_string(2, 2, ("%2.1ffps" % (fps)), color=(0, 128, 0), scale=2)
  450.         #print(clock.fps())
  451.         if bool(code):
  452.             for i in code:
  453.                 img = img.draw_rectangle(i.rect(), (0, 255, 0), 2, 0)
  454.                 xy[0] = i.x()
  455.                 xy[1] = (i.y() - 20)
  456.                 img = img.draw_string(xy[0], xy[1], (labels[i.classid()]), (0, 255, 0), scale=2)
  457.                 img = img.draw_string(xy[0], xy[1] + 24, '%.3f' % i.value(), color=(255, 0, 0), scale=2)
  458.             if(code[0].x()<code[-1].x()):
  459.                 global_uart_send_data.left_number = actual_number[code[0].classid()]
  460.                 global_uart_send_data.right_number = actual_number[(code[-1].classid())]
  461.             else:
  462.                 global_uart_send_data.left_number = actual_number[(code[-1].classid())]
  463.                 global_uart_send_data.right_number = actual_number[(code[0].classid())]
  464.             #print(global_uart_send_data.left_number,global_uart_send_data.right_number)
  465.         lcd.display(img)
  466.     read_data = k210_uart.read()
  467.     if read_data:
  468.         read_str = pack.unpack(read_data, print_hex)
  469.     lcd.display(img)  # Display image on lcd.
  470.     if is_stack_heap_mem_debug:
  471.         print("stack mem:"+str(gc.mem_free() / 1024)) # stack mem
  472.         print("heap mem:"+str(utils.heap_free() / 1024)) # heap mem
复制代码

关键的注释都添加了,可以帮助理解代码,这段代码同时实现了寻红线和数字识别,但是还做不到同时运行,在寻红线的时候没法数字识别,这两个状态之间的切换可以通过长按K210上的用户按键或者由立创梁山派来控制切换。当送药小车到达数字识别处时就控制K210进入数字识别模式,识别到数字后再控制K210进入寻红线模式。
  • 首先初始化LCD屏幕、配置摄像头、配置UART通信,并配置一个具有中断的按钮。然后,定义了感兴趣区域(ROIs)和一些用于在图像中查找色块的参数(具体可以去MaixPy的API文档中查找)。
  • 设置了在图像中检测色块和在里面绘制连线的参数。共定义了四个感兴趣区域(ROI):左侧、右侧、顶部和中心。每个ROI都有自己的色块检测参数,如pixel_threshold、area_threshold、x_stride和y_stride。这些参数用于过滤掉过小的色块或者通过跳过一些像素来提高检测速度,避免一些微小噪声小色块的影响。
  • find_blob函数处理输入图像(img),使用img.find_blobs方法在每个ROI中检测红色的色块。然后在色块周围绘制矩形并在其中心绘制十字标记来方便调试。检测到的色块的位置和面积信息存储在left_blob_location、right_blob_location、top_blob_location和center_blob_location结构中。如果在中心ROI中没有检测到色块,则将global_uart_send_data.recognition变量设置为1,否则设置为0,当红色线没了的时候就代表该停车了。draw_lines函数用于在上下和左右ROI的红色块之间绘制连线(上下左右ROI都识别到红色块就是一个交叉的十字),方便调试使用。
  • 定义了一个名为send_data_to_mcu的函数,用于将数据发送到立创梁山派。这里使用了ustruct.pack函数将数据打包为字节序列,然后调用pack.enpack方法将数据打包,最后通过k210_uart.write将数据发送出去。
  • 定义了一个名为on_timer的定时回调函数,用于定时发送串口数据。在这个函数中,根据图像中的各个位置计算出相应的偏移量,并将这些偏移量存储在global_uart_send_data对象中。然后,调用send_data_to_mcu函数将数据发送到立创梁山派的串口里面。然后,配置定时器0通道0,设置定时器的模式、周期、单位、回调函数等参数,并启动定时器。
  • 进行数字识别的相关配置,用init_yolo2加载模型并初始化KPU。
  • 在主循环中,根据工作模式选择执行巡线模式还是数字识别模式。在巡线模式下,调用find_blob、draw_roi、draw_lines等函数来处理摄像头采集到图像,并将处理后的图像显示在LCD上方便调试。在数字识别模式下,使用KPU运行模型并获取识别结果,将识别到的数字绘制在图像上,并将处理后的图像显示在LCD上。最后,程序会读取来自立创梁山篇串口发送来的数据,并使用pack.unpack方法解包数据。如果有数据,会执行解包,解包成功后会更新K210模式控制的标志位(巡线模式还是数字识别模式)。




回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2024-4-30 21:17 , Processed in 0.043397 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表