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
from ptp.PtpUsbTransport import PtpUsbTransport from ptp.PtpSession import PtpSession, PtpException from ptp import PtpValues ptpTransport = PtpUsbTransport(PtpUsbTransport.findptps()[0]) 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
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