QGIS导入Paddle报错无效的数据源
问题
在成功在QGIS中安装好Paddle-GPU后,使用时有个问题,当使用如下导入方式后
import paddle
QGIS会卡住,并重启一个新的QGIS界面,并在日志中显示如下错误
尝试导入cv2或者albumentations等均不会出现这种情况,但关闭新打开的界面,无视错误,可以发现Paddle是导入成功的!初步判断是Paddle某个地方和QGIS的不太兼容
解决
经过在QGIS\apps\Python39\Lib\site-packages\paddle中的探索,搞了半天终于发现问题!!!
问题出在QGIS\apps\Python39\Lib\site-packages\paddle\dataset\image.py中,如下代码,是不是很眼熟
# FIXME(minqiyang): this is an ugly fix for the numpy bug reported here
# https://github.com/numpy/numpy/issues/12497
if six.PY3:
import subprocess
import sys
import_cv2_proc = subprocess.Popen(
[sys.executable, "-c", "import cv2"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
out, err = import_cv2_proc.communicate()
retcode = import_cv2_proc.poll()
if retcode != 0:
cv2 = None
else:
import cv2
else:
try:
import cv2
except ImportError:
cv2 = None
问题就出来中间cv2那儿,sys.executable
获取到的解释器路径有问题,因为现在是在QGIS的环境了,他获取的是qgis-ltr-bin.exe,而我们需要一个python3/pythonw3的解释器。
我们可以在qgis-ltr-bin.env中新加一个环境变量
QGIS_PYTHON_INTERPRETER=D:\QGIS\bin\pythonw3.exe
然后修改image.py中的代码为如下
if six.PY3:
import subprocess
import sys
import os
exes = ["python", "python3", "pythonw", "pythonw3"]
if os.path.split(sys.executable)[-1].split(".")[0] not in exes:
sys.executable = os.environ["QGIS_PYTHON_INTERPRETER"]
import_cv2_proc = subprocess.Popen(
[sys.executable, "-c", "import cv2"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
out, err = import_cv2_proc.communicate()
retcode = import_cv2_proc.poll()
if retcode != 0:
cv2 = None
else:
import cv2
else:
try:
import cv2
except ImportError:
cv2 = None
OK,接下来保存重启QGIS,导入Paddle就不会弹窗报错了~