def make_all_gtruth_xml(box_strip_list, data, output_dir, basename): total_num_rows, num_cols = data.shape # starting strip as wide as the iamge with our base number of rows s = rects.Strip(width=num_cols, height=num_rows_in_strip) outfilename_fmt = "{0}_{1:03}_{2:03}_{3:04}.xml" row = 0 while row < total_num_rows: # 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 not isect: continue # print 'isect=',isect # 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 = os.path.join( output_dir, outfilename_fmt.format(basename, num_rows_in_strip, num_rows_to_slide, row)) # xmlfilename = output_dir + "/" + outfilename_fmt.format( # basename, num_rows_in_strip, num_rows_to_slide, row ) # print xmlfilename with open(xmlfilename, "w") as outfile: zone2xml.write_boxlist_to_xml(outfile, box_intersect_list) print "wrote", xmlfilename # slide the strip down by our window shift amount s.rect[0].y += num_rows_to_slide s.rect[1].y += num_rows_to_slide s.rect[2].y += num_rows_to_slide s.rect[3].y += num_rows_to_slide row += num_rows_to_slide
def make_all_gtruth_xml( box_strip_list, data, output_dir, basename) : total_num_rows,num_cols = data.shape # starting strip as wide as the iamge with our base number of rows s = rects.Strip(width=num_cols, height=num_rows_in_strip ) outfilename_fmt = "{0}_{1:03}_{2:03}_{3:04}.xml" row = 0 while row < total_num_rows : # 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 not isect : continue # print 'isect=',isect # 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 = os.path.join(output_dir, outfilename_fmt.format( basename, num_rows_in_strip, num_rows_to_slide, row ) ) # xmlfilename = output_dir + "/" + outfilename_fmt.format( # basename, num_rows_in_strip, num_rows_to_slide, row ) # print xmlfilename with open(xmlfilename,"w") as outfile : zone2xml.write_boxlist_to_xml( outfile, box_intersect_list ) print "wrote", xmlfilename # slide the strip down by our window shift amount s.rect[0].y += num_rows_to_slide s.rect[1].y += num_rows_to_slide s.rect[2].y += num_rows_to_slide s.rect[3].y += num_rows_to_slide row += num_rows_to_slide
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 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