Example #1
0
def applyPolyMask(eye, show=True):
    polymask = None
    eye.wip = cv2.bitwise_and(eye.wip, eye.polyMask)
    # eye.wip = cv2.bitwise_and(eye.wip, eye.manyPolyMask)
    if show:
        m3Show.imshow(eye.wip, "masked")
    return eye
Example #2
0
def contrast(inputImg, show):
    inputImg = PIL.Image.fromarray(inputImg)
    outputImg = ImageEnhance.Contrast(inputImg).enhance(1.4)
    if (show):
        m3Show.imshow(outputImg, "Contrast")
    outputImg = np.asarray(outputImg)
    return outputImg
Example #3
0
def makeCircularMask(photo, show=True, onlyOne=True):
    # for photo in photoArray:
    # print("photo")
    # photo = inputImg
    for face in photo.faces:
        # print("facee")
        for eye in face.eyes:
            # print("eye")
            maskImg = np.zeros_like(eye.wip)
            if not isinstance(eye.circle, type(None)):
                # firstCircle = eye.circle[0]
                if onlyOne:
                    i = eye.circle[0][0]
                    # print("i", i)
                    # print("onlyone", i[0], i[1], i[2])
                    cv2.circle(maskImg, (i[0], i[1]), i[2], (255, 255, 255),
                               -1)
                else:
                    for i in eye.circle[0, :]:
                        # print("i", i)
                        cv2.circle(maskImg, (i[0], i[1]), i[2],
                                   (255, 255, 255), -1)
                if (show):
                    m3Show.imshow(maskImg, "mask")
                eye.mask = maskImg
    return photo
Example #4
0
def batchcc(photoArray):
    for photo in photoArray:
        for face in photo.faces:
            for eye in face.eyes:
                ret, thresh1 = cv2.threshold(eye.iris,1,255,cv2.THRESH_BINARY)
                m3Show.imshow( eye.testMask, "MANMADE:")
                m3Show.imshow(thresh1, "thresh1:")
                compare(eye.testMask,thresh1)
    return photoArray
Example #5
0
def generateComparison(photoArray,
                       outputName=None,
                       attrs=None,
                       folderName=None,
                       exportFullMask=False,
                       CSV=True,
                       settings=None):
    # print("generateComparison
    os.makedirs("EXPORTS/" + folderName + "/", exist_ok=True)

    for photo in photoArray:
        facesToSave = []
        if (exportFullMask):
            os.makedirs("EXPORTS/" + folderName + "/", exist_ok=True)
            cv2.imwrite(
                "EXPORTS/" + folderName + "/" + os.path.basename(photo.path) +
                "_FullMask" + ".jpg", photo.fullMask)

        for face in photo.faces:
            if not (type(face.eyes) == type(None)):  # TODO: fix this
                for eye in face.eyes:
                    # print(attrs)
                    for attr in eye.__dict__.items():
                        # print("attr", attr)
                        if attr[0] in attrs:
                            # if
                            # print("attr[1].itemsize", attr[1].size)
                            if attr[1].size > 1:
                                # print(attr[0],attr[1], type(attr[1]), attr[1].shape)
                                facesToSave.append(attr[1])
                            # else:
                            #     facesToSave.append(eye.image)
            # if (len(eyesToSave) > 1):
            # facesToSave.append(eyesToSave)
            # else:
            # facesToSave.append(eyesToSave[0])
        now = datetime.now()
        now_string = now.strftime("%d-%m-%Y--%H-%M-%S")
        # print(facesToSave[0], type(facesToSave[0]))
        output = concat(facesToSave, direction="v")
        m3Show.imshow(output, "generateComparison output")
        if (folderName is not None):
            cv2.imwrite(
                "EXPORTS/" + folderName + "/" + os.path.basename(photo.path) +
                "_" + ".jpg", output)
        else:
            cv2.imwrite("EXPORTS/COMPARISONS/" + now_string + ".jpg", output)
        m3CSV.makeCSV(photoArray,
                      "EXPORTS/" + folderName + "/" + folderName + ".csv")
        file = open("EXPORTS/" + folderName + "/" + "settings.txt", "w+")
        count = 1
        # for element in m3F.funcArrToStr():
        #     print("element",element)
        file.write(str(settings))
        # count += 1
        file.close()
    return photoArray
