Raspberry Pi Camera Module.Check if connected - python

I'm programming a small application in python for the raspberry pi 3. The application uses the camera module.
I want to check if the camera is connected to the raspberry pi. The only way i found so far of doing just that is like this:
from picamera import PiCamera
try:
piCamera = PiCamera()
except Exception as e:
print("camera is not conencted")
While this works. I don't like this because if the camera is connected i'm suddenly using its resources and creating a piCamera object where i might not need it.
So my question is. Is there any better way of doing this? Something that just checks if the camera is connected.

Related

Python: How to get image from USB Webcam?

I am very new in programming with python :)
My Setup:
Windows 11 Pro
Python 3.7
OpenCV 4.7
Webcam: HD Pro Webcam C920 from logitech
I want to access the camera image in Python. Everything works with my integrated camera.
When I want to access the USB camera, nothing works. I do not get video from my USB webcam.
I try the following code to see, if there is a camera.
import cv2
cap = cv2.VideoCapture(0)
print(cap.isOpened())
It works with my integrated Webcam. I get a True
With cap = cv2.VideoCapture(2) i get False
But if i try this with cap = cv2.VideoCapture(1) i get nothing back and there is no error. The program just keeps running. My conclusion: Somehow the USB camera has to work. Otherwise I would also expect a False for cap = VideoCapture(1).
I have already found and tried many things on the Internet. Unfortunately, nothing has helped.
I have also tried pygame, but that does not seem to run under windows.
How do I get video from my USB camera?
Thanks for your help :)

Trying to run Raspberry Pi camera via SSH in remote Windows desktop

I'm trying to edit my raspberry program files from network via SSH. For trial and error, I had to look at how the camera is going (seeing video lives, editing the picture quality, maybe adding image recognition in the future). The program can't run from the SSH, at least the part that requires "camera view" to open usually in Linux desktop.
Is there any way to do this correctly? I know Raspberry Pi camera library ran on SystemX, as so the GUI library Tkinter, maybe there's a way to cross this somehow to .NET windows GUI?
edit 1: I use Raspbian OS 32 bit for the OS. I use picamera library. If it helps, there's an error says "PiCameraMMALError...:failed to create MMAL component..:out of memory." I tried connecting to SSH via Windows Powershell and Linux subsystem, both gave me same errors.
edit 2: adding error image, might be useful.
I wonder if there's some mechanism which requires Pi to be connected to monitor to display the camera directly? Or maybe data bandwidth problem? Or just SystemX incompability on ssh hosts?
edit[3]: Adding code snippets to make error checking easier. The code is mostly still just activating camera functions
import tkinter as tk
from picamera import PiCamera
from time import sleep
#camera initiation
camera = PiCamera()
camera.rotation = 180
camera.resolution = (1080, 1080)
#camera start
def cameraStart():
camera.start_preview()
camera.preview_fullscreen = False
camera.preview_window = (0, 0, 650, 480)
#camera stop
def cameraStop():
camera.stop_preview()
#main function:
def main():
while True:
cameraStart()
print("Menu\n")
print(1. Turn off camera")
choice = input("Enter the menu:")
if choice == 1:
cameraStop()
exit()
if __name__ == "__main__":
main()
edit 4: The camera now can works fine without monitor, looks like I misplaced the port before. Thanks to #marksetchell. Now the only problem is to output the camera to windows that ran the ssh. Can it sends the output through my Windows laptop?

How do I know that another program is trying to connect to camera being used in my program?

I made a program that uses a camera with Python.
When my program is using a camera, I would like to add a function that my program detects that another program is trying to connect the camera and cancels the camera connection so that another program can use the camera.
I would like to know if I can see that other programs are trying to use the camera when I am using it, and if so, what is the method?
camera = cv2.VideoCapture (cv2.CAP_DSHOW + cameraNo)
The camera connection uses "cv2.videoCapture()"

Open CV cannot read frames from USB camera on Raspberry Pi

I have a Logitech c270 usb webcam connected to my Raspberry Pi 3, running on a Jessie image. I have tried to capture frames with this simple tutorial code on
http://www.pyimagesearch.com/2016/02/22/writing-to-video-with-opencv/
Whenever I try to read frames in the while loop, it gives out this error:
NoneType object has no attribute 'shape'
I have printed out the vs.read() function and it also returns None object.
What can I do to resolve this problem?
NOTE: When I executed cmake to build the binaries for Open CV 3.1 on Raspberry Pi, I havent specified OpenCV to use V4L. Could this be a problem?
Thanks in advance.
It's Because your video stream object does not get attached with the camera. Hence no image is displayed.
If you are using Pi Camera, then make sure to type --picamera 1 as an argument while running the script.
else your camera is not connected to your Pi correctly.

How to stop SimpleCV camera stream?

I'm trying to learn SimpleCV using Python 2.7 in IDLE.
Once the camera form SimpleCV is initialized the camera become unavailable to other programs like native webcam application or skype etc.
from SimpleCV import *
camera = Camera()
After restarting the pc or logoff and logon the webcam becomes to those applications. It seems that even closing out from python IDLE, it doesn't close the camera stream. Is there any way to stop the camera stream of simplecv?
I couldn't replicate your issue, but if the webcam is still running even after your program terminates/you close IDLE, you can end the camera by going into task manager and killing all running Python processes.
After some experimenting, I found that if you want to accomplish the same thing directly inside the code, you could try simply deleting the reference altogether:
>>> import SimpleCV as scv
>>> cam = scv.Camera()
>>> del cam
Calling del cam caused the webcam indicator light on my laptop to turn off. Granted, this appears to be an undocumented (??) solution, so I'm not sure how robust it is. I would probably try testing this on several different laptops/webcams first, to make sure it works reliably and consistently.

Categories