I have recently upgraded my MacBook to run Catalina. This seems to have broken webcam access to secondary processes.
The following code ran like a charm on Mojave but won't let me access the webcam in the secondary process.
#!/usr/bin/env python3
from multiprocessing import Process
import cv2
def capture_cam(process):
cap = cv2.VideoCapture(0)
(ret, frame) = cap.read()
print(process, ret)
p_cam = Process(target=capture_cam, args=['Secondary'])
p_cam.start()
p_cam.join()
capture_cam('Primary')
The output under Catalina is
$ export OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES
$ python test.py
Primary True
The problem is very curious to me. I guess it has something to do with the new privacy settings in 10.15.
Related
The following code has now started to return an error to me:
cv2 import
capture = cv2.VideoCapture(1)
capture.release()
return
[ WARN:0] global /tmp/pip-req-build-qacpj5ci/opencv/modules/videoio/src/cap_v4l.cpp (893) open VIDEOIO(V4L2:/dev/video1): can't open camera by index
I have a video grabber connected and I had no problems with it earlier.
I don't see '/dev/video1' on my computer, but I see only '/dev/video0'.
From the information I found on the Internet, it seems to me that the most probable reason is that I did not close the capturing device.
I use ubuntu 18.04, python 3.6.9 and opencv-python===4.4.0.46.
I was running the code with jenkins as sudo (and sometimes the job was stopped manualy).
I tried to restart the computer and some other things I found on the Internet, but unfortunately nothing helped.
I'll add that at first I also couldn't run the webcam script in the computer (i.e. with parameter 0), but after some time I succeeded (in the meantime I was whisking some suspicious processes).
I have a project written in Python to capture the video of my webcam camera using opencv.
When i run this file it had an eror:
Error running 'Capture_image': Cannot run program"C:/Python/python.exe"(in directory"D:/Vien/Python/Python Program"): CreateProcess error = 2, specified file could not be found
Before this I've install 2 version of Python that is Python 3.8.2 and Python 3.6.8 and set up the interpreter with Python 3.6.8 and the link is " C:/Python 3.6.8 64bit/Python.exe "
Moreover, when I install Python I've install the Python 3.8.2 version first and create a directory to put it in disk C with file" Python" then I changed the name into "Python 3.8.2"
So I don't know what caused this error and how to fix this.
Can anyone realize what mistake I've made?
Thank you!
Here is the code( But I don't think the code caused this error):
import cv2
cap = cv2.VideoCapture(0)
while(True):
ret,frame = cap.read()
cv2.imshow('frame', frame)
cv2.waitKey(5)
cap.release()
I am trying to install OpenCv and run some code on PyCharm on Mac.
I have installed the opencv package as well as numpy but when I run the code this error shows up on Pycharm IDE: "cannot find reference 'VideoCapture' in __init__.py"
In fact, every time I write example_variable = cv2., PyCharm is not giving me any option to autocomplete with a function attached to cv2. Why is this happening?
Here's the code I am trying to run. I tried also running other codes but the same error displays:
import cv2
import numpy as np
cap = cv2.VideoCapture()
while True:
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame',gray)
if cv2.waitKey(1) & OxFF == ord('q'):
break
cap.release
cv2.destroyAllWindows()
Any tips on how to run this code smoothly? The end goal is to have my front webcam capture live video and react to it.
Try one of this solutions:
Right-click on source Directory
Mark Directory as -> source root
File --> Invalidate Caches / Restart... -> Invalidate and Restart
OR:
In your IDE, go to settings -> preferences -> django
Ensure your Root directory, your settings.py and your manage.py are in the right place
if you are in Django.
After all that, you can also delete the .idea folder in your root directory and restart the IDE.
pip install --force-reinstall --no-cache -U opencv-python==4.5.5.62
use this command line to avoid the reference issue.
Instead of import cv2 use from cv2 import cv2
I agree with "from cv2 import cv2"
there have an another way to solve problems:
** Do Not Use Pip Install **
I try to use download opencv_python-4.5.5.62-cp36-abi3-win_amd64.whl file and then manual double click to install
wait a minute, Pycharm IDE successfully Show code completion.
by the way,can your code run?
if code can run, above will help you a bit.
and your path(project python) should be Englisgh ; not be Chinese or other language.
This worked for
Before I was using python from Anaconda, instead
Choose the default python interpreter
So I'm just trying to run the basic OpenCV program
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA)
# Display the resulting frame
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
But for some reason when I try to run it (with python 2 or 3) I get this weird abort statement
[1] 74309 abort python3 index.py
(I'm guessing the 5 digit number is the PID) However, it does work if I pass in a path to an already existing video in the VideoCapture function. I am a beginner in all of this so I'm not really sure what the problem is
Thanks :)
I found the solution!
I tried running the script using the default terminal that comes with mac and it worked :) So it seems there was some weird issue with the third party terminal (iTerm) I was using
You have to authorize iTerm to access the camera (in System Preferences). By default, neither Terminal nor iTerm has this permission. macOS Mojave just asks to allow iTerm and your Python program works fine!
It's not a problem with iTerm, or maybe was a problem.
I had the same problem running OpenCV from VSCode. It turned out to be indeed permission error for accessing the camera on the new macOS. I couldn't get VSCode to ask for the permission (or add it manually through system preferences).
My solution was to run VSCode as root using
sudo /Applications/Visual Studio Code.app/Contents/MacOS/Electron
It's not the cleanest solution, but it worked for me.
In VSCode ,
1.type cmd+shift+p
2.type "shell command: Install code in PATH"
3. Close vscode
4. Use "sudo code" to open vscode
5. It will give warning not to run as a root user
6. Ignore the warning and run the file , you will not get the "Abort trap: 6" error anymore.
I am using Data science toolbox running ubuntu 14.04 through vagrant in windows.
I installed OpenCV and tried a very simple python code.
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('image1.jpg' , cv2.IMREAD_GRAYSCALE)
cv2.imshow('image' , img)
cv2.waitKey(0)
cv2.destroyAllwindows()
When i run this code i get an error -
(image:1267): Gtk-WARNING **: cannot open display:
I have searched on internet but not able to find a solution working for me.
I have tried -
export DISPLAY=:0.0
export DISPLAY=:0
and many more.
I have also tried xhost +localhost but this gives another error
xhost: unable to open display ""
Anyone know a way to solve this problem.
You can forward the display to your host, in your Vagrantfile, add the following lines
config.ssh.forward_agent = true
config.ssh.forward_x11 = true
You will need a X-server running on your windows machine (I personally use Quartz on my mac, you would need to get an equivalent for windows, like Xming) and then when you vagrant up to boot your VM, when you will run an X-program it will pop-up on your host.