개발자/Raspberry Pi

OpenCV 기반의 안면인식 1 - V4L2(Video4Linux2) 설치부터

지구빵집 2015. 7. 23. 09:57
반응형






아래 설명 도움 받은곳 : http://coeleveld.com/harald/291-pi-camera



카메리를 사용한다면 아래부터 확인한다.


라즈베리 파이의 카메라 모듈을 사용한다면 


$sudo raspi-config 명령을 사용하여 카메라를 Enable 해야 한다. 아래 명령으로 라즈베리 파이 카메라 모듈이 정상으로 동작하는지 확인한다


$raspistill -o myimage.jpg    -- 이미지 한장 촬영

 
$raspivid -o myvideo.h264 -t 10000  -- 10초 동영상 촬영




Pi Camera + Motion


현재 라즈베리 파이에 연결한 CSI Camera Board 는 USB webcam이 나니다.  그래서 라즈베리 파이가

카메라를 비데오 장치 (/dev/video0) 로 인식하게 Video4Linux driver를 사용해야 한다.


UV4L은 리눅스에서 카메라를 이용한 영상의 획득과 출력 장치의 제어를 위한 API, 드라이버 프레임 워크를 가리키며, USB 웹캠, TV 튜너장치등을 지원한다. 


라즈베리 파이에 UV4L 의 설치와 업그레이드

UV4L 설치를 위해 터미널에서 아래와 같은 명령을 실행한다.

    $ wget http://www.linux-projects.org/listing/uv4l_repo/lrkey.asc && sudo apt-key add ./lrkey.asc

실행 내용을 보자.



pi@raspberrypi ~ $ wget http://www.linux-projects.org/listing/uv4l_repo/lrkey.asc && sudo apt-key add ./lrkey.asc --2015-06-19 01:34:42--  http://www.linux-projects.org/listing/uv4l_repo/lrkey.asc
Resolving www.linux-projects.org (www.linux-projects.org)... 62.149.140.25
Connecting to www.linux-projects.org (www.linux-projects.org)|62.149.140.25|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1337 (1.3K) [application/pgp-signature]
Saving to: `lrkey.asc'

100%[========================================================================>] 1,337       --.-K/s   in 0s

2015-06-19 01:34:43 (18.0 MB/s) - `lrkey.asc' saved [1337/1337]

OK
pi@raspberrypi ~ $ ls -al lrkey.asc
-rw-r--r-- 1 pi pi 1337 Jul 22  2013 lrkey.asc
pi@raspberrypi ~ $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VkpV5QpIXAC86oCGbHe59PT2R2ER4i+J8JN
x1qu6XmEuXnXEWPtBawd7D9JFG0d7BlMRnMqBnuopdDmrK3B9KMq4ambFIwc7Oi2
Hp5XCwBOHoBrglzpQYhGBBgRAgAGBQI/3PVwAAoJEJnaXSr85jWkRY0AoI12RloF
/71dKJZ6Mws2HM8EBnP8AJ9ik4y7DEzRakKm1rGopUKRRF5Qqg==
=M2b1
-----END PGP PUBLIC KEY BLOCK-----
pi@raspberrypi ~ $



다음 파일을 여로 아래 내용을 추가한다. $sudo nano /etc/apt/sources.list :

    deb http://www.linux-projects.org/listing/uv4l_repo/raspbian/ wheezy main 


아래 명령을 실행한다. 


    $ sudo apt-get update
    $ sudo apt-get install uv4l uv4l-raspicam

위 두명령은 가장 최근 버전으로 UV4L 를 업그레이드 하고 인스톨한다.


부팅시에  UV4L 모듈이 로드되도록 하려면 아래와 같이 추가적으로 패키지를 설치한다.


    $ sudo apt-get install uv4l-raspicam-extras

설치 화면을 아래에 나타낸다. 아래부분에 warning 은 무시하기 바란다.



pi@raspberrypi ~ $ sudo apt-get install uv4l-raspicam-extras
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
  uv4l-raspicam-extras
