运行报错:Cannot find reference ‘imread‘ in ‘__init__.py‘

在pycharm上运行图像检测demo,cv2的功能报错

 __init__.py文件

import importlib
import os
import sys

from .cv2 import *
from .data import *

# wildcard import above does not import "private" variables like __version__
# this makes them available
globals().update(importlib.import_module("cv2.cv2").__dict__)

ci_and_not_headless = False

try:
    from .version import ci_build, headless

    ci_and_not_headless = ci_build and not headless
except:
    pass

# the Qt plugin is included currently only in the pre-built wheels
if sys.platform.startswith("linux") and ci_and_not_headless:
    os.environ["QT_QPA_PLATFORM_PLUGIN_PATH"] = os.path.join(
        os.path.dirname(os.path.abspath(__file__)), "qt", "plugins"
    )

# Qt will throw warning on Linux if fonts are not found
if sys.platform.startswith("linux") and ci_and_not_headless:
    os.environ["QT_QPA_FONTDIR"] = os.path.join(
        os.path.dirname(os.path.abspath(__file__)), "qt", "fonts"
    )

 可以看到CV2文件夹里子文件特别少。

 CV2中能调用的部分和CV2文件夹有关。

https://blog.csdn.net/m0_60258123/article/details/121416424

文章通过修改opencv的版本,改为4.5.3.56,问题解决了。尝试,报错。

 

 https://blog.csdn.net/baidu88vip/article/details/89313548

文章加入contrib模块,解决问题。尝试,报错。

 尝试用最简单的代码来试错

import cv2

image = cv2.imread("main.png")
cv2.namedWindow("Result", 0)
cv2.resizeWindow("Result", 1200, 600)
cv2.imshow("Result", image)
cv2.waitKey(0)

报错为:

qt.qpa.xcb: could not connect to display 
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "/home/llm/anaconda3/envs/pytorch_38/lib/python3.8/site-packages/cv2/qt/plugins" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

Available platform plugins are: xcb.

按照该文章方法尝试,查询显示窗口,并修改pycharm参数http://t.csdn.cn/xEw2Xicon-default.png?t=M85Bhttp://t.csdn.cn/xEw2X

 

 测试代码为:

import cv2
import numpy as np
import matplotlib.pylab as plt

image = cv2.imread('img.jpg')
print(image)
print(image.shape)
cv2.namedWindow("Result", 0)
cv2.resizeWindow("Result", 640, 480)
cv2.imshow('Result', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

 测试结果为:

 

 警告没有消失,但是opencv可以用。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值