Example #6
0
def findEyes2(photo, division, show=True):
    # downScaledDim = (round(w/100 *(scalePercent)),round(h/100 * (scalePercent)))
    h, w, c = photo.originalImage.shape
    downScaledDim = ((round(w / division)), round(h / division))
    copy = photo.originalImage.copy()
    pil_image = Image.fromarray(copy)
    inputImg = copy
    img = cv2.resize(inputImg, downScaledDim)
    dimToScaleUp = division
    detector = dlib.get_frontal_face_detector()
    dets = detector(img, 1)
    print("dets", dets)
    print("Number of faces detected: {}".format(len(dets)))
    for index, data in enumerate(dets):
        if index == 0:
            # print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
            #     k, d.left(), d.top(), d.right(), d.bottom()))
            # Get the landmarks/parts for the face in box d.
            shape = predictor(img, data)
            print("kkkk", index)
            # for part in shape.part
            # print("shape.parts():", shape.parts())
            # print("Part 0: {}, Part 1: {} ...".format(shape.part(0),
            # shape.part(1)))
            points = []
            imgwithPoints = img.copy()
            for i in range(shape.num_parts):
                point = shape.part(i)
                # for x, y in set:
                imgwithPoints = cv2.rectangle(imgwithPoints,
                                              (point.x - 10, point.y - 10),
                                              (point.x + 10, point.y + 10),
                                              (0, 0, 255), -1)
                points.append([point.x * division, point.y * division])
            cv2.imwrite(
                "pointsss" + datetime.now().strftime("%d-%m-%Y--%H-%M-%S") +
                ".jpg", imgwithPoints)
            m3Show.imshow(imgwithPoints, "imgwithPoints")
            for eye in photo.faces[0].eyes:
                cropRect = eye.cropRect
                print("cropRect", cropRect)
                for x, y in points:
                    #"The box is a 4-tuple defining the left, upper, right, and lower pixel coordinate"
                    # print("x, y", x,y)

                    imgwithPoints = img.copy()
                    if (x > cropRect[0] and x < cropRect[2]):
                        if (y > cropRect[1] and y < cropRect[3]):
                            # print("in bounds x, y", x, y)
                            eye.manyLandmarkPoints.append([x, y])
                            imgwithPoints = cv2.rectangle(
                                imgwithPoints, (x - 100, y - 100),
                                (x + 100, y + 100), (0, 0, 255), -1)
                m3Show.imshow(imgwithPoints, "in bounds! imgwithPoints")

    return photo
Example #7
0
def findEyes194(photo, division, show=True, ):
    img = photo.loResImage
    detector = dlib.get_frontal_face_detector()
    foundFaces = detector(img, 1)
    # print("foundFaces", foundFaces)
    # print("Number of faces detected: {}".format(len(foundFaces)))
    for index, data in enumerate(foundFaces):
        if index == 0:
            shape = predictor(img, data)
            e0points = []
            e1points = []
            for i in range(shape.num_parts):
                point = shape.part(i)

                # if (i >= 48 and i <= 69):  #
                if (i >= 40 and i <= 75):  #
                    e1points.append([round(point.x ), round(point.y )])
                if (i >= 20 and i <= 50):  #
                    e0points.append([round(point.x ), round(point.y )])
            # print("points", points)
            eyePoints = [e1points,e0points]
            eyeCount = 0
            for eye in photo.faces[0].eyes:
                # print("eyeCount",eyeCount)
                cropRect = eye.cropRect
                # print("cropRect", cropRect)
                # eye.manyLandmarkPoints = []
                points = eyePoints[eyeCount]
                # ys = []
                # print("points", points)
                margin = eye.margin
                upscaledLandmarks = []
                m3Show.imshow(eye.image,"eye.image")
                for x, y in points:
                    # ys.append(y)
                    # ensure that the points are within the croprect from 68 landmarks
                    # croprect: "The box is a 4-tuple defining the left, upper, right, and lower pixel coordinate"

                    if (x * division > cropRect[0] and x * division < cropRect[2]):
                        if (y * division > cropRect[1] and y * division< cropRect[3]):

                    # upscaledLandmarks.append([((x - (cropRect[0]-(margin*division))) ), ((y-(cropRect[1]-(margin*division))) )])
                            # print("cropRect", eye.cropRect)
                    # upscaledLandmarks.append([(((x * division)- ((cropRect[0])  - 0))) , ((((y*division) -((cropRect[1]) -  0)))) ])
                            # eye.manyLandmarkPoints.append([x, y])
                    # upscaledLandmarks.append([((x - (cropRect[0]-margin)) * division), ((y-(cropRect[1]-margin)) * division)])
                            upscaledLandmarks.append([((x - (eye.minX-eye.margin)) * division), ((y-(eye.minY-eye.margin)) * division)])
                # print("194upscaledLandmarks",upscaledLandmarks)
                # eye.mask194 = makePolyMask(photo.originalImage, upscaledLandmarks)
                eye.mask194 = makePolyMask(eye.image, upscaledLandmarks)

                # cv2.imwrite("EXPORTS/COMPARISONS/mask194.jpg", eye.mask194)
                eyeCount += 1
    return photo
Example #8
0
def makeCircularOutline(eye, circles, show=False, dest=None):
    # for face in photo.faces:
        # for eye in face.eyes:
    maskImg = eye.image.copy()
    if not isinstance(eye.circle, type(None)):
        # firstCircle = eye.circle[0]
        for i in circles[0, :]:
            print("i", i)
            cv2.circle(maskImg, (i[0], i[1]), i[2], (255, 255, 255), 2)
            # eye.mask = maskImg
            # setattr(eye, dest, maskImg)
            if (show):
                m3Show.imshow(maskImg, "mask")
    return maskImg
Example #9
0
def makeSmaller(eye, show=True, scale=5, debug=True, minimum=50):
    h, w, c = eye.image.shape
    # print("originalShape", h,w,c)
    # mini
    downScaledDim = ((round(w / scale)), round(h / scale))

    inputImg = eye.wip.copy()
    inputImg = cv2.resize(inputImg, downScaledDim)
    # if inputImg.shape[1] > minimum
    eye.wip = inputImg
    m3Show.imshow(eye.wip, "makeSmaller")
    # eye.
    if debug:
        print("makeSmaller: Shape : ", inputImg.shape)
    return eye
