TK1之opencv -07

5.3
查看Jetson TK1 L4T版本
$ head -n 1 /etc/nv_tegra_release
— # R21 (release), REVISION: 3.0,

查看系统位数(32/64),当然是32位的了
$ getconf LONG_BIT
此外可以“uname -a”查看,输出的结果中,如果有x86_64就是64位的,没有就是32位的。

#查询opencv版本
$ pkg-config –modversion OpenCV
REF: http://blog.csdn.net/zyazky/article/details/52388756

# and setup USB 3.0 port to run USB; usb_port_owner_info=2 indicates USB 3.0
$ sudo sed -i ‘s/usb_port_owner_info=0/usb_port_owner_info=2/’ /boot/extlinux/extlinux.conf

# Disable USB autosuspend
$ sudo sed -i ‘$s/$/ usbcore.autosuspend=-1/’ /boot/extlinux/extlinux.conf
// USB 3.0 is enabled. The default is USB 2.0, /boot/extlinux/extlinux.conf must be modified to enable USB 3.0.

// Two scripts are installed in /usr/local/bin. To conserve power, by default the Jetson suspends power to the USB ports when they are not in use.
In a desktop environment, this can lead to issues with devices such as cameras and webcams.
The first script disables USB autosuspend.
REF: http://www.cnphp6.com/detail/32448

一、相机
USB 3.0的5Gbps支持full-sized USB port (J1C2 connector) has enough bandwidth to allow sending uncompressed 1080p video streams.
USB 2.0的480 Mbps is the slowest of the possible camera interfaces, it usually only supports upto 720p 30fps

1.1、先看有木有USB 3.0接口?
$ lsusb
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 002: ID 8087:07dc Intel Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
全部2.0 root hub没有启用USB 3.0接口。

1.2、改成启用3.0
Enabling support for USB 3.0 on the full-sized USB port is easier, only have to change one parameter in /boot/extlinux/extlinux.conf:
Change usb_port_owner_info=0, to usb_port_owner_info=2,
and reboot.
or,
$ sudo sed -i ‘s/usb_port_owner_info=0/usb_port_owner_info=2/’ /boot/extlinux/extlinux.conf
这个usb_port_owner_info=2 indicates USB 3.0, 默认的是2.0 USB。

1.3、禁止自动挂起
To conserve power, by default the Jetson suspends power to the USB ports when they are not in use. In a desktop environment, this can lead to issues with devices such as cameras and webcams. Some USB devices & cameras have problems on Jetson TK1 due to automatic suspending of inactive USB ports in L4T 19.2 OS to save power.
So you might need to disable USB auto-suspend mode.
可以临时用一下,You can disable this temporarily until you reboot:
$ sudo bash -c ‘echo -1 > /sys/module/usbcore/parameters/autosuspend’
or,
to perform this automatically on every boot up, you can modify your ‘/etc/rc.local’ script, add this near the bottom of the file but before the “exit” line:
# Disable USB auto-suspend, since it disconnects some devices such as webcams on Jetson TK1.
echo -1 > /sys/module/usbcore/parameters/autosuspend
or,
# Disable USB autosuspend:
sudo sed -i ‘$s/$/ usbcore.autosuspend=-1/’ /boot/extlinux/extlinux.conf

三、配置
base on Logitech C920 + tegra-ubuntu 3.10.40-gc017b03:
$ lsusb

C920就是 046d:082d, explore what USB information
$ lsusb -d 046d:082d -v | less
都加载了哪些模块
$ lsmod

加载摄像头驱动,可以用 V4L2 访问摄像头,for example usb camera mdule, such as 摄像头模块 OV5640
$ sudo modprobe tegra_camera
— modprobe: ERROR: could not insert ‘tegra_camera’: Device or resource busy
以显式的加载模块
$ sudo modprobe uvcvideols -al libjpeg*

