Ejemplo n.º 1
0
def processImg(img, template_zero, template_one, imgpath):
    """ The pipeline for processing one image:
        1) crop out two rough barcode regions from the image
        2) run template matching against it with two templates with criteria,
           retrieving the best matches
        3) process matching result, transform into 01-bitstring
    Note: Only the front-side has a full barcodes on the UL/UR corners.
    The back-side, however, has "0", "" on the top, and "0", "0" on the
    bottom. We can leverage this information. 
    Output:
        list DECODINGS, dict MARKS_OUT.
    list DECODINGS: [str decoding_upperLeft, str decoding_upperRight]
    dict MARKS_OUT: maps {MARK_ON/MARK_OFF: [(imgpath, (x1,y1,x2,y2), LEFT/RIGHT), ...]}
    """
    w_img, h_img = cv.GetSize(img)
    w_fact, h_fact = 0.15, 0.20
    w_patch, h_patch = int(round(w_img * w_fact)), int(round(h_img * h_fact))
    x_off, y_off = 0, 10
    cv.SetImageROI(img, (x_off, y_off, w_patch, h_patch))
    patchLeft = tempmatch.smooth(img, 3, 3, bordertype='const', val=255.0)
    decodings_left, zero_locs_left, one_locs_left = decode_patch(
        patchLeft, template_zero, template_one, imgpath)
    cv.ResetImageROI(img)
    marks_out_left = create_marks_dict(zero_locs_left, one_locs_left, x_off,
                                       y_off, imgpath, LEFT)
    x_off = int(round(w_img - w_patch))
    cv.SetImageROI(img, (x_off, y_off, w_patch, h_patch))
    patchRight = tempmatch.smooth(img, 3, 3, bordertype='const', val=255.0)
    decodings_rht, zero_locs_rht, one_locs_rht = decode_patch(
        patchRight, template_zero, template_one, imgpath)
    cv.ResetImageROI(img)
    marks_out_rht = create_marks_dict(zero_locs_rht, one_locs_rht, x_off,
                                      y_off, imgpath, RIGHT)

    marks_out = marks_out_left
    for markval, tups in marks_out_rht.iteritems():
        marks_out.setdefault(markval, []).extend(tups)

    return (decodings_left, decodings_rht), marks_out