Example #10
0
def applyCircMask(photo, show=True, useOriginal=True):
    # for photo in photoArray:
    # print(photo)
    for face in photo.faces:
        for eye in face.eyes:
            if (eye.mask is None):
                m3F.printRed("THERE'S NOT MASK")
                break
            # print("eye.image.shape", eye.image.shape)
            # print("eye.mask.shape", eye.mask.shape)
            if useOriginal:
                eye.iris = cv2.bitwise_and(eye.image, eye.mask)
            else:
                eye.iris = cv2.bitwise_and(eye.wip, eye.mask)
            if show:
                m3Show.imshow(eye.iris, "masked")
    return photo
Example #11
0
def makeManyPolyMask(photo, show=True):
    # **********************************************************************
    # polyMask =  np.zeros_like(photo.originalImage)
    # **********************************************************************
    for face in photo.faces:
        for eye in face.eyes:
            polyMask =  np.zeros_like(eye.image)
            print("manyLandmarkPoints", eye.manyLandmarkPoints)
            polyMask = cv2.fillPoly(polyMask, np.int_([eye.manyLandmarkPoints]), (255, 255, 255))
            # m3Show.imshow(polyMask, "POLYMASK")
            # epm = m3Class.Eye(np.asarray(pil_image.crop(left)), left, lEyeCoor)
            # epm = polyMask.crop(eye.cropRect)
            # eye.manyPolyMask = m3F.typeSwap(m3F.typeSwap(polyMask).crop(eye.cropRect))
            eye.manyPolyMask = polyMask
            # if show:
            m3Show.imshow(eye.manyPolyMask, "manypoly")

    return photo
Example #12
0
def loadMasksForComparison(photoArray, maskFolder):
    maskImgs = glob.glob(maskFolder + "*.*g")  # load folder of masked images
    # print("maskImgs", maskImgs)
    maskImgs.sort()  # sort filenames, so 001test comes first, and so on
    # print("maskImgs", maskImgs)
    count = 0
    for photo in photoArray:
        # print("LOADING: ", maskImgs[count], "FOR ", photo.path)

        photo.testMask = cv2.imread(maskImgs[count], -1)
        m3Show.imshow(photo.testMask, "photo.testMask")
        count += 1
        for face in photo.faces:
            for eye in face.eyes:
                eye.testMask = m3F.typeSwap(
                    m3F.typeSwap(photo.testMask).crop(eye.cropRect))
            # typeswap, as we want
            # to use a pillow func on np-array.
    return photoArray
Example #13
0
def fullImgEyeOutline(photo, show):
    fullMask = photo.originalImage.copy()
    x, y, channels = fullMask.shape
    for face in photo.faces:
        for eye in face.eyes:
            if not isinstance(eye.mask, type(None)):
                coor = eye.cropRect
                # fullMask[coor[0]:x, coor[1]:y] = eye.mask

                for i in eye.circle[0, :]:
                    print("i", i)
                    cv2.circle(fullMask, (coor[0]+i[0], coor[1]+i[1]), i[2], (0, 255, 0), 2)

                    print("coor", coor)
                    #fullMask[coor[1]:coor[1]+eye.mask.shape[0], coor[0]:coor[0]+eye.mask.shape[1]] = eye.mask
    photo.mask = fullMask
    if (show):
        m3Show.imshow(photo.originalImage, "full original")
        m3Show.imshow(photo.mask, "full mask")
    return photo
Example #14
0
def mask(eye, img=None, mask=None, dest=None, show=True):
    # print(eye.__dict__.items())
    print("img", img)
    print("mask", mask)
    # m3Show.imshow(img, "mask masked")
    # m3Show.imshow(mask, "to compare")
    # print("eye in mask",eye)
    img = returnAttr(eye, img)
    mask = returnAttr(eye, mask)

    if (img is None or mask is None):
        setattr(eye, dest, np.zeros_like(img))
        m3F.printRed(" IMG OR MASK WAS NONE. NOT MASKING")
        eye.noCircles = True
        return eye
    # print("img.shape", img.shape)
    # print( "mask.shape", mask.shape)
    # destination = returnAttr(eye, dest)
    bah = cv2.bitwise_and(img, mask)
    setattr(eye, dest, bah)
    if show:
        m3Show.imshow(img, "image to be masked")
        m3Show.imshow(mask, "mask")
        m3Show.imshow(bah, "result")

    # print(eye.__dict__.items())

    return eye
