반응형

MS kinect ubuntu 에서 해보기. (freenect)

$ pyenv virtualenv 2.7.18 kinect

(처음엔 3으로 하려고 했는데 뭐 자꾸 문제 생겨서 어쩔수 없이 2로 함)

하다가

created virtual environment CPython2.7.18.final.0-64 in 87ms
  creator CPython2Posix(dest=/home/lionelj/.pyenv/versions/2.7.18/envs/kinect, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, wheel=bundle, setuptools=bundle, via=copy, app_data_dir=/home/lionelj/.local/share/virtualenv)
    added seed packages: pip==20.3.4, setuptools==44.1.1, wheel==0.36.2
  activators PythonActivator,CShellActivator,FishActivator,PowerShellActivator,BashActivator
Installing pip from https://bootstrap.pypa.io/get-pip.py...
Traceback (most recent call last):
  File "/home/lionelj/turtlebot3_ws/src/install/turtlebot3_teleop/lib/python3.8/site-packages/site.py", line 75, in <module>
    __boot()
  File "/home/lionelj/turtlebot3_ws/src/install/turtlebot3_teleop/lib/python3.8/site-packages/site.py", line 3, in __boot
    import os
  File "/home/lionelj/.pyenv/versions/2.7.18/envs/kinect/lib/python2.7/os.py", line 49, in <module>
    import posixpath as path
ImportError: No module named posixpath
error: failed to install pip via get-pip.py

이런 에러 나면
virtualenv 버전20 의 버그임. 그래서 버전을 낮춰야함

사용하고 있는 버전으로 python 을 활성화한다.
$ pyenv shell 2.7.18

virtualenv 버전 확인

$ pip freeze | grep virtu
virtualenv==20.4.3

20버전이면 문제 발생한다.

다음 명령으로 다운그레이드 한다. 명령은 upgrade 인데 downgrade도 같은 명령으로 하면 된다고 한다.

$ pip install --upgrade virtualenv==16.7.7

잘 됐나 다시 확인한다.

$ pip freeze | grep virtu
virtualenv==16.7.7

잘 됐으니까 다시 한다.

$ pyenv virtualenv 2.7.18 kinect

잘됐다.

이제 freenect 빌드해서 써본다.

git clone https://github.com/OpenKinect/libfreenect
cd libfreenect
mkdir build
cd build
cmake -L .. # -L lists all the project options
make

파이썬 래퍼 켜려면 
cmake .. -DBUILD_PYTHON=ON
make

파이썬3용 하려면
cmake .. -DBUILD_PYTHON3=ON
make

해보니 파이썬3는 뭐 없어서 문제 많았다.
어쩔수없이 파이썬2 버전으로 한다.

아까 만든거로 activate 한다.

$ pyenv activate kinect

cd /home/lionelj/dev/kinect/libfreenect/wrappers/python
여기 있는걸 돌려볼거다.

README를 읽어라.
일단 이렇게 인스톨하라고 써있다.
난 로컬에 한다.

  • Global Install: sudo python setup.py install
  • Local Directory Install: python setup.py build_ext --inplace

numpy 가 없어서 안된다.

$ pyp install numpy

깔았으니 다시 해본다.

$ python setup.py build_ext --inplace

이제 됐다.

뭐 안되는사람은 cython 깔아야 될수도 있다.

일단 해본다.

$ python ./demo_cv_sync.py

ImportError: No module named cv

cv가 없어서 안된다.

또 깔아준다.

$ pip install opencv-python

에러난다.

Collecting opencv-python
  Downloading opencv-python-4.3.0.38.tar.gz (88.0 MB)
     |████████████████████████████████| 88.0 MB 116 kB/s 
  Installing build dependencies ... done
  Getting requirements to build wheel ... error
  ERROR: Command errored out with exit status 1:
   command: /home/lionelj/.pyenv/versions/2.7.18/envs/kinect_py2/bin/python2.7 /home/lionelj/.pyenv/versions/2.7.18/envs/kinect_py2/lib/python2.7/site-packages/pip/_vendor/pep517/_in_process.py get_requires_for_build_wheel /tmp/tmphogkiY
       cwd: /tmp/pip-install-8WSrFF/opencv-python
  Complete output (22 lines):
  Traceback (most recent call last):
    File "/home/lionelj/.pyenv/versions/2.7.18/envs/kinect_py2/lib/python2.7/site-packages/pip/_vendor/pep517/_in_process.py", line 280, in <module>
      main()
    File "/home/lionelj/.pyenv/versions/2.7.18/envs/kinect_py2/lib/python2.7/site-packages/pip/_vendor/pep517/_in_process.py", line 263, in main
      json_out['return_val'] = hook(**hook_input['kwargs'])
    File "/home/lionelj/.pyenv/versions/2.7.18/envs/kinect_py2/lib/python2.7/site-packages/pip/_vendor/pep517/_in_process.py", line 114, in get_requires_for_build_wheel
      return hook(config_settings)
    File "/tmp/pip-build-env-UARB7H/overlay/lib/python2.7/site-packages/setuptools/build_meta.py", line 146, in get_requires_for_build_wheel
      return self._get_build_requires(config_settings, requirements=['wheel'])
    File "/tmp/pip-build-env-UARB7H/overlay/lib/python2.7/site-packages/setuptools/build_meta.py", line 127, in _get_build_requires
      self.run_setup()
    File "/tmp/pip-build-env-UARB7H/overlay/lib/python2.7/site-packages/setuptools/build_meta.py", line 243, in run_setup
      self).run_setup(setup_script=setup_script)
    File "/tmp/pip-build-env-UARB7H/overlay/lib/python2.7/site-packages/setuptools/build_meta.py", line 142, in run_setup
      exec(compile(code, __file__, 'exec'), locals())
    File "setup.py", line 448, in <module>
      main()
    File "setup.py", line 99, in main
      % {"ext": re.escape(sysconfig.get_config_var("EXT_SUFFIX"))}
    File "/home/lionelj/.pyenv/versions/2.7.18/envs/kinect_py2/lib/python2.7/re.py", line 210, in escape
      s = list(pattern)
  TypeError: 'NoneType' object is not iterable
  ----------------------------------------
