导航

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

    r4yk

    @r4yk

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

    r4yk 关注

    r4yk 发布的帖子

    • 使用H7 Plus发送HTTP报文会提示运行内存不足

      H7 Plus的额RAM不是32M吗?感觉很容易内存就没了。而且我经过手动内存释放后,还是会有内存不足的提示。

      import sensor
      import network
      import socket
      import gc
      from pyb import UART
      from pyb import LED
      
      led = LED(1)
      led_p = LED(2)
      led.on()
      led_p.off()
      
      uart = UART(3, 9600)
      
      type_ = "3"
      type_data =("\r\n" + type_).encode('utf-8')
      
      # AP info
      SSID = ""  # Network SSID
      KEY = ""  # Network key
      
      PORT = 9999
      HOST = ""
      
      # 构建 HTTP 请求头和主体
      boundary = '----WebKitFormBoundary7MA4YWxkTrZu0gW'
      content_type = 'image/jpg'
      # 构建请求体
      body_start_type = (
          "\r\n"
          "--" + boundary + "\r\n"
          "Content-Disposition: form-data; name=\"type\"\r\n"
      ).encode('utf-8')
      # 构建请求体尾
      body_end = ("\r\n--" + boundary + "--\r\n").encode('utf-8')
      
      # 初始化感光元件
      sensor.reset()
      sensor.set_pixformat(sensor.RGB565) # Modify as you like.
      sensor.set_framesize(sensor.SVGA) # Modify as you like.
      
      # 初始化wlan模块并连接到网络
      wlan = network.WLAN(network.STA_IF)
      wlan.active(True)
      wlan.connect(SSID, KEY)
      
      while not wlan.isconnected():
          print('Trying to connect to "{:s}"...'.format(SSID))
          time.sleep_ms(1000)
      
      led.off()
      led_p.on()
      
      print("WiFi Connected ", wlan.ifconfig())
      
      # 通过DNS获取addr信息
      addr = socket.getaddrinfo(HOST, PORT)[0][4]
      print(addr)
      del addr
      
      while True:
          read_data = uart.read(10)
          if read_data != None:
              if read_data and read_data != b'\x00' * 10:  # 检查数据是否全是0
      
                  # 解码字节串为字符串
                  string_data = read_data.decode('utf-8')
                  del read_data
      
                  # 添加文件扩展名
                  filename = string_data + ".jpg"
                  del string_data
      
                  led_p.off()
      
                  # 拍照
                  frame = sensor.snapshot()
                  frame.save(filename)
                  del frame
      
                  f = open(filename, 'rb')
                  file_data = f.read()
                  f.close()
                  del f
      
                  # 构建请求体
                  body_start_data = (
                      "--" + boundary + "\r\n"
                      "Content-Disposition: form-data; name=\"image\"; filename=\"" + filename + "\"\r\n"
                      "Content-Type: " + content_type + "\r\n\r\n"
                  ).encode('utf-8')
                  del filename
      
                  # 完整的请求体
                  body = (body_start_data + file_data + body_start_type + type_data + body_end)
                  del body_start_data
                  del file_data
      
                  # 构建请求头
                  headers = (
                      "POST /images/upload HTTP/1.1\r\n"
                      "Host: " + HOST + ":" + str(PORT) + "\r\n"
                      "Content-Length: " + str(len(body)) + "\r\n"
                      "Content-Type: multipart/form-data; boundary=" + boundary + "\r\n"
                      "Connection: keep-alive\r\n\r\n"
                  ).encode('utf-8')
      
                  # 完整的请求
                  request_data = headers + body
                  del headers
                  del body
      
                  # 创建 socket 连接
                  sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                  sock.connect((HOST, PORT))
      
                  # 发送 HTTP 请求
                  sock.sendall(request_data)
                  del request_data
      
                  # 接收服务器响应
                  data = sock.recv(4096)
      
                  sock.close()
                  del sock
      
                  led_p.on()
      
                  gc.collect()
      
      
      发布在 OpenMV Cam
      R
      r4yk
    • 如何使用tf.free_from_fb()释放导入内存
          
      

      请在这里粘贴代码

      sensor.reset()                         # Reset and initialize the sensor.
          sensor.set_pixformat(sensor.RGB565)    # Set pixel format to RGB565 (or GRAYSCALE)
          sensor.set_framesize(sensor.QVGA)      # Set frame size to QVGA (320x240)
          sensor.set_windowing((240, 240))       # Set 240x240 window.
          sensor.skip_frames(time=2000)          # Let the camera adjust.
      
          net = None
          labels = None
          min_confidence = 0.5
      
          try:
              # load the model, alloc the model file on the heap if we have at least 64K free after loading
              net = tf.load("trained.tflite", load_to_fb=uos.stat('trained.tflite')[6] > (gc.mem_free() - (64*1024)))
          except Exception as 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) + ')')
      
          colors = [ # Add more colors if you are detecting more than 7 types of classes at once.
              (255,   0,   0),
              (  0, 255,   0),
              (255, 255,   0),
              (  0,   0, 255),
              (255,   0, 255),
              (  0, 255, 255),
              (255, 255, 255),
          ]
      
          clock = time.clock()
      
          clock.tick()
      
          Mask_Flag = False
          Count = 0
          Check_Num = 40
      
          for i in range(Check_Num):
      
              img = sensor.snapshot()
      
              # detect() returns all objects found in the image (splitted out per class already)
              # we skip class index 0, as that is the background, and then draw circles of the center
              # of our objects
      
              for i, detection_list in enumerate(net.detect(img, thresholds=[(math.ceil(min_confidence * 255), 255)])):
                  if (i == 0):
                      continue # background class
      
                  if (len(detection_list) == 0):
                      continue # no detections for this class?
      
                  print("********** %s **********" % labels[i])
      
                  for d in detection_list:
                      [x, y, w, h] = d.rect()
                      center_x = math.floor(x + (w / 2))
                      center_y = math.floor(y + (h / 2))
                      print('x %d\ty %d' % (center_x, center_y))
                      img.draw_circle((center_x, center_y, 12), color=colors[i], thickness=2)
      
                  if("mask" == labels[i]):
                      Count = Count + 1
                      if(Count >= 20):
                          Mask_Flag = True
                          break
      
          tf.free_from_fb()
          print(clock.fps(), "fps", end="\n\n")
      

      使用net = tf.load("trained.tflite", load_to_fb=uos.stat('trained.tflite')[6] > (gc.mem_free() - (64*1024)))加载后想释放内存,看了函数库tf.free_from_fb()可以释放内存,但是再载入模型报错内存不足。手册上tf.free_from_fb()没有输入参数,我想知道如何使用这个函数

      发布在 OpenMV Cam
      R
      r4yk