コード例 #1
0
def my_application():
   from gamera.toolkits.ocr.classes import Page
   # Make the image variable a global variable
   # IF YOU DON'T DO THIS, THE WINDOW WILL DISAPPEAR!
   global image, ots, ots_up, ots_mid, box_image, ots_low
   print sys.argv[-1]
   #image_color = load_image(sys.argv[-1])
   picture = mh.imread(sys.argv[-1], as_grey=True)
   picture = picture.astype(np.uint8)
   # Load the image
   image = numpy_io.from_numpy(picture)#load_image(sys.argv[-1]
   #image = image_color.to_greyscale()
   # Display the image in a window
   #image.simple_sharpen()
   ots = image.otsu_threshold()
   thresh = image.otsu_find_threshold()
   #thresh_low = int(thresh* 1.05)
   thresh_mid = thresh_plus = int(thresh * 1.0)
   ots_mid = image.threshold(thresh_mid)
   my_filter(ots_mid)
   ots_up = ots_mid.image_copy()#image.threshold(thresh_plus)
   #ots_up.remove_border()
   
  # ots_low = image.threshold(thresh_low)
   print "OTSU: " + str(thresh)
   print "OTSU Mid: " + str(thresh_mid)
   print "OTSU * 1.15: " + str(thresh_plus)
   #ots_low = filter_and_bbox(ots_low)
   #ots = filter_and_bbox(ots)
   (ots_up,ots_mid) = filter_and_bbox(ots_up)
   #ots_mid = only_app_crit(ots_mid,x)
   #print "x is " + str(x)
   ots_up.display("OTSU-border")
   ots_mid.display()
コード例 #2
0
def my_application():
    from gamera.toolkits.ocr.classes import Page
    # Make the image variable a global variable
    # IF YOU DON'T DO THIS, THE WINDOW WILL DISAPPEAR!
    global image, ots, ots_up, ots_mid, box_image, ots_low
    print sys.argv[-1]
    #image_color = load_image(sys.argv[-1])
    picture = mh.imread(sys.argv[-1], as_grey=True)
    picture = picture.astype(np.uint8)
    # Load the image
    image = numpy_io.from_numpy(picture)  #load_image(sys.argv[-1]
    #image = image_color.to_greyscale()
    # Display the image in a window
    #image.simple_sharpen()
    ots = image.otsu_threshold()
    thresh = image.otsu_find_threshold()
    thresh_low = int(thresh * 0.90)
    thresh_mid = int(thresh * 1.05)
    thresh_plus = int(thresh * 1.18)
    ots_up = image.threshold(thresh_plus)
    ots_mid = image.threshold(thresh_mid)
    ots_low = image.threshold(thresh_low)
    print "OTSU: " + str(thresh)
    print "OTSU Mid: " + str(thresh_mid)
    print "OTSU * 1.15: " + str(thresh_plus)
    ots_low = filter_and_bbox(ots_low)
    ots = filter_and_bbox(ots)
    ots_up = filter_and_bbox(ots_up)
    ots_up.display("OTSU+")
    #ots_mid = show_lines(p)
    #box_image.display()
    #my_filter(ots_mid)
    ots_up.display("OTSU+FIL")
    ots_mid = filter_and_bbox(ots_mid)
    ots_mid.display("MID")
    ots.display("OTSU")
    ots_low.display("OTSU_LOW")
    image.display()
コード例 #3
0
def my_application():
   from gamera.toolkits.ocr.classes import Page
   # Make the image variable a global variable
   # IF YOU DON'T DO THIS, THE WINDOW WILL DISAPPEAR!
   global image, ots, ots_up, ots_mid, box_image, ots_low
   print sys.argv[-1]
   #image_color = load_image(sys.argv[-1])
   picture = mh.imread(sys.argv[-1], as_grey=True)
   picture = picture.astype(np.uint8)
   # Load the image
   image = numpy_io.from_numpy(picture)#load_image(sys.argv[-1]
   #image = image_color.to_greyscale()
   # Display the image in a window
   #image.simple_sharpen()
   ots = image.otsu_threshold()
   thresh = image.otsu_find_threshold()
   thresh_low = int(thresh* 0.90)
   thresh_mid = int(thresh * 1.05)
   thresh_plus = int(thresh * 1.18)
   ots_up = image.threshold(thresh_plus)
   ots_mid = image.threshold(thresh_mid)
   ots_low = image.threshold(thresh_low)
   print "OTSU: " + str(thresh)
   print "OTSU Mid: " + str(thresh_mid)
   print "OTSU * 1.15: " + str(thresh_plus)
   ots_low = filter_and_bbox(ots_low)
   ots = filter_and_bbox(ots)
   ots_up = filter_and_bbox(ots_up)
   ots_up.display("OTSU+")
   #ots_mid = show_lines(p)
   #box_image.display()
   #my_filter(ots_mid)
   ots_up.display("OTSU+FIL")
   ots_mid = filter_and_bbox(ots_mid)
   ots_mid.display("MID")
   ots.display("OTSU")
   ots_low.display("OTSU_LOW")
   image.display()
