2020ǯ04��27��

���ץƥ�����ե�����ʰץ���������ǧ�������Ѥ���

¤Ï¤Æ¤Ê¥Ö¥Ã¥¯¥Þ¡¼¥¯¤ËÅÐÏ¿

�������� Intel ���ǥץ������ RealSense D415 �� 3DiVi ���� Nuitrack SDK ���Ȥ߹�碌�ǥ���������ǧ�����Ƥ��ޤ���������ư��ˤϻ��Ӥ˿����� D415 �����˽���Υ���������ǥ��ޡ��Ⱦ���������ƥ��Ȥ����ͻҤ����Ƥ��ޤ���

���Τ褦�˴��Ԥɤ����ȿ����������ΤǤ��������Τ��Ȥ����ˤʤäƤ��ޤ�����

  • �����ǤΥ���������ǧ���Ͽ��Τι��ʸ��Ф�����Ȥ��Ƥ���
  • ���Τ���Ω�̤Ǥ���°̤Ǥ��줪��������Ȥò¥«¥ï¿½ï¿½ï¿½ï¿½è¼¨ï¿½ï¿½ï¿½ï¿½É¬ï¿½×¤ï¿½ï¿½ï¿½ï¿½ï¿½

���Τ�����λ���� Kinect ��Ʊ���Τ褦�Ǥ������������ʤθ��Ф��Ӥ���Ŭ�ڤ˼��̤��뤿���ɬ�פǤ����������Ǥ�äȥ�դʤ�Τ⤢��Фȹͤ��ޤ��������ﴶ�ФǤϥ����Ĥ����Ĥˤ⤰�������ޤ޻������������Ȥʤ����äȻؼ���Ф��Ȥ��ä����Ȥ�Ǥ�������������Ǥ�������ʤ櫓�ǻ�˴�ñ�ʤ����ߤ��äƤߤ뤳�Ȥˤ��ޤ�����

�ͤ����Ȥ��ơ����������������Τ˽���ο��ʾ�ΰ�ư�򤶤ä��긡�Τ������ˤ��θ�����Ƚ�ꤹ�뤳�Ȥ����ꤷ�ޤ������¸���ˡ��ͤ��ʤ������Ǥ��������ä򤷤��Ȥ�������OpenCV �ǥ��ץƥ�����ե��������Ѥ��ƤϤɤ������פȤ����쥹�ݥ󥹤�����ޤ������긵�Ǥ��ȥ�å��� API �ʥ��: @nonbiri15 �ͤˤ��ɥ�����Ȥ������� ���Ƥ����Ȥ����ǥ��ץƥ�����ե����Τ��Ȥ��Τ餺�ˤ��ꡢ@icoxfog417 �ͤμ��ε��������Ӥ����礦�ɤ狼��䤹���ȤƤ⻲�ͤˤʤ�ޤ�����

  • OpenCV�ǤȤ館���������ư��Optical Flow - qiita.com/icoxfog417
    �����֤�ư���β��ϤˤĤ��Ƥϡ��͡�����Ū�Ȥ����¸������ˡ������ޤ��������Ǥϡ��ޤ�Optical Flow��������ǤɤΤ褦�ʰ��֤Ť��ˤʤ�Τ��������Ƥ����ޤ���
    • �ȥ�å���
      • ��Ū: ��������������ʪ��(�ͤ䥪�֥�������)�����פ�����
      • ��ˡ: �ꥢ�륿����˹Ԥ���ΤȤ����Ǥʤ���ΤΡ����̤���2����
            :
    • �ե�������
      • ��Ū: ��������Dz����ɤ�ư�����Τ����Τ�����(��¬�оݤ���ޤäƤ���ȥ�å��󥰤Ȥϰۤʤ�)
      • ��ˡ: �����������ħŪ�����˹ʤäƲ��Ϥ���sparse���ȡ��������Τ�ư������Ϥ���dense�������̤Ǥ���
      • -> sparse��: Lucas-Kanadeˡ�ʤ�
      • -> dense��: Horn-Schunckˡ��Gunnar Farnebackˡ�ʤ�

�ʲ��� GitHub ��� OpenCV �����Υ���ץ�ץ������Ǥ�������� Dense��̩�˷��� Gunnar Farneback ˡ���Ѥ����Ƥ��ޤ���

