导航

    • 登录
    • 搜索
    • 版块
    • 产品
    • 教程
    • 论坛
    • 淘宝
    1. 主页
    2. gy5f
    G
    • 举报资料
    • 资料
    • 关注
    • 粉丝
    • 屏蔽
    • 帖子
    • 楼层
    • 最佳
    • 群组

    gy5f

    @gy5f

    0
    声望
    1
    楼层
    576
    资料浏览
    0
    粉丝
    0
    关注
    注册时间 最后登录

    gy5f 关注

    gy5f 发布的帖子

    • 我用串口发送单个num就可以,但是以数据包的行使就不行,而且还自动断开openmv,只能识别一次,而且打印不了

      Tensorflow数字识别

      import sensor, image, time, os, tf, uos, gc
      from pyb import UART
      uart = UART(3, 9600)
      net = None
      labels = None
      
      # 初始化函数
      def num_init_setup():
          global sensor, net, labels, clock      # 设置为全局变量
          sensor.reset()                         # 初始化感光元件
          sensor.set_pixformat(sensor.GRAYSCALE) # 设置图像格式为灰度
          sensor.set_framesize(sensor.QVGA)      # 设置图像大小为 QVGA (320x240)
          sensor.set_windowing((240, 240))       # 设置窗口大小为 240x240
          sensor.skip_frames(10)                 # 跳过一些帧,使以上设置生效
      
          try:
              # 加载模型文件
              net = tf.load("trained.tflite", load_to_fb=uos.stat('trained.tflite')[6] > (gc.mem_free() - (64*1024)))
          except Exception as e:
              print(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) + ')')
      
          clock = time.clock()        # 创建时钟对象
      
      # 主函数
      def num_main():
          while 1:
              clock.tick()                # 更新FPS帧率时钟
              img = sensor.snapshot()     # 拍一张照片并返回图像
      
              # default settings just do one detection... change them to search the image...
              for obj in net.classify(img, min_scale=1.0, scale_mul=0.8, x_overlap=0.5, y_overlap=0.5):
                  #print("**********\nPredictions at [x=%d,y=%d,w=%d,h=%d]" % obj.rect())
                  img.draw_rectangle(obj.rect())
                  # 将分类和对应的相似度以列表套元组形式返回
                  predictions_list = list(zip(labels, obj.output()))
                  # 打印分类和与图像中检测到的物体的对应的相似度
                  #for i in range(len(predictions_list)):
                      #print("%s = %f" % (predictions_list[i][0], predictions_list[i][1]))
                  if max(obj.output()) > 0.7:   # 当识别到的最大的相似度大于0.7时才认为是识别到了数字
                      # 将识别到的数字赋值给num
                      num = labels[obj.output().index(max(obj.output()))]
                      # 打印识别到的数字
                      print('识别到的数字是 %s' % num)
                      #uart.write('\n')
                      ##uart.write("识别到了,大傻瓜!!!\r\n")
                      #uart.write(num)
                      num1 = bytearray([0xFE,0xBC,num,0xEF])
                      uart.write(num1)
                      time.sleep_ms(100)
      
              #print(clock.fps(), "fps")  # 打印帧率
      
      # 程序入口
      if __name__ == '__main__':
          num_init_setup()  # 执行初始化函数
          try:
              num_main()    # 执行主函数
          except:
              pass
      

      ![3_1714829935249_QQ图片20240504213838.jpg](正在上传 100%) ![2_1714829935249_QQ图片20240504213831.jpg](正在上传 100%) ![1_1714829935249_QQ图片20240504213825.jpg](正在上传 100%) ![0_1714829935247_QQ图片20240504213803.jpg](正在上传 100%)

      发布在 OpenMV Cam
      G
      gy5f