问题发生情况:由于opencv不支持写入avc1(h264)的视频,需要自己编译opencv源码,但是编译后的opencv用pyinstaller是打包不了的,所以有了如下的解决方法
- opencv提前做的准备
安装openh264:conda install -c conda-forge openh264
安装opencv依赖:
sudo apt install ubuntu-restricted-extras
sudo apt install build-essential cmake git python3-dev python3-numpy libavcodec-dev libavformat-dev libswscale-dev libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev libgtk-3-dev libpng-dev libjpeg-dev libopenexr-dev libtiff-dev libwebp-dev libopencv-dev
sudo apt remove -y x264 ffmpeg libx264-dev
sudo apt install x264 libx264-dev ffmpeg pkg-config
2.opencv编译
下载opencv源码(我是4.5.3):
https://github.com/opencv/opencv.git
当然你也可以走捷径:https://pan.baidu.com/s/1Z3KbRWyW_wYMYuUK6_SHjw
提取码:lyb8
解压后,opencv目录下新建build文件夹,再执行下边命令
cmake -D OPENCV_ENABLE_NONFREE=ON -D BUILD_opencv_python2=OFF -D WITH_GDAL=ON -D OPENCV_PYTHON3_VERSION=3.7 -D PYTHON_DEFAULT_EXECUTABLE=/home/roo/anaconda3/envs/ceak/bin/python -D PYTHON3_LIBRARY=/home/roo/anaconda3/envs/ceak/lib/python3.7 -D PYTHON3_EXECUTABLE=/home/roo/anaconda3/envs/ceak/bin/python -D PYTHON3_INCLUDE_DIR=/home/roo/anaconda3/envs/ceak/include/python3.7m -D PYTHON3_PACKAGES_PATH=/home/roo/anaconda3/envs/ceak/lib/python3.7/site-packages ..
(这里要注意,conda虚拟环境路径、python版本)
make -j$[$(nproc)-1]
sudo make install
以上安装教程来源于此链接,摘抄了些许对我有用的
3.pyinstaller打包问题
正常命令是pyinstaller demo.py
这里我们需要再加上–paths pyinstaller demo.py --paths="你的anaconda env路径/site-packages/cv2/pythonx.x"
正常情况下会出现,opencv找不到以来的lib文件和so文件的问题,提示检查opencv安装路径
正常pip install opencv-python下,cv2文件夹会有一个.so文件,大概五六十兆,而自己编译的opencv不会打包成一个so文件,而是一个so以及多个lib,这些不会被pyinstall打包,我们需要自己移进去,具体要移动的文件夹就是opencv编译后build目录下的lib文件夹,把整个lib文件夹覆盖要打包的目录
,然后打开cv2文件夹的__init__.py,修改PYTHON_EXTENSIONS_PATHS
的路径
def bootstrap():
import sys
import copy
save_sys_path = copy.copy(sys.path)
if hasattr(sys, 'OpenCV_LOADER'):
print(sys.path)
raise ImportError('ERROR: recursion is detected during loading of "cv2" binary extensions. Check OpenCV installation.')
sys.OpenCV_LOADER = True
DEBUG = False
if hasattr(sys, 'OpenCV_LOADER_DEBUG'):
DEBUG = True
import platform
if DEBUG: print('OpenCV loader: os.name="{}" platform.system()="{}"'.format(os.name, str(platform.system())))
LOADER_DIR = os.path.dirname(os.path.abspath(os.path.realpath(__file__)))
import os
PYTHON_EXTENSIONS_PATHS = [f'{os.getcwd()}/lib/python3'] #修改依赖路径
BINARIES_PATHS = []
#以下代码省略.....
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)