コード例 #4
0
def performGreekOCR(options):
   import mahotas as mh
   import numpy as np
   from pylab import imshow, gray, show
   #from os import path
   from gamera.plugins import numpy_io
#   features = ["aspect_ratio", "volume64regions", "moments", "nholes_extended"]   
#I think these are size-invariant
#   features = ["aspect_ratio","moments","nholes","nholes_extended","skeleton_features","top_bottom","volume","volume16regions","volume64regions","zernike_moments"]
   MAX_CCS = 6500
   features = ["aspect_ratio","moments","ncols_feature","nholes","nholes_extended","nrows_feature","skeleton_features","top_bottom","volume","volume16regions","volume64regions","zernike_moments"]
   image_files = []
   g = GreekOCR(splits=options["split"],feats=features)
   g.mode = options["mode"] + "body"
   g.autogroup = options["autogroup"]
   g.debug = options["debug"] 
   g.load_trainingdata(options['trainingdata'])
   g_appcrit = GreekOCR(splits=options["split"], feats=features)
   g_appcrit.mode = options["mode"] + "appcrit"
   g_appcrit.autogroup = options["autogroup"]
   g_appcrit.debug = options["debug"]
   g_appcrit.load_trainingdata(options['trainingdata'])
   
   if options["hocrfile"]:
      g.hocr = (options["hocrfile"])
   if options["settingsfile"]:
      g.load_settings(options["settingsfile"])
      g_appcrit.load_settings(options["settingsfile"])
   if options["otsu"]:
      otsu_factors_string = options["otsu"].split(',')
      otsu_factors = [float(x) for x in otsu_factors_string]
   else:
      otsu_factors = [0]
   if options["directory"]:
      image_files = os.listdir(options["directory"])
      image_files = [os.path.join(options["directory"],x) for x in image_files]
      test = re.compile(".png$",re.IGNORECASE)
      image_files = filter(test.search, image_files)
      image_files.sort()
   elif options["imagefile"]:
      image_files = options["imagefile"]
   image_file_count = 1;
   image_path = os.path.abspath(image_files[0])
   image_split_path = os.path.split(image_path)
   book_code = os.path.split(image_split_path[0])[1]
   book_id = 0
   if options.has_key("sql") and options["sql"]:
      book_id = sql_make_book_and_return_id(book_code)
  # if options.has_key("hocrout") and options["hocrout"]:
  #     hocr_tree = hocr_make_tree_and_return(book_code)
   for image_file in image_files:

      image_path = os.path.abspath(image_file)
      image_split_path = os.path.split(image_path)
      book_code = os.path.split(image_split_path[0])[1]#directory name
      image_file_name = image_split_path[1]
      imageBase, imageEx = os.path.splitext(image_file_name)
      threshold_info = ""
      print "Now working with image: " + image_file_name
      internal_image_file_path = os.path.join(book_code, image_file_name) 
      if imageEx == ".jp2":
         try:
            jp2Image = mh.imread(image_file, as_grey=True)
            jp2Image = jp2Image.astype(np.uint8)
            imageIn = numpy_io.from_numpy(jp2Image)
         except:
            print "Unexpected error:", sys.exc_info()[0]
            raise
      else:
         try:
            imageIn = load_image(image_file)
         except:
            continue
      imageType = imageIn.data.pixel_type
      if imageType != ONEBIT:
         if imageType != GREYSCALE:
            imageIn = imageIn.to_greyscale()
         otsu_thresh = imageIn.otsu_find_threshold()
      for otsu_factor in otsu_factors:
         if options.has_key("hocrout") and options["hocrout"]:
            hocr_tree = hocr_make_tree_and_return(book_code)
         if imageIn.data.pixel_type == ONEBIT:
            threshold_info = "thresh_128"
            otsu_thresh = 1.0
            image = imageIn
            if options["debug"]:
               print "image is ONEBIT; doing no threshold optimization."
         else:
            current_thresh = otsu_thresh * otsu_factor
            if current_thresh > 253.0:
               current_thresh = 253.0
            current_thresh = int(current_thresh)
            threshold_info = "thresh_" + str(int(current_thresh))# + "=" + str(otsu_factor)
            image = imageIn.threshold(current_thresh)
            print "Otsu factor: ", otsu_factor, " threshold: ", current_thresh
         if options["hocrfile"]:
            hocr_to_use = string.replace(options["hocrfile"],"%s",imageBase)
            g.hocr = hocr_to_use
            if options["debug"]:
               print "using '" + hocr_to_use + "' as hocr file"
         if options.has_key("filter") and options["filter"] == True:
             count = 0
             ccs = image.cc_analysis()
             if options.has_key("debug"):
                if options["debug"] == True:
                   print "filter started on",len(ccs) ,"elements..."
             #filter long vertical runs left over from margins
	     
	          
