def _get_scanner(config, devid, preferred_sources=None): logger.info("Will scan using %s" % str(devid)) resolution = config['scanner_resolution'].value logger.info("Will scan at a resolution of %d" % resolution) dev = pyinsane2.Scanner(name=devid) config_source = config['scanner_source'].value if 'source' not in dev.options: logger.warning( "Can't set the source on this scanner. Option not found") elif preferred_sources: pyinsane2.set_scanner_opt(dev, 'source', preferred_sources) elif config_source: pyinsane2.set_scanner_opt(dev, 'source', [config_source]) if 'resolution' not in dev.options: logger.warning("Can't set the resolution on this scanner." " Option not found") else: pyinsane2.set_scanner_opt(dev, 'resolution', [resolution]) if 'mode' not in dev.options: logger.warning("Can't set the mode on this scanner. Option not found") else: try: pyinsane2.set_scanner_opt(dev, 'mode', ['Color']) except pyinsane2.PyinsaneException as exc: logger.warning("Failed to set scan mode: {}".format(exc)) pyinsane2.maximize_scan_area(dev) return (dev, resolution)
def __init__(self, args, face_recognition=None, device=None, set_device=True): self.args = args if face_recognition is None: face_utils.set_face_model(args, verbose=False) face_recognition = face.Recognition( face_crop_size=args.image_size, face_crop_margin=args.margin, gpu_memory_fraction=args.gpu_memory_fraction, ) self.face_recognition = face_recognition if args.load_document_type_detector: # loading document type detector self.document_detector = document_type_detector.DocumentTypeDetector( ) self.document_detector.load_document_means( self.args.document_type_detector_means_path) else: self.document_detector = None if device is None and set_device: # setting scanner pyinsane2.init() device = pyinsane2.Scanner(name=args.scanner_name) device.options["mode"].value = "Color" device.options["resolution"].value = 600 if args.hp_sane_scanner: device.options["tl-x"].value = 1 * 65536 device.options["tl-y"].value = 1 * 65536 device.options["br-x"].value = 84 * 65536 device.options["br-y"].value = 51 * 65536 self.scanner_device = device
def test_get_devices(self): devices = pyinsane2.get_devices() if len(devices) == 0: # if there are no devices found, create a virtual device. # see sane-test(5) and /etc/sane.d/test.conf pyinsane2.Scanner("test")._open() devices = pyinsane2.get_devices() self.assertTrue(len(devices) > 0)
def do(self): self.emit("source-finding-start") try: logger.info("Looking for sources of device [%s]" % (self.__devid)) device = pyinsane2.Scanner(name=self.__devid) sys.stdout.flush() if 'source' in device.options: sources = device.options['source'].constraint else: sources = [] logger.info("Sources found: %s" % str(sources)) sys.stdout.flush() for source in sources: name = self.__get_source_name_translated(source) self.emit('source-found', name, source, (source == self.__selected_source)) logger.info("Got all the sources") finally: self.emit("source-finding-end")
def do(self): self.emit("resolution-finding-start") try: logger.info("Looking for resolution of device [%s]" % (self.__devid)) device = pyinsane2.Scanner(name=self.__devid) sys.stdout.flush() if 'resolution' in device.options: resolutions = device.options['resolution'].constraint else: resolutions = [] if resolutions: logger.info("Resolutions found: %s" % str(resolutions)) else: logger.warning( "No possible resolutions specified. Assuming default") resolutions = [75, 100, 150, 200, 300, 600, 1200] sys.stdout.flush() # Sometimes sane return the resolutions as a integer array, # sometimes as a range (-> tuple). So if it is a range, we turn # it into an array if isinstance(resolutions, tuple): if len(resolutions) <= 2: interval = 25 else: interval = resolutions[2] if interval < 25: interval = 25 res_array = [] for res in range(resolutions[0], resolutions[1] + 1, interval): res_array.append(res) resolutions = res_array for resolution in resolutions: name = self.__get_resolution_name(resolution) self.emit('resolution-found', name, resolution, (resolution == self.__selected_resolution)) logger.info("Got all the resolutions") finally: self.emit("resolution-finding-end")
def setup_device(device_name=None, resolution=300, mode="Gray"): pyinsane2.init() if device_name is not None: device = pyinsane2.Scanner(name="brother4:net1;dev0") else: devices = pyinsane2.get_devices() print("Scanners found:") for i, d in enumerate(devices): print(f"[{i}] {d}") device = devices[int(input("Select which one to use: "))] print(f"Will use: {device}") pyinsane2.set_scanner_opt(device, "source", ["Auto", "FlatBed"]) pyinsane2.set_scanner_opt(device, "resolution", [resolution]) try: pyinsane2.maximize_scan_area(device) except Exception as exc: print(f"Failed to maximize scan area: {exc}") pyinsane2.set_scanner_opt(device, "mode", [mode]) return device
def _do(self): # find the best resolution : the default calibration resolution # is not always available resolutions = [x[1] for x in self.__resolutions_store] resolutions.sort() resolution = DEFAULT_CALIBRATION_RESOLUTION for nresolution in resolutions: if nresolution > DEFAULT_CALIBRATION_RESOLUTION: break resolution = nresolution logger.info("Will do the calibration scan with a resolution of %d" % resolution) # scan dev = pyinsane2.Scanner(name=self.__devid) if self.__source: if dev.options['source'].capabilities.is_active(): dev.options['source'].value = self.__source logger.info("Scanner source set to '%s'" % self.__source) try: pyinsane2.set_scanner_opt(dev, 'resolution', [resolution]) except pyinsane2.PyinsaneException as exc: logger.warning( "Unable to set scanner resolution to {}: {}".format( resolution, exc ) ) logger.exception(exc) resolution = int(dev.options['resolution'].value) logger.warning("Falling back to current resolution: {}".format( resolution )) try: pyinsane2.set_scanner_opt(dev, 'mode', ["Color"]) except pyinsane2.PyinsaneException as exc: logger.warning("Unable to set scanner mode !" " May be 'Lineart': {}".format(exc)) logger.exception(exc) try: pyinsane2.maximize_scan_area(dev) except pyinsane2.PyinsaneException as exc: logger.warning("Failed to maximize the scan area." " May only scan part of the image: {}".format(exc)) logger.exception(exc) scan_session = dev.scan(multiple=False) scan_size = scan_session.scan.expected_size self.emit('calibration-scan-info', scan_size[0], scan_size[1]) last_line = 0 try: while self.can_run: scan_session.scan.read() next_line = scan_session.scan.available_lines[1] if (next_line > last_line + 50): chunk = scan_session.scan.get_image(last_line, next_line) self.emit('calibration-scan-chunk', last_line, chunk) last_line = next_line time.sleep(0) # Give some CPU time to PyGtk if not self.can_run: self.emit('calibration-scan-canceled') scan_session.scan.cancel() return except EOFError: pass return (scan_session.images[-1], resolution)