����Ū�ʥ����֥����� PC ����³���Ƥ��Υץ�������¹Ԥ����ͻҤ�ʲ���ư��˼���Ƥ��ޤ������������������Τ�ư�����ʻ����κ�ɸ����������ª����Ƥ����ͻҤ����Ū��ɽ������ޤ���

��ScreenShot��

���Υ���ץ�ץ������˾岼�����������ؤΰ�ư���礭��Ƚ�ꤹ�������ä��Ƥߤޤ���������ư������Τ�ư��ޤ���

��ɮ�����ץ������Υ����������ɤǤ����ޤ��ޤ����ɤ�;�ϤϤ����Τκ��󥪥ץƥ�����ե������������˿��줿���Ȥ��礭�ʼ��ϤǤ��������������ʻȤ������Ǥ������Ǥ���

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# ��̩�٥��ץƥ�����ե��������Ѥ����ʰץ���������ǧ���λ��
#
# Web �������������������̤ˤĤ���
# Left, Right, Up, Down �������ؤΰ�ư���Τ�������
#
# OpenCV �����β�������ץ�˽������ɲä������
# https://github.com/opencv/opencv/blob/master/samples/python/opt_flow.py
#
# 2020-04
#

import sys
import time
import numpy as np
import cv2

# ͭ����ư�̤�����
THRESHOLD_DETECT = 300

# �ڤ�ΤƤ�����
THRESHOLD_IGNORE = 2

# ���ꥢ������������ x, y �����ؤ�����ư��
MOVE = [0, 0]

# ��ưȯ��������
COUNT_MOVE = 0

# ľ��Υ��������㸡�Х����ƥ���� msec
TIME_DETECT = 0

def millis():
  return int(round(time.time() * 1000))

def put_header(img, str):
  cv2.putText(img, str, (26, 50), cv2.FONT_HERSHEY_SIMPLEX, 2, (255,0,0), 3)
  cv2.putText(img, str, (24, 48), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0,255), 3)

# ���ꥤ�᡼���˹�̩�٥��ץƥ�����ե����Υ٥��ȥ�����Ťͤ������ʸ����
# ���碌�ƥ���������κ����岼 �������ؤΰ�ư������Ƚ��
def draw_flow(img, flow, step=16):
  global MOVE, COUNT_MOVE, TIME_DETECT
  # �������᡼���� Hight, Width
  h, w = img.shape[:2]
  # ���᡼���νIJ��������� step �ֳֻ���˴�Ť��IJ��ʻ�����ɸ�����������
  y, x = np.mgrid[step/2:h:step, step/2:w:step].reshape(2,-1).astype(int)
  # �ʻ�����ɸ�������Ƿ����б������ư�����������
  fx, fy = flow[y,x].T

  # �Ƴʻ���������Ȥ������褹����ʬ����ʥ٥��ȥ�ˤ����������
  lines = np.vstack([x, y, x+fx, y+fy]).T.reshape(-1, 2, 2)
  lines = np.int32(lines + 0.5)
  vis = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)

  # ��ư�٥��ȥ���������
  cv2.polylines(vis, lines, 0, (0, 255, 0))
  #for (x1, y1), (_x2, _y2) in lines:
  #  cv2.circle(vis, (x1, y1), 2, (0, 0, 255), -1)

  # ��ʬ��������������ǤˤĤ���
  # �������齪���ޤǤ� x, y �������ؤΰ�ư�̤��߷פ�����
  vx = vy = 0
  for i in range(len(lines)):
    val = lines[i][1][0]-lines[i][0][0]
    if abs(val) >= THRESHOLD_IGNORE:
      vx += val
    val = lines[i][1][1]-lines[i][0][1]
    if abs(val) >= THRESHOLD_IGNORE:
      vy += val

  # ����ư�̤˲û�
  MOVE[0] += vx
  MOVE[1] += vy

  # ��ư�̤��߷פ���������Ͱʾ�ʤ��ư���Ƚ��
  if abs(vx) >= THRESHOLD_DETECT or abs(vy) >= THRESHOLD_DETECT:
    # ��ưȯ�������󥿤�û�
    COUNT_MOVE += 1
  # ���������̤���ʤ��ư��λ���֤Ȳ���
  else:
    mx = my = 0
    if COUNT_MOVE > 0 and \
    millis() - TIME_DETECT > 1000: # ��������
      # x, y �������ؤΰ�ư�̤�ʿ�Ѥ����
      mx = int(MOVE[0]/COUNT_MOVE)
      my = int(MOVE[1]/COUNT_MOVE)
      # x, y ���������줫�ΰ�ư��ʿ�Ѥ���������Ͱʾ�ʤ�
      # �����岼�Τɤ������ؤΰ�ư����Ƚ�ꤷ��ɽ��
      if abs(mx) >= THRESHOLD_DETECT or abs(my) >= THRESHOLD_DETECT:
        TIME_DETECT = millis()
        if abs(mx) >= abs(my):
          if mx >= 0:
            put_header(vis, 'LEFT')
          else:
            put_header(vis, 'RIGHT')
        else:
          if my >= 0:
            put_header(vis, 'DOWN')
          else:
            put_header(vis, 'UP')
    # ����ư�̤Ȱ�ưȯ�������󥿤򥯥ꥢ
    MOVE = [0, 0]
    COUNT_MOVE = 0

  return vis

