User avatar
dobra-dobra
Posts: 34
Joined: Wed Dec 26, 2012 2:04 pm
Location: Poland

Python code to detect if camera is available

Tue Jun 04, 2013 3:14 pm

Hi

I'm looking for a way to detect in Python code if camera module is connected to RPi. I have a script which start at boot to take pictures for timelapse, but when I run Pi without camera connected it causes errors.

Thanks in advance for any clue.

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2869
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: Python code to detect if camera is available

Tue Jun 04, 2013 4:52 pm

I can't answer the direct question, but perhaps there is another way?

Using a try: except: block in Python, if it causes errors you can program around them and the script won't exit.

You may already know this though, but just in case.

na razie ;)
Alex Eames RasPi.TV, RasP.iO

User avatar
dobra-dobra
Posts: 34
Joined: Wed Dec 26, 2012 2:04 pm
Location: Poland

Re: Python code to detect if camera is available

Tue Jun 04, 2013 8:35 pm

Thanks for response. I use Python code to call raspistill in a loop, and so errors which I get (I don't remember them now) are from raspistill, not from Python. So if I understand correctly I can't use Python exceptions.

But sometimes when you ask a question it is easier to find a solution by yourself, and I think that I may have found one. I can try to check if picture was taken and file written, and if it does - that means that camera is connected. And if not - I will break the loop. Still I will get error once, but thats a whole lot better than getting them every 2 seconds.

So...

Dziękuję. :D

znanev
Posts: 6
Joined: Fri Oct 19, 2012 3:22 pm

Re: Python code to detect if camera is available

Wed Jun 05, 2013 11:04 am

Hi,

you could use the output of this command:

Code: Select all

vcgencmd get_camera
When the camera is connected, the output is like this:
supported=1 detected=1
I haven't tried disconnecting the camera, but I suppose that if you have the latest firmware, supported will still be 1, but detected should show 0.


Hope this helps :)

sharix
Posts: 200
Joined: Thu Feb 16, 2012 11:29 am
Location: Slovenia

Re: Python code to detect if camera is available

Wed Jun 05, 2013 1:30 pm

With no camera attached and camera not enabled with raspi-config I get:

Code: Select all

/opt/vc/bin/vcgencmd get_camera
supported=0 detected=0

User avatar
dobra-dobra
Posts: 34
Joined: Wed Dec 26, 2012 2:04 pm
Location: Poland

Re: Python code to detect if camera is available

Thu Jun 06, 2013 5:49 am

That is exactly what I needed, thanks a lot.

User avatar
pingu512
Posts: 64
Joined: Sun Apr 01, 2012 2:39 pm
Location: Chester
Contact: Website

Re: Python code to detect if camera is available

Sat Jun 08, 2013 1:41 pm

I added this to the /etc/rc.local file just above exit 0

Code: Select all

string=$(vcgencmd get_camera)
substring="detected=1"
if test "${string#*$substring}" != "$string"
then
    /home/pi/image_run/capture.sh    # Camera connected start capture
else
    echo "Not camera found"    # Camera not connected.
fi
http://www.weather.dragontail.co.uk

User avatar
pingu512
Posts: 64
Joined: Sun Apr 01, 2012 2:39 pm
Location: Chester
Contact: Website

Re: Python code to detect if camera is available

Sat Jun 08, 2013 2:06 pm

The above code did not work when the camera was connected. string=$(vcgencmd get_camera) hung the process. Think again

sharix
Posts: 200
Joined: Thu Feb 16, 2012 11:29 am
Location: Slovenia

Re: Python code to detect if camera is available

Sat Jun 08, 2013 3:12 pm

Try with the whole path:

Code: Select all

string=$(/opt/vc/bin/vcgencmd get_camera)

User avatar
dobra-dobra
Posts: 34
Joined: Wed Dec 26, 2012 2:04 pm
Location: Poland

Re: Python code to detect if camera is available

Sun Jun 09, 2013 6:29 pm

I can confirm, that when camera is connected vcgencmd get_camera does not return what expected. I made updates of system lately (apt-get update, rpi-update), but I'm not sure if this is related. After some tinkering (disconnecting and connecting module, disabling and enabling camera in raspi-config) I managed to get it working and I was able to test my sketch, but after reboot it doesn't work again and I can't reproduce my solution. But still I can use raspistill without any problem, so camera works as it should.
Can someone else confirm this?

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2869
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: Python code to detect if camera is available

Sun Jun 09, 2013 9:17 pm

dobra-dobra wrote:I can confirm, that when camera is connected vcgencmd get_camera does not return what expected. I made updates of system lately (apt-get update, rpi-update), but I'm not sure if this is related. After some tinkering (disconnecting and connecting module, disabling and enabling camera in raspi-config) I managed to get it working and I was able to test my sketch, but after reboot it doesn't work again and I can't reproduce my solution. But still I can use raspistill without any problem, so camera works as it should.
Can someone else confirm this?
Yes, I tried it last night and my Pi didn't respond to this command at all...

from /home/pi with and without sudo
or from the driectory where the command resides, with and without sudo.

Oh and I had to reboot the Pi after that before the camera would work again. Niestety, ale nie udalo sie :(
Alex Eames RasPi.TV, RasP.iO

afargas
Posts: 1
Joined: Tue Oct 27, 2015 11:44 am

Re: Python code to detect if camera is available

Tue Oct 27, 2015 11:55 am

I know that this conversation is from time ago, but in order to help other people, I developed a working code detecting if the camera is connected or not, I call it from a script added in rc.local:

#! /bin/sh
s=$(/opt/vc/bin/vcgencmd get_camera)
sb="supported=1 detected=1"
case $sb in
$s)
echo "Camera detected"
;;
*)
echo "Camera not detected $sb not in $
;;
esac

GuruBastade
Posts: 4
Joined: Tue Dec 22, 2015 12:13 pm

Re: Python code to detect if camera is available

Thu Jan 07, 2016 8:02 am

Hello,
How do I have a new pop up screen showing message "camera is not connected"?

User avatar
r3d4
Posts: 967
Joined: Sat Jul 30, 2011 8:21 am
Location: ./

Re: Python code to detect if camera is available

Thu Jan 07, 2016 8:07 am

GuruBastade wrote:Hello,
How do I have a new pop up screen showing message "camera is not connected"?
"xmessage Or whiptail" . :?:

Return to “Camera board”