1

So here it goes. I've been trying for the last few days to figure out how to properly use exceptions, specifically the ValueError. I've researched it over and over again, but am still struggling to put into practice.

This is a quick Python code I wrote where I tried to use it.

#!/etc/python

from PIL import Image
from time import sleep
from subprocess import call

import time
import os
import PIL
import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)

pad0 = 23
GPIO.setup(pad0, GPIO.IN)

pad0alreadyPressed = False

while True:

    pad0pressed = not GPIO.input(pad0)

    if pad0pressed and not pad0alreadyPressed:
        print "Pad 0 pressed"
        try:
            call(["gphoto2", "--auto-detect"])
            call(["gphoto2", "--capture-image-and-download","--filename","/home/pi/booth/test1.jpg"])
        except ValueError:
            print("focus error.  Trying again")
            time.sleep(5)
            call(["gphoto2", "--auto-detect"])
            call(["gphoto2", "--capture-image-and-download","--filename","/home/pi/booth/test1.jpg"])
    pad0alreadyPressed = pad0pressed

The idea was to put the adafruit code for their 5 pad cap sensor together with gphoto2 to be able to take a photo with touch sensor. Unfortunately the camera has difficult time focusing and taking a photo when things are too close. It gives an error saying that it can't focus.

Pad 0 pressed
Model                          Port                                            
----------------------------------------------------------
Nikon DSC D300s (PTP mode)     usb:001,006     
/home/pi/booth/images/capture/Capture-160726160447.jpg

*** Error ***              
Out of Focus
ERROR: Could not capture image.
ERROR: Could not capture.

I was attempting to put an except catching the error, and retrying to take a photo after 5 seconds allowing for focus to try again, but I still get the same outcome. Can somebody help me understand what I'm doing wrong, and what is the correct way to do this. Or am I using the ValueError incorrectly? Again, all I want is to be able to trigger my shutter with a cap sensor and to avoid getting stuck on the focus error by attempting to retake it.

All help would be greatly appreciated.

Thanks in advance.

  • You need to edit your question and say what you expect to happen and say what actually happens. – joan Jul 28 '16 at 7:59
  • Two things come to mind: The ValueError - where does it come from? You should print the ValueError with something like except ValueError as e:and then print e. That way you know what the error is about. Another thing is that you should not try to do the same thing in the except statement that was just caught. I've tried a similar script and unfortunately I can't get my G10 to complain about focusing, but gphoto2 will ask if I want to overwrite the file, when I use the same filename as you have done here. I get no error for this, though, execution of the script just stops until I reply. – jogco Jul 28 '16 at 8:21
  • Attempted to edit the question. Hope that helps. – fidel Jul 28 '16 at 17:22
  • What version of (lib)gphoto2 are you using? I saw this line in the libgphoto2 news: "* Nikon DSLR: out of focus during capture fixed, various bugfixes" for release 2.5.2 – jogco Jul 28 '16 at 20:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.