def main():
  cam = cv2.VideoCapture(0)
  # �ǽ�Υե졼����ɤ߹���
  _ret, prev = cam.read()
  # ���쥤�������벽�����ݻ�
  prevgray = cv2.cvtColor(prev, cv2.COLOR_BGR2GRAY)

  while True:
    # �ե졼���ɤ߹���
    _ret, img = cam.read()
    # ���쥤�������벽
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # ����ʬ�Ⱥ���ʬ�Υ��᡼�������̩�٥��ץƥ�����ե��������
    flow = cv2.calcOpticalFlowFarneback(prevgray, gray, None, 0.5, 3, 15, 3, 5, 1.2, 0)

    # ����ʬ���ݻ����Ƥ���
    prevgray = gray

    # flow ��Ļ벽
    # https://docs.opencv.org/3.1.0/d7/d8b/tutorial_py_lucas_kanade.html
    """
    hsv = np.zeros_like(img)
    hsv[...,1] = 255
    mag, ang = cv2.cartToPolar(flow[...,0], flow[...,1])
    hsv[...,0] = ang*180/np.pi/2
    hsv[...,2] = cv2.normalize(mag,None,0,255,cv2.NORM_MINMAX)
    rgb = cv2.cvtColor(hsv,cv2.COLOR_HSV2BGR)
    cv2.imshow('hsv', rgb)
    """

    # �����˥ե����Υ٥��ȥ�����Ťͤ�ɽ��
    cv2.imshow('flow', draw_flow(gray, flow))

    ch = cv2.waitKey(1)
    if ch & 0xFF == ord('q') or ch == 27: # ESC
      break

if __name__ == '__main__':
  main()
  cv2.destroyAllWindows()


(tanabe)
klab_gijutsu2 at 14:56��Comments(0)������¾ 

���ε����˥����Ȥ���

̾��:
URL:
  ����òµ­²ï¿½: ɾ��: ��    ��
 
 
 
Blog�⸡��
�ǿ�����
Archives
���Υ֥����ˤĤ���
DSAS�Ȥϡ�KLab �����ۤ����Ѥ��Ƥ��륳��ƥ�ĥ����ӥ��Ѥ�Linux�١����Υ���ե�Ǥ�������5����Υǡ������󥿤ˤƹ��ۤ������Ѥ��Ƥ��ޤ������桹��DSAS����Ȥ��䤹�����������ˡ������Ƥ����ϤDZ��ѤǤ��뤳�Ȥ��ܻؤ��ơ��������ɤ˶Ф���Ǥ��ޤ���
���Υ֥����Ǥϡ������ DSAS �ǻȤäƤ��뵻�ѤξҲ�䡢�¸����Ƥߤ���̤���𡢥ȥ�֥�˴������ޤ줿���ηи��̤ʤɡ���������������������򿥤�ޤ��ƾҲ𤷤Ƥ��������Ȼפ��ޤ���
�ǿ�������
<%==comments[n].author%>
<% } %>