示例#1
0
def train(folder, indices=None):
    D = []
    files = os.listdir(folder)
    files.sort()
    if not indices:
        indices = range(len(files))
    for index in indices:
        img_name = files[index]
        img = cv2.imread(folder + '/' + img_name)
        cbr_img = cbr.CheckboxReader(img)
        score = cbr_img.compute_boxscore(boxsize=17)
        D.append(score)
    D = np.array(D)
    return D
示例#2
0
def test(thresh, folder, indices=None):
    PR = 0
    files = os.listdir(folder)
    files.sort()
    if not indices:
        indices = range(len(files))
    for index in indices:
        img_name = files[index]
        img = cv2.imread(folder + '/' + img_name)
        cbr_img = cbr.CheckboxReader(img)
        score = cbr_img.compute_boxscore(boxsize=17)
        if thresh < score:
            PR += 1.0
    PR /= (len(indices) * 1.0)
    return PR
def _find_languages(env, img, line_vals, test):
    r"""
    Crop a small part
    of the original picture around the position of each language
    check box.

    [TODO] Implement again something to transform coordinates and be
           tolerant when scans are not in same dpi as template or are
           misaligned.

    This analysis should be quite fast due to the small size of the
    pictures to analyze (should be a square of about 20-30 pixels large).

    Algorithm for finding the checked language is the following:
    1. Get the histogram of the cropped picture of the checkbox
    2. Consider only the dark pixels (brightness value between 0-120)
       (Max brightness is 256)
    3. Look for the checkbox which has the most dark pixels count, as the
       checked checkbox must be darker than the empty ones.
    4. Returns true (checked) only if the pixels count is 25% more than the
       second candidate checkbox, and only if the second candidate has less
       than 25% more of dark pixels.

    :param env env: Odoo variable env
    :param img: Image to analyze
    :param dict line_vals: Dictonnary containing the data for a line\
        (and the template)
    :param bool test: Enable the test mode (will save some img)
    :returns: None
    """
    line_vals['letter_language_id'] = False
    template = env['correspondence.template'].browse(line_vals['template_id'])
    if not template:
        return

    # Color for writing lang in test result image. The color order is BGR
    lang_color = (0, 97, 232)
    test_img = []

    h, w = img.shape[:2]
    # Candidate for checked language (checkbox, pixels count)
    maxDark = (0, 0)
    # Second candidate
    secondDark = (0, 0)
    # Checkbox which has the less dark pixels count
    minDark = (sys.maxint, sys.maxint)

    for checkbox in template.checkbox_ids:
        a = checkbox.y_min
        b = checkbox.y_max
        c = checkbox.x_min
        d = checkbox.x_max
        if not (0 < a < b < h and 0 < c < d < w):
            continue
        checkbox_image = cbr.CheckboxReader(img[a:b + 1, c:d + 1])
        sumLows = checkbox_image.get_pixels_count(max_brightness=120)
        if sumLows > maxDark[1]:
            secondDark = maxDark
            maxDark = (checkbox, sumLows)
        elif sumLows > secondDark[1]:
            secondDark = (checkbox, sumLows)
        if sumLows < minDark[1]:
            minDark = (checkbox, sumLows)

        if test:
            # Produce image of checkboxes to see the result of the crop
            pos = (int(checkbox.x_max - checkbox.x_min) / 2,
                   int(checkbox.y_max - checkbox.y_min) / 2)
            img_lang = np.copy(img[a:b + 1, c:d + 1])
            code_iso = checkbox.language_id.code_iso
            if code_iso:
                cv2.putText(img_lang, code_iso, pos, cv2.FONT_HERSHEY_SIMPLEX,
                            1, lang_color)
            test_img.append(img_lang)

    # A checked box represents 20% of pixels
    # We test the difference between second checkbox with most dark
    # pixels and checkbox with the least dark pixels is less than 25%
    maxDiff = maxDark[1] - secondDark[1]
    minDiff = secondDark[1] - minDark[1]
    found = minDiff <= 0.25 * minDark[1] < maxDiff
    if found:
        lang = maxDark[0].language_id
        line_vals['letter_language_id'] = lang.id
    if test:
        test_data = manyImages2OneImage(test_img, 2)
        line_vals['lang_preview'] = test_data
        line_vals['test_letter_language'] = lang.code_iso if found else 'nope'
def _find_languages(env, img, line_vals, test, resize_ratio=1.0):
    """
    Crop a small part
    of the original picture around the position of each language
    check box.

    [TODO] Implement again something to transform coordinates and be
           tolerant when scans are not in same dpi as template or are
           misaligned.

    This analysis should be quite fast due to the small size of the
    pictures to analyze (should be a square of about 20-30 pixels large).

    Algorithm for finding the checked language is the following:
    1. Detect the box coordinates
    2. Compute Canny edges with two different approach and merge them
    3. Depending on the number of detected edges and a decision threshold
    we classe each box to True or False
    4. If 0 or more tha 1 box is checked, we don't return any result

    :param env env: Odoo variable env
    :param img: Image to analyze
    :param dict line_vals: Dictonnary containing the data for a line\
        (and the template)
    :param bool test: Enable the test mode (will save some img)
    :returns: None
    """
    line_vals['letter_language_id'] = False
    template = env['correspondence.template'].browse(line_vals['template_id'])
    if not template:
        return

    # Color for writing lang in test result image. The color order is BGR
    lang_color = (0, 97, 232)
    test_img = []
    h, w = img.shape[:2]

    checked = []
    checkbox_list = []
    for checkbox in template.checkbox_ids:
        a = int(checkbox.y_min * resize_ratio)
        b = int(checkbox.y_max * resize_ratio)
        c = int(checkbox.x_min * resize_ratio)
        d = int(checkbox.x_max * resize_ratio)
        if not (0 < a < b < h and 0 < c < d < w):
            continue
        checkbox_image = cbr.CheckboxReader(img[a:b + 1, c:d + 1])

        score = checkbox_image.compute_boxscore(boxsize=17)
        checkbox_list.append(checkbox)
        checked.append(checkbox_image.decision_threshold < score)

        if test:
            # Produce image of checkboxes to see the result of the crop
            pos = (int(checkbox.x_max - checkbox.x_min) / 2,
                   int(checkbox.y_max - checkbox.y_min) / 2)
            img_lang = np.copy(img[a:b + 1, c:d + 1])
            code_iso = checkbox.language_id.code_iso
            if code_iso and False:
                cv2.putText(img_lang, code_iso, pos, cv2.FONT_HERSHEY_SIMPLEX,
                            1, lang_color)
            test_img.append(img_lang)

    found = False
    checked_ind = [i for i, val in enumerate(checked) if val]
    langs = map(lambda ind: checkbox_list[ind], checked_ind)
    if len(langs) == 1:
        found = True
        lang = langs[0].language_id
        line_vals['letter_language_id'] = lang.id

    if test:
        test_data = manyImages2OneImage(test_img, 2)
        line_vals['lang_preview'] = test_data
        if found:
            vals = lang.code_iso
        else:
            vals = 'nope: ' + ', '.join(
                map(lambda l: l.language_id.code_iso, langs))
        line_vals['test_letter_language'] = vals