编译需求
- gcc -----------------------------7.5.0
- g++ ---------------------------- 7.5.0
- ubuntu ----------------------- 18.04
- python ------------------------ 3.6.9
- ffmpeg ------------------------ 3.4.1
- opencv ------------------------ 3.4.0
编译ffmpeg
下载网址:http://www.ffmpeg.org/releases/
- 安装依赖
sudo apt-get install -y nasm yasm cmake mercurial
sudo apt-get install -y libxcb-xfixes0-dev pkg-config texinfo wget zlib1g-dev
sudo apt-get install -y libtool libva-dev libvdpau-dev libvorbis-dev libxcb1-dev libxcb-shm0-dev
sudo apt-get install -y libfreetype6-dev libsdl2-dev libtheora-dev
sudo apt-get install -y autoconf automake build-essential git libass-dev
sudo apt-get install libx264-dev libx265-dev libnuma-dev libgtk-3-dev
- 安装ffmpeg
cd ffmpeg-4.3
sudo -s
# 安装了yasm和nasm 就不需要加–disable-yasm
./configure --enable-shared --enable-libx264 --enable-gpl --prefix=/usr/local/ffmpeg
make && make install
如果要make有缓存 通过make clean即可
- 配置环境ffmpeg变量
sudo gedit /etc/profile
export LD_LIBRARY_PATH=$/usr/local/ffmpeg/lib:$LD_LIBRARY_PATH
source /etc/profile
- 建立软链接
cd /usr/bin
sudo ln -s /home/lqs/Downloads/ffmpeg-3.4.1/ffprobe ffprobe
sudo ln -s /home/lqs/Downloads/ffmpeg-3.4.1/ffmpeg ffmpeg
sudo ln -s /home/lqs/Downloads/ffmpeg-3.4.1/ffplay ffplay
- 查看ffmpeg
ffmpeg
- 如果要通过自己编译后的ffmpeg文件来反编译opencv
1.未编译前的源代码复制到aarch64-linux-gnu文件夹下(共9个文件)
cp -r libavcodec libavfilter libavresample libpostproc libswscale libavdevice libavformat libavutil libswresample /usr/include/x86_64-linux-gnu
2.将编译后的头文件全部复制到aarch64-linux-gnu文件夹下(共8个文件)
sudo cp -r /usr/local/ffmpeg/include/* /usr/include/x86_64-linux-gnu
- 卸载ffmpeg
sudo make uninstall
sudo apt-get --purge remove ffmpeg
ffmpeg -version
复制完了我们可以开始编译opencv了
编译opencv
- 下载好了opencv的源代码之后,我们进行解压,在配置依赖环境
sudo apt-get install libopencv-dev build-essential cmake git libprotobuf-dev
sudo apt-get install libgtk2.0-dev pkg-config python-dev python-numpy
sudo apt-get install libsnappy-dev gtk+-3.0 yasm Doxygen
sudo apt-get install libdc1394-22 libopenblas-dev liblmdb-dev
sudo apt-get install libdc1394-22-dev libjpeg-dev libpng12-dev libtiff4-dev
sudo apt-get install libjasper-dev libhdf5-serial-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libxine-dev
sudo apt-get install libgstreamer0.10-dev libgflags-dev
sudo apt-get install libgstreamer-plugins-base0.10-dev libv4l-dev libtbb-dev
sudo apt-get install libqt4-dev libfaac-dev libgoogle-glog-dev
sudo apt-get install libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev
sudo apt-get install libtheora-dev libvorbis-dev protobuf-compiler liblapack-dev
sudo apt-get install libxvidcore-dev x264 v4l-utils unzip liblapacke-dev
sudo apt-get install libavresample-dev libgphoto2-dev libavutil-dev
sudo apt-get install --no-install-recommends libboost-all-dev ccache libatlas-base-dev
- cmake
jetson
cmake -D WITH_IPP=OFF -D BUILD_opencv_python3=YES -D INSTALL_PYTHON_EXAMPLES=OFF -D BUILD_EXAMPLES=OFF -D BUILD_opencv_python3=ON -D BUILD_opencv_python2=OFF -D BUILD_opencv_java=OFF -D PYTHON3_EXECUTABLE=/usr/bin/python3.6 -D PYTHON3_INCLUDE_DIR=/usr/include/python3.6 -D PYTHON3_INCLUDE_PATH=/usr/include/python3.6 -D PYTHON3_LIBRARY=/usr/lib/aarch64-linux-gnu/libpython3.6m.so.1.0 -D HAVE_opencv_python3=ON -D PYTHON3_NUMPY_INCLUDE_DIRS=/usr/local/lib/python3.6/dist-packages/numpy/core/include -D PYTHON3_PACKAGES_PATH=/usr/local/lib/python3.6/dist-packages -D HAVE_opencv_python3=ON -D OPENCV_ENABLE_MEMORY_SANITIZER=ON -D WITH_QT=OFF -D ENABLE_CXX11=ON -D WITH_CUDA=OFF -D FFMPEG_INCLUDE_DIR=/usr/local/ffmpeg/include -D FFMPEG_LIB_DIR=/usr/local/ffmpeg/lib -D ENABLE_PRECOMPILED_HEADERS=OFF ..
ubuntu
cmake -D WITH_IPP=OFF -D BUILD_opencv_python3=YES -D INSTALL_PYTHON_EXAMPLES=OFF -D BUILD_EXAMPLES=OFF -D BUILD_opencv_python3=ON -D BUILD_opencv_python2=OFF -D BUILD_opencv_java=OFF -D PYTHON3_EXECUTABLE=/usr/bin/python3.6 -D PYTHON3_INCLUDE_DIR=/usr/include/python3.6 -D PYTHON3_INCLUDE_PATH=/usr/include/python3.6 -D PYTHON3_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.6m.so.1.0 -D HAVE_opencv_python3=ON -D PYTHON3_NUMPY_INCLUDE_DIRS=/usr/lib/python3/dist-packages/numpy/core/include -D PYTHON3_PACKAGES_PATH=/usr/lib/python3/dist-packages -D HAVE_opencv_python3=ON -D OPENCV_ENABLE_MEMORY_SANITIZER=ON -D WITH_QT=OFF -D ENABLE_CXX11=ON -D WITH_CUDA=OFF -D FFMPEG_INCLUDE_DIR=/usr/local/ffmpeg/include -D FFMPEG_LIB_DIR=/usr/local/ffmpeg/lib -D ENABLE_PRECOMPILED_HEADERS=OFF ..
- make
sudo make -j8 #这里的8表示线程的数量
cp /home/lqs/Downloads/opencv-3.4.0/build/lib/python3/* /home/lqs/anaconda3/lib/python3.7/site-packages/
- install
make install
- check
python
import cv2
cv2.__version__
- 添加环境变量
#在/etc/ld.so.conf.d/opencv.conf添加安装opencv的路径:
sudo gedit /etc/ld.so.conf.d/opencv.conf
/usr/local/lib
sudo ldconfig # 使配置生效
#在/etc/bash.bashrc中添加pkgconfig路径:
sudo gedit /etc/bash.bashrc
"""
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
"""
source /etc/bash.bashrc # 使配置生效
sudo updatedb
- 至此,安装成功,可以使用命令pkg-config --modversion opencv3查看是否安装成功以及版本号
- 卸载opencv
sudo apt-get purge libopencv*
卸载板子上的opencv
卸载编译安装的opencv
cd /home/lqs/Downloads/opencv-3.4.0/build/
sudo make uninstall && cd .. && rm -rf build
cd /usr && find . -name "*opencv*" | xargs sudo rm -rf
pkg-config --modversion opencv
测试
打开rtsp摄像头
import cv2
cap = cv2.VideoCapture("rtsp://admin:a12345678@10.16.55.150:555/Streaming/Channels/1")
ret, frame = cap.read()
while ret:
ret, frame = cap.read()
cv2.imshow("frame",frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cv2.destroyAllWindows()
cap.release()
打开rtsp摄像头并获取ntp时间戳
import cv2
video = r'/home/lqs/Documents/face_detect_yolov4_yolov4tiny_ssd-master/action_detection/BalanceBeam.avi'
cap = cv2.VideoCapture("rtsp://admin:a12345678@10.16.55.150:555/Streaming/Channels/1")
while cap.isOpened():
frame_exists, curr_frame = cap.read()
# if frame_exists:
k = cap.getRTPTimeStampSeconds()
l = cap.getRTPTimeStampFraction()
time_shift = 0x100000000
#because in the getRTPTimeStampSeconds()
#function, seconds was multiplied by 0x10000000
seconds = time_shift * k
m = (time_shift * k) + l
print("Imagetimestamp: %i" % m)
cv2.imshow('1',curr_frame)
keyCode = cv2.waitKey(1) & 0xFF
if keyCode == 27:# ESC键退出
break
cap.release()
cv2.destroyAllWindows()
编译问题
- 问题1 libavcodec/libx264.c:282:9: error: ‘x264_bit_depth’ undeclared (first use in this function); did you mean ‘x264_picture_t’?
解决办法:打开libx264.c,找到相应的代码行,将“x264_bit_depth”改成“X264_BIT_DEPTH”即可 - 问题2 ffbuild/common.mak:60: recipe for target ‘libavdevice/v4l2.o’ failed
解决办法:链接 - 问题3 /usr/include/aarch64-linux-gnu/sys/videoio.h:45:10: fatal error: opencv2/core/core_c.h: No such file or directory
解决办法:链接 - 问题4:target ‘libavutil/libavutil.so.55’ failed make: *** [libavutil/libavutil.so.55] Error 1
解决办法:编译后的源代码不干净,需要重新修改源代码来进行编译 - 问题5: Package ‘gtk±3.0’ not found
解决办法:sudo apt-get install build-essential libgtk-3-dev - 问题6: Package ‘gtk±2.0’ not found
解决办法: apt-get install libgtk2.0-dev - 问题7:fatal error: sys/videoio.h No such file or directory
- 解决办法:如果编译ffmpeg出现这个错,直接注释掉c4l2…h文件下video.h和types.h的头函数。
sudo cp /usr/include/opencv2/videoio/videoio_c.h /usr/include/x86_64-linux-gnu/sys
sudo ln -s /usr/include/x86_64-linux-gnu/sys/videoio_c.h /usr/include/x86_64-linux-gnu/sys/videoio.h