python: opencv root directory

  • Last Update :
  • Techknowledgy :

You can use cv2.__file__ to get path to the module and then use os.path to resolve symlinks and do some path manipulation. This line of code returns the directory of the haarcascades files on my Mac OS homebrew installation. It may work on other installations too.

from os.path
import realpath, normpath
normpath(realpath(cv2.__file__) + '../../../../../share/OpenCV/haarcascades')

cv2.data.haarcascades - string pointing to a directory, e.g.:

>>> import cv2
>>> cv2.data
<module 'cv2.data' from 'C:\\Users\\USERNAME\\Anaconda3\\envs\\py36\\lib\\site-packages\\cv2\\data\\__init__.py'>
   >>> cv2.data.haarcascades
   'C:\\Users\\USERNAME\\Anaconda3\\envs\\py36\\lib\\site-packages\\cv2\\data\\'
   >>> cv2.__version__
   '4.0.0'

So you could do:

if hasattr(cv2, 'data'):
   print('Cascades are here:', cv2.data.haarcascades)
else:
   print('This may not work:')
print(normpath(realpath(cv2.__file__) + '../../../../../share/OpenCV/haarcascades'))

One convention you can use to deal with such portability issues is use configuration files. In your case, you could have a file ~/.myprojectrc, which could contain:

[cv]
cvroot = '/usr/local/Cellar/opencv/2.4.2/share/OpenCV/`

Files in this format can be read by ConfigParser objects, which should go something like:

import ConfigParser
import os
CONFIG = ConfigParser.ConfigParser()
config_path = os.path.join(os.path.expanduser('~'), '.myprojectrc')
if not os.path.exists(config_path):
   raise Exception('You need to make a "~/.myprojectrc" file with your cv path or something')
CONFIG.read(config_path)

   ...

   cv_root = CONFIG.get('cv', 'cvroot')

Suggestion : 2

I am using OpenCV for various object detectors, and I am finding it difficult to write portable code.,Is there a variable that holds the OpenCV root for OpenCV? That way I could write something like:,For instance, to load a face detector, on a mac with OpenCV installed via homebrew, I have to write:,UPDATE: It looks like this is not just a problem for me; it is also a problem for the OpenCV documentation. The documentation contains the following broken example code:

For instance, to load a face detector, on a mac with OpenCV installed via homebrew, I have to write:

haar = cv.Load('/usr/local/Cellar/opencv/2.4.2/share/OpenCV/haarcascades/haarcascade_frontalface_default.xml')

Is there a variable that holds the OpenCV root for OpenCV? That way I could write something like:

haar = cv.Load(os.path.join(OpenCVRoot, "haarcascades",
   "haarcascade_frontalface_default.xml"))

UPDATE: It looks like this is not just a problem for me; it is also a problem for the OpenCV documentation. The documentation contains the following broken example code:

>>>
import cv
   >>>
   image = cv.LoadImageM("lena.jpg", cv.CV_LOAD_IMAGE_GRAYSCALE) >>>
   cascade = cv.Load("../../data/haarcascades/haarcascade_frontalface_alt.xml") >>>
   print cv.HaarDetectObjects(image, cascade, cv.CreateMemStorage(0), 1.2, 2, 0, (20, 20))[((217, 203, 169, 169), 24)]

You can use cv2.__file__ to get path to the module and then use os.path to resolve symlinks and do some path manipulation. This line of code returns the directory of the haarcascades files on my Mac OS homebrew installation. It may work on other installations too.

from os.path
import realpath, normpath
normpath(realpath(cv2.__file__) + '../../../../../share/OpenCV/haarcascades')

Suggestion : 3

Last updated: Sep 05 '16 , asked 2016-09-05 11:42:42 -0500 , Asked: 2016-09-05 11:41:52 -0500

I would like to access

/usr/local / share / OpenCV / haarcascades / haarcascade_frontalface_default.xml

from OpenCV distribution, but in a platform-independent way, e.g. in Python

os.path.join(cv2.OPENCV_DATA_DIR, "haarcascades", "haarcascade_frontalface_default.xml")

Suggestion : 4

Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.,To install OpenCV using the terminal on Ubuntu from this article http://namhuy.net/1205/how-to-install-opencv-on-ubuntu.html,Making statements based on opinion; back them up with references or personal experience., Short story where a couple trapped in a fallout shelter is afflicted by roaches

Per answer https://askubuntu.com/a/990597/455406 , if you installed manually you can try

pkg - config--libs--cflags opencv

To install OpenCV using the terminal on Ubuntu from this article http://namhuy.net/1205/how-to-install-opencv-on-ubuntu.html

$ su -
   # apt - get update
# apt - get install build - essential
# apt - get install libavformat - dev
# apt - get install x264 v4l - utils ffmpeg
# apt - get install libcv2 .3 libcvaux2 .3 libhighgui2 .3 python - opencv opencv - doc libcv - dev libcvaux - dev libhighgui - dev

Suggestion : 5

Optional: on Linux use some of the manylinux images as a build hosts if maximum portability is needed and run auditwheel for the wheel after build,Option 1 - Main modules package: pip install opencv-python,Option 3 - Headless main modules package: pip install opencv-python-headless,You'll have the wheel file in the dist folder and you can do with that whatever you wish Optional: on Linux use some of the manylinux images as a build hosts if maximum portability is needed and run auditwheel for the wheel after build Optional: on macOS use delocate (same as auditwheel but for macOS) for better portability

If you would like the build produce all compiler commands, then the following combination of flags and environment variables has been tested to work on Linux:

export CMAKE_ARGS = '-DCMAKE_VERBOSE_MAKEFILE=ON'
export VERBOSE = 1