0 upgraded, 1 newly installed, 0 to remove and 9 not upgraded.
Need to get 4,264 B of archives.
After this operation, 44.0 kB of additional disk space will be used.
Get:1 http://www.linux-projects.org/listing/uv4l_repo/raspbian/ wheezy/main uv4l-raspicam-extras armhf 1.2 [4,264 B]
Fetched 4,264 B in 0s (4,540 B/s)
Selecting previously unselected package uv4l-raspicam-extras.
(Reading database ... 84575 files and directories currently installed.)
Unpacking uv4l-raspicam-extras (from .../uv4l-raspicam-extras_1.2_armhf.deb) ...
Setting up uv4l-raspicam-extras (1.2) ...
update-rc.d: using dependency based boot sequencing
update-rc.d: warning: default start runlevel arguments (2 3 4 5) do not match uv4l_raspicam Default-Start values (S)
update-rc.d: warning: default stop runlevel arguments (0 1 6) do not match uv4l_raspicam Default-Stop values (none)
pi@raspberrypi ~ $


위 패키지는 사용자 편의를 위해 드라이버의 실행과 종료를 위한 스크립트를 제공한다. 사용법은 아래와 같다.


    $ sudo service uv4l_raspicam restart  -- UV4L 서비스 시작명령


pi@raspberrypi ~ $ sudo service uv4l_raspicam restart
[....] Starting UV4L Raspberry CSI Camera Driver: uv4l
<notice> [core] Trying driver 'raspicam' from built-in drivers...
<warning> [core] Driver 'raspicam' not found
<notice> [core] Trying driver 'raspicam' from external plug-in's...
<notice> [driver] Dual Raspicam Video4Linux2 Driver v1.9.27 built Feb 21 2015
<notice> [driver] Selected format: 1920x1080, encoding: mjpeg, JPEG Video Capture
<notice> [driver] Framerate max. 30 fps
<notice> [driver] ROI: 0, 0, 1, 1
<notice> [core] Device detected!
<notice> [core] Registering device node /dev/video0
pi@raspberrypi ~ $

UV4L 서비스를 시작할때 uv4l 는 컨피그 파일  /etc/uv4l/uv4l-raspicam.conf 을 분석하여 드라이버 옵션으로 설정된 값들을 반영한다. 원하는 값으로 수정한 경우 부팅시에 그대로 적용된다. 


이제 CSI 카메라 모듈을 위한 UV4L core component 와 Video4Linux2 driver 를 설치하였다.  예기치 않은 에러와 드라이버 에러가 발생하면 업데이트를 실행하고 다시 해보기 바란다.


$sudo apt-get update && sudo apt-get upgrade


더 많은 정보와 옵션을 보기 위해 아래 명령어를 사용한다.

    $ man uv4l
    $ man uv4l-raspicam

사용 가능한 옵션을 보기위해 아래 명령어를 사용한다.

    $ uv4l --help --driver raspicam --driver-help

uv4l-raspicam-extras package 을 설치하지 않았다면 아래 명령어를 사용하여 수동으로 로드해야 한다.


    $ uv4l --driver raspicam --auto-video_nr --width 640 --height 480 --encoding jpeg

아래는 JPEG 이미지 캡쳐 명령어를 실행한다.

    $ dd if=/dev/video0 of=snapshot.jpeg bs=11M count=1

실행되는 드라이버를 종료하기 위해 아래 명령어를 사용한다.


    $ pkill uv4l

라즈베리 파이 카메라 보드 드라이버에 더해서 아래와 같은 추가 모듈과 드라이버를 설치할 수 있다.


    $ sudo apt-get install uv4l-server
    $ sudo apt-get install uv4l-uvc
    $ sudo apt-get install uv4l-xscreen
    $ sudo apt-get install uv4l-mjpegstream


아래 명령어로 uv4l 을 다시 시작한다. 


$ sudo service uv4l_raspicam restart  -- UV4L 서비스 시작명령


위의 패키지들에서 HTTP Streaming Server module을 설치하였다면 브라우저에 접속하여 라즈베리 파이의 IP, 주소에 접속한다.


 http://raspberry Pi IP 주소:8080/