Example #15
0
def findEyes2(photo, division, show=True):
    detector = dlib.get_frontal_face_detector()

    # downScaledDim = (round(w/100 *(scalePercent)),round(h/100 * (scalePercent)))
    h, w, c = photo.originalImage.shape
    downScaledDim = ((round(w / division)), round(h / division))
    copy = photo.originalImage.copy()
    pil_image = Image.fromarray(copy)
    inputImg = copy
    img = cv2.resize(inputImg, downScaledDim)
    dimToScaleUp = division
    predictor = dlib.shape_predictor
    ("MODELS/shape_predictor_194_face_landmarks.dat")
    dets = detector(img, 1)
    print("dets", dets)
    print("Number of faces detected: {}".format(len(dets)))
    for k, d in enumerate(dets):
        # print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
        #     k, d.left(), d.top(), d.right(), d.bottom()))
        # Get the landmarks/parts for the face in box d.
        shape = predictor(img, d)
        print("kkkk", k)
        # for part in shape.part
        # print("shape.parts():", shape.parts())
        # print("Part 0: {}, Part 1: {} ...".format(shape.part(0),
        # shape.part(1)))

        imgwithPoints = img.copy()
        for i in range(shape.num_parts):
            p = shape.part(i)
            print("p", p)
            # for x, y in set:
            imgwithPoints = cv2.rectangle(imgwithPoints, (p.x - 10, p.y - 10),
                                          (p.x + 10, p.y + 10), (0, 0, 255),
                                          -1)
        cv2.imwrite(
            "pointsss" + datetime.now().strftime("%d-%m-%Y--%H-%M-%S") +
            ".jpg", imgwithPoints)
        m3Show.imshow(imgwithPoints, "imgwithPoints")
Example #16
0
def makePolyMask(photoForDims, polys, show=True):
    # polyMask =  np.zeros_like(photo.originalImage)
    polyMask =  np.zeros_like(photoForDims)
    # print("polyMask.shape",polyMask.shape)
    # print("EYE COOR", eye.landmarkPoints)
    # print("np.int_(polys)", np.int_([polys]))
    polyMask = cv2.fillPoly(polyMask, np.int_([polys]), (255, 255, 255))
    # polymask = cv2.fillConvexPoly(polyMask, np.int_([eye.landmarkPoints]), (255, 255, 255),1000)
    # polymask = cv2.drawContours(polyMask, np.int_([eye.landmarkPoints]),-1, (255, 255, 255), 2)
    # polymask = cv2.approxPolyDP(np.int_([eye.landmarkPoints]),4, True)
    # xs, ys = [], []
    # for x, y in eye.eye.landmarkPoints:
    #     xs.append(x)
    #     ys.append(y)
    # polymask = cv2.poly
    # print(polymask)
    if show:
        m3Show.imshow(polyMask, "POLYMASK")
    # epm = m3Class.Eye(np.asarray(pil_image.crop(left)), left, lEyeCoor)
    # epm = polyMask.crop(eye.cropRect)
    # eye.polyMask = m3F.typeSwap(m3F.typeSwap(polyMask).crop(eye.cropRect))

    return polyMask
Example #17
0
def makeCircularMask(img, circles, show=True, onlyOne=True):
    # for photo in photoArray:
    # print("photo")
    # photo = inputImg
    # print("eye")
    maskImg = np.zeros_like(img)
    # if not isinstance(eye.circle, type(None)):
    # firstCircle = eye.circle[0]
    if circles is not None:
        if onlyOne:
            i = circles[0][0]
            # print("i", i)
            # print("onlyone", i[0], i[1], i[2])
            cv2.circle(maskImg, (i[0], i[1]), i[2], (255, 255, 255), -1)
        else:
            for i in circles[0, :]:
                # print("i", i)
                cv2.circle(maskImg, (i[0], i[1]), i[2], (255, 255, 255), -1)
    else:
        return maskImg
    if (show):
        m3Show.imshow(maskImg, "mask")
    return maskImg
Example #18
0
def canny(inputImg, show=True, thresh1=100, thresh2=100):
    out = cv2.Canny(inputImg, thresh1, thresh2)
    if show:
        m3Show.imshow(out, "canny")
    return out
Example #19
0
def pixelcomparison(inputImg, inputImg2, show):

    orgHandMask = inputImg
    orgAutoMask = inputImg2

    m3Show.imshow(orgHandMask,"handMask")

    height,width= orgHandMask.shape
    imgSize=height*width 
    TruePositive = 0.0 
    FalsePositive = 0.0
    FalseNegative = 0.0
    handMaskAccumLum = 0.0
    autoMaskAccumLum = 0.0

    autoMask = orgAutoMask.astype("float64")
    handMask = orgHandMask.astype("float64")

    for y in range(height):
        for x in range(width):

            autoMask[y,x]= (orgAutoMask[y,x]/255)
            handMask[y,x]= (orgHandMask[y,x]/255)

            if (autoMask[y,x] == handMask[y,x]):
                TruePositive += handMask[y,x]

            if (autoMask[y,x] < handMask[y,x]):
                FalseNegative += (handMask[y,x]-autoMask[y,x])
                TruePositive += autoMask[y,x]/handMask[y,x]

            if (autoMask[y,x] > handMask[y,x]):
                FalsePositive += autoMask[y,x] - handMask[y,x]
                TruePositive += handMask[y,x]/autoMask[y,x]

            handMaskAccumLum += handMask[y,x]
            autoMaskAccumLum += autoMask[y,x]        

    print(autoMask)          

    print ("handMaskAccumLum",handMaskAccumLum)

    print("TruePositive",TruePositive)
    print("FalseNegative",FalseNegative)
    print("FalsePositive",FalsePositive)

    TruePositive= (TruePositive/handMaskAccumLum)*100
    FalseNegative=(FalseNegative/handMaskAccumLum) *100
    FalsePositive1=(FalsePositive/autoMaskAccumLum)*100
    FalsePositive=(FalsePositive/handMaskAccumLum)*100

    print("TruePositive%",TruePositive,"% of accumulated pixel values of handmask")
    print("FalseNegative%",FalseNegative,"% of accumulated pixel values of handmask")
    print("FalsePositive%",FalsePositive,"% of accumulated pixel values of handmask")
    print("FalsePositive%",FalsePositive1,"% of accumulated pixel values of automask")


    autoMask = autoMask*255
    handMask = handMask*255

    autoMask = orgAutoMask.astype("uint8")
    handMask = orgHandMask.astype("uint8")

    M3Show.imshow(orgHandMask,"handMask")
    M3Show.imshow(orgAutoMask,"autoMask")
