Example #1
0
def _generate_stats_file(stats_file_name, input_directory='.'):
    """Generate statistics file.

  The function generates a statistics file. The contents of the file are in the
  format <frame_name> <barcode>, where frame name is the name of every frame
  (effectively the frame number) and barcode is the decoded barcode. The frames
  and the helper .txt files are removed after they have been used.
  """
    file_prefix = os.path.join(input_directory, 'frame_')
    stats_file = open(stats_file_name, 'w')

    print 'Generating stats file: %s' % stats_file_name
    for i in range(1, _count_frames_in(input_directory=input_directory) + 1):
        frame_number = helper_functions.zero_pad(i)
        barcode_file_name = file_prefix + frame_number + '.txt'
        png_frame = file_prefix + frame_number + '.png'
        entry_frame_number = helper_functions.zero_pad(i - 1)
        entry = 'frame_' + entry_frame_number + ' '

        if os.path.isfile(barcode_file_name):
            barcode = _read_barcode_from_text_file(barcode_file_name)
            os.remove(barcode_file_name)

            if _check_barcode(barcode):
                entry += (helper_functions.zero_pad(int(barcode[0:11])) + '\n')
            else:
                entry += 'Barcode error\n'  # Barcode is wrongly detected.
        else:  # Barcode file doesn't exist.
            entry += 'Barcode error\n'

        stats_file.write(entry)
        os.remove(png_frame)

    stats_file.close()
def _generate_stats_file(stats_file_name, input_directory='.'):
  """Generate statistics file.

  The function generates a statistics file. The contents of the file are in the
  format <frame_name> <barcode>, where frame name is the name of every frame
  (effectively the frame number) and barcode is the decoded barcode. The frames
  and the helper .txt files are removed after they have been used.
  """
  file_prefix = os.path.join(input_directory, 'frame_')
  stats_file = open(stats_file_name, 'w')

  print 'Generating stats file: %s' % stats_file_name
  for i in range(1, _count_frames_in(input_directory=input_directory) + 1):
    frame_number = helper_functions.zero_pad(i)
    barcode_file_name = file_prefix + frame_number + '.txt'
    png_frame = file_prefix + frame_number + '.png'
    entry_frame_number = helper_functions.zero_pad(i-1)
    entry = 'frame_' + entry_frame_number + ' '

    if os.path.isfile(barcode_file_name):
      barcode = _read_barcode_from_text_file(barcode_file_name)
      os.remove(barcode_file_name)

      if _check_barcode(barcode):
        entry += (helper_functions.zero_pad(int(barcode[0:11])) + '\n')
      else:
        entry += 'Barcode error\n'  # Barcode is wrongly detected.
    else:  # Barcode file doesn't exist.
      entry += 'Barcode error\n'

    stats_file.write(entry)
    os.remove(png_frame)

  stats_file.close()
def generate_upca_barcodes(number_of_barcodes,
                           barcode_width,
                           barcode_height,
                           output_directory='.',
                           path_to_zxing='zxing-read-only'):
    """Generates UPC-A barcodes.

  This function generates a number_of_barcodes UPC-A barcodes. The function
  calls an example Java encoder from the Zxing library. The barcodes are
  generated as PNG images. The width of the barcodes shouldn't be less than 102
  pixels because otherwise Zxing can't properly generate the barcodes.

  Args:
    number_of_barcodes(int): The number of barcodes to generate.
    barcode_width(int): Width of barcode in pixels.
    barcode_height(int): Height of barcode in pixels.
    output_directory(string): Output directory where to store generated
      barcodes.
    path_to_zxing(string): The path to Zxing.

  Return:
    (bool): True if the conversion is successful.
  """
    base_file_name = os.path.join(output_directory, "barcode_")
    jars = _form_jars_string(path_to_zxing)
    command_line_encoder = 'com.google.zxing.client.j2se.CommandLineEncoder'
    barcode_width = str(barcode_width)
    barcode_height = str(barcode_height)

    errors = False
    for i in range(number_of_barcodes):
        suffix = helper_functions.zero_pad(i)
        # Barcodes starting from 0
        content = helper_functions.zero_pad(i, 11)
        output_file_name = base_file_name + suffix + ".png"

        command = [
            "java", "-cp", jars, command_line_encoder,
            "--barcode_format=UPC_A",
            "--height=%s" % barcode_height,
            "--width=%s" % barcode_width,
            "--output=%s" % (output_file_name),
            "%s" % (content)
        ]
        try:
            helper_functions.run_shell_command(
                command,
                fail_msg=('Error during barcode %s generation' % content))
        except helper_functions.HelperError as err:
            print >> sys.stderr, err
            errors = True
    return not errors
