Exemple #1
0
def main(argv):
  try:
    opts, args = getopt.getopt(argv, "i:d:")
  except getopt.GetoptError:
    usage()
    sys.exit(2)
  INFILE = None
  FORMDATA = None
  for opt, arg in opts:
    if opt == "-h":
      usage()
      sys.exit()
    elif opt == "-i":
      INFILE = arg
    elif opt == "-d":
      FORMDATA = arg
  if not (INFILE or FORMDATA):
    usage()
    sys.exit(2)
  #
  # TODO: First we should read the registration mark data, which will apply to the whole set of forms
  # for a particular survey project. Second, we fix the scanned image. Third, we read the barcode,
  # so we can look up the bubble set information. Last, we check which bubbles have been filled in.
  #
  # Read registration mark data
  rmarks = rm.readform(FORMDATA)
  #
  # Load the image of the filled-out form
  form_img = Image.open(INFILE)
  #
  # Adjust image to fit the prototype
  # TODO: After we try fixing the image once, the registration markers are easier to find.
  # So if we try fixing again, we get closer. This is a little hacky, though. We should make
  # that part of the registration procedure.
  form_img_fixed = rm.fiximage(form_img, rmarks[0], rmarks[1], rmarks[2])
  form_img_fixed = rm.fiximage(form_img_fixed, rmarks[0], rmarks[1], rmarks[2])
  #
  # Read the barcode
  barcodedata = bc.readbarcode(form_img_fixed)
  print("bar code data: %s" % barcodedata)
  #
  # Read form data
  formsets = Bubble.readform(FORMDATA)
  #
  # Check responses to each bubble set
  responses = []
  for bs in formsets.sets:
    responses.append(bs.get_single_answer(form_img_fixed))
  #
  print("Form ID: %s" % formsets.form_id)
  for i in range(len(responses)):
    print("Response #%s: %s" % (i, responses[i]))
  if not noact:
    try:
      update_status(survey_id, img_id, 'working')
    except APIError, e:
      return 2

  # Load the image of the filled-out form
  imagestring = StringIO.StringIO(img_data)
  form_img = Image.open(imagestring)

  # Adjust image to fit the prototype
  # TODO: After we try fixing the image once, the registration markers are
  # easier to find.  So if we try fixing again, we get closer. This is a little
  # hacky, though.  We should make that part of the registration procedure.
  print 'Fixing image'
  form_img_fixed = rm.fiximage(form_img, rmarks[0], rmarks[1], rmarks[2])
  form_img_fixed = rm.fiximage(form_img_fixed, rmarks[0], rmarks[1], rmarks[2])

  # Read the barcode
  print 'Reading barcode'
  # Crop out an area around the barcode, to make life easier on the decoder.
  bc_bbox = paperinfo['barcode']['bbox']
  dpi = paperinfo['dpi']
  paper_size = (int(8.5*dpi), int(11.0*dpi))
  crop_buffer = 50
  crop_bbox = (max(bc_bbox[0] - crop_buffer, 0),
               max(bc_bbox[1] - crop_buffer, 0),
               min(bc_bbox[2] + crop_buffer, paper_size[0]),
               min(bc_bbox[3] + crop_buffer, paper_size[1]))
  # We don't need to crop when using the local tool. It will find the barcode
  # effectively.