Example #20
0
def findEyes(photo, division, show=True):
    # print("photo type", type(photo))
    h, w, c = photo.originalImage.shape

    # downScaledDim = (round(w/100 *(scalePercent)),round(h/100 * (scalePercent)))
    downScaledDim = ((round(w / division)), round(h / division))
    copy = photo.originalImage.copy()
    pil_image = Image.fromarray(copy)
    inputImg = copy
    inputImg = cv2.resize(inputImg, downScaledDim)
    dimToScaleUp = division
    # if show:
    # print("originalShape", h, w, c)
    # print("downScaledDim", downScaledDim)
    # m3Show.imshow(m3F.typeSwap(pil_image), "image to find eyes on")
    # m3Show.imshow(photo.originalImage, " FINDEYS TEST")
    # print("proccesing", inputImgPath),

    # inputImg = cv2.imread(inputImgPath)

    face_landmarks_list = face_recognition.face_landmarks(inputImg)
    #print("I found {} face(s) in this photograph.".format(len(face_landmarks_list)))
    #print("face_landmarks_list type was", type(face_landmarks_list))
    # Create a PIL imagedraw object so we can draw on the picture
    # pil_image = Image.fromarray(inputImg)

    if (len(face_landmarks_list) == 0):

        m3F.printRed(" found no faces in this picture")
        plt.imshow(cv2.cvtColor(inputImg, cv2.COLOR_RGB2BGR))
        plt.show()
        return [m3Class.Face(noFaceImg=inputImg)]
    faces = []
    for face_landmarks in face_landmarks_list:

        lEyeCoor = face_landmarks['left_eye']
        rEyeCoor = face_landmarks['right_eye']
        print()
        mg = (lEyeCoor[3][0] - lEyeCoor[0][0]) * 0.2 * 10

        # **********************************************************************
        imgwithPoints = inputImg.copy()
        for eye in (lEyeCoor, rEyeCoor):
            for x, y in eye:
                imgwithPoints = cv2.rectangle(imgwithPoints, (x - 2, y - 20),
                                              (x + 20, y + 20), (0, 0, 255),
                                              -1)
        # **********************************************************************
        # if debug:
        #     m3Show.imshow(imgwithPoints, "with points")
        eyes = []
        for eyeLandmarkPoints in lEyeCoor, rEyeCoor:
            xs, ys = [], []
            for x, y in eyeLandmarkPoints:
                xs.append(x)
                ys.append(y)
                print("x y ", x, y)
            xs.sort(reverse=True)
            ys.sort(reverse=True)
            maxX = xs[0]
            maxY = ys[0]
            xs.sort(reverse=False)
            ys.sort(reverse=False)
            minX = xs[0]
            minY = ys[0]
            # print("maxX,maxY,minX,minY", maxX, maxY, minX, minY)
            margin = round((maxX - minX) * 0.2)
            cropRect = (minX - margin, minY - margin, maxX + margin,
                        maxY + margin)
            cropRect = [round(division * num) for num in cropRect]
            # for num in margin: num * division
            if show:
                m3Show.imshow(np.asarray(pil_image.crop(cropRect)), "cropRect")
            upscaledLandmarks = []
            for x, y in eyeLandmarkPoints:
                upscaledLandmarks.append((x * division, y * division))
                # y = y * division
            # print("upscaledLandmarks", upscaledLandmarks)
            eyes.append(
                m3Class.Eye(np.asarray(pil_image.crop(cropRect)), cropRect,
                            upscaledLandmarks))
        faces.append(m3Class.Face(eyes))
    return faces