매뉴얼은 아래 명령으로 볼 수 있다.

    $ man uv4l-server
    $ man uv4l-uvc
    $ man uv4l-xscreen
    $ man uv4l-mjpegstream


---  아래 모션 캡쳐 부분을 참고하라.

Install Motion from the official Raspbian repository:

    raspberrypi ~ $ sudo apt-get install motion

To start with Motion, download and try the following configuration file, which you can change later according to your needs:

    raspberrypi ~ $ wget http://linux-projects.org/downloads/examples/motion.conf 

Now run the driver in background, for example:

    raspberrypi ~ $ uv4l --driver raspicam --auto-video_nr
    [notice] [core] Device detected!
    [notice] [core] Registering device node /dev/video0

Finally, run Motion:

    raspberrypi ~ $ LD_PRELOAD=/usr/lib/uv4l/uv4lext/armv6l/libuv4lext.so motion -c ./motion.conf

If you run motion as daemon and the camera is not detected properly for some reasons, try to load the driver with the --extension-presence=1 option. Check if the Motion HTTP server is up and running on your Raspberry Pi. For example, from your PC with firefox:

    mypc ~ $ firefox http://raspberrypi:8081/ 

---


cd /etc/motion
wget http://linux-projects.org/downloads/examples/motion.conf
uv4l --driver raspicam --auto-video_nr --extension-presence=1
LD_PRELOAD=/usr/lib/uv4l/uv4lext/armv6l/libuv4lext.so motion -c /etc/motion/motion.conf
[20518728] Processing thread 0 - config file /etc/motion/motion.conf
[20518728] Motion 3.2.12 Started
[20518728] Motion going to daemon mode

[2099016] Processing thread 0 - config file /etc/motion/motion.conf
[2099016] Motion 3.2.12 Started
[2099016] ffmpeg LIBAVCODEC_BUILD 3482368 LIBAVFORMAT_BUILD 3478785
[0] Thread 1 is from /etc/motion/motion.conf
[0] motion-httpd/3.2.12 running, accepting connections
[0] motion-httpd: waiting for data on port TCP 8080
[1] Thread 1 started
[1] cap.driver: "raspicam"
[1] cap.card: "Camera Board OV5647"
[1] cap.bus_info: "CSI"
[1] cap.capabilities=0x05000001
[1] - VIDEO_CAPTURE
[1] - READWRITE
[1] - STREAMING
[1] Test palette YU12 (320x240)
[1] Using palette YU12 (320x240) bytesperlines 480 sizeimage 115200 colorspace 00000000
[1] found control 0x00980900, "brightness", range 0,100
[1]     "brightness", default 50, current 50
[1] found control 0x00980901, "contrast", range -100,100
[1]     "contrast", default 0, current 0
[1] found control 0x00980902, "saturation", range -100,100
[1]     "saturation", default 0, current 0
[1] found control 0x0098090e, "red balance", range 0,800
[1]     "red balance", default 100, current 100
[1] found control 0x0098090f, "blue balance", range 0,800
[1]     "blue balance", default 100, current 100
[1] found control 0x08000000, "shutter speed", range 0,65535
[1]     "shutter speed", default 0, current 0
[1] found control 0x08000001, "zoom factor", range 1,8
[1]     "zoom factor", default 0, current 1
[1] mmap information:
[1] frames=4
[1] 0 length=115200
[1] 1 length=115200
[1] 2 length=115200
[1] 3 length=115200
[1] Using V4L2
[1] Resizing pre_capture buffer to 1 items
[1] Started stream webcam server in port 8081
[1] File of type 1 saved to: /home/pi/01-20150115082251-00.jpg

^C
[0] httpd - Finishing
[0] httpd Closing
[0] httpd thread exit
[1] Thread exiting
[1] Calling vid_close() from motion_cleanup
[1] Closing video device /dev/video0
[0] Motion terminating

Experiment with changes in Motion config file:
daemon: on
width: 1280
height: 720
output_normal: off // do not save still images
webcam_localhost: off // allow remote access to live video stream


일단 여기까지...


여기부터는 잘 안되서 다음 포스팅으로...ㅠ.ㅠ.







반응형