How to use the pytesseract.image_to_data function in pytesseract

To help you get started, we’ve selected a few pytesseract examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github madmaze / pytesseract / tests / test_pytesseract.py View on Github external
def test_image_to_data_common_output(test_file, output):
    """Test and compare the type of the result."""
    result = image_to_data(test_file, output_type=output)
    expected_keys = [
        'level', 'page_num', 'block_num', 'par_num', 'line_num', 'word_num',
        'left', 'top', 'width', 'height', 'conf', 'text'
    ]

    if output is Output.BYTES:
        assert isinstance(result, bytes)

    elif output is Output.DICT:
        assert isinstance(result, dict)
        assert bool(set(result.keys()).intersection(expected_keys))

    elif output is Output.STRING:
        assert isinstance(result, unicode if IS_PYTHON_2 else str)
        for key in expected_keys:
            assert key in result
github mozilla / iris_firefox / iris / api / core.py View on Github external
if in_image is None:
        stack_image = _region_grabber(in_region, True)
    else:
        stack_image = in_image

    match_min_len = 12
    input_image = stack_image
    input_image_array = np.array(input_image)
    debug_img = input_image_array

    if with_image_processing:
        input_image = process_image_for_ocr(image_array=input_image)
        input_image_array = np.array(input_image)
        debug_img = cv2.cvtColor(input_image_array, cv2.COLOR_GRAY2BGR)

    processed_data = pytesseract.image_to_data(input_image)

    length_x, width_y = stack_image.size
    dpi_factor = max(1, int(OCR_IMAGE_SIZE / length_x))

    final_data, debug_data = [], []
    is_uhd, uhd_factor = get_uhd_details()

    for line in processed_data.split('\n'):
        try:
            data = line.encode('ascii').split()
            if len(data) is match_min_len:
                precision = int(data[10]) / float(100)
                virtual_data = {'x': int(data[6]),
                                'y': int(data[7]),
                                'width': int(data[8]),
                                'height': int(data[9]),
github glitchassassin / lackey / lackey / Ocr.py View on Github external
def find_all_in_image(self, image, text, confidence=0.6):
        """
        Finds all blocks of text in `image` that match `text`.
        Currently ignores confidence
        """
        confidence = confidence*100 # Scaling for pytesseract
        data = pytesseract.image_to_data(image)
        print(data)
        reader = csv.DictReader(data.split("\n"), delimiter="\t", quoting=csv.QUOTE_NONE)
        rects = [r for r in reader]
        # Debug.info("Rects: " + repr(rects))
        line = []
        matches = []
        for rect in rects:
            if len(line) and (line[0]["page_num"], line[0]["block_num"], line[0]["par_num"], line[0]["line_num"]) == (rect["page_num"], rect["block_num"], rect["par_num"], rect["line_num"]):
                # This rect is on the same line
                line.append(rect)
            else:
                Debug.info("Line: " + " ".join(e["text"] for e in line if e["text"] is not None)) # int(e["conf"]) > confidence and 
                line = [rect]
            
            if self._check_if_line_matches(line, text, confidence):
                matches.append(self._reduce_line_matches(line, text, confidence))
github mozilla / iris / src / core / api / finder / text_search3.py View on Github external
def _get_first_word(sentence_list, image_list):
    """Finds all occurrences of the first searched word."""
    first_word = sentence_list.split()[0]
    cutoff_type = 'digit' if _replace_multiple(first_word, digit_chars, '').isdigit() else 'string'
    words_found = []
    for index_image, stack_image in enumerate(image_list):
        for index_scale, scale in enumerate(range(1, TRY_RESIZE_IMAGES + 1)):
            stack_image = stack_image.resize([stack_image.width * scale, stack_image.height * scale])
            processed_data = pytesseract.image_to_data(stack_image)
            for index_data, line in enumerate(processed_data.split('\n')[1:]):
                d = line.split()
                if len(d) == OCR_RESULT_COLUMNS_COUNT:
                    cutoff = cutoffs[cutoff_type]['max_cutoff']
                    while cutoff >= cutoffs[cutoff_type]['min_cutoff']:
                        if difflib.get_close_matches(first_word, [d[11]], cutoff=cutoff):
                            try:
                                vd = _create_rectangle_from_ocr_data(d, scale)
                                if not _is_similar_result(words_found, vd.x, vd.y, WORD_PROXIMITY):
                                    words_found.append(vd)
                            except ValueError:
                                continue
                        cutoff -= cutoffs[cutoff_type]['step']
    return words_found
github mozilla / iris / src / core / api / finder / text_search2.py View on Github external
def _text_search(text, img, multiple_search=False):
    cutoffs = {'string': {'min_cutoff': 0.6, 'max_cutoff': 0.9, 'step': 0.1},
               'digit': {'min_cutoff': 0.75, 'max_cutoff': 0.9, 'step': 0.05}}

    digit_chars = ['.', '%', ',']
    cutoff_type = 'digit' if _replace_multiple(text, digit_chars, '').isdigit() else 'string'

    raw_gray_image = img.get_gray_image()
    enhanced_image = ImageEnhance.Contrast(img.get_gray_image()).enhance(10.0)
    stack_images = [raw_gray_image, enhanced_image]
    final_data = []

    for stack_image in stack_images:
        for index, scale in enumerate(range(1, TRY_RESIZE_IMAGES + 1)):
            stack_image = stack_image.resize([img.get_gray_image().width * scale, img.get_gray_image().height * scale])
            processed_data = pytesseract.image_to_data(stack_image)
            for line in processed_data.split('\n'):
                d = line.split()
                if len(d) == OCR_RESULT_COLUMNS_COUNT:
                    cutoff = cutoffs[cutoff_type]['max_cutoff']
                    while cutoff >= cutoffs[cutoff_type]['min_cutoff']:
                        if difflib.get_close_matches(text, [d[11]], cutoff=cutoff):
                            try:
                                x = int(int(d[6]) / scale)
                                y = int(int(d[7]) / scale)
                                width = int(int(d[8]) / scale)
                                height = int(int(d[9]) / scale)
                                virtual_data = Rectangle(x, y, width, height)

                                if multiple_search:
                                    if len(final_data) == 0:
                                        final_data.append(virtual_data)
github jpnaterer / smashscan / ocr.py View on Github external
start_time = time.time()
    pytess_result = pytesseract.image_to_boxes(frame, lang="eng",
        config="--psm 8", output_type=pytesseract.Output.DICT)
    print(pytess_result)
    util.display_total_time(start_time)

    bbox_list = list()
    for i, _ in enumerate(pytess_result['bottom']):
        tl = (pytess_result['left'][i], pytess_result['bottom'][i])
        br = (pytess_result['right'][i], pytess_result['top'][i])
        bbox_list.append((tl, br))
    util.show_frame(frame, bbox_list=bbox_list, wait_flag=True)

    start_time = time.time()
    pytess_data = pytesseract.image_to_data(frame, lang="eng",
        config="--psm 8", output_type=pytesseract.Output.DICT)
    print(pytess_data)
    util.display_total_time(start_time)

    bbox_list = list()
    for i, conf in enumerate(pytess_data['conf']):
        if int(conf) != -1:
            print("\tconf: {}".format(conf))
            tl = (pytess_data['left'][i], pytess_data['top'][i])
            br = (tl[0]+pytess_data['width'][i], tl[1]+pytess_data['height'][i])
            bbox_list.append((tl, br))
    util.show_frame(frame, bbox_list=bbox_list, wait_flag=True)
github apolinario / WikiColorize / wikicolorize.py View on Github external
def is_document(image):
	try:
		print(pytesseract.image_to_data(image))
		return True
	except Exception as e:
		return False