text: """
  Copyright 2017-2018 yhenon (https://github.com/yhenon/)
  Copyright 2017-2018 Fizyr (https://fizyr.com)
  
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
  
      http://www.apache.org/licenses/LICENSE-2.0
  
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
  """
  
  from .generator import Generator
  from ..utils.image import read_image_bgr, read_image_as_mono, read_image_as_grayscale
  from ..utils.image import resize_image
  
  import numpy as np
  from PIL import Image
  from six import raise_from
  
  import csv
  import sys
  import os.path
  import math
  
  def _parse(value, function, fmt):
      """
      Parse a string into a value, and format a nice ValueError if it fails.
  
      Returns `function(value)`.
      Any `ValueError` raised is catched and a new `ValueError` is raised
      with message `fmt.format(e)`, where `e` is the caught `ValueError`.
      """
      try:
          return function(value)
      except ValueError as e:
          raise_from(ValueError(fmt.format(e)), None)
  
  
  def _read_classes(csv_reader):
      result = {}
      for line, row in enumerate(csv_reader):
          try:
              class_name, class_id = row
          except ValueError:
              raise_from(ValueError('line {}: format should be \'class_name,class_id\''.format(line)), None)
          class_id = _parse(class_id, int, 'line {}: malformed class ID: {{}}'.format(line))
  
          if class_name in result:
              raise ValueError('line {}: duplicate class name: \'{}\''.format(line, class_name))
          result[class_name] = class_id
      return result
  
  def _read_annotations(csv_reader, classes):
      result = {}
      for line, row in enumerate(csv_reader):
          try:
              img_file, x1, y1, x2, y2, class_name = row
          except ValueError:
              raise_from(ValueError('line {}: format should be \'img_file,x1,y1,x2,y2,class_name\' or \'img_file,,,,,\''.format(line)), None)
  
          if img_file not in result:
              result[img_file] = []
  
          # If a row contains only an image path, it's an image without annotations.
          if (x1, y1, x2, y2, class_name) == ('', '', '', '', ''):
              continue
  
          x1 = _parse(x1, int, 'line {}: malformed x1: {{}}'.format(line))
          y1 = _parse(y1, int, 'line {}: malformed y1: {{}}'.format(line))
          x2 = _parse(x2, int, 'line {}: malformed x2: {{}}'.format(line))
          y2 = _parse(y2, int, 'line {}: malformed y2: {{}}'.format(line))
  
          # Check that the bounding box is valid.
          if x2 <= x1:
              raise ValueError('line {}: x2 ({}) must be higher than x1 ({})'.format(line, x2, x1))
          if y2 <= y1:
              raise ValueError('line {}: y2 ({}) must be higher than y1 ({})'.format(line, y2, y1))
  
          # check if the current class name is correctly present
          if class_name not in classes:
              raise ValueError('line {}: unknown class name: {}'.format(line, class_name))
  
          result[img_file].append({'x1': x1, 'x2': x2, 'y1': y1, 'y2': y2, 'class': class_name})
      return result
  
  def _open_for_csv(path):
      """
      Open a file with flags suitable for csv.reader.
  
      This is different for python2 it means with mode 'rb',
      for python3 this means 'r' with "universal newlines".
      """
      if sys.version_info[0] < 3:
          return open(path, 'rb')
      else:
          return open(path, 'r', newline='')
  
  
  class CSVGenerator(Generator):
      def __init__(
          self,
          csv_data_file,
          csv_class_file,
          mean_image_file,
          image_data_generator,
          base_dir=None,
          num_channels=3,
          **kwargs
      ):
          self.image_names = []
          self.video_names = []
          self.image_data  = {}
          self.base_dir    = base_dir
  
          if num_channels == 3: 
              self.image_type = 'rgb'
          else:
              self.image_type = 'mono'
              
          if mean_image_file is not None:
  
              self.mean_image = np.load(mean_image_file)
          else:
              self.mean_image = None
  
          # Take base_dir from annotations file if not explicitly specified.
          if self.base_dir is None:
              self.base_dir = os.path.dirname(csv_data_file)
  
          # parse the provided class file
          try:
              with _open_for_csv(csv_class_file) as file:
                  self.classes = _read_classes(csv.reader(file, delimiter=','))
          except ValueError as e:
              raise_from(ValueError('invalid CSV class file: {}: {}'.format(csv_class_file, e)), None)
  
          self.labels = {}
          for key, value in self.classes.items():
              self.labels[value] = key
  
          # csv with img_path, x1, y1, x2, y2, class_name
          try:
              with _open_for_csv(csv_data_file) as file:
                  self.image_data, self.video_names = _read_video_annotations(reader, self.classes)        except ValueError as e:
              raise_from(ValueError('invalid CSV annotations file: {}: {}'.format(csv_data_file, e)), None)
          self.image_names = list(self.image_data.keys())
          self.video_data = self.__parse_video_data()
          super(CSVGenerator, self).__init__(image_data_generator, **kwargs)
  
      def __parse_video_data(self):
          video_data = []
          for video in self.video_names.keys():
              print(video)
              for det in self.video_names[video]:
                  frame_num = int(det['frame'])
                  # Max frame num is 8978 atm
                  # TODO: Make dynamic based on video fps & length
                  if frame_num > 8978:
                      frame_num = 8978
                  cap = cv2.VideoCapture(video)
                  cap.set(cv2.CAP_PROP_POS_FRAMES, frame_num)
                  ret, frame = cap.read()
                  if ret:
                      print(frame_num)
                      self.video_data.append(frame)
                  else:
                      print("Extracting frame {} failed....".format(frame_num))
                      ret, frame = cap.read()
                      if ret:
                          video_data.append(frame)
                      else:
                          # Retry 4 times, throw exception on fail
                          for i in range(4):
                              print("Extracting frame {} failed again, retrying...".format(frame_num))
                              ret, frame = cap.read()
                              if ret:
                                  video_data.append(frame)
                                  break
                              else:
                                  if i == 4:
                                      raise Exception
          return video_data
  
      def size(self):
          return len(self.image_names)
  
      def num_classes(self):
          return max(self.classes.values()) + 1
  
      def name_to_label(self, name):
          return self.classes[name]
  
      def label_to_name(self, label):
          return self.labels[label]
  
      def image_path(self, image_index):
          return os.path.join(self.base_dir, self.image_names[image_index])
  
      def image_aspect_ratio(self, image_index):
          # PIL is fast for metadata
          image = Image.open(self.image_path(image_index))
          return float(image.width) / float(image.height)
  
      def load_image(self, image_index):
          image_index = image_index - 1 # zero based
          img = self.video_data[image_index]
          return img
  
      def load_annotations(self, image_index):
          path   = self.image_names[image_index]
          annots = self.image_data[path]
          boxes  = np.zeros((len(annots), 5))
  
          for idx, annot in enumerate(annots):
              class_name = annot['class']
              boxes[idx, 0] = float(annot['x1'])
              boxes[idx, 1] = float(annot['y1'])
              boxes[idx, 2] = float(annot['x2'])
              boxes[idx, 3] = float(annot['y2'])
              boxes[idx, 4] = self.name_to_label(class_name)
  
          return boxes
  