def generate_upca_barcodes(number_of_barcodes, barcode_width, barcode_height,
                           output_directory='.',
                           path_to_zxing='zxing-read-only'):
  """Generates UPC-A barcodes.

  This function generates a number_of_barcodes UPC-A barcodes. The function
  calls an example Java encoder from the Zxing library. The barcodes are
  generated as PNG images. The width of the barcodes shouldn't be less than 102
  pixels because otherwise Zxing can't properly generate the barcodes.

  Args:
    number_of_barcodes(int): The number of barcodes to generate.
    barcode_width(int): Width of barcode in pixels.
    barcode_height(int): Height of barcode in pixels.
    output_directory(string): Output directory where to store generated
      barcodes.
    path_to_zxing(string): The path to Zxing.

  Return:
    (bool): True if the conversion is successful.
  """
  base_file_name = os.path.join(output_directory, "barcode_")
  jars = _form_jars_string(path_to_zxing)
  command_line_encoder = 'com.google.zxing.client.j2se.CommandLineEncoder'
  barcode_width = str(barcode_width)
  barcode_height = str(barcode_height)

  errors = False
  for i in range(number_of_barcodes):
    suffix = helper_functions.zero_pad(i)
    # Barcodes starting from 0
    content = helper_functions.zero_pad(i, 11)
    output_file_name = base_file_name + suffix + ".png"

    command = ["java", "-cp", jars, command_line_encoder,
               "--barcode_format=UPC_A", "--height=%s" % barcode_height,
               "--width=%s" % barcode_width,
               "--output=%s" % (output_file_name), "%s" % (content)]
    try:
      helper_functions.run_shell_command(
          command, fail_msg=('Error during barcode %s generation' % content))
    except helper_functions.HelperError as err:
      print >> sys.stderr, err
      errors = True
  return not errors
Example #5
0
def _count_frames_in(input_directory='.'):
    """Calculates the number of frames in the input directory.

  The function calculates the number of frames in the input directory. The
  frames should be named frame_xxxx.png, where xxxx is the number of the frame.
  The numbers should start from 1 and should be consecutive.

  Args:
    input_directory(string): The input directory.
  Return:
    (int): The number of frames.
  """
    file_prefix = os.path.join(input_directory, 'frame_')
    file_exists = True
    num = 1

    while file_exists:
        file_name = (file_prefix + helper_functions.zero_pad(num) + '.png')
        if os.path.isfile(file_name):
            num += 1
        else:
            file_exists = False
    return num - 1
def _count_frames_in(input_directory = '.'):
  """Calculates the number of frames in the input directory.

  The function calculates the number of frames in the input directory. The
  frames should be named frame_xxxx.png, where xxxx is the number of the frame.
  The numbers should start from 1 and should be consecutive.

  Args:
    input_directory(string): The input directory.
  Return:
    (int): The number of frames.
  """
  file_prefix = os.path.join(input_directory, 'frame_')
  file_exists = True
  num = 1

  while file_exists:
    file_name = (file_prefix + helper_functions.zero_pad(num) + '.png')
    if os.path.isfile(file_name):
      num += 1
    else:
      file_exists = False
  return num - 1