ERROR: Command errored out with exit status 1: /home/lionelj/.pyenv/versions/2.7.18/envs/kinect_py2/bin/python2.7 /home/lionelj/.pyenv/versions/2.7.18/envs/kinect_py2/lib/python2.7/site-packages/pip/_vendor/pep517/_in_process.py get_requires_for_build_wheel /tmp/tmphogkiY Check the logs for full command output.

여기 보면

https://stackoverflow.com/questions/63346648/python-2-7-installing-opencv-via-pip-virtual-environment

python2.7 은 opencv-python-4.3.0.38 안된다고 한다.

낮은 버전으로 깔아준다.

$ pip2 install opencv-python==4.2.0.32

다시 해본다.

$ python ./demo_cv_sync.py

아 짜증..
샘플이 다 opencv-python 2.4.9 버전으로 맞춰셔 있어서 실행이 안됨.
돌릴라면 전부 cv2 로 컨버팅 해야한다.

구버전 opencv 받아서 하려고 했는데 아예 사라짐.

$ pip install opencv-python==2.4.9

DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality.
ERROR: Could not find a version that satisfies the requirement opencv-python==2.4.9 (from versions: 3.1.0.0, 3.1.0.1, 3.1.0.2, 3.1.0.3, 3.1.0.4, 3.1.0.5, 3.2.0.6, 3.2.0.7, 3.2.0.8, 3.3.0.9, 3.3.0.10, 3.3.1.11, 3.4.0.12, 3.4.0.14, 3.4.1.15, 3.4.2.16, 3.4.2.17, 3.4.3.18, 3.4.4.19, 3.4.5.20, 3.4.6.27, 3.4.7.28, 3.4.8.29, 3.4.9.31, 3.4.10.37, 4.0.0.21, 4.0.1.23, 4.0.1.24, 4.1.0.25, 4.1.1.26, 4.1.2.30, 4.2.0.32, 4.3.0.38)
ERROR: No matching distribution found for opencv-python==2.4.9

걍 새버전으로 바꿔서 해봄.
카메라는 나오네.
다른 기능은 없나??

depth를 받을 수 있군.

import pygame
import numpy as np
import sys
from freenect import sync_get_depth as get_depth


def make_gamma():
    """
    Create a gamma table
    """
    num_pix = 2048  # there's 2048 different possible depth values
    npf = float(num_pix)
    _gamma = np.empty((num_pix, 3), dtype=np.uint16)
    for i in xrange(num_pix):
        v = i / npf
        v = pow(v, 3) * 6
        pval = int(v * 6 * 256)
        lb = pval & 0xff
        pval >>= 8
        if pval == 0:
            a = np.array([255, 255 - lb, 255 - lb], dtype=np.uint8)
        elif pval == 1:
            a = np.array([255, lb, 0], dtype=np.uint8)
        elif pval == 2:
            a = np.array([255 - lb, lb, 0], dtype=np.uint8)
        elif pval == 3:
            a = np.array([255 - lb, 255, 0], dtype=np.uint8)
        elif pval == 4:
            a = np.array([0, 255 - lb, 255], dtype=np.uint8)
        elif pval == 5:
            a = np.array([0, 0, 255 - lb], dtype=np.uint8)
        else:
            a = np.array([0, 0, 0], dtype=np.uint8)

        _gamma[i] = a
    return _gamma


gamma = make_gamma()


if __name__ == "__main__":
    fpsClock = pygame.time.Clock()
    FPS = 30  # kinect only outputs 30 fps
    disp_size = (640, 480)
    pygame.init()
    screen = pygame.display.set_mode(disp_size)
    font = pygame.font.Font('DejaVuSans.ttf', 32)  # provide your own font
    while True:
        events = pygame.event.get()
        for e in events:
            if e.type == pygame.QUIT:
                sys.exit()
        fps_text = "FPS: {0:.2f}".format(fpsClock.get_fps())
        # draw the pixels

        # get the depth readinngs from the camera
        depth = np.rot90(get_depth()[0])
        # the colour pixels are the depth readings overlayed onto the gamma table
        pixels = gamma[depth]
        temp_surface = pygame.Surface(disp_size)
        pygame.surfarray.blit_array(temp_surface, pixels)
        pygame.transform.scale(temp_surface, disp_size, screen)
        screen.blit(font.render(fps_text, 1, (255, 255, 255)), (30, 30))
        pygame.display.flip()
        fpsClock.tick(FPS)
반응형

'Embeded > ROS' 카테고리의 다른 글

ROS2 apt update key 문제.  (0) 2021.05.31
RPLidar A1 을 터틀봇3 turtlebot3 에 붙이는 방법.  (0) 2021.01.26
터틀봇3 알아보기.  (0) 2021.01.15
Posted by Real_G