Example #21
0
def pixelcomparison(photoArray, eyeAttr="", show=True):
    for photo in photoArray:
        for face in photo.faces:
            for eye in face.eyes:
                if eye.noCircles is True:
                    eye.TP = 0
                    eye.FN = 0
                    eye.FP = 0
                    eye.TPFN = 0
                    eye.TN = 0
                    eye.rTP = 0
                    eye.rFN = 0
                    eye.rFP = 0
                    eye.rTN = 0
                    eye.rHandAccum = 0
                    eye.rAutoAccum = 0
                else:

                    # ret, autoThresh = cv2.threshold(eye.iris,1,255,cv2.THRESH_BINARY)
                    orgAutoMask = getattr(eye, eyeAttr)
                    print("orgAutoMask", eyeAttr)
                    # cv2.imwrite(orgAutoMask, )
                    # m3Show.imshow("orgAutoMask", orgAutoMask)
                    # m3Show.imshow("orgHandMask",eye.testMask )
                    # file = open("EXPORTS/" + "blur4.txt","w+")
                    # file.write(np.array2string(orgAutoMask,max_line_width=None, precision=None, suppress_small=None, threshold=10000000))
                    # count += 1
                    # file.close()
                    # ret, orgAutoMask = cv2.threshold(orgAutoMask,1,255,cv2.THRESH_BINARY)
                    # ret, orgHandMask = cv2.threshold(eye.testMask,1,255,cv2.THRESH_BINARY)
                    orgHandMask = eye.testMask
                    m3Show.imshow(eye.testMask, "testmask")
                    # ret, orgHandMask = cv2.threshold(orgHandMask,1,255,cv2.THRESH_BINARY)
                    # print("orgHandMask", eye.testMask)
                    # print( orgAutoMask.shape, "orgAutoMask", orgHandMask.shape, "orgHandMask")
                    # print("orgHandMask", eye.testMask)
                    # orgAutoMask=cv2.cvtColor(orgAutoMask, cv2.COLOR_BGR2GRAY)
                    # orgHandMask=cv2.cvtColor(orgHandMask, cv2.COLOR_BGR2GRAY)
                    # orgHandMask = m3F.getRed(orgHandMask, False)
                    # orgAutoMask = m3F.getRed(orgAutoMask, False)
                    # m3Show.imshow(orgAutoMask, "orgAutoMask")
                    # m3Show.imshow(orgHandMask, "orgHandMask")

                    # m3Show.imshow(orgHandMask,"handMask")

                    height, width, c = orgHandMask.shape
                    imgSize = height * width
                    TruePositive = 0.0
                    TrueNegative = 0.0
                    FalsePositive = 0.0
                    FalseNegative = 0.0
                    handMaskAccumLum = 0.0
                    autoMaskAccumLum = 0.0

                    autoMask = orgAutoMask.astype("float64")
                    handMask = orgHandMask.astype("float64")
                    # print("autoMask", autoMask)
                    TPi, FNi, FPi, TNi = None, None, None, None
                    # TPi = np.ndarray(shape=(autoMask.shape), dtype=np.uint8)
                    # FNi = np.ndarray(shape=(autoMask.shape), dtype=np.uint8)
                    # FPi = np.ndarray(shape=(autoMask.shape), dtype=np.uint8)
                    orgAutoMask
                    TPi = np.zeros_like(orgAutoMask)
                    TNi = np.zeros_like(orgAutoMask)
                    FNi = np.zeros_like(orgAutoMask)
                    FPi = np.zeros_like(orgAutoMask)
                    print(autoMask.shape, "autoMask", handMask.shape,
                          "handMask")
                    for y in range(height):
                        for x in range(width):

                            autoMask[y, x, 0] = (orgAutoMask[y, x, 0] / 255)
                            handMask[y, x, 0] = (orgHandMask[y, x, 0] / 255)

                            # TPi = np.zeros_like(eye.image)
                            # print("autoMask[y,x,0]", autoMask[y,x,0],"handMask[y,x,0]",handMask[y,x,0])
                            # if (autoMask[y,x,0] == handMask[y,x,0]):
                            #     TruePositive += handMask[y,x,0]
                            #     TPi[y,x] = (0,255,0)
                            # if (autoMask[y,x,0] == 0 and handMask[y,x,0] == 0):
                            #     # TruePositive += handMask[y,x,0]
                            #     TNi[y,x] = (0,127,0)
                            #
                            # elif (autoMask[y,x,0] < handMask[y,x,0]):
                            #     FalseNegative += (handMask[y,x,0]-autoMask[y,x,0])
                            #     # TruePositive += autoMask[y,x,0]/handMask[y,x,0]
                            #     TruePositive += handMask[y,x,0]/autoMask[y,x,0]
                            #     FNi[y,x] = (255,0,0)
                            #
                            # elif (autoMask[y,x,0] > handMask[y,x,0]):
                            #     FalsePositive += autoMask[y,x,0] - handMask[y,x,0]
                            #     TruePositive += autoMask[y,x,0]/handMask[y,x,0]
                            #     FPi[y,x] = (0,0,255)

                            if (autoMask[y, x, 0] == handMask[y, x, 0]):
                                TruePositive += handMask[y, x, 0]
                                TPi[y, x] = (0, 255, 0)
                                TrueNegative += 1 - handMask[y, x, 0]

                            if (autoMask[y, x, 0] == 0
                                    and handMask[y, x, 0] == 0):
                                TNi[y, x] = (0, 127, 0)
                                # TN += 1

                            if (autoMask[y, x, 0] < handMask[y, x, 0]):
                                FalseNegative += (handMask[y, x, 0] -
                                                  autoMask[y, x, 0])
                                TruePositive += autoMask[y, x, 0]
                                FNi[y, x] = (255, 0, 0)
                                TrueNegative += 1 - handMask[y, x, 0]

                            if (autoMask[y, x, 0] > handMask[y, x, 0]):
                                FalsePositive += autoMask[y, x,
                                                          0] - handMask[y, x,
                                                                        0]
                                TruePositive += handMask[y, x, 0]
                                FPi[y, x] = (0, 0, 255)
                                TrueNegative += 1 - autoMask[y, x, 0]

                            handMaskAccumLum += handMask[y, x, 0]
                            autoMaskAccumLum += autoMask[y, x, 0]

                    # print(autoMask)
                    #
                    print("handMaskAccumLum", handMaskAccumLum)

                    print("TruePositive", TruePositive)
                    print("FalseNegative", FalseNegative)
                    print("FalsePositive", FalsePositive)
                    eye.rTP = TruePositive
                    eye.rFN = FalseNegative
                    eye.rFP = FalsePositive
                    eye.rTN = TrueNegative
                    eye.rHandAccum = handMaskAccumLum
                    eye.rAutoAccum = autoMaskAccumLum

                    TruePositive = (TruePositive / handMaskAccumLum) * 100
                    FalseNegative = (FalseNegative / handMaskAccumLum) * 100
                    FalsePositive1 = (FalsePositive / autoMaskAccumLum) * 100
                    FalsePositive = (FalsePositive / handMaskAccumLum) * 100
                    TrueNegative = (TrueNegative / handMaskAccumLum) * 100
                    eye.TP = TruePositive
                    eye.FN = FalseNegative
                    eye.FP = FalsePositive
                    eye.TN = TrueNegative
                    eye.TPFN = FalseNegative + TruePositive
                    eye.TPi = TPi
                    # eye.TPi = TPi
                    eye.FNi = FNi
                    eye.FPi = FPi
                    eye.TNi = TNi
                    # m3Show.imshow(eye.TPi, "TPI")
                    # m3Show.imshow(eye.FNi, "FNi")
                    # m3Show.imshow(eye.FPi, "FPi")
                    # m3Show.imshow(eye.TNi, "TNi")
                    # eye.TP = TruePositive
                    print("TruePositive%", TruePositive,
                          "% of accumulated pixel values of handmask")
                    print("FalseNegative%", FalseNegative,
                          "% of accumulated pixel values of handmask")
                    print("FalsePositive%", FalsePositive,
                          "% of accumulated pixel values of handmask")
                    print("FalsePositive%", FalsePositive1,
                          "% of accumulated pixel values of automask")
                    print("FalseNegative + TruePositive",
                          FalseNegative + TruePositive)

                    autoMask = autoMask * 255
                    handMask = handMask * 255

                    autoMask = orgAutoMask.astype("uint8")
                    handMask = orgHandMask.astype("uint8")

                # M3Show.imshow(orgHandMask,"handMask")
                # M3Show.imshow(orgAutoMask,"autoMask")
    return photoArray
