Cannot turn on Mac Webcam through OpenCV python
Asked Answered
A

4

9

I am new to opencv and trying to access my Macbook's built-in camera through OpenCV python but it gives an error.

import cv2

frameWidth = 640
frameHeight = 480
cap = cv2.VideoCapture(0)
cap.set(3, frameWidth)
cap.set(4, frameHeight)
cap.set(10,150)

while True:
   success, img = cap.read()
   cv2.imshow("Result", img)
   if cv2.waitKey(1) & 0xFF == ord('q'):
       break

Traceback (most recent call last):
  File "/Users/hasanaktas/PycharmProjects/OpencvPython/project3.py", line 12, in <module>
    cv2.imshow("Result", img)
cv2.error: OpenCV(4.2.0) /Users/travis/build/skvark/opencv-python/opencv/modules/highgui/src/window.cpp:376: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'imshow'

already tried changing VideoCapture(0) to VideoCapture(1) and adding the following code but still didn't help. Btw using PyCharm

cap.release()
cv2.destroyAllWindows()
Appendage answered 23/5, 2020 at 22:26 Comment(3)
What is cap.set(10,150)? Try to avoid ugly constants like that and use the parameter names defined for you.Shortterm
How are you calling it? I was told at work that Apple recently blocked anything that was not signed by an Apple Developer from accessing any of the peripherals. This gave us problems because it would not let Audacity access the mic. We had to build a shell script wrapper around it. Bash was signed so the bash script that called audacity was allowed to access the mic.Sheeb
I don't recommend using the opencv video streaming functions with Apple devices; I've never gotten them to work. Instead, maybe consider streaming from an AVCaptureSession and sending the individual frames to your function. After all, a video is merely a series of individual images. Try using the UIImageToMat function (you may have to use the c++ library).Geneviegenevieve
J
10

I had the same issue on my Mac. I simply replaced

cv2.VideoCapture(0)

with

cv2.VideoCapture(1)

works like a charm.

Jimmy answered 15/3, 2022 at 17:38 Comment(0)
D
8

There are two suggestions I would like to mention.

#1: Enable your terminal or PyCharm to reach the camera.


  • Go to System Preferences-> Security and Privacy -> Camera and add PyCharm to the list.

    • enter image description here

#2 Instead of while True use while cap.isOpened(), so you can know that PyCharm or terminal can access your camera.

  • import cv2
    
    frameWidth = 640
    frameHeight = 480
    cap = cv2.VideoCapture(0)
    cap.set(3, frameWidth)
    cap.set(4, frameHeight)
    cap.set(10,150)
    
    while cap.isOpened():
        success, img = cap.read()
        if success:
            cv2.imshow("Result", img)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
    
Dilettantism answered 1/11, 2020 at 19:23 Comment(1)
what if it's not on the list?Intermezzo
A
0

I had the same problem and I did not find any solution. By trial and error, I found that my OpenCV version is corrupted. As a result, I deleted it and install a new fresh one. You can use one of these two options: 1. Terminal run brew uninstall opencv3 to uninstall opencv then install it using sudo apt-get install libopencv-dev python-opencv command.

2. Anaconda Actually, I used this method for my own problem.

  1. open anaconda
  2. go to the environment section and select your environment as follow: enter image description here then click on installed and search for opencv: enter image description here select opencv package and uninstall it. then try to re-install opencv by selecting not installed and searching for opencv. be careful to install the correct version. enter image description here
Araucania answered 23/10, 2021 at 4:1 Comment(0)
S
-2

Try to increase frame width/height, for example use:

frameWidth = 1048 frameHeight = 1028

Signac answered 10/11, 2022 at 18:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.