Ejemplo n.º 2
0
                                    sequoia.ORIG_IMG_H, exmpl_imgsize[0],
                                    exmpl_imgsize[1])
        IsymB = sequoia.rescale_img(IsymB, sequoia.ORIG_IMG_W,
                                    sequoia.ORIG_IMG_H, exmpl_imgsize[0],
                                    exmpl_imgsize[1])
        IsymC = sequoia.rescale_img(IsymC, sequoia.ORIG_IMG_W,
                                    sequoia.ORIG_IMG_H, exmpl_imgsize[0],
                                    exmpl_imgsize[1])
        IsymD = sequoia.rescale_img(IsymD, sequoia.ORIG_IMG_W,
                                    sequoia.ORIG_IMG_H, exmpl_imgsize[0],
                                    exmpl_imgsize[1])
        IsymE = sequoia.rescale_img(IsymE, sequoia.ORIG_IMG_W,
                                    sequoia.ORIG_IMG_H, exmpl_imgsize[0],
                                    exmpl_imgsize[1])

    Itemp0 = tempmatch.smooth(Itemp0, 3, 3, bordertype='const', val=255.0)
    Itemp1 = tempmatch.smooth(Itemp1, 3, 3, bordertype='const', val=255.0)
    IsymA = tempmatch.smooth(IsymA, 3, 3, bordertype='const', val=255.0)
    IsymB = tempmatch.smooth(IsymB, 3, 3, bordertype='const', val=255.0)
    IsymC = tempmatch.smooth(IsymC, 3, 3, bordertype='const', val=255.0)
    IsymD = tempmatch.smooth(IsymD, 3, 3, bordertype='const', val=255.0)
    IsymE = tempmatch.smooth(IsymE, 3, 3, bordertype='const', val=255.0)
    backsmap = {}  # maps {ballotid: [backpath_i, ...]}

    def mygauge_tick(N=1):
        """ Add N ticks to the progress bar. """
        for _ in xrange(N):
            queue.put(True)

    if num_pages == 1:
        return handle_singleside(ballots, Itemp0, Itemp1, IsymA, IsymB, IsymC,
Ejemplo n.º 3
0
def main():
    args = sys.argv[1:]
    arg0 = args[0]
    do_display_output = True if 'display' in args else False
    if do_display_output:
        outdir = args[-1]
    else:
        outdir = None
    do_profile = True if 'profile' in args else False
    if os.path.isdir(arg0):
        imgpaths = []
        for dirpath, dirnames, filenames in os.walk(arg0):
            for imgname in [f for f in filenames if isimgext(f)]:
                imgpaths.append(os.path.join(dirpath, imgname))
    else:
        imgpaths = [arg0]

    template_zero_path = "sequoia_template_zero_skinny.png"
    template_one_path = "sequoia_template_one_skinny.png"
    sidesymbol_path = "sequoia_side_symbol.png"

    Izero = cv.LoadImage(template_zero_path, cv.CV_LOAD_IMAGE_GRAYSCALE)
    Ione = cv.LoadImage(template_one_path, cv.CV_LOAD_IMAGE_GRAYSCALE)
    Isidesym = cv.LoadImage(sidesymbol_path, cv.CV_LOAD_IMAGE_GRAYSCALE)
    IsymA = cv.LoadImage(SYMA_IMGPATH, cv.CV_LOAD_IMAGE_GRAYSCALE)
    IsymB = cv.LoadImage(SYMB_IMGPATH, cv.CV_LOAD_IMAGE_GRAYSCALE)
    IsymC = cv.LoadImage(SYMC_IMGPATH, cv.CV_LOAD_IMAGE_GRAYSCALE)
    IsymD = cv.LoadImage(SYMD_IMGPATH, cv.CV_LOAD_IMAGE_GRAYSCALE)
    IsymE = cv.LoadImage(SYME_IMGPATH, cv.CV_LOAD_IMAGE_GRAYSCALE)

    # Rescale IZERO/IONE/ISIDESYM to match this dataset's image dimensions
    exmpl_imgsize = cv.GetSize(cv.LoadImage(imgpaths[0]))
    if exmpl_imgsize != (ORIG_IMG_W, ORIG_IMG_H):
        print "...rescaling images..."
        Izero = rescale_img(Izero, ORIG_IMG_W, ORIG_IMG_H, exmpl_imgsize[0],
                            exmpl_imgsize[1])
        Ione = rescale_img(Ione, ORIG_IMG_W, ORIG_IMG_H, exmpl_imgsize[0],
                           exmpl_imgsize[1])
        Isidesym = rescale_img(Isidesym, ORIG_IMG_W, ORIG_IMG_H,
                               exmpl_imgsize[0], exmpl_imgsize[1])
        IsymA = rescale_img(IsymA, ORIG_IMG_W, ORIG_IMG_H, exmpl_imgsize[0],
                            exmpl_imgsize[1])
        IsymB = rescale_img(IsymB, ORIG_IMG_W, ORIG_IMG_H, exmpl_imgsize[0],
                            exmpl_imgsize[1])
        IsymC = rescale_img(IsymC, ORIG_IMG_W, ORIG_IMG_H, exmpl_imgsize[0],
                            exmpl_imgsize[1])
        IsymD = rescale_img(IsymD, ORIG_IMG_W, ORIG_IMG_H, exmpl_imgsize[0],
                            exmpl_imgsize[1])
        IsymE = rescale_img(IsymE, ORIG_IMG_W, ORIG_IMG_H, exmpl_imgsize[0],
                            exmpl_imgsize[1])

    Izero = tempmatch.smooth(Izero, 3, 3, bordertype='const', val=255.0)
    Ione = tempmatch.smooth(Ione, 3, 3, bordertype='const', val=255.0)
    Isidesym = tempmatch.smooth(Isidesym, 3, 3, bordertype='const', val=255.0)
    IsymA = tempmatch.smooth(IsymA, 3, 3, bordertype='const', val=255.0)
    IsymB = tempmatch.smooth(IsymB, 3, 3, bordertype='const', val=255.0)
    IsymC = tempmatch.smooth(IsymC, 3, 3, bordertype='const', val=255.0)
    IsymD = tempmatch.smooth(IsymD, 3, 3, bordertype='const', val=255.0)
    IsymE = tempmatch.smooth(IsymE, 3, 3, bordertype='const', val=255.0)

    t = time.time()
    err_imgpaths = []
    for imgpath in imgpaths:
        I = cv.LoadImage(imgpath, cv.CV_LOAD_IMAGE_GRAYSCALE)
        print "For imgpath {0}:".format(imgpath)
        side, isflip = get_side(I, IsymA, IsymB, IsymC, IsymD, IsymE)
        if side == None:
            print "    ERROR GET_SIDE"
            err_imgpaths.append(imgpath)
            continue
        elif side == 1:
            print "    Detected Backside, isflip={0}".format(isflip)
            continue
        cv.ResetImageROI(I)
        if isflip:
            cv.Flip(I, I, flipMode=-1)
        decodings, marklocs = decode(I, Izero, Ione, _imgpath=imgpath)
        if decodings == None:
            print "    ERROR DECODE"
            err_imgpaths.append(imgpath)
            continue
        else:
            print "    {0} isflip={1}".format(decodings, isflip)
        if not do_display_output:
            continue
        # Output colorful image with interpretation displayed nicely
        Icolor = cv.LoadImage(imgpath, cv.CV_LOAD_IMAGE_COLOR)
        if isflip:
            cv.Flip(Icolor, Icolor, flipMode=-1)
        for marktype, tups in marklocs.iteritems():
            if marktype == MARK_ON:
                color = cv.CV_RGB(0, 0, 255)
            else:
                color = cv.CV_RGB(255, 0, 0)
            for (imgpath, (x1, y1, x2, y2), userdata) in tups:
                cv.Rectangle(Icolor, (x1, y1), (x2, y2), color, thickness=2)
        imgname = os.path.split(imgpath)[1]
        outrootdir = os.path.join(outdir, imgname)
        try:
            os.makedirs(outrootdir)
        except:
            pass
        outpath = os.path.join(
            outrootdir, "{0}_bbs.png".format(os.path.splitext(imgname)[0]))
        cv.SaveImage(outpath, Icolor)

    dur = time.time() - t
    print "...Finished Decoding {0} images ({1} s).".format(len(imgpaths), dur)
    print "    Avg. Time per Image: {0} s".format(dur / float(len(imgpaths)))
    print "    Number of Errors: {0}".format(len(err_imgpaths))
    print "Done."