Example #22
0
def batchProcess2(inputFolder, functionArray, export):
    # print("batchProcess ran with folder: " + inputFolder)
    inputImages = glob.glob(inputFolder + "*.*g")
    outputFolder = "../PICTURES"
    # if not (os.path.exists(outputFolder)):  # if outfolder does not exist, create it
    # print("inputImages", inputImages)
    if (export):
        now = datetime.now()
        now_string = now.strftime("-%d-%m-%Y---%H-%M-%S")
        outputFolder = os.getcwd() + "/EXPORTS/" + inputFolder.replace(
            "PICTURES/", "").replace("/", "") + now_string
        # print("path", outputFolder)
        os.mkdir(outputFolder)
        # print("../PICTURES/" + datetime.date())
    # print("output folder did not exist,", outputFolder, "created.")


# **********************************************************************
# IF FACE
    faceArray = []
    didEyes = False
    for function in functionArray:
        if (function.__name__ == "findEyes"):
            didEyes = True
            eyeFunc = function
            currentFunction = functionArray[function]

            for imagePath in inputImages:
                inputImage = cv2.imread(imagePath, -1)
                currentFunction["inputImg"] = inputImage
                face = m3Class.Immer(inputImage, imagePath)
                face.eyes = function(**currentFunction)
                faceArray.append(face)
    functionArray.pop(eyeFunc)
    # **********************************************************************
    if (didEyes):
        inputImages = faceArray
    for imagePath in inputImages:
        m3Show.imshow(imagePath.orginalImage, "original photo")
        print("************************************************************\
            ****************************************************")

        # -1 means "return the loaded image as is (with alpha channel)."
        if (didEyes):
            inputImage = imagePath
        else:
            print("processing: " + imagePath)
            inputImage = cv2.imread(imagePath, -1)
        for function in functionArray:
            currentFunction = functionArray[function]
            # print("current function: ", current)
            currentFunctionName = function.__name__
            print("function name ", function.__name__)
            # m3F.imshow(inputImage, "BATCH DEBUGGING")
            # https://realpython.com/python-kwargs-and-args/
            if (didEyes):
                if (imagePath.eyes is not None):
                    for eye in imagePath.eyes:
                        # print("eye coor: " + str(eye.coordinates))
                        if (currentFunctionName == "findCircleSimple"
                                or currentFunctionName == "makeCircularMask" or
                                currentFunctionName == "makeCircularOutline"):
                            currentFunction["inputImg"] = eye
                            eye = function(**currentFunction)
                        elif not (currentFunctionName == "makeFullMask"
                                  or currentFunctionName == "makePolyMask"
                                  or currentFunctionName == "applyPolyMask"
                                  or currentFunctionName == "makeComparison" or
                                  currentFunctionName == "fullImgEyeOutline"):
                            currentFunction["inputImg"] = eye.image
                            eye.image = function(**currentFunction)
                        # elif not (currentFunctionName == "makeFullMask"):
                        #     currentFunction["inputImg"] = eye.image
                        #     eye.image = function(**currentFunction)
                    if (currentFunctionName == "makeFullMask"
                            or currentFunctionName == "makePolyMask"
                            or currentFunctionName == "applyPolyMask"
                            or currentFunctionName == "fullImgEyeOutline"):
                        print("FULL OR POLY")
                        currentFunction["inputImg"] = imagePath
                        imagePath = function(**currentFunction)
                    if (currentFunctionName == "makeComparison"):
                        currentFunction["faceArray"] = faceArray
                        currentFunction["functionArray"] = functionArray
                        function(**currentFunction)
            else:
                currentFunction["inputImg"] = inputImage
                outputImage = function(**currentFunction)
                inputImage = outputImage
        if (export and not didEyes):
            imagePath2 = imagePath.path.replace(inputFolder, "")
            outPath = outputFolder + "/" + imagePath2
            # print("OUT PATH: ", outPath)
            cv2.imwrite(outPath, outputImage)
