Forum

Author Topic: import cv2 causes photoscan crash  (Read 2851 times)

jamiemccoll1@gmail.com

  • Newbie
  • *
  • Posts: 5
    • View Profile
import cv2 causes photoscan crash
« on: January 18, 2018, 03:09:57 AM »
Hi Forum,

I have installed both numpy and opencv-python via pip using

./python3.5 -m pip install numpy
./python3.5 -m pip install opencv-python

from the photoscan-pro/python/bin directory.

From within the Photoscan console I am then able to use numpy via:
>>> import numpy

When trying to do the same for opencv via:
>>> import cv2
this causes Photoscan to close itself and a crash report message to pop up.

From the photoscan-pro/python/bin directory I also accessed python by running:
./python3.5
>>>import numpy   #(this works)
>>>import cv2

This produces an error:
 File "<stdin>", line 1, in <module>
ImportError: /opt/ros/kinetic/lib/python2.7/dist-packages/cv2.so: undefined symbol: PyCObject_Type

So I think that Photoscan is trying to access a different cv2 than the one in its site-packages folder, which is written for python2.7 instead of Photoscan's python3.5?

Is there a way to correct this and let Photoscan use the opencv distribution in it's own folders?

Thank you!

PolarNick

  • Jr. Member
  • **
  • Posts: 97
    • View Profile
Re: import cv2 causes photoscan crash
« Reply #1 on: January 18, 2018, 06:19:15 PM »
You can build OpenCV from sources with this script (tested on Ubuntu 16.04):

Code: [Select]
# Configure path to photoscan:
PHOTOSCAN_PATH=<.../photoscan-pro>
PYTHON_PATH=${PHOTOSCAN_PATH}/python

OPENCV_VERSION=3.4.0
INSTALLATION_PATH=/opt/opencv-${OPENCV_VERSION}
sudo mkdir -p ${INSTALLATION_PATH}

# Install numpy (OpenCV requires it)
CPPFLAGS=-I${PYTHON_PATH}/include/python3.5m LDFLAGS=-L${PYTHON_PATH}/lib ${PYTHON_PATH}/bin/python3.5 -m pip install numpy==1.08

# # If you see error like "ImportError: cannot import name 'HTTPSHandler'" - you need to execute these lines and after that try to install numpy again:
#
# wget http://snapshot.debian.org/archive/debian/20110406T213352Z/pool/main/o/openssl098/libssl0.9.8_0.9.8o-7_amd64.deb
# sudo dpkg -i libssl0.9.8_0.9.8o-7_amd64.deb
# rm libssl0.9.8_0.9.8o-7_amd64.deb

wget https://github.com/opencv/opencv/archive/${OPENCV_VERSION}.zip
unzip ${OPENCV_VERSION}.zip
rm ${OPENCV_VERSION}.zip
cd opencv-${OPENCV_VERSION}

# # Alternatively you can get the hole git repository (this is slower):
# git clone https://github.com/opencv/opencv
# cd opencv
# git checkout tags/${OPENCV_VERSION}

mkdir build
cd build

sudo apt update
sudo apt install -y cmake

# Configure opencv build:
#
# You can compile opencv_contrib with this option:
#      -D OPENCV_EXTRA_MODULES_PATH=.../opencv_contrib/modules \
# If you have CUDA SDK installed - you can speedup compilation by disabling CUDA support:
#      -D WITH_CUDA=OFF \

cmake -D CMAKE_BUILD_TYPE=RELEASE \
      -D CMAKE_INSTALL_PREFIX=${INSTALLATION_PATH} \
      -D PYTHON3_EXECUTABLE=${PYTHON_PATH}/bin/python3.5 \
      ..
make -j8
# Installing OpenCV library w.r.t. INSTALLATION_PATH variable
sudo make install
cd ..

# Now you can cleanup build dir with:
# rm -rf build
# You can even cleanup opencv source code dir:
# cd ..
# rm -rf opencv-${OPENCV_VERSION}

# Add cv2 package to photoscan's bundled python
ln -s ${INSTALLATION_PATH}/lib/python3.5/site-packages/cv2.cpython-35m-x86_64-linux-gnu.so ${PYTHON_PATH}/lib/python3.5/site-packages/cv2.cpython-35m-x86_64-linux-gnu.so
« Last Edit: January 18, 2018, 06:34:25 PM by PolarNick »

jamiemccoll1@gmail.com

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: import cv2 causes photoscan crash
« Reply #2 on: January 19, 2018, 01:50:26 AM »
Thank you! I will try this shortly.