Why do I got error while video capturing with opencv - python

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).

Related

How to connect Pycharm with microphone on macOS

I have a problem with my microphone connectivity using Pycharm.
I ran the below code on Spyder and terminal and they were fine.
However, on Pycharm, it seemed run without error but just without result as well.
I see that both my terminal and Spyder have access to microphone (system preferences -> security & privacy). If anyone knows how to trigger the system to allow microphone connectivity using Pycharm, any answers will be appreciated.
Thanks!
I also tried to record my voice using pyaudio and that worked fine with Spyder and terminal, but no audio was recorded if using Pycharm (though the file was created).
simple code:
import speech_recognition as sr
r = sr.Recognizer()
with sr.Microphone() as source:
print("Speak")
audio = r.listen(source)
print("Stop")
try:
print("You said " + r.recognize_google(audio))
except:
pass
Even though it is an older question, I stumbled upon the same problem under MacOS. So maybe someone can find this useful:
Problem is that every App running on recent MacOS versions needs permissions for security relevant services, e.g. recording from the microphone. Unfortunately PyCharm did not request the needed permission, and it is not possible to set the permissions by hand. Every app you build and debug with PyCharm does not have those permissions either.
Maybe Jetbrains did already fix this, so be sure to use the latest version. For me, a little workaround did the trick: Just start Pycharm from Terminal, as long as Terminal has the microphone permissions, everything works fine. I did it with a small script:
#!/bin/sh
cd /Applications/PyCharm\ CE.app/Contents/MacOS/
./pycharm
Make sure that PyCharm also has access to microphones in the settings, and also run PyCharm as administrator when launching it.

OpenCV Python Scripts Mac "aborts"

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.

Run OpenCV script on start with imshow

I have an OpenCV/python script running at my Raspberry Pi which reads the camera and shows the stream on the RCA monitor connected to the Pi.
Now I want the script to be loaded on boot. I already tried a cronjob #reboot, the /etc/rc.locale, /etc/profile and ~/.bash_profile.
I see the red light of the camera turning on every time, but the image won't be shown. Is there any solution to run the script? (I have no external input devices connected)
Thank you!
I know its quite an old thread, but I thought you might be still interested in a solution that worked for me. Hopefully, that can help you as well:
Raspberry Pi - Autostart OpenCv-Script - Error with cv::imshow()
In short: You most likely have problems with the DISPLAY / XAUTHORITY environment variable.

How do I execute python OpenCV programs without sudo?

I am trying python OpenCV and facing multiple issues (mostly permission related) as I cannot execute the video capture scripts without sudo. On the other hand, using sudo is creating permission issues with output files when they need to be shared with other processes.
When I'm not using sudo, this is the error message I get when cv2.VideoCapture(0) is called, cv2 being opencv module:
cv2.error:/home/sidmeister/opencv/modules/videoio/src/cap_gstreamer.cpp:818: error: (-2) GStreamer: unable to start pipeline
in function cvCaptureFromCAM_GStreamer
Going through the source code I understand that gst_element_set_state( ) function is returning GST_STATE_CHANGE_FAILURE. And, as I understand, that's a permission issue!
So, I circle back to my original point, is there any way to overcome these permission issues?
Add your user to the group video
gpasswd -a sidmeister video

Raspberry pi:enable camera module

I have attached an usb camera with raspberry pi and also enable it using sudo raspi-config command . I rebooted the raspberry pi after enabling it, but it doesn't enable - so getting error (error is..mmal: Camera is not detected. Please check carefully the camera module is installed correctly)
When run command (raspistill -o image.jpg) to capture an image, I have also run update and upgrade command to update raspberry pi but doesn't solve problem.
And main thing when I run ffnpng -i /dev/video video.avi, it records video so camera working so why this error coming? And what is the solution for that.
Is there any other way or command to cature image? plz help me. Thank u in advance.
Have you installed the fswebcam module? You can get it using:
sudo apt-get install fswebcam
Images are taken using the command fswebcam <image_name>.jpg
A full tutorial can be found here from raspberrypi.org.
It looks like the problem is the connection between camera module and Raspberry PI CSI interface. Check if the CSI cable is correctly connected to CSI interface on the Raspberry PI board or try to unplug camera cable and plug it again.
raspistillyuv and raspistill are official image capturing programs for the camera module: link
USB cameras are not mounted under mmal (Only CSI). Try running v4l2-ctl --list-devices to see if your camera is mounted correctly. You will see there is a /dev/videoX like string under your camera if it's mounted correctly. Use that X value to access the camera.
As an example, in OpenCV(Python) you can capture an image as bellow,
import cv2
cap = cv2.VideoCapture(X)
ret, frame = cap.read()
cv2.imwrite("image_name.jpg",frame)
cap.release()

Categories

Resources