def slice_boxes(boxfilename): # iterate through a box list, making strip slices. box_list = zonebox.load_boxes(boxfilename) # for box in box_list : # print box # s = Strip(box=box) # print s # find the largest Y index in our boxes (the maximum row) bounding_box = box_list_bounding_box(box_list) print "bounding_box=", bounding_box # use lower left row count as the number of rows we will iterate num_rows = bounding_box["lower_right"]["row"] print "num_rows=", num_rows # make a starting strip num_rows_in_strip = 300 s = Strip(width=2560, height=num_rows_in_strip) row = 0 while row < num_rows: print "strip=", s for box in box_list: print "box=", box isect = strip_intersect(Strip(box=box), s) if isect: print "intersect=", isect else: print "no intersections" s.next_strip() row += num_rows_in_strip
def slice_boxes( boxfilename) : # iterate through a box list, making strip slices. box_list = zonebox.load_boxes(boxfilename) # for box in box_list : # print box # s = Strip(box=box) # print s # find the largest Y index in our boxes (the maximum row) bounding_box = box_list_bounding_box( box_list ) print "bounding_box=",bounding_box # use lower left row count as the number of rows we will iterate num_rows = bounding_box["lower_right"]["row"] print "num_rows=",num_rows # make a starting strip num_rows_in_strip = 300 s = Strip(width=2560,height=num_rows_in_strip) row = 0 while row < num_rows : print "strip=",s for box in box_list : print "box=",box isect = strip_intersect( Strip(box=box), s ) if isect : print "intersect=",isect else : print "no intersections" s.next_strip() row += num_rows_in_strip
def make_sliding_strips_from_box(boxfilename): basename = get_basename(boxfilename) # create the output directory for all the files I'm about to create output_dir = make_output_dir(basename) # return box_list = zonebox.load_boxes(boxfilename) # load the image associated with this box list # assume all the boxes have the same image name (they should) imgfilename = "IMAGEBIN/{0}BIN.png".format(box_list[0].document_id) # get the image as a numpy array data = mkslices.load_image(imgfilename) strip_list = make_all_strips_images(data, basename, output_dir) # # Now make the ground truth files for each strip # # convert the box list into a list of strips box_strip_list = [rects.Strip(box=box) for box in box_list] # slice up the ground truth into individual XML files make_all_gtruth_xml(box_strip_list, data, output_dir, basename)
def draw_boxes( boxfilename, outdir=None) : box_list = zonebox.load_boxes(boxfilename) print box_list # get the document id from the box file # imgfilename = "IMAGEBIN/{0}BIN.TIF".format( box_list[0].document_id ) imgfilename = "IMAGEBIN/{0}BIN.png".format( box_list[0].document_id ) # write the output image to this file and perhaps to another directory if not outdir is None : outfilename = outdir + "/" + get_basename(imgfilename) + "_zoneboxes.png" else : outfilename = get_basename(imgfilename) + "_zoneboxes.png" img = load_image(imgfilename) draw = ImageDraw.Draw(img) # draw the zone boxes onto the image for box in box_list : box.sanity() # print box.document_id # print box.corner_one # print box.corner_two upper_left = (box.corner_one["col"],box.corner_one["row"]) lower_right = (box.corner_two["col"],box.corner_two["row"]) draw.rectangle( (upper_left,lower_right), outline="red" ) img.save(outfilename) print "wrote",outfilename
def make_sliding_strips_from_box( boxfilename ) : basename = get_basename( boxfilename ) # create the output directory for all the files I'm about to create output_dir = make_output_dir(basename) # return box_list = zonebox.load_boxes( boxfilename ) # load the image associated with this box list # assume all the boxes have the same image name (they should) imgfilename = "IMAGEBIN/{0}BIN.png".format( box_list[0].document_id ) # get the image as a numpy array data = mkslices.load_image( imgfilename ) strip_list = make_all_strips_images( data, basename, output_dir ) # # Now make the ground truth files for each strip # # convert the box list into a list of strips box_strip_list = [ rects.Strip(box=box) for box in box_list ] # slice up the ground truth into individual XML files make_all_gtruth_xml( box_strip_list, data, output_dir, basename )
def draw_boxes(boxfilename, outdir=None): box_list = zonebox.load_boxes(boxfilename) print box_list # get the document id from the box file # imgfilename = "IMAGEBIN/{0}BIN.TIF".format( box_list[0].document_id ) imgfilename = "IMAGEBIN/{0}BIN.png".format(box_list[0].document_id) # write the output image to this file and perhaps to another directory if not outdir is None: outfilename = outdir + "/" + get_basename( imgfilename) + "_zoneboxes.png" else: outfilename = get_basename(imgfilename) + "_zoneboxes.png" img = load_image(imgfilename) draw = ImageDraw.Draw(img) # draw the zone boxes onto the image for box in box_list: box.sanity() # print box.document_id # print box.corner_one # print box.corner_two upper_left = (box.corner_one["col"], box.corner_one["row"]) lower_right = (box.corner_two["col"], box.corner_two["row"]) draw.rectangle((upper_left, lower_right), outline="red") img.save(outfilename) print "wrote", outfilename
def make_gtruth_slices(boxfilename): basename = get_basename(boxfilename) box_list = zonebox.load_boxes(boxfilename) print "found", len(box_list), "boxes" # load the image associated with this box list # assume all the boxes have the same image name (they should) imgfilename = "IMAGEBIN/{0}BIN.png".format(box_list[0].document_id) img = drawboxes.load_image(imgfilename) print img.mode, img.size num_cols, num_rows = img.size print "rows={0} cols={1}".format(num_rows, num_cols) draw = ImageDraw.Draw(img) # starting strip as wide as the iamge with our base number of rows s = rects.Strip(width=num_cols, height=strip_rows) box_strip_list = [rects.Strip(box=box) for box in box_list] data = np.asarray(img, dtype="uint8") print "shape=", data.shape # draw the ground truth in blue as sanity check (should see no blue in the # output image) for box_strip in box_strip_list: upper_left = box_strip.rect[0].x, box_strip.rect[0].y lower_right = box_strip.rect[2].x, box_strip.rect[2].y draw.rectangle((upper_left, lower_right), outline="blue") # iterate the strip down the page, calculating all the box intersections # for each strip row = 0 strip_counter = 0 while row < num_rows: print "strip=", s upper_left = s.rect[0].x, s.rect[0].y lower_right = s.rect[2].x, s.rect[2].y draw.rectangle((upper_left, lower_right), outline="green") # linear search all the boxes searching for those that match this strip box_intersect_list = [] for box_strip in box_strip_list: isect = rects.strip_intersect(box_strip, s) if isect: print 'isect=', isect # PIL's Draw is x,y order upper_left = isect.rect[0].x, isect.rect[0].y lower_right = isect.rect[2].x, isect.rect[2].y draw.rectangle((upper_left, lower_right), outline="red") # adjust the intersections so the new ground truth of the box # intersections starts at row=0 (making new images out of # strips so need ground truth for each image strip) for rect in isect.rect: # subtract out the starting Y position of upper left rect.y -= s.rect[0].y print "adjusted isect=", isect # save this intersection; we'll write to a new XML file box_intersect_list.append(isect) # save the intersections as XML xmlfilename = "{0}_s{1}.xml".format(basename, strip_counter) with open(xmlfilename, "w") as outfile: zone2xml.write_boxlist_to_xml(outfile, box_intersect_list) print "wrote", xmlfilename s.next_strip() row += strip_rows strip_counter += 1 outfilename = "{0}_out.png".format(basename) img.save(outfilename) print "wrote", outfilename
def convert_zonebox_to_xml( outfile, boxfilename) : box_list = zonebox.load_boxes(boxfilename) write_boxlist_to_xml( outfile, box_list )
def convert_zonebox_to_xml(outfile, boxfilename): box_list = zonebox.load_boxes(boxfilename) write_boxlist_to_xml(outfile, box_list)
def make_gtruth_slices( boxfilename ) : basename = get_basename( boxfilename ) box_list = zonebox.load_boxes( boxfilename ) print "found",len(box_list),"boxes" # load the image associated with this box list # assume all the boxes have the same image name (they should) imgfilename = "IMAGEBIN/{0}BIN.png".format( box_list[0].document_id ) img = drawboxes.load_image(imgfilename) print img.mode, img.size num_cols,num_rows = img.size print "rows={0} cols={1}".format( num_rows, num_cols ) draw = ImageDraw.Draw(img) # starting strip as wide as the iamge with our base number of rows s = rects.Strip(width=num_cols, height=strip_rows ) box_strip_list = [ rects.Strip(box=box) for box in box_list ] data = np.asarray(img,dtype="uint8") print "shape=",data.shape # draw the ground truth in blue as sanity check (should see no blue in the # output image) for box_strip in box_strip_list : upper_left = box_strip.rect[0].x, box_strip.rect[0].y lower_right = box_strip.rect[2].x, box_strip.rect[2].y draw.rectangle( (upper_left,lower_right), outline="blue") # iterate the strip down the page, calculating all the box intersections # for each strip row = 0 strip_counter = 0 while row < num_rows : print "strip=",s upper_left = s.rect[0].x, s.rect[0].y lower_right = s.rect[2].x, s.rect[2].y draw.rectangle( (upper_left,lower_right), outline="green" ) # linear search all the boxes searching for those that match this strip box_intersect_list = [] for box_strip in box_strip_list : isect = rects.strip_intersect( box_strip, s ) if isect : print 'isect=',isect # PIL's Draw is x,y order upper_left = isect.rect[0].x, isect.rect[0].y lower_right = isect.rect[2].x, isect.rect[2].y draw.rectangle( (upper_left,lower_right), outline="red" ) # adjust the intersections so the new ground truth of the box # intersections starts at row=0 (making new images out of # strips so need ground truth for each image strip) for rect in isect.rect : # subtract out the starting Y position of upper left rect.y -= s.rect[0].y print "adjusted isect=",isect # save this intersection; we'll write to a new XML file box_intersect_list.append( isect ) # save the intersections as XML xmlfilename = "{0}_s{1}.xml".format( basename, strip_counter ) with open(xmlfilename,"w") as outfile : zone2xml.write_boxlist_to_xml( outfile, box_intersect_list ) print "wrote", xmlfilename s.next_strip() row += strip_rows strip_counter += 1 outfilename = "{0}_out.png".format( basename ) img.save(outfilename) print "wrote",outfilename