##               #Agressive run filtering
##               median_height = median([cc.nrows for cc in ccs])
##               for cc in ccs:
##               #TODO: add another condition that keeps these at edges of page
##                       if((cc.nrows / cc.ncols > 6) and (cc.nrows > 1.5 * median_height) ):
##                               cc.fill_white()
##                               del cc
##                               count = count + 1
             median_black_area = median([cc.black_area()[0] for cc in ccs])
             for cc in ccs:
               if(cc.black_area()[0] > (median_black_area * 10)):
                 cc.fill_white()
                 del cc
                 count = count + 1
             for cc in ccs:
               if(cc.black_area()[0] < (median_black_area / 10)):
                 cc.fill_white()
                 del cc
                 count = count + 1
             if options.has_key("debug") and options["debug"] == True:
                print "filter done.",len(ccs)-count,"elements left."
         if (len(ccs) < 5) or (len(ccs) > MAX_CCS):
                 print "Error: there are " + str(len(ccs)) +  " ccs. Max is " + str( MAX_CCS) +  " Omitting this image."
		 #raise ImageSegmentationError("Error: there are " + str(len(ccs)) +  " ccs. Max is " + str( MAX_CCS) +  " Omitting this image.")
         else:
            if options.has_key("deskew") and options["deskew"] == True:
              #from gamera.toolkits.otr.otr_staff import *
              if options.has_key("debug") and options["debug"] == True:
                print "\ntry to skew correct..."
              rotation = image.rotation_angle_projections(-10,10)[0]
              img = image.rotate(rotation,0)
              if options.has_key("debug") and options["debug"] == True:
                print "rotated with",rotation,"angle"
            if options.has_key("mode") and options["mode"] == "teubner":
               (body_image, app_crit_image) = splitAppCritTeubner(image)
               output = g.process_image(body_image)
               if app_crit_image:
                  print "there is an app. crit image"
                  appcrit_output = g_appcrit.process_image(app_crit_image)
               else:
                  print "there is no app. crit image"
                  appcrit_output = ""
               output = output + appcrit_output
            else:
               output = g.process_image(image) 
            output_file_name_base = options["unicodeoutfile"] + imageBase + "_" +imageEx[1:] + "_" + threshold_info
            if options.has_key("debug") and options["debug"] == True:
               g.save_debug_images(output_file_name_base)
               if options.has_key("mode") and options["mode"] == "teubner" and app_crit_image:
                  #TODO: make more general
                  g_appcrit.save_debug_images(output_file_name_base + "_appcrit")
            if options.has_key("hocrout") and options["hocrout"]:
               #if we turned this on, we would make a separate div for each page of input
               #hocr_tree = hocr_make_page_and_return_div(internal_image_file_path,image_file_count,book_id,hocr_tree)
               g.store_hocr(internal_image_file_path,hocr_tree)
               if options.has_key("mode") and options["mode"] == "teubner" and app_crit_image:
                  g_appcrit.store_hocr(internal_image_file_path,hocr_tree)
            if options.has_key("sql") and options["sql"]:
               page_id = sql_make_page_and_return_id(internal_image_file_path,image_file_count,book_id)
               g.store_sql(image_path,page_id) 
            if options.has_key("unicodeoutfile"):
                
               if options.has_key("hocrout") and options["hocrout"]:
                  g.save_text_hocr(hocr_tree, output_file_name_base + ".html")
               else:
                  g.save_text_unicode( output_file_name_base + ".txt")
                  if options.has_key("mode") and options["mode"] == "teubner":
                     #TODO: make the above more general
                     g_appcrit.save_text_unicode( output_file_name_base + "_appcrit.txt")
            elif options.has_key("teubneroutfile"):
               g.save_text_teubner(options["teubneroutfile"])
            else:
               print output
      image_file_count += 1