C1W1 Ungraded lab. Creating the detect_and_draw_box function

When running following snipped in my jupyter notebook in Docker following error occurs:

import cv2
import cvlib as cv
from cvlib.object_detection import draw_bbox


def detect_and_draw_box(filename, model="yolov3-tiny", confidence=0.5):
    """Detects common objects on an image and creates a new image with bounding boxes.

    Args:
        filename (str): Filename of the image.
        model (str): Either "yolov3" or "yolov3-tiny". Defaults to "yolov3-tiny".
        confidence (float, optional): Desired confidence level. Defaults to 0.5.
    """
    
    # Images are stored under the images/ directory
    img_filepath = f'images/{filename}'
    
    # Read the image into a numpy array
    img = cv2.imread(img_filepath)
    
    # Perform the object detection
    bbox, label, conf = cv.detect_common_objects(img, confidence=confidence, model=model)
    
    # Print current image's filename
    print(f"========================\nImage processed: {filename}\n")
    
    # Print detected objects with confidence level
    for l, c in zip(label, conf):
        print(f"Detected object: {l} with confidence level of {c}\n")
    
    # Create a new image that includes the bounding boxes
    output_image = draw_bbox(img, bbox, label, conf)
    
    # Save the image in the directory images_with_boxes
    cv2.imwrite(f'images_with_boxes/{filename}', output_image)
    
    # Display the image with bounding boxes
    display(Image(f'images_with_boxes/{filename}'))
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-5-20bb683bd852> in <module>
      1 import cv2
----> 2 import cvlib as cv
      3 from cvlib.object_detection import draw_bbox
      4 
      5 

/opt/conda/lib/python3.8/site-packages/cvlib/__init__.py in <module>
      6 from .face_detection import detect_face
      7 from .object_detection import detect_common_objects
----> 8 from .gender_detection import detect_gender
      9 from .utils import get_frames, animate

/opt/conda/lib/python3.8/site-packages/cvlib/gender_detection.py in <module>
      1 import os
      2 import cv2
----> 3 from tensorflow.keras.utils import get_file
      4 
      5 initialize = True

/opt/conda/lib/python3.8/site-packages/tensorflow/__init__.py in <module>
     39 import sys as _sys
     40 
---> 41 from tensorflow.python.tools import module_util as _module_util
     42 from tensorflow.python.util.lazy_loader import LazyLoader as _LazyLoader
     43 

/opt/conda/lib/python3.8/site-packages/tensorflow/python/__init__.py in <module>
     38 # pylint: disable=wildcard-import,g-bad-import-order,g-import-not-at-top
     39 
---> 40 from tensorflow.python.eager import context
     41 
     42 # pylint: enable=wildcard-import

/opt/conda/lib/python3.8/site-packages/tensorflow/python/eager/context.py in <module>
     37 from tensorflow.python.client import pywrap_tf_session
     38 from tensorflow.python.eager import executor
---> 39 from tensorflow.python.eager import monitoring
     40 from tensorflow.python.framework import c_api_util
     41 from tensorflow.python.framework import device as pydev

/opt/conda/lib/python3.8/site-packages/tensorflow/python/eager/monitoring.py in <module>
     26 from tensorflow.python import pywrap_tfe
     27 from tensorflow.python.client import pywrap_tf_session
---> 28 from tensorflow.python.framework import c_api_util
     29 from tensorflow.python.util import compat
     30 

/opt/conda/lib/python3.8/site-packages/tensorflow/python/framework/c_api_util.py in <module>
    155 
    156 
--> 157 @tf_contextlib.contextmanager
    158 def tf_buffer(data=None):
    159   """Context manager that creates and deletes TF_Buffer.

AttributeError: module 'tensorflow.python.util.tf_contextlib' has no attribute 'contextmanager'

Do you have any ideas what may go wrong?

Hi @el_comendante
First of all welcome to our community
Which approach are you using to run this ungraded lab?
Conda vs Docker, Windows vs Linux?
Which is the python version of your environment?
Best regards

Hi @fabioantonini,

Thank you for your reply.

I am using Docker on Linux Ubuntu.

docker pull deeplearningai/mlepc1w1-ugl:jupyternb

This is the exact version of the docker which I pulled from deeplearningai DTR.
As a next step I run Docker container in an interactive mode and I open jupyter notebook in my web browser on localhost:8888. Everything seems to be working correctly until import cvlib as cv which throws an exception.

I use Python environment encapsulated in the Docker image mentioned above, that’s why it’s surprising for me that I see such error while running jupyter notebook.

Any suggestions what might be wrong?

Best Regards,

Hi @el_comendante

I agree that what you see is quite strange. :sweat:
I have a Ubuntu Linux 18.04.
To run the docker image I ran
sudo docker run -it --rm -p 8888:8888 -p 8000:8000 --mount type=bind,source="$(pwd)",target=/home/jovyan/work deeplearningai/mlepc1w1-ugl:jupyternb

and I didn’t find the issue you have. Have you run the docker with ‘sudo’?

BR

yes, docker container is run with sudo command. :frowning:

I was able to fix my environment.

Maybe it will be useful for others.

It seems I pulled docker image without sudo command.

I removed docker image and pulled it once again with sudo and now I do not see this error anymore.

@fabioantonini thanks for assistance!

Warm Regards,

1 Like