AttributeError: module 'cv2.cv2' has no attribute 'freetype' in OpenCV
Asked Answered
J

1

6

I'm using a docker container with minimal packages installed. I installed opencv-contrib-python using pip3 install opencv-contrib-python and it installed the latest build 4.1.025 and have verified that it was running and my program was ok with it. However, now I need the freetype module but when I try:

import cv2 as cv
ft = cv.freetype.createFreeType2()

It throws the AttributeError as in the title. I tried reinstalling different versions namely: 4.0.0.21, 4.0.1.23, 4.0.1.24, 4.1.0.25 to no avail. I am using the headless version mainly(since I'm using docker and I don't need any gui functionality). Another thing I found out was not all the modules can be imported I have tried (ccalib, tracking, sfm, xobjdetect etc) and these modules throw the same error. Does this mean that some modules are not available through the pip3 installation? Thank you.

Jacal answered 3/5, 2019 at 0:38 Comment(0)
B
3

I'm pretty late to this issue, but I hope this helps someone.

I've added links to an easy to follow guide at the bottom.

Here are the essential steps, you'd need to follow:

  • Install CMake & Git (if you haven't already)
  • In a folder git vcpkg (git clone https://github.com/Microsoft/vcpkg.git)
  • Install freetype and harfbuzz using vcpkg commandline
    • bootstrap-vcpkg.bat
    • vcpkg install freetype:x64-windows
    • vcpkg install harfbuzz:x64-windows
  • Locate vcpkg.cmake file and note down its path
  • Ensure C++ build tools are installed in Visual Studio 19
  • Download and extract OpenCV & OpenCV contrib
  • Create a empty dir called "build" inside the extracted OpenCV
  • Modify CMakeLists.txt inside freetype module folder in opencv-contrib (Link)
  • Modify and run the following command:
    • cmake -G "Visual Studio 16 2019" -B D:\code\downloads\opencv-4.3.0\build -D BUILD_NEW_PYTHON_SUPPORT=ON -D BUILD_PYTHON_SUPPORT=ON -D BUILD_opencv_python3=yes -D PYTHON_DEFAULT_EXECUTABLE=D:\anaconda3\python.exe -D OPENCV_SKIP_PYTHON_LOADER=ON -D PYTHON_LIBRARY=D:\anaconda3\libs\python36.lib -D OPENCV_EXTRA_MODULES_PATH=D:\code\downloads\opencv_contrib-4.3.0\modules -D OPEN_CV_FORCE_PYTHON_LIBS=yes -D CMAKE_TOOLCHAIN_FILE=D:\code\downloads\vcpkg\scripts\buildsystems\vcpkg.cmake
  • Use CMake GUI to make changes referenced at this link (LINK)
  • Once necessary changes are made, click Configure and Generate.
  • Building DEBUG & RELEASE versions using Visual Studio 19
  • Test the installation by importing cv2.freetype from the environment you setup.

There's a comprehensive written guide over here:

Also, I've made a video inspired by the same tutorial, which can be found here:

I would recommend watching the video and keeping this guide open on the side.

Cheers!

Brigade answered 24/4, 2020 at 6:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.