Пример #1
0
 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
Пример #2
0
def the_wicked_switch():
    req=urlopen('http://web.mit.edu/puzzle/www/2012/puzzles/a_circus_line/the_wicked_switch/1.png')
    data=req.read()
    req.close()
    img=ImageGrid(data)
    grid=img.grid()
    grid.features.extend(img.read_background())
    grid.features.extend(img.read_text_ocr())
    grid.features.extend(outside_bars(grid))
    save_xlsx(grid,'the_wicked_switch.xlsx')
Пример #3
0
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')
Пример #4
0
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
Пример #5
0
 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
Пример #6
0
def display():
    try:
        if 'file' in request.files:
            data=request.files['file'].read()
        elif request.form['url'].startswith('http'):
            data=urlopen(request.form['url']).read()
        else:
            return redirect(url_for('select'))
        img=ImageGrid(data)
        dim=img.dimensions()
        if dim[0]<=0 or dim[1]<=0:
            return render_template('select.html',error_msg='Failed to recognize crossword grid.')
        d=TableData(img=img)
        t=Table(d)
        return render_template('convert.html',table=t,data=d.to_json())
    except ValueError:
        return render_template('select.html',error_msg='File format not recognized.')
    except URLError as e:
        return render_template('select.html',error_msg='Could not load url: {}.'.format(e.reason.strerror))
Пример #7
0
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())
Пример #8
0
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())
Пример #9
0
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