$ ls /dev/vi*
$ cheese –device=/dev/video?
JPEG parameter struct mismatch: library thinks size is 432, caller expects 488
问题解决: 板子上运行的jpeglib 版本是80 下载的是62 ,所以出错
The jpeg error might be because it finds a wrong version of the jpeg library. I think there’s one in the Ubuntu rootfs and one in the nvidia binaries. You may want to move the nvidia version away temporarily and test again.
说得对。Problem in the library /usr/lib/arm-linux-gnueabihf/libjpeg.so.8.0.2. Gstreamer hopes that this address will be based on a special version of the library, but some packages are replaced by the library to its. The developers have promised to deal with the problem
As a workaround, you can try replacing this library the library /usr/lib/arm-linux-gnueabihf/tegra/libjpeg.so
$ cd /usr/lib/arm-linux-gnueabihf
$ ls -al libjpeg*
lrwxrwxrwx 1 root root 16 Dec 20 2013 libjpeg.so -> libjpeg.so.8.0.2
lrwxrwxrwx 1 root root 16 Dec 20 2013 libjpeg.so.8 -> libjpeg.so.8.0.2
-rw-r–r– 1 root root 157720 Dec 20 2013 libjpeg.so.8.0.2
$ ls -al tegra/libjp*
-rwxrwxr-x 1 root root 305028 Dec 17 16:03 tegra/libjpeg.so
so,
$ sudo ln -sf tegra/libjpeg.so ./libjpeg.so
$ cheese
OKAY
***### if needed we can go back–<–
$ sudo ln -sf libjpeg.so.8.0.2 ./libjpeg.so

测试下, let’s go …
$ export DISPLAY=:0

四、openCV的例程能跑?
第一个:
# Test a simple OpenCV program. Creates a graphical window, hence you should plug a HDMI monitor in or use a remote viewer such as X Tunneling or VNC or TeamViewer on your desktop.
cd ~/opencv-2.4.10/samples/cpp
g++ edge.cpp -lopencv_core -lopencv_imgproc -lopencv_highgui -o edge
./edge
第二个:
# If you have a USB webcam plugged in to your board, then test one of the live camera programs.
g++ laplace.cpp -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_calib3d -lopencv_contrib -lopencv_features2d -lopencv_flann -lopencv_gpu -lopencv_legacy -lopencv_ml -lopencv_objdetect -lopencv_photo -lopencv_stitching -lopencv_superres -lopencv_video -lopencv_videostab -o laplace
./laplace
第三个:
# Test a GPU accelerated OpenCV sample.
cd ../gpu
g++ houghlines.cpp -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_calib3d -lopencv_contrib -lopencv_features2d -lopencv_flann -lopencv_gpu -lopencv_legacy -lopencv_ml -lopencv_objdetect -lopencv_photo -lopencv_stitching -lopencv_superres -lopencv_video -lopencv_videostab -o houghlines
./houghlines ../cpp/logo_in_clutter.png

CPU Time : 217.342 ms
CPU Found : 39
GPU Time : 138.108 ms
GPU Found : 199

五、自己的例程能跑?
第一个:
g++ opencv_stream.cpp -I/usr/include/opencv -L/usr/lib -lopencv_calib3d -lopencv_contrib -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_gpu -lopencv_highgui -lopencv_imgproc -lopencv_legacy -lopencv_ml -lopencv_objdetect -lopencv_photo -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videostab -lopencv_esm_panorama -lopencv_facedetect -lopencv_imuvstab -lopencv_tegra -lopencv_vstab -L/usr/local/cuda/lib -lcufft -lnpps -lnppi -lnppc -lcudart -lrt -lpthread -lm -ldl -o camera_stream

/usr/bin/ld: cannot find -lopencv_esm_panorama
/usr/bin/ld: cannot find -lopencv_facedetect
/usr/bin/ld: cannot find -lopencv_imuvstab
/usr/bin/ld: cannot find -lopencv_tegra
/usr/bin/ld: cannot find -lopencv_vstab
Note, All the libraries mentioned above are not required for building these samples. But they are given in case of any modifications to the code.
so,
g++ opencv_stream.cpp -I/usr/include/opencv -L/usr/lib -lopencv_calib3d -lopencv_contrib -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_gpu -lopencv_highgui -lopencv_imgproc -lopencv_legacy -lopencv_ml -lopencv_objdetect -lopencv_photo -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videostab -L/usr/local/cuda/lib -lcufft -lnpps -lnppi -lnppc -lcudart -lrt -lpthread -lm -ldl -o camera_stream
./ camera_stream

第二个:
$ g++ opencv_canny.cpp -I/usr/local/include/opencv -L/usr/local/lib/
-lopencv_contrib -lopencv_core -lopencv_features2d -lopencv_flann
-lopencv_gpu -lopencv_highgui -lopencv_imgproc -lopencv_legacy -lopencv_ml
-lopencv_nonfree -lopencv_objdetect -lopencv_ocl -lopencv_photo -lopencv_stitching
-lopencv_superres -lopencv_video -lopencv_videostab
-L/usr/local/cuda/lib -lcufft -lnpps -lnppi -lnppc -lcudart -lrt -lpthread -lm -ldl -o camera_canny

