为什么 Utils.py 中的库没有加载?
Why are the libraries in Utils.py not loading?
我无法确定导致此问题的原因。基本上,我正在研究在 conda 环境中运行的 jupyter notebook。
我创建了一个文件“Myutils.py”,它有一些辅助功能供以后使用。该文件保存在与笔记本相同的文件夹中。
鉴于我已经在笔记本和 Myutils 文件中进行了必要的导入,我仍然遇到名称错误:
笔记本中:
第一个单元格:
import cv2
import pytesseract
import numpy as np
import os
import matplotlib.pyplot as plt
from MyUtils import *
%matplotlib inline
然后:
img = cv2.imread('./yolov5/runs/detect/exp/crops/address/422.jpg')
plt.imshow(remove_noise(img))
产生此错误消息:
NameError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_17892/550149434.py in <module>
1 img = cv2.imread('./yolov5/runs/detect/exp/crops/address/422.jpg')
----> 2 plt.imshow(remove_noise(img))
~\Documents\Machine Learning\Projects\CIN OCR\MyUtils.py in remove_noise(image)
5 def get_grayscale(image):
6 return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
----> 7
8 # noise removal
9 def remove_noise(image):
NameError: name 'cv2' is not defined
而且,在 Myutils.py 中,我们有:
import cv2
import numpy as np
# get grayscale image
def get_grayscale(image):
return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
通过执行 from Myutils import *
,您将覆盖之前的 import cv2
。
我不知道这是否是导致您的错误的原因,但值得避免 import *
除非您确定没有冲突 - 参见例如:
Why is "import *" bad?
我无法确定导致此问题的原因。基本上,我正在研究在 conda 环境中运行的 jupyter notebook。 我创建了一个文件“Myutils.py”,它有一些辅助功能供以后使用。该文件保存在与笔记本相同的文件夹中。 鉴于我已经在笔记本和 Myutils 文件中进行了必要的导入,我仍然遇到名称错误:
笔记本中:
第一个单元格:
import cv2
import pytesseract
import numpy as np
import os
import matplotlib.pyplot as plt
from MyUtils import *
%matplotlib inline
然后:
img = cv2.imread('./yolov5/runs/detect/exp/crops/address/422.jpg')
plt.imshow(remove_noise(img))
产生此错误消息:
NameError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_17892/550149434.py in <module>
1 img = cv2.imread('./yolov5/runs/detect/exp/crops/address/422.jpg')
----> 2 plt.imshow(remove_noise(img))
~\Documents\Machine Learning\Projects\CIN OCR\MyUtils.py in remove_noise(image)
5 def get_grayscale(image):
6 return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
----> 7
8 # noise removal
9 def remove_noise(image):
NameError: name 'cv2' is not defined
而且,在 Myutils.py 中,我们有:
import cv2
import numpy as np
# get grayscale image
def get_grayscale(image):
return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
通过执行 from Myutils import *
,您将覆盖之前的 import cv2
。
我不知道这是否是导致您的错误的原因,但值得避免 import *
除非您确定没有冲突 - 参见例如:
Why is "import *" bad?