Why are the libraries in Utils.py not loading?

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)
~AppDataLocalTemp/ipykernel_17892/550149434.py in <module>
      1 img = cv2.imread('./yolov5/runs/detect/exp/crops/address/422.jpg')
----> 2 plt.imshow(remove_noise(img))

~DocumentsMachine LearningProjectsCIN OCRMyUtils.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)

Answer

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?