Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Pythonではじめる3Dセンシング / 3D Sensing with Python

moonlight-aska
July 22, 2018
1.2k

Pythonではじめる3Dセンシング / 3D Sensing with Python

2018年7月22日開催の「大江橋Pythonの会#1」の資料です.

moonlight-aska

July 22, 2018
Tweet

Transcript

  1. 自己紹介 鶴田 彰 外資系メーカー勤務 昔は, ・パターン認識(音声, 文字, etc) ・ユーザ適応(レコメンド, etc)

    なども・・・ 最近は, 週末プログラマとして また機械学習に再チャレンジ中! Twitter @moonlight_aska Blog:みらいテックラボ http://mirai-tec.hatenablog.com
  2. 3Dセンサカメラの年表 Kinect for XBOX 360/Windows Xtion PRO/PRO LIVE 2010 2012

    2014 2016 2018 Kinect for Windows v2 Xtion 2 RealSense D415/435 RealSense F200 RealSense SR300 Microsoft ASUS Intel SoftKinetic PrimeSense Depth Sense Series
  3. RealSense Depth Camera D400 Series Intel RealSense D415 項目 D415

    深度技術 Active IR stereo Vision 出力解像度(DepthStream) 最大1280x720 出力フレームレート (DepthStream) 最大30fps 最大レンジ 10m以上 コネクタ USB 3.0 Type-C OS Windows, Linux, MacOS 2018年1月発売
  4. RealSense SDK 2.x  RealSense D400シリーズのソフトウェア開発キット 注) RealSense SDK 1.0と互換性なし

     C, C++, Python, .NET(C#), JavaScript(Node.js)に対応  「人物や手、顔などの姿勢推定」「3次元形状復元」などの 応用機能は提供なし https://github.com/IntelRealSense/librealsense
  5. RealSense SDKインストール インストール成功すると, /usr/local/lib下に librealsense-file.a librealsense2.so librealsense2.so.2 librealsense2.so.2.13.0 pybackend2.cpython-36m-x86_64-linux-gnu.so pybackend2.cpython-36m-x86_64-linux-gnu.so.2

    pybackend2.cpython-36m-x86_64-linux-gnu.so.2.13.0 pyrealsense2.cpython-36m-x86_64-linux-gnu.so pyrealsense2.cpython-36m-x86_64-linux-gnu.so.2 pyrealsense2.cpython-36m-x86_64-linux-gnu.so.2.13.0
  6. Depth画像の表示(1) コード: import pyrealsense2 as rs import numpy as np

    import cv2 # ストリーム(Color/Depth)の設定 config = rs.config() config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30) config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30) # ストリーミング開始 pipeline = rs.pipeline() profile = pipeline.start(config)
  7. Depth画像の表示(2) try: while True: # フレーム待ち(Color & Depth) frames =

    pipeline.wait_for_frames() color_frame = frames.get_color_frame() depth_frame = frames.get_depth_frame() if not depth_frame or not color_frame: continue color_image = np.asanyarray(color_frame.get_data()) depth_color_frame = rs.colorizer().colorize(depth_frame) depth_color_image = np.asanyarray(depth_color_frame.get_data()) # 表示 images = np.hstack((color_image, depth_color_image)) cv2.namedWindow('RealSense', cv2.WINDOW_AUTOSIZE) cv2.imshow('RealSense', images) if cv2.waitKey(1) & 0xff == 27: break
  8. ファイルへの保存 コード: import pyrealsense2 as rs import numpy as np

    import cv2 # ストリーム(Color/Depth/Infrared)の設定 config = rs.config() config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30) config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30) config.enable_stream(rs.stream.infrared, 640, 480, rs.format.y8, 30) config.enable_record_to_file('./data/d415data.bag') # ストリーミング開始 pipeline = rs.pipeline() profile = pipeline.start(config) try: while True:
  9. ファイルの再生 コード: # ストリーム(Color/Depth/Infrared)の設定 config = rs.config() config.enable_device_from_file('./data/d415data.bag') config.enable_stream(rs.stream.color, 640,

    480, rs.format.bgr8, 30) config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30) # ストリーミング開始 pipeline = rs.pipeline() profile = pipeline.start(config) try: while True: # フレーム待ち(Color & Depth) frames = pipeline.wait_for_frames() color_frame = frames.get_color_frame() depth_frame = frames.get_depth_frame() 注) ファイルはリピート再生される!!
  10. 距離情報の利用(1) コード: # ストリーミング開始 pipeline = rs.pipeline() profile = pipeline.start(config)

    # 距離[m] = depth * depth_scale depth_sensor = profile.get_device().first_depth_sensor() depth_scale = depth_sensor.get_depth_scale() clipping_distance_in_meters = 1.0 # meter clipping_distance = clipping_distance_in_meters / depth_scale # Alignオブジェクト生成 align_to = rs.stream.color align = rs.align(align_to)
  11. 距離情報の利用(2) try: while True: # フレーム待ち(Color & Depth) frames =

    pipeline.wait_for_frames() aligned_frames = align.process(frames) color_frame = aligned_frames.get_color_frame() depth_frame = aligned_frames.get_depth_frame() if not depth_frame or not color_frame: continue color_image = np.asanyarray(color_frame.get_data()) depth_image = np.asanyarray(depth_frame.get_data()) # Depth画像前処理(1m以内を画像化) grey_color = 153 depth_image_3d = np.dstack((depth_image,depth_image,depth_image)) bg_removed = np.where((depth_image_3d > clipping_distance) | (depth_image_3d <= 0), grey_color, color_image)
  12. システム構成図 Intel RealSense D415 ASUS VivoMini UN62 USB3.0 People Counter

    with OpenCV Python – FEMB https://fedemejia.com/?p=8 上記サイトのPeople Counterをベースに, 3Dセンサカメラ対応
  13. 人物領域の検出 画 像 取 り 込 み 背 景 差

    分 cv2.createBackgroundSubtractorMOG2()
  14. 人物領域の検出 画 像 取 り 込 み 背 景 差

    分 二 値 化 ノ イ ズ 除 去 cv2.morphologyEx()
  15. 人物領域の検出 画 像 取 り 込 み 背 景 差

    分 二 値 化 ノ イ ズ 除 去 輪 郭 抽 出 cv2.findContours()
  16. 人物のトラッキング 追跡中 人物 ID=10 ID=12 ID=13 処理中 フレーム 人 物

    領 域 重 心 計 算 追 跡 中 人 物 と 照 合 人 物 情 報 更 新 ト ラ ッ キ ン グ
  17. 最後に  3Dセンシングに興味もってもらったらOK.  Intel RealSense SDK 2.xのPythonドキュメント少ないが, 画像入力程度なら十分. ただ,

    細かな設定など使いこなすならC/C++で…  Depth画像で画像処理やDeep Learningやるなら, OpenCVやKerasなどの使い方もぜひ覚えましょう!!