要讓攝像頭工做,還須要從新編譯和安裝OpenCV組件,由於Nvidia Jetpack自帶安裝了一個名爲OpenCV4Tegra的OpenCV的特殊閉源版本,該版本針對Jetson進行了優化,而且比開源版本略快。雖然OpenCV4Tegra運行速度比普通OpenCV 2好,但其版本都不支持視頻捕獲gstreamer,所以咱們沒法輕鬆從中獲取視頻。python
您能夠使用正確的選項從源代碼編譯OpenCV3.x支持視頻抓取。git
咱們將使用自編譯的OpenCV 3替換OpenCV4Tegra。github
推薦使用自動安裝腳本,包括全部刪除,安裝基礎框架,下載,編譯,導入python的全部內容。web
4.1.1express
https://github.com/AastaNV/JEP/blob/master/script/install_opencv4.1.1_Jetson.shbash
#!/bin/bash # # Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. # # NVIDIA Corporation and its licensors retain all intellectual property # and proprietary rights in and to this software, related documentation # and any modifications thereto. Any use, reproduction, disclosure or # distribution of this software and related documentation without an express # license agreement from NVIDIA Corporation is strictly prohibited. # folder=${HOME}/src user="nvidia" passwd="nvidia" echo "** Remove other OpenCV first" sudo apt-get purge *libopencv* echo "** Install requirement" #sudo apt-get update sudo apt-get install -y build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev sudo apt-get install -y libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev sudo apt-get install -y python2.7-dev python3.6-dev python-dev python-numpy python3-numpy sudo apt-get install -y libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libdc1394-22-dev sudo apt-get install -y libv4l-dev v4l-utils qv4l2 v4l2ucp sudo apt-get install -y curl sudo apt-get update echo "** Download opencv-4.1.1" cd $folder curl -L https://github.com/opencv/opencv/archive/4.1.1.zip -o opencv-4.1.1.zip curl -L https://github.com/opencv/opencv_contrib/archive/4.1.1.zip -o opencv_contrib-4.1.1.zip unzip opencv-4.1.1.zip unzip opencv_contrib-4.1.1.zip cd opencv-4.1.1/ echo "** Apply patch" sed -i 's/include <Eigen\/Core>/include <eigen3\/Eigen\/Core>/g' modules/core/include/opencv2/core/private.hpp echo "** Building..." mkdir release cd release/ cmake -D WITH_CUDA=ON -D CUDA_ARCH_BIN="5.3,6.2,7.2" -D CUDA_ARCH_PTX="" -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.1.1/modules -D WITH_GSTREAMER=ON -D WITH_LIBV4L=ON -D BUILD_opencv_python2=ON -D BUILD_opencv_python3=ON -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF -D BUILD_EXAMPLES=OFF -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local .. make -j3 sudo make install echo 'export PYTHONPATH=$PYTHONPATH:'$PWD'/python_loader/' >> ~/.bashrc source ~/.bashrc echo "** Install opencv-4.1.1 successfully" echo "** Bye :)"
3.xapp
https://github.com/jkjung-avt/jetson_nano框架
人臉識別的代碼以下,能夠用vscode打開代碼,而後直接運行example中的例子。curl
https://github.com/ageitgey/face_recognitionide
若是要實時識別人臉,能夠參考代碼:
examples/facerec_from_webcam_faster.py
獲取攝像頭是代碼可能須要修改,這個主要取決於你使用什麼樣的攝像頭類型。好比下面地方修改:
我之後會在github上新建一個項目來特別處理不一樣的攝像頭問題。
# Get a reference to webcam #0 (the default one) gst_str = ('nvarguscamerasrc ! ' 'video/x-raw(memory:NVMM), ' 'width=(int)1920, height=(int)1080, ' 'format=(string)NV12, framerate=(fraction)30/1 ! ' 'nvvidconv flip-method=2 ! ' 'video/x-raw, width=(int){}, height=(int){}, ' 'format=(string)BGRx ! ' 'videoconvert ! appsink').format(1920, 1080) video_capture = cv2.VideoCapture(gst_str, cv2.CAP_GSTREAMER)
用一個衛生紙的捲紙芯就能夠把攝像頭穩定的立起來。