python3 setup.py bdist_wheel--build - type = Debug

Suggestion : 6

Checkout MediaPipe repository. $ cd $HOME $ git clone https://github.com/google/mediapipe.git # Change directory into MediaPipe root directory $ cd mediapipe , Checkout MediaPipe repository. $ git clone https://github.com/google/mediapipe.git # Change directory into MediaPipe root directory $ cd mediapipe , Checkout MediaPipe repository. $ git clone https://github.com/google/mediapipe.git $ cd mediapipe , Checkout MediaPipe repository. C:\Users\Username\mediapipe_repo> git clone https://github.com/google/mediapipe.git # Change directory into MediaPipe root directory C:\Users\Username\mediapipe_repo> cd mediapipe

$ cd $HOME
$ git clone https: //github.com/google/mediapipe.git

   # Change directory into MediaPipe root directory
$ cd mediapipe
$ sudo apt - get install - y\
libopencv - core - dev\
libopencv - highgui - dev\
libopencv - calib3d - dev\
libopencv - features2d - dev\
libopencv - imgproc - dev\
libopencv - video - dev
# WORKSPACE
new_local_repository(
   name = "linux_opencv",
   build_file = "@//third_party:opencv_linux.BUILD",
   path = "/usr",
)

# opencv_linux.BUILD
for OpenCV 2 / 3 installed from Debian package
cc_library(
   name = "opencv",
   linkopts = [
      "-l:libopencv_core.so",
      "-l:libopencv_calib3d.so",
      "-l:libopencv_features2d.so",
      "-l:libopencv_highgui.so",
      "-l:libopencv_imgcodecs.so",
      "-l:libopencv_imgproc.so",
      "-l:libopencv_video.so",
      "-l:libopencv_videoio.so",
   ],
)
# WORKSPACE
new_local_repository(
   name = "linux_opencv",
   build_file = "@//third_party:opencv_linux.BUILD",
   path = "/usr",
)

# opencv_linux.BUILD
for OpenCV 4 installed from Debian package
cc_library(
   name = "opencv",
   hdrs = glob([
      # Uncomment according to your multiarch value(gcc - print - multiarch):
      # "include/aarch64-linux-gnu/opencv4/opencv2/cvconfig.h",
      # "include/arm-linux-gnueabihf/opencv4/opencv2/cvconfig.h",
      # "include/x86_64-linux-gnu/opencv4/opencv2/cvconfig.h",
      "include/opencv4/opencv2/**/*.h*",
   ]),
   includes = [
      # Uncomment according to your multiarch value(gcc - print - multiarch):
      # "include/aarch64-linux-gnu/opencv4/",
      # "include/arm-linux-gnueabihf/opencv4/",
      # "include/x86_64-linux-gnu/opencv4/",
      "include/opencv4/",
   ],
   linkopts = [
      "-l:libopencv_core.so",
      "-l:libopencv_calib3d.so",
      "-l:libopencv_features2d.so",
      "-l:libopencv_highgui.so",
      "-l:libopencv_imgcodecs.so",
      "-l:libopencv_imgproc.so",
      "-l:libopencv_video.so",
      "-l:libopencv_videoio.so",
   ],
)
# WORKSPACE
new_local_repository(
   name = "linux_opencv",
   build_file = "@//third_party:opencv_linux.BUILD",
   path = "/usr/local",
)

# opencv_linux.BUILD
for OpenCV 2 / 3 installed to / usr / local
cc_library(
   name = "opencv",
   linkopts = [
      "-L/usr/local/lib",
      "-l:libopencv_core.so",
      "-l:libopencv_calib3d.so",
      "-l:libopencv_features2d.so",
      "-l:libopencv_highgui.so",
      "-l:libopencv_imgcodecs.so",
      "-l:libopencv_imgproc.so",
      "-l:libopencv_video.so",
      "-l:libopencv_videoio.so",
   ],
)
# WORKSPACE
new_local_repository(
   name = "linux_opencv",
   build_file = "@//third_party:opencv_linux.BUILD",
   path = "/usr/local",
)

# opencv_linux.BUILD
for OpenCV 4 installed to / usr / local
cc_library(
   name = "opencv",
   hdrs = glob([
      "include/opencv4/opencv2/**/*.h*",
   ]),
   includes = [
      "include/opencv4/",
   ],
   linkopts = [
      "-L/usr/local/lib",
      "-l:libopencv_core.so",
      "-l:libopencv_calib3d.so",
      "-l:libopencv_features2d.so",
      "-l:libopencv_highgui.so",
      "-l:libopencv_imgcodecs.so",
      "-l:libopencv_imgproc.so",
      "-l:libopencv_video.so",
      "-l:libopencv_videoio.so",
   ],
)

Suggestion : 7

February 8, 2021 By 7 Comments

#include <opencv2/opencv.hpp>
using namespace cv;

int main(void) {

	// Read image in GrayScale mode
	Mat image = imread("boy.jpg", 0);

	// Save grayscale image
	imwrite("boyGray.jpg", image);

	// To display the image
	imshow("Grayscale Image", image);
	waitKey(0);

	return 0;
}
mkdir build
cd build
cmake - G "Visual Studio 16 2019"..
cmake--build.--config Release
cd..
   .\build\ Release\ sampleCode.exe
conda create--name virtualenv python = 3.8
conda activate virtualenv
pip install opencv - contrib - python
# activate environment
conda activate virtualenv

# start python prompt
python

#
import cv2 and print version
import cv2
print(cv2.__version__)

# If OpenCV is installed correctly, the above command should output OpenCV version.

# Exit and deactivate environment
exit()
conda deactivate