Beispiel #1
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
Beispiel #2
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
Beispiel #3
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
Beispiel #4
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
Beispiel #5
0
def run(tempeye, tempResolution, tempMin_dist, tempParam_1, tempParam_2,
        tempMinRadius, tempMaxRadius, tempShow):
    print(
        "***************************************************************************************"
    )

    img = tempeye
    print(tempMinRadius)
    isEyeClass = False
    eye = None
    if isinstance(img, type(m3Class.Eye())):
        isEyeClass = True
        eye = tempeye
        img = eye.wip
    if not isinstance(img, type(None)):
        cimg = img

        if len(img.shape) == 3:
            img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

        circles = cv2.HoughCircles(img,
                                   cv2.HOUGH_GRADIENT,
                                   tempResolution,
                                   tempMin_dist,
                                   param1=tempParam_1,
                                   param2=tempParam_2,
                                   minRadius=tempMinRadius,
                                   maxRadius=tempMaxRadius)

        #        circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,,120,param1=200,param2=10,
        #                                   minRadius=int(m3F.typeSwap(img).height/6),maxRadius=int(m3F.typeSwap(img).height/2.5))

        if not isinstance(circles, type(None)):

            if (circles.size != 0):
                circles = np.uint16(np.around(circles))
                # print(circles)
                index = 0
                for i in circles[0, :]:
                    #if img[i[1],i[0]] < 15:

                    # draw the outer circle
                    cv2.circle(cimg, (i[0], i[1]), i[2], (0, 255 - index, 0),
                               2)
                    # draw the center of the circle
                    cv2.circle(cimg, (i[0], i[1]), 2, (0, 0, 255 - index), 3)
                    index += 30

            if (tempShow):
                m3F.imshow(cimg, "Circle")
                m3F.printGreen("CIRCLES FOUND^^^")
                print("img out", img.shape)
            if isEyeClass:
                eye.circle = circles
                print("RETURNED AN EYE WITH CIRCLES")
                return eye
            else:
                return img
        else:
            if (tempShow):
                m3F.imshow(cimg, "no circles found")
                m3F.printRed("NO CIRCLES FOUND^^^")
    else:
        m3F.printRed("Image is NONE")
Beispiel #6
0
def runDouble(tempeye, tempResolution, tempMin_dist, tempParam_1, tempParam_2,
              tempMinRadius, tempMaxRadius, tempShow):
    print(
        "***************************************************************************************"
    )

    img = tempeye
    print(tempMinRadius)

    if not isinstance(img, type(None)):
        cimg = img
        if len(img.shape) == 3:
            img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

        bwimg = img.copy()

        circles = cv2.HoughCircles(img,
                                   cv2.HOUGH_GRADIENT,
                                   tempResolution,
                                   tempMin_dist,
                                   param1=tempParam_1,
                                   param2=tempParam_2,
                                   minRadius=tempMinRadius,
                                   maxRadius=tempMaxRadius)

        if not isinstance(circles, type(None)):

            if (circles.size != 0):
                circles = np.uint16(np.around(circles))
                # print(circles)

                for i in circles[0, :]:
                    bwimg.fill(0)
                    # draw the outer circle
                    cv2.circle(bwimg, (i[0], i[1]), i[2], 255, -1)
                    # draw the center of the circle
                    # cv2.circle(cimg, (i[0], i[1]), 2, (0, 0, 255), 3)
                    # m3F.imshow(bwimg,"circle")

                    mask = cv2.bitwise_and(img, bwimg)
                    # finalImg = cv2.cvtColor(mask, cv2.COLOR_BGR2RGB)

                    #m3F.imshow(mask, "final img")

                    pupilCircle = cv2.HoughCircles(mask,
                                                   cv2.HOUGH_GRADIENT,
                                                   1,
                                                   50,
                                                   param1=200,
                                                   param2=10,
                                                   minRadius=int(i[2] / 6),
                                                   maxRadius=int(i[2] / 4 * 3))
                    if not isinstance(pupilCircle, type(None)):

                        if (pupilCircle.size != 0):
                            pupilCircle = np.uint16(np.around(pupilCircle))
                            # print(circles)
                            for j in pupilCircle[0, :]:
                                wiggle = 5
                                if i[0] + wiggle > j[0] > i[0] - wiggle and i[
                                        1] + wiggle > j[1] > i[1] - wiggle:
                                    # draw the outer circle
                                    cv2.circle(mask, (j[0], j[1]), j[2],
                                               (255, 255, 0), 1)
                                    # draw the center of the circle
                                    cv2.circle(mask, (j[0], j[1]), 2,
                                               (0, 0, 255), 3)
                            m3F.imshow(mask, "final img")

            if (tempShow):
                m3F.imshow(cimg, "Circle")
                m3F.printGreen("CIRCLES FOUND^^^")
                print("img out", img.shape)
            return img
        else:
            if (tempShow):
                m3F.imshow(cimg, "no circles found")
                m3F.printRed("NO CIRCLES FOUND^^^")
    else:
        m3F.printRed("Image is NONE")
Beispiel #7
0
def run(tempeye, tempResolution, tempMin_dist, tempParam_1, tempParam_2,
        tempMinRadius, tempMaxRadius, tempShow):
    print(
        "***************************************************************************************"
    )

    img = tempeye.copy()
    print(tempMinRadius)
    # isEyeClass = False
    # eye = None
    # if isinstance(img, type(m3Class.Eye())):
    #     isEyeClass = True
    #     eye = tempeye
    #     img = eye.wip.copy()
    # print("DID EYE")
    if not isinstance(img, type(None)):
        cimg = img.copy()

        if len(img.shape) == 3:
            img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

        # print("Hough run : min:", tempMinRadius, " max: ", tempMaxRadius)
        circles = cv2.HoughCircles(img,
                                   cv2.HOUGH_GRADIENT,
                                   tempResolution,
                                   tempMin_dist,
                                   param1=tempParam_1,
                                   param2=tempParam_2,
                                   minRadius=tempMinRadius,
                                   maxRadius=tempMaxRadius)

        #        circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,,120,param1=200,param2=10,
        #                                   minRadius=int(m3F.typeSwap(img).height/6),maxRadius=int(m3F.typeSwap(img).height/2.5))
        # print("circles!!!!", circles)
        # return circles
        if not isinstance(circles, type(None)):

            if (circles.size != 0):
                circles = np.uint16(np.around(circles))
                # print(circles)
                index = 0
                if (tempShow):
                    for i in circles[0, :]:
                        # if img[i[1],i[0]] < 15:
                        # draw the outer circle
                        cv2.circle(cimg, (i[0], i[1]), i[2], (0, 255, 0), 2)
                        # draw the center of the circle
                        cv2.circle(cimg, (i[0], i[1]), 2, (0, 0, 255), 3)
                # eye.houghOutline = cimg

                    m3F.imshow(cimg, "Circle")
                    m3F.printGreen("CIRCLES FOUND^^^")
                    print("img out", img.shape)
            return circles
        else:
            # eye.noCircles = True
            if (tempShow):
                m3F.imshow(cimg, "no circles found")
                m3F.printRed("NO CIRCLES FOUND^^^")
            return None
    else:
        m3F.printRed("Image is NONE")