1

I am having trouble identifying what causes this problem. Basically, I am working on a jupyter notebook that runs on a conda environment. I have created a file "Myutils.py" which has some helper function for later use. the file is saved in the same folder as the notebook. Giving that I have made the necessary imports in the notebook as well as in the Myutils file, I still got a name error:

In notebook:

first cell:

import cv2
import pytesseract
import numpy as np
import os
import matplotlib.pyplot as plt
from MyUtils import *
%matplotlib inline

then:

img = cv2.imread('./yolov5/runs/detect/exp/crops/address/422.jpg')
plt.imshow(remove_noise(img))

produces this error message:

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

And, in the Myutils.py, we have:

import cv2
import numpy as np

# get grayscale image
def get_grayscale(image):
    return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
hamza boulahia
  • 197
  • 1
  • 2
  • 11
  • By doing `from Myutils import *` you are overriding the earlier `import cv2` - don't know if this is what is causing your bug, but it's worth avoiding `import *` unless you are certain there are no conflicts - see e.g.: https://stackoverflow.com/questions/2386714/why-is-import-bad – match Sep 20 '21 at 10:41
  • I tried removing the * and specifying the function to be used, but the same error appeared. – hamza boulahia Sep 20 '21 at 11:40
  • Actually, after restarting the whole process and by specifying the functions in the import code line, it worked! So, I guess the problem is indeed within the import * as you mentioned. Thanks! – hamza boulahia Sep 20 '21 at 13:03
  • You can write your comment as an answer. – hamza boulahia Sep 20 '21 at 13:05

1 Answers1

1

By doing from Myutils import * you are overriding the earlier import cv2.

I don't know if this is what is causing your bug, but it's worth avoiding import * unless you are certain there are no conflicts - see e.g:

Why is "import *" bad?

match
  • 10,388
  • 3
  • 23
  • 41