【问题标题】:Error 'module' object has no attribute 'freetype'错误“模块”对象没有属性“freetype”
【发布时间】:2018-05-23 10:29:23
【问题描述】:

我正在使用此代码Link,但它显示错误 模块对象没有属性 我试图 pip install freetype 但什么也没发生。任何人都可以请指导我。

import cv2
import numpy as np   
img = np.zeros((100, 300, 3), dtype=np.uint8)

ft = cv2.freetype.createFreeType2()
ft.loadFontData(fontFileName='Ubuntu-R.ttf',
                id=0)
ft.putText(img=img,
           text='Quick Fox',
           org=(15, 70),
           fontHeight=60,
           color=(255,  255, 255),
           thickness=-1,
           line_type=cv2.LINE_AA,
           bottomLeftOrigin=True)

cv2.imwrite('image.png', img)

【问题讨论】:

  • 你安装了哪个版本的opencv?检查import cv2; print(cv2.__version__)
  • 据我所知,freetype 模块是在 opencv 3.2.0 中添加的。
  • 是python 3.3.0
  • 不是 Python 版本。 OpenCV 版本?尝试@WarrenWeckesser 的 cmets 中的命令。
  • 对不起它的 opecv 3.3.0

标签: python numpy opencv freetype freetype2


【解决方案1】:

如果 cv2.freetype 不能在 python 中运行,你仍然可以使用 freetype-py 模块。

我已经为 python2/3 的 opencv 中的 PIL 库 api 调用编写了一个包装器,可以通过以下方式使用:(从 https://github.com/bunkahle/PILasOPENCV 下载)

from __future__ import print_function
import PILasOPENCV as Image
import PILasOPENCV as ImageDraw
import PILasOPENCV as ImageFont
import cv2

font = ImageFont.truetype("arial.ttf", 30)
print(font)
im = Image.new("RGB", (512, 512), "grey")
draw = ImageDraw.Draw(im)
text = "Some text in arial"
draw.text((100, 250), text, font=font, fill=(0, 0, 0))
print(ImageFont.getsize(text, font))
mask = ImageFont.getmask(text, font)
print(type(mask))
cv2.imshow("mask", mask)
im.show()
im_numpy = im.getim()
print(type(im_numpy), im_numpy.shape, im_numpy.dtype)

它在后台使用 freetype-py 模块。 PILasOPENCV 实际上是将旧的 PIL 项目迁移到 OPENCV 的项目。使用

安装
setup.py install 

pip install PILasOPENCV 

更多细节和测试可以在github中找到。

【讨论】:

  • 我收到此错误:Traceback (most recent call last): File "/Users/jonas/Projects/SudokuSolver/SudokuDetectorPy/genTestData2.py", line 16, in <module> draw.text((30, 70), text, font=font, fill=255) File "/Users/jonas/anaconda3/lib/python3.6/site-packages/PILasOPENCV.py", line 2102, in text ink, fill = self._getink(fill) File "/Users/jonas/anaconda3/lib/python3.6/site-packages/PILasOPENCV.py", line 1795, in _getink if not self.mode[0] in ("1", "L", "I", "F") and isinstance(ink, numbers.Number): TypeError: 'NoneType' object is not subscriptable
【解决方案2】:

您只是缺少opencv-contrib,您可以使用pip install opencv-contrib-python 安装它。

【讨论】:

  • 对我不起作用。在 Ubuntu 20.10 上,我执行了 sudo apt install python3-9,然后我确保 import cv2 不起作用(未安装)然后我执行了 python3.9 -m pip install opencv-contrib-python,我可以执行 import cv2,但随后 cv2.freetype 引发了 AttributeError: module 'cv2.cv2' has no attribute 'freetype'。另外opencv-contrib-python 是“用于 Python 的非官方预构建的仅 CPU 的 OpenCV 包”,它不是 OpenCV 的扩展,以防有人和我有同样的印象。
  • 对我来说工作得很好。 Python 版本:3.8.2 和 cv2 版本:4.5.1
【解决方案3】:
  1. 安装harfbuzz 和freetype 按照这个link
  2. 构建 opencv-contrib 遵循这个link 和这个link
  3. 在您的本地站点包中添加一个指向绑定的符号链接:

    ln -s /usr/local/lib/python3.5/site-packages/cv2.cpython-35m-x86_64-linux-gnu.so $HOME/.pyenv/versions/3.5.2/lib/python3。 5/站点包/

【讨论】:

    【解决方案4】:

    [这是一个 WINDOWS 特定指南]

    我在这个问题上已经很晚了,但我希望这对某人有所帮助。

    我在底部添加了指向易于遵循的指南的链接。

    以下是您需要遵循的基本步骤:

    • 安装 CMake 和 Git(如果尚未安装)
    • 在一个文件夹中 git vcpkg (git clone https://github.com/Microsoft/vcpkg.git)
    • 使用 vcpkg 命令行安装 freetype 和 harfbuzz
      • bootstrap-vcpkg.bat
      • vcpkg install freetype:x64-windows
      • vcpkg install harfbuzz:x64-windows
    • 找到 vcpkg.cmake 文件并记下其路径
    • 确保在 Visual Studio 19 中安装了 C++ 构建工具
    • 下载并提取 OpenCV 和 OpenCV 贡献
    • 在提取的 OpenCV(不是 OpenCV-contrib)中创建一个名为“build”的空目录
    • 在 opencv-contrib (Link) 的 freetype 模块文件夹中修改 CMakeLists.txt
    • 修改并运行以下命令:
      • 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
    • 使用 CMake GUI 进行此链接中引用的更改 (LINK)
    • 进行必要的更改后,单击配置和生成。
    • 使用 Visual Studio 19 构建 DEBUG 和 RELEASE 版本
    • 通过从您设置的环境中导入 cv2.freetype 来测试安装。

    这里有一个全面的书面指南:

    另外,我制作了一个受相同教程启发的视频,可以在这里找到:

    我建议您观看视频并将本指南放在一边。

    干杯!

    【讨论】:

      【解决方案5】:

      我发现opencv-contrib-python-4.5.3.56 没有freetype。我把它降级到版本4.4.0.46,然后就可以了。

      pip3 install opencv-contrib-python==4.4.0.46
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-29
        • 2011-10-26
        • 1970-01-01
        • 2021-08-11
        • 2011-06-19
        相关资源
        最近更新 更多