DSASȯԤ:ǥץͺۤʥ륫פȤѤ
2020ǯ0430

ǥץͺۤʥ륫פȤѤ

ϤƤʥ֥åޡϿ


ǥץ Intel RealSense D415 ȤäƤ뤦ˤդȤʤȤͤޤ

  • ̤ˡǥץλӤϤνϤϢ³Ūե졼ǡּˤäơưפȤƽ뤳Ȥˤ
  • ǡ줬ΥˤĤưۤʤڤʣξȤƱ˼ǽʥǥХǤ뤳Ȥϥ륫ȤƤͭѤǤϤʤ

RealSense D400 ꡼νϤϡ1˰Ū RGB 顼 2˼٤αƶˤֳơ3ΤΰִطŪɽ줿پ뤳ȤǤޤλξȹ礹ȥΰ֤ξ¿ŪˤȤ館뤳ȤǽǤ򼨤ޤ

ΥˡʲΤȤǡ֤뤤פ˳褫ΤǤϤʤȹͤޤ

  • D400 ꡼֤ιԤ¿ͤ͡οоݤȤ븫Ѥ
  • ƻνŤݤΤӥǥǤϤʤɬ׻ˤΤŻ߲򻣱Ƥ륹륫ȤưȤˤ깴«ޤ
  • ֳ١ǤưθΤ뤳Ȥ԰Ǥ¿ˤͭʰݳǧ¸Ǥ

ʹ󥵤ѤݳǧΤ꤬Ȥޤϸߤץ饤١ȤѤ³ƤΤǤˤβ󤬲äа¿ä礭ʤꤽǤ

Raspberry Pi 3 B+ Υåȥå

Τ褦ʷǰ켰֡ѤȤаξʥۥȵȤ߹碌뤳ȤޤǤ礦 RealSense D400 ꡼ Raspberry PI 3 ˤбƤޤ

ä Raspberry Pi 3 B+ δĶꤷޤλޤǤ꤬ä⤢ĤƤޤ

realsense raspberry pi 3פΥɤ򸡺ȡεƱͤ˥饺ѥ RealSense D400 ꡼Ѥ뤿 OS ĶȤ Ubuntu Mate 16.x Ȥ¿ҥåȤ롣ޤϤε򻲹ͤ˽ʤ᤿Ȥ Raspberry Pi 3 B+ Ubuntu Mate 16.x ư뤳ȤϤǤʤäԿ˻פʤõܤˤ֤ޤˡͤˤ뼡εƤΤä
˾򤵤ʲε˹Ԥ夤Raspbian λѤȤƤǡThese steps are for D435 with Raspberry Pi3 and 3+פȤ롣
εƤϺǽŪˤΤäˤ꤬Ȥ⤤Ĥä˺򤫤ͤƤʲ˹롣
  • www.raspberrypi.org ۤƤRaspbian Buster with desktopפˤŬѤǤʤΡ4.14.34-v7+פ˶ᤤСõ2018-11-13-raspbian-stretch-full (4.14.79-v7+) פȤ
    • ϢμǤΥӥɤˤϻ֤뤳Ȥա긵ǤϻԺʬΥޤȡ 20٤פ

  • λؼɤ raspi-config ޥɤ OpenGL Driver ͭˤ OpenCV ѤץǤɽԶ礬롣@PINTO ͤˤʲεˤ˴Ϣ뵭Ҥߤ
    • ǥץRealSenseD435ǡ ʵ̾άĺޤ - qiita.com/PINTO
      OpenGL Driver ͭˤȡOpenCV imshow ᥽åɤưʤʤɬפ롣 εμ Τ褦ˡOpenCV imshow ᥽åɤʤOpenGLϥ᥽åɤʤƤϤʤʤա
    긵Ǥ OpenGL OpenCV Ǥ뤿դ OpenGL ɥ饤Ф̵ [raspi-config] - [7.Advanced Options] - [A7 GL Driver] - [G3 Legacy]ˤץߥ󥰤ˤ OpenCV ѤƤ롣ǤǤϸ RealSense Viewer Ťˤʤ뤿ŬȤʬɬס

