C1W1 Ungraded Lab - Import Error for OpenCV on Mac M1

I followed the instructions here to install tesnorflow and opencv for Mac M1. OpenCV installed correctly but I get the following error when I try to look up the version as mentioned in the above link. Upon searching it seems to do something with path but I couldn’t exactly understand how to resolve this. I’m on Monterey 12.4 if that helps.

Traceback (most recent call last):
File “”, line 1, in
ImportError: dlopen(/Applications/miniconda3/envs/coursera-mlops/lib/python3.8/site-packages/cv2.cpython-38-darwin.so, 0x0002): Library not loaded: @rpath/liblapack.3.dylib
Referenced from: /Applications/miniconda3/envs/coursera-mlops/lib/libopencv_core.4.5.3.dylib
Reason: tried: ‘/Applications/miniconda3/envs/coursera-mlops/lib/liblapack.3.dylib’ (no such file), ‘/Applications/miniconda3/envs/coursera-mlops/lib/liblapack.3.dylib’ (no such file), ‘/Applications/miniconda3/envs/coursera-mlops/lib/liblapack.3.dylib’ (no such file), ‘/Applications/miniconda3/envs/coursera-mlops/lib/python3.8/site-packages/…/…/liblapack.3.dylib’ (no such file), ‘/Applications/miniconda3/envs/coursera-mlops/lib/python3.8/site-packages/…/…/liblapack.3.dylib’ (no such file), ‘/Applications/miniconda3/envs/coursera-mlops/bin/…/lib/liblapack.3.dylib’ (no such file), ‘/Applications/miniconda3/envs/coursera-mlops/bin/…/lib/liblapack.3.dylib’ (no such file), ‘/usr/local/lib/liblapack.3.dylib’ (no such file), ‘/usr/lib/liblapack.3.dylib’ (no such file)

1 Like

I’ve asked a mentor with an M1 mac to help you out. While waiting, could you please try the lab on linux?

1 Like

If it helps, I tried installing tensorflow following these steps and opencv following these steps. This helps in eliminating the error above and the kernel dying problem in the other thread.
Basically used these commands

conda install conda-forge::tensorflow
conda install -c conda-forge opencv

However, now when I run the code I get the following error. I don’t understand because the code works flawlessly on my windows machine. I have a windows and mac machine but mac is my primary machine so would love to solve for this.

for image_file in image_files:
    detect_and_draw_box(image_file)

IndexError Traceback (most recent call last)
Cell In [10], line 2
1 for image_file in image_files:
----> 2 detect_and_draw_box(image_file)

Cell In [9], line 22, in detect_and_draw_box(filename, model, confidence)
19 img = cv2.imread(img_filepath)
21 # Perform the object detection
—> 22 bbox, label, conf = cv.detect_common_objects(img, confidence=confidence, model=model)
24 # Print current image’s filename
25 print(f"========================\nImage processed: {filename}\n")

File /Applications/miniconda3/envs/mlep-w1-lab/lib/python3.8/site-packages/cvlib/object_detection.py:135, in detect_common_objects(image, confidence, nms_thresh, model, enable_gpu)
131 net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA)
133 net.setInput(blob)
→ 135 outs = net.forward(get_output_layers(net))
137 class_ids =
138 confidences =

File /Applications/miniconda3/envs/mlep-w1-lab/lib/python3.8/site-packages/cvlib/object_detection.py:29, in get_output_layers(net)
25 def get_output_layers(net):
27 layer_names = net.getLayerNames()
—> 29 output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
31 return output_layers

File /Applications/miniconda3/envs/mlep-w1-lab/lib/python3.8/site-packages/cvlib/object_detection.py:29, in (.0)
25 def get_output_layers(net):
27 layer_names = net.getLayerNames()
—> 29 output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
31 return output_layers

IndexError: invalid index to scalar variable.

1 Like

Super late to this thread - but if anyone is still having the same issue, updating cvlib to version 0.2.7 worked for me

1 Like

Hi All, if anyonelese using mac m1 is still facting this issue, here’s what I’ve done to resolve this

installed opencv-python 4.5.3.56 instead of opencv 4.5.3 as mentioed on the readme file and upgrade cvlib to 0.2.7 in the requirements.txt file.

Hope this helps.

1 Like