def capture_thread(capture_count): # PTP Protocol Prep ptpTransport = PtpUsbTransport( PtpUsbTransport.findptps(PtpUsbTransport.USB_CLASS_PTP)) bulk_in, bulk_out, interrupt_in = \ PtpUsbTransport.retrieve_device_endpoints( PtpUsbTransport.findptps(PtpUsbTransport.USB_CLASS_PTP)) ptpSession = PtpSession(ptpTransport) vendorId = PtpValues.Vendors.STANDARD image_file_name = None camera = Camera.query.get(session_id) try: # Open device session ptpSession.OpenSession() for _ in range(capture_count): ptpSession.InitiateCapture( objectFormatId=PtpValues.StandardObjectFormats.EXIF_JPEG) # Check for new object added after capture objectid = None while True: evt = ptpSession.CheckForEvent(None) if evt == None: raise Exception("Capture did not complete") if evt.eventcode == PtpValues.StandardEvents.OBJECT_ADDED: objectid = evt.params[0] break # Download newly added object image_file_name = \ "latest_%s.jpg" % datetime.now().strftime( "%Y_%m_%d_%H_%M_%S") save_path = ensure_path_available( os.path.expanduser("~") + "/Documents/optic-nerve/images/") + image_file_name if objectid is not None: with open(save_path, "wb") as file: ptpSession.GetObject(objectid, file) camera.image_file_name = image_file_name db.session.commit() except PtpException as e: raise PtpException( "PTP Exception: %s" % PtpValues.ResponseNameById(e.responsecode, vendorId), ptpSession, ptpTransport) except Exception as e: raise Exception(e) # Close the session del ptpSession del ptpTransport # Set camera state to complete camera.camera_state = Camera.STATE_COMPLETE db.session.commit()
def begin_timelapse(file, delay): """Simple function to initiate camera capture and store the result into the provided file object.n Note: Callers should expect the image to have saved correctly if an exception was not thrown. Args: param1: file object opened using 'wb' to result in <class '_io.BufferedWriter'> param2: delay between captures. In milliseconds. """ ptpTransport = PtpUsbTransport(PtpUsbTransport.findptps(PtpUsbTransport.USB_CLASS_PTP)) bulk_in, bulk_out, interrupt_in = \ PtpUsbTransport.retrieve_device_endpoints(PtpUsbTransport.findptps(PtpUsbTransport.USB_CLASS_PTP)) ptpSession = PtpSession(ptpTransport) vendorId = PtpValues.Vendors.STANDARD try: # Open device session ptpSession.OpenSession() print(ptpSession.GetFormattedDeviceInfoString()) # Initiate timelapse capture events id = 0 while True: ptpSession.InitiateCapture(objectFormatId=PtpValues.StandardObjectFormats.EXIF_JPEG) # Check for new object added after capture objectid = None while True: evt = ptpSession.CheckForEvent(None) if evt == None: raise Exception("Capture did not complete") if evt.eventcode == PtpValues.StandardEvents.OBJECT_ADDED: objectid = evt.params[0] break # Download newly added object if objectid != None: if file is None: file = open("capture_%i.jpg" % id, "wb") ptpSession.GetObject(objectid, file) file.close() id+=1 ptpSession.DeleteObject(objectid) # Delay between shots time.sleep(delay) except PtpException as e: print("PTP Exception: %s" % PtpValues.ResponseNameById(e.responsecode, vendorId)) except Exception as e: print("An exception occurred: %s" % e) traceback.print_exc() # Close the session del ptpSession del ptpTransport
ptpSession = PtpSession(ptpTransport) vendorId = PtpValues.Vendors.STANDARD try: ptpSession.OpenSession() deviceInfo = ptpSession.GetDeviceInfo() vendorId = deviceInfo.VendorExtensionID id = 0 while True: ptpSession.InitiateCapture( objectFormatId=PtpValues.StandardObjectFormats.EXIF_JPEG) objectid = None while True: evt = ptpSession.CheckForEvent(None) if evt == None: raise Exception("Capture did not complete") if evt.eventcode == PtpValues.StandardEvents.OBJECT_ADDED: objectid = evt.params[0] break if objectid != None: file = open("capture_%i.jpg" % id, "w") ptpSession.GetObject(objectid, file) file.close() id += 1 ptpSession.DeleteObject(objectid) except PtpException, e: print "PTP Exception: %s" % PtpValues.ResponseNameById(
class CHDKPtpCapture: def __init__(self): self.autofocuslocked = False def connect(self): """Connects to the first available ptp device, ptpSession is used to access most ptp commands""" ptps = PtpUsbTransport.findptps() print ptps self.ptpTransport = PtpUsbTransport(ptps[0]) self.ptpSession = PtpSession(self.ptpTransport) self.vendorId = PtpValues.Vendors.STANDARD self.ptpSession.OpenSession() self.deviceInfo = self.ptpSession.GetDeviceInfo() print "ser: " + self.deviceInfo.SerialNumber self.vendorId = self.deviceInfo.VendorExtensionID print "model: " + self.deviceInfo.Model return True def capture(self): """Captures an image and returns it's objectid""" self.activateShootingMode() lua_script = "shoot()" logging.debug("sending script") executeScript(self.ptpSession, lua_script, CHDKPtpValues.ScriptingLanguage.LUA, wait=True) logging.debug("script finished") # below couple of lines in while loop taken from Capture.py of pyptp objectid = None while True: evt = self.ptpSession.CheckForEvent(None) if evt == None: raise Exception("Capture did not complete") if evt.eventcode == PtpValues.StandardEvents.OBJECT_ADDED: objectid = evt.params[0] break return objectid def activateShootingMode(self): """Activates shooting mode on the camera, allowing the camera to shoot while under usb control""" lua_script = """ switch_mode_usb(1) rec,vid,mode=get_mode() while rec == false do rec,vid,mode=get_mode() end """ executeScript(self.ptpSession, lua_script, CHDKPtpValues.ScriptingLanguage.LUA, wait=True) def disableFlash(self): self.activateShootingMode() lua_script = "set_prop(143,2) -- turns flash off on SX200IS" executeScript(self.ptpSession, lua_script, CHDKPtpValues.ScriptingLanguage.LUA, wait=True) def enableAutoFlash(self): self.activateShootingMode() lua_script = "set_prop(143,1) -- turns flash to auto on SX200IS" executeScript(self.ptpSession, lua_script, CHDKPtpValues.ScriptingLanguage.LUA, wait=True) def lockAutofocus(self): """Prevents the camera from autofocusing""" self.activateShootingMode() lua_script = "set_aflock(1)" executeScript(self.ptpSession, lua_script, CHDKPtpValues.ScriptingLanguage.LUA, wait=True) self.autofocuslocked = True def unlockAutofocus(self): """Allows the camera to autofocus""" self.activateShootingMode() lua_script = "set_aflock(0)" executeScript(self.ptpSession, lua_script, CHDKPtpValues.ScriptingLanguage.LUA, wait=True) self.autofocuslocked = False def autofocus(self): """Attempts to autofocus the camera""" self.activateShootingMode() lua_script = "press('shoot_half')" executeScript(self.ptpSession, lua_script, CHDKPtpValues.ScriptingLanguage.LUA, wait=True) time.sleep( 2 ) # we must wait for a short time for the autofocus to complete, otherwise if we lockAutofocus right afterwards we can cause the program to lock up def downloadAndSaveObject(self, objectid, savepath): file = open(savepath, "w") self.ptpSession.GetObject(objectid, file) file.close() def deleteObject(self, objectid): self.ptpSession.DeleteObject(objectid) def getAutofocusLocked(self): return self.autofocuslocked
def capture_new_image(delete_from_device=False, download_image=True): """Initiate camera capture and store the result. Note: Callers should expect the image to have saved correctly if an exception was not thrown. Args: param1: file object opened using 'wb' to result in <class '_io.BufferedWriter'>. """ # PTP Protocol Prep ptpTransport = PtpUsbTransport( PtpUsbTransport.findptps(PtpUsbTransport.USB_CLASS_PTP)) bulk_in, bulk_out, interrupt_in = \ PtpUsbTransport.retrieve_device_endpoints( PtpUsbTransport.findptps(PtpUsbTransport.USB_CLASS_PTP)) ptpSession = PtpSession(ptpTransport) vendorId = PtpValues.Vendors.STANDARD try: # Open device session ptpSession.OpenSession() ptpSession.InitiateCapture( objectFormatId=PtpValues.StandardObjectFormats.EXIF_JPEG) # Check for new object added after capture objectid = None while True: evt = ptpSession.CheckForEvent(None) if evt == None: raise Exception("Capture did not complete") if evt.eventcode == PtpValues.StandardEvents.OBJECT_ADDED: objectid = evt.params[0] break # Download newly added object image_file_name = \ "latest_%s.jpg" % datetime.now().strftime("%Y_%m_%d_%H_%M_%S") if download_image: save_path = ensure_path_available( os.path.expanduser("~") + "/Documents/optic-nerve/images/") + image_file_name if objectid is not None: with open(save_path, "wb") as file: ptpSession.GetObject(objectid, file) ptpSession.DeleteObject(objectid) return image_file_name except PtpException as e: raise PtpException( "PTP Exception: %s" % PtpValues.ResponseNameById(e.responsecode, vendorId), ptpSession, ptpTransport) except Exception as e: raise Exception(e) # Close the session del ptpSession del ptpTransport