$ g++ opencv_canny.cpp -I/usr/local/include/opencv -L/usr/local/lib/ -lopencv_contrib -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_gpu -lopencv_highgui -lopencv_imgproc -lopencv_legacy -lopencv_ml -lopencv_nonfree -lopencv_objdetect -lopencv_ocl -lopencv_photo -lopencv_stitching -lopencv_superres -lopencv_video -lopencv_videostab -L/usr/local/cuda/lib -lcufft -lnpps -lnppi -lnppc -lcudart -lrt -lpthread -lm -ldl -o camera_canny
$ ./camera_canny

Conclusion
Use OpenCV4tegra only if the performance is really required. Check in the documentation for OpenCV4tegra whether the functions you are using are optimized or not.

五、GPU加速的OpenCV人体检测
(Full Body Detection) to build the OpenCV HOG (Hough Of Gradients) sample person detector program:
cd opencv-2.4.10/samples/gpu
g++ hog.cpp -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_calib3d -lopencv_contrib -lopencv_features2d -lopencv_flann -lopencv_gpu -lopencv_legacy -lopencv_ml -lopencv_nonfree -lopencv_objdetect -lopencv_photo -lopencv_stitching -lopencv_superres -lopencv_video -lopencv_videostab -o hog

./hog –video 768×576.avi
You can run the HOG demo such as on a pre-recorded video of people walking around. The HOG demo displays a graphical output, hence you should plug a HDMI monitor in or use a remote viewer such as X Tunneling or VNC or TeamViewer on your desktop in order to see the output.
Full Body Detection
./hog –camera /dev/video0
Note: This looks for whole bodies and assumes they are small, so you need to stand atleast 5m away from the camera if you want it to detect you!

六、GPU加速的OpenCV人脸检测(Face Detection)
$ cd gpu
g++ cascadeclassifier.cpp -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_calib3d -lopencv_contrib -lopencv_features2d -lopencv_flann -lopencv_gpu -lopencv_legacy -lopencv_ml -lopencv_objdetect -lopencv_photo -lopencv_stitching -lopencv_superres -lopencv_video -lopencv_videostab -o cascadeclassifier
$ ./cascadeclassifier –camera 0

运行结果FPS平均7fps,没有达到实时检测效果,要做的是并行编程将cuda与OpenCV有效地结合,实现实时人脸检测。

七、optical
光流实现
从摄像头获取数据,还可以从视频文件获取数据。
g++ optical.cpp -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_video -lopencv_contrib -lopencv_features2d -lopencv_flann -lopencv_gpu -lopencv_legacy -lopencv_ml -lopencv_objdetect -lopencv_photo -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_calib3d -o optical
optica 光流

光流追踪
g++ follow.cpp -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_video -lopencv_contrib -lopencv_features2d -lopencv_flann -lopencv_gpu -lopencv_legacy -lopencv_ml -lopencv_objdetect -lopencv_photo -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_calib3d -o follow
optica 光流追踪

APP:需要hack的地方
Patching OpenCV
The OpenCV version 2.4.9 and cuda version 6.5 are not compatible. It requires a few modifications in OpenCV.
Edit the file /modules/gpu/src/nvidia/core/NCVPixelOperations.hpp in the opencv source code main directory. Remove the keyword “static” from lines 51 to 58, 61 to 68 and from 119 to 133 in it. This is an example:
Before:
template<> static inline __host__ __device__ Ncv8u _pixMaxVal() {return UCHAR_MAX;}
template<> static inline __host__ __device__ Ncv16u _pixMaxVal() {return USHRT_MAX;}
After:
template<> inline __host__ __device__ Ncv8u _pixMaxVal() {return UCHAR_MAX;}
template<> inline __host__ __device__ Ncv16u _pixMaxVal() {return USHRT_MAX;}

Patching libv4l
This version of OpenCV supports switching resolutions using libv4l library. But it has a limitation. It does not support cameras with resolutions higher than 5 Mega Pixel. Modifying libv4l2 library is required to add that support.
Follow the steps mentioned below to obtain the source code for libv4l2 library, modify and build it and then install it:
$ apt-get source v4l-utils
$ sudo apt-get build-dep v4l-utils
$ cd v4l-utils-/
Edit the file lib/libv4l2/libv4l2-priv.h as follows:
Modify the line from
“#define V4L2_FRAME_BUF_SIZE (4096 * 4096)”
To:
“#define V4L2_FRAME_BUF_SIZE (3 * 4096 * 4096)”
$ dpkg-buildpackage -rfakeroot -uc -b
$ cd ..
$ sudo dpkg -i libv4l-0__.deb
Now OpenCV will support higher resolutions as well.

0 回复

发表评论

Want to join the discussion?
Feel free to contribute!

发表评论