Opencv on MAC Is Not Opening Usb Web Camera

OpenCV crash on OS X when reading USB cam in separate process

Your problem stems from the way python spans its subprocess using os.fork. The OpenCV video backend on Mac uses QTKit which uses CoreFoundation these parts of MacOS are not save to run in a forked subprocess, sometimes they just complain, sometimes they crash.

You need to create the subprocess without using os.fork. This can be achieved with python 2.7.

You need to use billiard (https://github.com/celery/billiard/tree/master/billiard)
It serves as a replacement for pythons multiprocessing and has some very useful improvements.

from billiard import Process, forking_enable
import cv2 #does matter where this happens when you don't use fork

def cam():
vc = cv2.VideoCapture(0) #modify 0/1 to toggle between USB and internal camera
while True:
junk,image = vc.read()

forking_enable(0) # Is all you need!
camProcess = Process( target=cam )
camProcess.start()

while True:
pass

alright, lets add a more complete example:

from billiard import Process, forking_enable

def cam(cam_id):
import cv2 #doesn't matter if you import here or in cam()
vc = cv2.VideoCapture(cam_id) #modify 0/1 to toggle between USB and internal camera
while True:
junk,image = vc.read()
cv2.imshow("test",image)
k = cv2.waitKey(33)
if k==27: # Esc key to stop
break

def start():

forking_enable(0) # Is all you need!
camProcess = Process(target=cam, args=(0,))
camProcess.start()

if __name__ == '__main__':
start()
cam(1)

You need two cameras attached for this:It should open a window and run each camera in a separate process (one on the main process one in a spawned one). I use this strategy to stream images form multiple cameras at once each in its own python process.

Opencv & OSX: USB camera acess

As a OSX user and Opencv user, troubles with OSX & opencv are more than reccurent.

(I'm not stalking you but following your progression).

There is a trick/fix for your issue.

You just need to disable and re-enable the iSight camera to get access to which ever camera you want afterward.

Step 0:

Create a backup of your OSX install (You should always have one)

Step 1:

Desactivate the SIP, this is something you should never do if you aren't sure of what you are doing but there is no other choices.
Reboot your mac, during boot press command+R.
Once the UI has loaded, go in the top bar and choose utilty->terminal and type:

csrutil disable; reboot

this will desable your SIP and reboot your mac into "normal" mode.

Step2:

sudo chmod a-r /System/Library/Frameworks/CoreMediaIO.framework/Versions/A/Resources/VDC.plugin/Contents/MacOS/VDC
sudo chmod a-r /System/Library/PrivateFrameworks/CoreMediaIOServicesPrivate.framework/Versions/A/Resources/AVC.plugin/Contents/MacOS/AVC
sudo chmod a-r /System/Library/QuickTime/QuickTimeUSBVDCDigitizer.component/Contents/MacOS/QuickTimeUSBVDCDigitizer
sudo chmod a-r /Library/CoreMediaIO/Plug-Ins/DAL/AppleCamera.plugin/Contents/MacOS/AppleCamera
sudo chmod a-r /Library/CoreMediaIO/Plug-Ins/FCP-DAL/AppleCamera.plugin/Contents/MacOS/AppleCamera

Step3: (optional)
test in opencv, you should get an error when trying
cv::VideoCapture cap(0);
error is a block of text telling you have no camera/drivers, its normal.

Step4:
reboot

Step5:

sudo chmod a+r /System/Library/Frameworks/CoreMediaIO.framework/Versions/A/Resources/VDC.plugin/Contents/MacOS/VDC
sudo chmod a+r /System/Library/PrivateFrameworks/CoreMediaIOServicesPrivate.framework/Versions/A/Resources/AVC.plugin/Contents/MacOS/AVC
sudo chmod a+r /System/Library/QuickTime/QuickTimeUSBVDCDigitizer.component/Contents/MacOS/QuickTimeUSBVDCDigitizer
sudo chmod a+r /Library/CoreMediaIO/Plug-Ins/DAL/AppleCamera.plugin/Contents/MacOS/AppleCamera
sudo chmod a+r /Library/CoreMediaIO/Plug-Ins/FCP-DAL/AppleCamera.plugin/Contents/MacOS/AppleCamera

Step6:

Retest in opencv; unplug your usb camera try:
cv::VideoCapture cap(1);
error output should tell you that you have a range of 0-0 in cv::VideoCapture instead of the 0-1 that xcode is telling you by default.

Congratulation you can now access USB/webcam in opencv on Xcode and Sierra.

Step7:(optional BUT HIGLY recommanded)

Reactivate your SIP (if you arent sure of what you are doing in the terminal/download on your mac DO IT).
Reboot your mac press command+R and once the UI is here choose Utility->Terminal and type:

csrutil enable;

THEN create a new restauration point of your mac ("OSX Serra - date - Opencv access Webcam" or whatever with an EXPLICIT name).

Then in terminal type:

reboot

Step8: Enjoy.

Once you have an opencv setup running and if you don't use Objective-C/iDevice programming you should avoid to update Xcode anymore has it tend to break opencv install pretty hard

OpenCV command line app can't access camera under macOS Mojave

The problem was that the c++ program, for whatever reason, wasn't requesting camera access. I took the advice of @gerwin in the comments to give it a try with Python. Running that program from Terminal resulted in Terminal asking for camera access. Once I granted that, the c++ program was able to access the camera when run from Terminal.

As far as CodeRunner, I'm not sure how to get CodeRunner to run Python programs under a virtual environment so I haven't been able to run a Python OpenCV program to get it to ask for camera access. So at the moment I can't use CodeRunner to run a c++ program that accesses the camera.

Unable to detect web cam in OpenCV

Ok, first... does your webcam work with other webcam applications?

Your code is a little messed up! You create a window named Example2_9 , but you try to draw with cvShowImage() to another window (named cam) that doesn't exist! Fix that! Replace the occurrences of cam by Example2_9.

IF that doesn't solve the problem, I would probably replace the beginning of main() by this:

int main( int argc, char** argv ) 
{
cvNamedWindow( "Example2_9", CV_WINDOW_AUTOSIZE );
CvCapture* capture;

capture = cvCreateCameraCapture( -1 ); //yes, if 0 doesn't work try with -1
assert( capture != NULL );

You code lacks error checking in several places, be careful. One of the functions could be returning an error and you'll never know until you do the proper check.

You can also find a bunch of other OpenCV examples on Google that calls cvCaptureFromCAM() instead of cvCreateCameraCapture(). If the above suggestions doesn't work, try it!

One more thing, on my Macbook Pro I have to use cvCaptureFromCAM(0) for the application to work. On Linux, I always use cvCaptureFromCAM(-1).



Related Topics



Leave a reply



Submit