def read_image(data, args): try: from cross2sheet.image import ImageGrid except ImportError as e: if e.name in ('cv2', 'numpy'): raise NotRecognized( 'Image detection disabled because the module %s was not found.' % e.name) else: raise e try: img = ImageGrid(data) except ValueError: raise NotRecognized grid = img.grid() if args.detect_background: grid.features.extend(img.read_background(args.color_levels)) if args.detect_bars: grid.features.extend(img.read_bars()) if args.autonumber_cells_with_text: grid.features.extend(img.autonumber_if_text_found()) if args.ocr_text: grid.features.extend(img.read_text_ocr()) if args.autonumber is None: args.autonumber = not (args.autonumber_cells_with_text or args.ocr_text) return grid
def a_puzzle_with_answer_nowhere_man(): req=urlopen('http://web.mit.edu/puzzle/www/2014/puzzle/puzzle_with_answer_nowhere_man/grid.png') data=req.read() req.close() img=ImageGrid(data) grid=img.grid() grid.features.extend(img.read_background()) grid.features.extend(img.read_bars()) grid.features.extend(outside_bars(grid)) grid.features.extend(autonumber(grid)) save_xlsx(grid,'nowhere_man.xlsx')
class ImageTest(unittest.TestCase): def setUp(self): url=self.url if url.startswith('20'): url='http://web.mit.edu/puzzle/www/'+url req=urlopen(url) data=req.read() req.close() self.img=ImageGrid(data) self.maxDiff=None def test_all(self): detected=(len(self.img.breaks[0])-1,len(self.img.breaks[1])-1) expected=(self.rows,self.cols) self.assertEqual(expected,detected,'wrong dimensions') if hasattr(self,'fill'): with self.subTest('fill'): grid=self.img.grid() grid.features.extend(self.img.read_background()) f=grid_to_string(grid) self.assertEqual(self.fill.strip(),f.strip()) if hasattr(self,'bars'): with self.subTest('bars'): grid=self.img.grid() grid.features.extend(self.img.read_bars()) grid.features.extend(outside_bars(grid)) b=bars_to_string(grid) self.assertEqual(self.bars.strip(),b.strip()) if hasattr(self,'cells_with_text'): with self.subTest('cells_with_text'): if self.cells_with_text=='auto': grid=self.img.grid() grid.features.extend(self.img.read_background()) grid.features.extend(self.img.read_bars()) grid.features.extend(autonumber(grid)) self.cells_with_text=labels_to_string(grid) grid=self.img.grid() grid.features.extend(self.img.autonumber_if_text_found()) t=labels_to_string(grid) self.assertEqual(self.cells_with_text.strip(),t.strip())
class ImageTest(unittest.TestCase): def setUp(self): url = self.url if url.startswith('20'): url = 'http://web.mit.edu/puzzle/www/' + url req = urlopen(url) data = req.read() req.close() self.img = ImageGrid(data) self.maxDiff = None def test_all(self): detected = (len(self.img.breaks[0]) - 1, len(self.img.breaks[1]) - 1) expected = (self.rows, self.cols) self.assertEqual(expected, detected, 'wrong dimensions') if hasattr(self, 'fill'): with self.subTest('fill'): grid = self.img.grid() grid.features.extend(self.img.read_background()) f = grid_to_string(grid) self.assertEqual(self.fill.strip(), f.strip()) if hasattr(self, 'bars'): with self.subTest('bars'): grid = self.img.grid() grid.features.extend(self.img.read_bars()) grid.features.extend(outside_bars(grid)) b = bars_to_string(grid) self.assertEqual(self.bars.strip(), b.strip()) if hasattr(self, 'cells_with_text'): with self.subTest('cells_with_text'): if self.cells_with_text == 'auto': grid = self.img.grid() grid.features.extend(self.img.read_background()) grid.features.extend(self.img.read_bars()) grid.features.extend(autonumber(grid)) self.cells_with_text = labels_to_string(grid) grid = self.img.grid() grid.features.extend(self.img.autonumber_if_text_found()) t = labels_to_string(grid) self.assertEqual(self.cells_with_text.strip(), t.strip())
def read_image(data,args): try: from cross2sheet.image import ImageGrid except ImportError as e: if e.name in ('cv2','numpy'): raise NotRecognized('Image detection disabled because the module %s was not found.'%e.name) else: raise e try: img=ImageGrid(data) except ValueError: raise NotRecognized grid=img.grid() if args.detect_background: grid.features.extend(img.read_background(args.color_levels)) if args.detect_bars: grid.features.extend(img.read_bars()) if args.autonumber_cells_with_text: grid.features.extend(img.autonumber_if_text_found()) if args.ocr_text: grid.features.extend(img.read_text_ocr()) if args.autonumber is None: args.autonumber=not (args.autonumber_cells_with_text or args.ocr_text) return grid