Exemplo n.º 1
0
def cropFromFloat(x):
  x = x * 255
  x = np.asarray(x, dtype='uint8')
  # y = x.reshape(SMALL_SIZE)
  y = facedetection.cropFace(x, rescaleForReconigtion=1)
  if y is None:
    return None
  return y.reshape(SMALL_SIZE)
Exemplo n.º 2
0
def cropFromFloat(x):
    x = x * 255
    x = np.asarray(x, dtype='uint8')
    # y = x.reshape(SMALL_SIZE)
    y = facedetection.cropFace(x, rescaleForReconigtion=1)
    if y is None:
        return None
    return y.reshape(SMALL_SIZE)
Exemplo n.º 3
0
def readCropEqualize(path, extension, crop, doRecognition, equalize=False,
                     isColoured=False):
  if not crop and doRecognition:
    raise Exception("you asked for the reading process to crop the images but do no face detection")

  if equalize:
    dirforres = "detection-cropped-equalized"
  else:
    dirforres = "detection-cropped"

  pathForCropped = os.path.join(path, dirforres)

  if crop:
    if doRecognition:
      if not os.path.exists(pathForCropped):
        os.makedirs(pathForCropped)

      imageFiles = [(os.path.join(dirpath, f), f)
        for dirpath, dirnames, files in os.walk(path)
        for f in fnmatch.filter(files, '*.' + extension)]

      images = []

      for fullPath, shortPath in imageFiles:
        # Do not do this for already cropped images
        if pathForCropped in fullPath:
          continue

        print fullPath
        print shortPath
        # img = Image.open(fullPath)
        # img = np.array(img.getdata()).reshape(img.size[0], img.size[1])
        # print img.shape
        img = cv2.imread(fullPath, 0)
        print img == None

        face = facedetection.cropFace(img)


        if not face == None:

          face = resize(face, SMALL_SIZE)
          if equalize:
            face = equalizeFromFloatCLAHE(face)

          face = face.reshape(SMALL_SIZE)
          # Only do the resizing once you are done with the cropping of the faces
          # Check that you are always saving them in the right format
          print "face.min"
          print face.min()

          print "face.max"
          print face.max()

          assert face.min() >=0 and face.max() <=1
          images += [face.reshape(-1)]

          # Save faces as files
          croppedFileName = os.path.join(pathForCropped, shortPath)
          io.imsave(croppedFileName, face)
    # If not doing face detection live
    else:
      images = []
      imageFiles = [os.path.join(dirpath, f)
        for dirpath, dirnames, files in os.walk(pathForCropped)
        for f in fnmatch.filter(files, '*.' + extension)]

      for f in imageFiles:
        img = cv2.imread(f, 0)
        if type(img[0,0]) == np.uint8:
          print "rescaling unit"
          img = img / 255.0

        img = resize(img, SMALL_SIZE)
        images += [img.reshape(-1)]
  # If not doing recognition here, just reading from the initial faces
  else:
    images = []
    imageFiles = [os.path.join(dirpath, f)
      for dirpath, dirnames, files in os.walk(path) if dirnames not in ["detection-cropped-equalized", "detection-cropped"]
      for f in fnmatch.filter(files, '*.' + extension)]

    for i in imageFiles:
      assert not "detection-cropped" in imageFiles

    for f in imageFiles:
      img = cv2.imread(f, 0)
      if type(img[0,0]) == np.uint8:
        print "rescaling unit"
        img = img / 255.0

      img = resize(img, SMALL_SIZE)

      if equalize:
        img = equalizeFromFloatCLAHE(img)

      images += [img.reshape(-1)]

  assert len(images) != 0

  print len(images)
  return np.array(images)
Exemplo n.º 4
0
def readCropEqualize(path,
                     extension,
                     crop,
                     doRecognition,
                     equalize=False,
                     isColoured=False):
    if not crop and doRecognition:
        raise Exception(
            "you asked for the reading process to crop the images but do no face detection"
        )

    if equalize:
        dirforres = "detection-cropped-equalized"
    else:
        dirforres = "detection-cropped"

    pathForCropped = os.path.join(path, dirforres)

    if crop:
        if doRecognition:
            if not os.path.exists(pathForCropped):
                os.makedirs(pathForCropped)

            imageFiles = [(os.path.join(dirpath, f), f)
                          for dirpath, dirnames, files in os.walk(path)
                          for f in fnmatch.filter(files, '*.' + extension)]

            images = []

            for fullPath, shortPath in imageFiles:
                # Do not do this for already cropped images
                if pathForCropped in fullPath:
                    continue

                print fullPath
                print shortPath
                # img = Image.open(fullPath)
                # img = np.array(img.getdata()).reshape(img.size[0], img.size[1])
                # print img.shape
                img = cv2.imread(fullPath, 0)
                print img == None

                face = facedetection.cropFace(img)

                if not face == None:

                    face = resize(face, SMALL_SIZE)
                    if equalize:
                        face = equalizeFromFloatCLAHE(face)

                    face = face.reshape(SMALL_SIZE)
                    # Only do the resizing once you are done with the cropping of the faces
                    # Check that you are always saving them in the right format
                    print "face.min"
                    print face.min()

                    print "face.max"
                    print face.max()

                    assert face.min() >= 0 and face.max() <= 1
                    images += [face.reshape(-1)]

                    # Save faces as files
                    croppedFileName = os.path.join(pathForCropped, shortPath)
                    io.imsave(croppedFileName, face)
        # If not doing face detection live
        else:
            images = []
            imageFiles = [
                os.path.join(dirpath, f)
                for dirpath, dirnames, files in os.walk(pathForCropped)
                for f in fnmatch.filter(files, '*.' + extension)
            ]

            for f in imageFiles:
                img = cv2.imread(f, 0)
                if type(img[0, 0]) == np.uint8:
                    print "rescaling unit"
                    img = img / 255.0

                img = resize(img, SMALL_SIZE)
                images += [img.reshape(-1)]
    # If not doing recognition here, just reading from the initial faces
    else:
        images = []
        imageFiles = [
            os.path.join(dirpath, f)
            for dirpath, dirnames, files in os.walk(path) if dirnames not in
            ["detection-cropped-equalized", "detection-cropped"]
            for f in fnmatch.filter(files, '*.' + extension)
        ]

        for i in imageFiles:
            assert not "detection-cropped" in imageFiles

        for f in imageFiles:
            img = cv2.imread(f, 0)
            if type(img[0, 0]) == np.uint8:
                print "rescaling unit"
                img = img / 255.0

            img = resize(img, SMALL_SIZE)

            if equalize:
                img = equalizeFromFloatCLAHE(img)

            images += [img.reshape(-1)]

    assert len(images) != 0

    print len(images)
    return np.array(images)