Example #23
0
def findEyes68(photo, division, show=True, debug=False):
    h, w, c = photo.originalImage.shape
    downScaledDim = ((round(w/division)), round(h/division))
    copy = photo.originalImage.copy()
    pil_image = Image.fromarray(copy)
    inputImg = copy
    inputImg = cv2.resize(inputImg, downScaledDim)
    photo.loResImage = inputImg

    if debug:
        print("originalShape", h, w, c)
        print("downScaledDim", downScaledDim)
        m3Show.imshow(m3F.typeSwap(pil_image), "image to find eyes on")

    face_landmarks_list = face_recognition.face_landmarks(inputImg)

    if debug:
        print("I found {} face(s) in this photograph.".format(len(face_landmarks_list)))
        print("face_landmarks_list type was", type(face_landmarks_list))

    if (len(face_landmarks_list) == 0):

        m3F.printRed(" found no faces in this picture")
        plt.imshow(cv2.cvtColor(inputImg, cv2.COLOR_RGB2BGR))
        plt.show()
        return [m3Class.Face(noFaceImg=inputImg)]

    faces = []
    for face_landmarks in face_landmarks_list:

        LeftEyeLandmarks = face_landmarks['left_eye']
        rightEyeLandmarks = face_landmarks['right_eye']

        # imgwithPoints = inputImg.copy()
        # for eye in (LeftEyeLandmarks, rightEyeLandmarks):
        #     for x, y in eye:
        #         imgwithPoints = cv2.rectangle(imgwithPoints, (x - 20, y - 20), (x + 20, y + 20), (0, 0, 255), -1)
        # **********************************************************************
        # if debug:
        #     m3Show.imshow(imgwithPoints, "with points")
        eyes = []
        for eyeLandmarkPoints in LeftEyeLandmarks, rightEyeLandmarks:
            xs, ys = [], []
            for x, y in eyeLandmarkPoints:
                xs.append(x)
                ys.append(y)
                # print("x y ", x, y)
            xs.sort(reverse=True)
            ys.sort(reverse=True)
            maxX = xs[0]
            maxY = ys[0]
            xs.sort(reverse=False)
            ys.sort(reverse=False)
            minX = xs[0]
            minY = ys[0]
            # print("maxX,maxY,minX,minY", maxX, maxY, minX, minY)
            # **********************************************************************
            margin = round((maxX - minX) * 0.2   )
            cropRect = (minX - margin, minY - margin,
                        maxX + margin, maxY + margin)
            cropRectNoMargin = (minX , minY ,
                        maxX, maxY )
            # **********************************************************************
            # margin = round((maxX - minX) * 0.2)
            # cropRect = (minX, minY,
            #             maxX, maxY)
            # **********************************************************************
            cropRect = [round(division*num) for num in cropRect]
            # for num in margin: num * division
            if show:
                m3Show.imshow(np.asarray(pil_image.crop(cropRect)), "cropRect")
            upscaledLandmarks = []
            # croprect: "The box is a 4-tuple defining the left, upper, right, and lower pixel coordinate"
            croppedEye = np.asarray(pil_image.crop(cropRect))
            # print("croppedEye.shape", croppedEye.shape)
            # print("cropRect", cropRect, [round(num/division) for num in cropRect])

            for x, y in eyeLandmarkPoints:
                upscaledLandmarks.append([((x - (minX-margin)) * division), ((y-(minY-margin)) * division)])


                # upscaledLandmarks.append([x * division, y *division])
                # print("minX, x, minY, y, division", (minX, x, minY, y, division, margin))
                #
                # x = x * division
                # y = y * division
                # left = cropRect[0] + margin
                # top = cropRect[1] + margin
                # upscaledLandmarks.append((x-left)-(y-top))
                # y = y * division
            # print("upscaledLandmarks", upscaledLandmarks)
            e = m3Class.Eye(croppedEye, cropRect, upscaledLandmarks)
            print(upscaledLandmarks)
            e.mask68 = makePolyMask(croppedEye, upscaledLandmarks)
            # cv2.imwrite("EXPORTS/COMPARISONS/mask68.jpg", e.mask68)

            e.margin = margin
            e.CRnoMargin = cropRectNoMargin
            e.minX = minX
            e.minY = minY
            eyes.append(e)
        faces.append(m3Class.Face(eyes))
    return faces
Example #24
0
def sobel(inputImg, show=True):
    out = cv2.Sobel(inputImg, inputImg.depth(), 0, 1, 3)
    if show:
        m3Show.imshow(out, "show")