opencv issues with M1 MAC - OpenCV imshow doesnot work

Viewed 7575

I purchased a M1 Mac. Is anyone having issues with imshow with opencv. I did pip install opencv-python and brew install opencv and brew install opencv as well.

import cv2
import urllib
import numpy as np
import requests
url = 'https://www.visitcalifornia.com/sites/visitcalifornia.com/files/styles/welcome_image/public/vc_crtr_borntobewild_module_mendocino_st_rf_623667652_1280x640.jpg'
from skimage import io
img = io.imread(url)
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
cv2.imshow('URL Image', img)
cv2.waitKey()

and also


import cv2

cv2.namedWindow("preview")
vc = cv2.VideoCapture(0)

if vc.isOpened(): # try to get the first frame
    rval, frame = vc.read()
else:
    rval = False

while rval:
    cv2.imshow("preview", frame)
    rval, frame = vc.read()
    key = cv2.waitKey(20)
    if key == 27: # exit on ESC
        break

cv2.destroyWindow("preview")
vc.release()

does not work for me

6 Answers

It was solved by

 pip install opencv-python opencv-python-headless

I was able to solve this by building from the source code of OpenCV 4.5, use this link to get the source code

Try to install Miniconda from https://docs.conda.io/en/latest/miniconda.html then activate it and try to install cv2 pip install opencv-python getting 80-90 FPS for just reading image & showing from my side, all are working in M1 using Miniconda

Opening the terminal in rosetta and doing a pip install(inside virtual environment) helped me build the opencv. Before this , while I was doing it with the M1 terminal, it was giving errors stating some architecture issue. Guess this did the trick to me. Create Virtual Environment -> Activate it -> (Rosetta Terminal) -> pip3 install opencv-python

Install OpenCV using pip. This normally gives a ffmpeg error, so install that first

First do:

pip3 install ffmpeg

and then

pip3 install opencv-python

I thought I had the same issue. The window was not popping up when using cv2.imshow() or cv2.namedWindow(). I realized hours later that the window was there in the Mac menu bar, I just had to click on it.

Related