• OpenMV VSCode 扩展发布了,在插件市场直接搜索OpenMV就可以安装
  • 如果有产品硬件故障问题,比如无法开机,论坛很难解决。可以直接找售后维修
  • 发帖子之前,请确认看过所有的视频教程,https://singtown.com/learn/ 和所有的上手教程http://book.openmv.cc/
  • 每一个新的提问,单独发一个新帖子
  • 帖子需要目的,你要做什么?
  • 如果涉及代码,需要报错提示全部代码文本,请注意不要贴代码图片
  • 必看:玩转星瞳论坛了解一下图片上传,代码格式等问题。
  • 为什么脱机运行WiFi视频流传输,总是断网



    • MJPEG Streaming AP.

      这个例子展示了如何在AccessPoint模式下进行MJPEG流式传输。

      Android上的Chrome,Firefox和MJpegViewer App已经过测试。

      连接到OPENMV_AP并使用此URL:http://192.168.1.1:8080查看流。

      import sensor, image, time, network, usocket, sys, math, ustruct
      from pyb import UART
      from pid import PID

      uart = UART(3,115200,bits=8, parity=None, stop=1, timeout_char = 1000)
      GRAYSCALE_THRESHOLD = [(0, 64)]
      ROIS = [ # [ROI, weight]
      (0, 100, 160, 20, 0.7),
      (0, 050, 160, 20, 0.3),
      (0, 000, 160, 20, 0.4)
      ]
      weight_sum = 0
      for r in ROIS: weight_sum += r[4]

      SSID ='OPENMV_AP' # Network SSID
      KEY ='1234567890' # wifi密码(必须为10字符)
      HOST = '' # 使用第一个可用的端口
      PORT = 8080 # 任意非特权端口

      sensor.reset()
      sensor.set_contrast(1)
      sensor.set_brightness(1)
      sensor.set_saturation(1)
      sensor.set_gainceiling(16)
      sensor.set_auto_gain(False)
      sensor.set_auto_whitebal(False)
      sensor.set_framesize(sensor.QQVGA)
      sensor.set_pixformat(sensor.GRAYSCALE)
      sensor.skip_frames(time = 2000)
      #sensor.set_vflip(True)
      #sensor.set_hmirror(True)

      wlan = network.WINC(mode=network.WINC.MODE_AP)
      wlan.start_ap(SSID, key=KEY, security=wlan.WEP, channel=2)

      def start_streaming(s):

      def sending_data(data):
          global uart;
          data = ustruct.pack("<bbb",
                         x,
                         0x0d,
                         0x0a
                         )
          uart.write(data);
      
      print ('Waiting for connections..')
      client, addr = s.accept()
      # 将客户端套接字超时设置为2秒
      client.settimeout(2.0)
      print ('Connected to ' + addr[0] + ':' + str(addr[1]))
      
      # 从客户端读取请求
      data = client.recv(1024)
      # 应该在这里解析客户端请求
      
      # 发送多部分head
      client.send("HTTP/1.1 200 OK\r\n" \
                  "Server: OpenMV\r\n" \
                  "Content-Type: multipart/x-mixed-replace;boundary=openmv\r\n" \
                  "Cache-Control: no-cache\r\n" \
                  "Pragma: no-cache\r\n\r\n")
      
      # FPS clock
      clock = time.clock()
      a=0
      n=0
      # 开始流媒体图像
      #注:禁用IDE预览以增加流式FPS。
      while (True):
          if uart.any():
              a = uart.read(1)
              print(a[0])
      
              if a[0]==0x00:
                  img = sensor.snapshot().binary(GRAYSCALE_THRESHOLD).invert()
                  cframe = img.compressed(quality=35)
                  header = "\r\n--openmv\r\n" \
                       "Content-Type: image/jpeg\r\n"\
                       "Content-Length:"+str(cframe.size())+"\r\n\r\n"
                  client.send(header)
                  client.send(cframe)
                  centroid_sum = 0
                  most_pixels = 5000
                  for r in ROIS:
                      blobs = img.find_blobs(GRAYSCALE_THRESHOLD, roi=r[0:4], merge=True)
                      if blobs:
                          largest_blob = 0
                          for i in range(len(blobs)):
                              if blobs[i].pixels() > most_pixels:
                                  most_pixels = blobs[i].pixels()
                                  largest_blob = i
                          img.draw_rectangle(blobs[largest_blob].rect())
                          img.draw_cross(blobs[largest_blob].cx(),
                                         blobs[largest_blob].cy())
                          centroid_sum += blobs[largest_blob].cx() * r[4]
                  center_pos = (centroid_sum / weight_sum)
                  deflection_angle = 0
                  deflection_angle = -math.atan((center_pos-80)/60)
                  deflection_angle = math.degrees(deflection_angle)
                  x = int(deflection_angle)
                  sending_data(x)
                  print("Turn Angle: %d" % deflection_angle)
      
              if a[0]==0x01:
                  img = sensor.snapshot()
                  sensor.set_pixformat(sensor.RGB565)
      
                  cframe = img.compressed(quality=35)
                  header = "\r\n--openmv\r\n" \
                       "Content-Type: image/jpeg\r\n"\
                       "Content-Length:"+str(cframe.size())+"\r\n\r\n"
                  client.send(header)
                  client.send(cframe)
                  sensor.snapshot().save("singtown%s.jpg" % (n) )
                  n +=1
                  #print(n)
      

      while (True):
      # 创建服务器套接字
      s = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM)
      try:
      # Bind and listen
      s.bind([HOST, PORT])
      s.listen(5)

          # 设置服务器套接字超时
          # 注意:由于WINC FW bug,如果客户端断开连接,服务器套接字必须
          # 关闭并重新打开。在这里使用超时关闭并重新创建套接字。
          s.settimeout(3)
          start_streaming(s)
      except OSError as e:
          s.close()
          print("socket error: ", e)
          #sys.print_exception(e)