äФƼ긵 Raspberry Pi 3 B+ D415 ѤǤ֤ˤʤޤ꺢ʥǤǤ

  • ;̤ʤ RealSense D400 ꡼ʤ°Υߥ˻ӡʲΤդĤμ̿ˤϾʤ긵Ǥ󥳡λѤƤޤ

ưˤϤδĶǸ Intel RealSense Viewer ¹ԤͻҤƤޤʤǤǤλˤ饺ѥ¦ OpenGL Driver ͭˤƤޤ

D400 ֳȿپ٤Ȥδط

RealSense D400 ꡼Ͽٷ¬ˤդĤֳ󥵡ʥˤѤƥ쥪ƥѤƤꡢ¬ٸŪȤɥåȥѥֳԤȤǽȤʤäƤޤϥǥեȤͭǤ

Τ褦˺ RGB, ֳ, ٤ΤߤäĤβ򤢤碌ª뤳ȤݥȤǤֳǽֳͳȿپξ˱ƶڤܤޤ긵Ǥδѻ̤餳ΤȤƤߤޤ

ޤֳͭ / ̵֤ D415 μ褷ƥե졼Ż߲ʲ˼ޤֳϤդĤֳκ¦ΤΤѡʤʤֳϤޤǤ⿼ٷ¬ŪȤΤǤΤޤޤβϻŪ˰ŤᡢOpenCV ٤ȥȥ饹ȤǤ¤٤ƷǤƤޤʲϤ⥯å礭ɽ

  • ֳ
    RGB
    ֳ L ֳ Lߡ
  • ֳʤ
    RGB
    ֳ L ֳ L ߡ

    Υץ뤫鼡Τ褦ȽǤޤ

    • D415 ֳӤΤߤǰȤХɥåȥѥαǤߤ򤱤뤿̵ֳۤޤͻ
    • ̵ֳȾη̤Τ褦˿پμΩä
    • οپϥΤΰִطİǤפʾǤ뤿᥯ꥢƤǤ뤳Ȥ˾ޤ
    • ֳϥɥåȥѥǤ򤫤Τ褦ʼγݤǽ
    • ʾλͤ碌ֳͭȤۤŪͭȹͤ

    ʤߤˡ̵ֳͭ򥹥ȥ꡼ߥ󥰤ڤؤ뤳ȤϤǤޤ󡣥ץμ褹뤿Ѱդ Python ɤʲ˰Ѥޤ

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    #
    # D415_IRemitterTest.py
    #
    # for Intel RealSense D415
    #
    # 2020-03
    #
    
    import pyrealsense2 as rs
    import numpy as np
    import cv2
    
    # ̵ֳͭ
    USE_IR_EMITTER = True
    #USE_IR_EMITTER = False
    
    WIDTH = 640
    HEIGHT = 480
    FPS = 15
    
    # ᡼١ȥ饹Ȥ
    # https://www.pynote.info/entry/opencv-change-contrast-and-brightness
    def adjust(img, alpha=1.0, beta=0.0):
      dst = alpha * img + beta
      return np.clip(dst, 0, 255).astype(np.uint8)
    
    def main():
      # RealSense ȥ꡼ߥ󥰳
      pipeline = rs.pipeline()
      config = rs.config()
      config.enable_stream(rs.stream.depth, WIDTH, HEIGHT, rs.format.z16, FPS)
      config.enable_stream(rs.stream.color, WIDTH, HEIGHT, rs.format.bgr8, FPS)
      config.enable_stream(rs.stream.infrared, 1, WIDTH, HEIGHT, rs.format.y8, FPS)
      profile = pipeline.start(config)
    
      device = profile.get_device()
      depth_sensor = device.first_depth_sensor()
      if USE_IR_EMITTER:
        # IR 
        depth_sensor.set_option(rs.option.emitter_enabled, 1)
        laser_range = depth_sensor.get_option_range(rs.option.laser_power)
        depth_sensor.set_option(rs.option.laser_power,  laser_range.max)
      else:
        # IR ̵
        depth_sensor.set_option(rs.option.emitter_enabled, 0)
    
      try:
        while True:
          # RealSense ե졼ǡ
          frames = pipeline.wait_for_frames()
          depth_frame = frames.get_depth_frame()
          color_frame = frames.get_color_frame()
          ir_frame = frames.get_infrared_frame()
    
          depth_img_raw = np.asanyarray(depth_frame.get_data())
          color_img = np.asanyarray(color_frame.get_data())
          ir_img_raw = np.asanyarray(ir_frame.get_data())
          depth_img = cv2.applyColorMap(cv2.convertScaleAbs(depth_img_raw, alpha=0.08), cv2.COLORMAP_JET)
          # IR ե졼।᡼٤Ĵ
          ir_img = adjust(ir_img_raw, alpha=2.0, beta=40.0)
    
          # ƥե졼।᡼ɽ
          cv2.imshow('RGB', color_img)
          cv2.imshow('IRRaw', ir_img_raw)
          cv2.imshow('IR', ir_img)
          cv2.imshow('Depth', depth_img)
    
          # Ƚ
          key = cv2.waitKey(1)
          if key & 0xFF == ord('q') or key == 27: # ESC
            cv2.destroyAllWindows()
            break
          elif key & 0xFF == ord('s'):
            # ե졼।᡼
            cv2.imwrite('ImgRGB.jpg', color_img)
            cv2.imwrite('ImgIRRaw.jpg', ir_img_raw)
            cv2.imwrite('ImgIR.jpg', ir_img)
            cv2.imwrite('ImgDepth.jpg', depth_img)
    
      finally:
        pipeline.stop()
    
    if __name__ == '__main__':
      main()
    
    

    ưθΤˤĤ

    ˺ݤäȤ⥷ꥢʾȹͤΤϡоݼԤޤäưʤ֡פǤ礦Τᡢ뤵ˤ餺ư̵ͭŬڤİǤ褦ˤƤȹͤޤμʤȤֳ餫νϤȤưθΤԤȤפΩޤ
    ä긵ǰäиϤޤǤͥåȾξ򻲹ͤ˹ͤƤޤʲƤǤδʰŪʼߤޤޤǤե졼֤ξѲ̤򺬵ȤƤǿθǤϴޤߤޤ

    1. ե졼ǡѤΥ롼פ˼㴳Υǥ쥤ʥѥ᡼ AˤߤƤ
    2. 롼ֳե졼ˤΥԡݻƤ
    3. ֳե졼ȾʬȤκʬ
    4. 嵭ʬ˴ޤޤƥԥͤˤĤƽ͡ʥѥ᡼ Bˤ 0, 255 ͤˤդ뤤ʬ
    5. Ͳη̡255 οηʥѥ᡼ C˰ʾǤФδ֤Υ˲餫ѲäΤȽꤹ
      ƥѥ᡼ϼϤŬڤͤФ

    ΤưǼƤߤޤ

    1. ʬΥե졼
    2. ʬβ Τȼ򾯤ưƤߤ
    3. 嵭 1, 2 κʬ
    4. ԥͤͤ 0 255 ʬ

     

    ޤǤ򸵤˰ʲ˴ŤƤΥץꥱޤMQTT ֥ˤ֤ Beebotte ѤޤJavaScript Ѥ API å󶡤Ƥ뤿 Web ֥饦ΥƥĤǥå󤹤ȤǽǤ

    • оݤȤ˥饺ѥ + D415 ֤Υץ A Ư
    • 긵Ǥ Web ֥饦 HTML ꥽ B Ǿ˥
    • A D415 Υե졼ǡǾưθΤ»
      • Ѳ RGB, ֳ, , ͲγƲǡ Google Drive إåץ
      • åץɴλ MQTT å B ϤΥåȥڡ
      • ¦ HTML ꥽ B ΥեΥܥ󲡲ˤǤդΥߥ󥰤 A ؤΥå󥰤 RGB, ֳ, λƤ Google Drive إåץɤؼǽ
        • A Ͼ嵭å RGB, ֳ, ٤γƲǡ Google Drive إåץ
          • ΤȤβåȤݻ
        • A åץɴλ MQTT å B ϤΥåȥڡ

    Ϣ򹵤ޤ

    ʾƧޤäѰդץǤ

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    #
    # AnpiRealSense.py
    #
    # for Intel RealSense D415
    #
    # 2020-03
    #
    import threading
    import datetime
    import time
    import os
    import requests
    
    import pyrealsense2 as rs
    import numpy as np
    import cv2
    
    import paho.mqtt.client as mqtt
    import json
    
    from pydrive.auth import GoogleAuth
    from pydrive.drive import GoogleDrive
    
    # for RealSense
    WIDTH = 640
    HEIGHT = 480
    FPS = 15
    
    # for MQTT
    MQTT_TOKEN = 'token_w5xxxxxxxxxx'
    MQTT_HOSTNAME = 'mqtt.beebotte.com'
    MQTT_PORT = 8883
    MQTT_TOPIC = 'Anpi/q01'
    MQTT_CACERT = 'mqtt.beebotte.com.pem'
    MQTT_URL_PUB = 'https://script.google.com/xxxx/exec'
    
    # for Google Drive
    ID_RGB = '1YTEcpNyVv5axxxxxxxxxx'
    ID_IR = '1qt0tzv5-DfJxxxxxxxxxx'
    ID_DEPTH = '17A8Xybfz6qoxxxxxxxxxx'
    
    ID_RGB_PREV = '1sxb67Zspim2xxxxxxxxxx'
    ID_IR_PREV = '1A9C-9vgo97Kxxxxxxxxxx'
    ID_DEPTH_PREV = '1oJSOeebzL0exxxxxxxxxx'
    
    ID_RGB_MOVE = '1cMLgJB3_zcDxxxxxxxxxx'
    ID_IR_MOVE = '1rbaVHzpD4m-xxxxxxxxxx'
    ID_DEPTH_MOVE = '1dSdfAOy4oHqxxxxxxxxxx'
    ID_IR_DIFF = '1JuZLW1g8eaOxxxxxxxxxx'
    
    FN_RGB   = 'RGB'
    FN_IR    = 'IR'
    FN_DEPTH = 'Depth'
    
    FN_SUFFIX      = '.jpg'
    FN_SUFFIX_PREV = '_prev.jpg'
    FN_SUFFIX_MOVE = '_move.jpg'
    FN_SUFFIX_DIFF = '_diff.jpg'
    
    QUEUE = 0
    Q_UPDATE = 100
    Q_DETECT = 200
    
    # MQTT ͥΩХå
    def on_mqtt_connect(client, userdata, flags, respons_code):
      client.subscribe(MQTT_TOPIC)
      print('mqtt on_connect')
    
    # MQTT åХå
    def on_mqtt_message(client, userdata, msg):
      global QUEUE
      QUEUE = Q_UPDATE
      print('mqtt on_message')
      print(msg.topic + ' ' + str(msg.payload))
    
    # ᡼ջʬɽ
    def putDateTime(img):
      dt_str = datetime.datetime.now().strftime('%Y-%m-%d %H:%M')
      cv2.putText(img, dt_str, (20, 36), 
        cv2.FONT_HERSHEY_DUPLEX, 1, (0, 0, 0), 2, cv2.LINE_AA )
      cv2.putText(img, dt_str, (18, 34), 
        cv2.FONT_HERSHEY_DUPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA )
    
    # jpg ե뷲򹹿
    def rotateFile(fname, img):
      fn = fname + FN_SUFFIX
      fn_prev = fname + FN_SUFFIX_PREV
      if os.path.exists(fn):
        if os.path.exists(fn_prev):
          os.remove(fn_prev)
        os.rename(fn, fn_prev)
      cv2.imwrite(fn, img)
      if os.path.exists(fn_prev):
        return True
      return False
    
    # Google Drive سƥե졼 jpg ǡ򥢥åץ
    def upload_data(id, fname, gdrive):
      f = gdrive.CreateFile({'id': id})
      f.SetContentFile(fname)
      f.Upload()
      print('upload_data [' + fname +']')
    
    def do_upload(mode, rgb_img, ir_img, depth_img, ir_img_diff, gdrive):
      print('do_upload start')
      putDateTime(rgb_img)
      putDateTime(ir_img)
      putDateTime(depth_img)
      if mode == Q_UPDATE: # ǤդλǤΥʥåץå
        if rotateFile(FN_RGB, rgb_img):
          upload_data(ID_RGB_PREV, FN_RGB + FN_SUFFIX_PREV, gdrive)
        upload_data(ID_RGB, FN_RGB + FN_SUFFIX, gdrive)
        if rotateFile(FN_DEPTH, depth_img):
          upload_data(ID_DEPTH_PREV, FN_DEPTH + FN_SUFFIX_PREV, gdrive)
        upload_data(ID_DEPTH, FN_DEPTH + FN_SUFFIX, gdrive)
        if rotateFile(FN_IR, ir_img):
          upload_data(ID_IR_PREV, FN_IR + FN_SUFFIX_PREV, gdrive)
        upload_data(ID_IR, FN_IR + FN_SUFFIX, gdrive)
      elif mode == Q_DETECT: # ưθλ
        cv2.imwrite(FN_RGB + FN_SUFFIX_MOVE, rgb_img)
        cv2.imwrite(FN_DEPTH + FN_SUFFIX_MOVE, depth_img)
        cv2.imwrite(FN_IR + FN_SUFFIX_MOVE, ir_img)
        cv2.imwrite(FN_IR + FN_SUFFIX_DIFF, ir_img_diff)
        upload_data(ID_RGB_MOVE, FN_RGB + FN_SUFFIX_MOVE, gdrive)
        upload_data(ID_DEPTH_MOVE, FN_DEPTH + FN_SUFFIX_MOVE, gdrive)
        upload_data(ID_IR_MOVE, FN_IR + FN_SUFFIX_MOVE, gdrive)
        upload_data(ID_IR_DIFF, FN_IR + FN_SUFFIX_DIFF, gdrive)
    
      requests.post(MQTT_URL_PUB, data={'hoge':'1'})
      print('do_upload done')
    
    # ᡼١ȥ饹Ȥ
    # https://www.pynote.info/entry/opencv-change-contrast-and-brightness
    def adjust(img, alpha=1.0, beta=0.0):
      dst = alpha * img + beta
      return np.clip(dst, 0, 255).astype(np.uint8)
    
    def main():
      global QUEUE
    
      # init MQTT
      print('init mqtt start')
      mqtt_client = mqtt.Client()
      mqtt_client.username_pw_set('token:%s'%MQTT_TOKEN)
      mqtt_client.on_connect = on_mqtt_connect
      mqtt_client.on_message = on_mqtt_message
      mqtt_client.tls_set(MQTT_CACERT)
      mqtt_client.connect(MQTT_HOSTNAME, port=MQTT_PORT, keepalive=60)
      print('init mqtt done')
    
      # init Google Drive 
      print('init gdrive start')
      gauth = GoogleAuth()
      gauth.CommandLineAuth()
      gdrive = GoogleDrive(gauth)
      print('init gdrive done')
    
      # RealSense ȥ꡼ߥ󥰳
      print('rs streaming starting')
      pipeline = rs.pipeline()
      config = rs.config()
      config.enable_stream(rs.stream.depth, WIDTH, HEIGHT, rs.format.z16, FPS)
      config.enable_stream(rs.stream.color, WIDTH, HEIGHT, rs.format.bgr8, FPS)
      config.enable_stream(rs.stream.infrared, 1, WIDTH, HEIGHT, rs.format.y8, FPS)
      profile = pipeline.start(config)
      print('rs streaming started')
    
      # IR 
      device = profile.get_device()
      depth_sensor = device.first_depth_sensor()
      depth_sensor.set_option(rs.option.emitter_enabled, 1)
      laser_range = depth_sensor.get_option_range(rs.option.laser_power)
      depth_sensor.set_option(rs.option.laser_power,  laser_range.max)
    
      ir_img_prev = np.empty(0)
      ir_img_diff = np.empty(0)
    
      time_start = time.time()
      time_deetected = 0
    
      try:
        while True:
          mqtt_client.loop(0.1)
    
          # RealSense ե졼ǡ
          frames = pipeline.wait_for_frames()
          depth_frame = frames.get_depth_frame()
          color_frame = frames.get_color_frame()
          ir_frame = frames.get_infrared_frame()
    
          if not depth_frame or not color_frame or not ir_frame:
            continue
    
          depth_img_raw = np.asanyarray(depth_frame.get_data())
          color_img = np.asanyarray(color_frame.get_data())
          ir_img_raw = np.asanyarray(ir_frame.get_data())
          depth_img = cv2.applyColorMap(cv2.convertScaleAbs(depth_img_raw, alpha=0.08), cv2.COLORMAP_JET)
          # IR ե졼।᡼٤Ĵ
          ir_img = adjust(ir_img_raw, alpha=2.0, beta=40.0)
    
          # ưθ
          # Υ褱˥ȥ꡼ߥ󥰳Ϥ 10٤ϴDz
          if ir_img_prev.size != 0 and time.time() - time_start > 10:
            # ľ IR ե졼।᡼Ȥκʬ᡼
            ir_diff = cv2.absdiff(ir_img, ir_img_prev)
            #  80 ͤ 0, 255 Ͳ
            ret, ir_img_diff  = cv2.threshold(ir_diff, 80, 255, cv2.THRESH_BINARY)
            cv2.imshow('diff', ir_img_diff)
            # 255 ͥݥȤΤߤ
            ar = ir_img_diff[ir_img_diff == 255]
            # ηʾʤưȽ
            if ar.size > 200:
              #print(ar.size)
              # 󥢥åץɤ 30ʬ̤вʤ饹å
              if time.time() - time_deetected >= 1800:
                print('motion detected')
                time_deetected = time.time()
                QUEUE = Q_DETECT
    
          # ʬ IR ե졼।᡼򼡲Ѥ˥ԡ
          ir_img_prev = ir_img.copy()
    
          # ƥե졼।᡼ɽ
          cv2.imshow('RGB', color_img)
          cv2.imshow('IR', ir_img)
          cv2.imshow('Depth', depth_img)
    
          # Ƚ
          key = cv2.waitKey(1)
          if key & 0xFF == ord('q') or key == 27: # ESC
            cv2.destroyAllWindows()
            break
          elif key & 0xFF == ord('s'):
            QUEUE = Q_UPDATE
    
          # åץɻؼ
          if QUEUE != 0:
            # Google Drive سƥե졼 jpg ǡ򥢥åץ
            do_upload(QUEUE, 
              color_img, ir_img, depth_img, ir_img_diff,
              gdrive)
            QUEUE = 0
    
          time.sleep(0.2) # 200ms
    
      finally:
        pipeline.stop()
        print('exit')
    
    if __name__ == '__main__':
      main()
    
    

    HTML ꥽ʥ֡

    ưǻѤƤ HTML ڡȼºݤ˻Ƥƥե졼Ū˺ƹ֤򲼵ؼƤޤ

    • ǥڡ    target="_blank"

    Υǥڡ˺λǻѤºݤ HTML Ҥ򥳥ȤȤưѤƤޤʳƼ ID ϥߡǤˡ

    ưͻ


    (tanabe)
           
  • klab_gijutsu2 at 09:30Comments(0)¾ 

    ε˥Ȥ

    ̾:
    URL:
      򵭲: ɾ:    
     
     
     
    Blog⸡
    Archives
    Υ֥ˤĤ
    DSASȤϡKLab ۤѤƤ륳ƥĥӥѤLinux١ΥեǤ5Υǡ󥿤ˤƹۤѤƤޤ桹DSASȤ䤹ˡƤϤDZѤǤ뤳Ȥܻؤơɤ˶ФǤޤ
    Υ֥Ǥϡ DSAS ǻȤäƤ뵻ѤξҲ䡢¸Ƥߤ̤𡢥ȥ֥˴ޤ줿ηи̤ʤɡ򿥤ޤƾҲ𤷤ƤȻפޤ
    ǿ