# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/BinayakDotel/opencv/blob/master/LandMarkDetection.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + id="NYvEVdxZjZpV"
# #!curl -o myPic.png https://d1qsx5nyffkra9.cloudfront.net/sites/default/files/article-image/eminence-organics-acne-face-mapping.jpg
import cv2 as cv
from matplotlib import pyplot as plt
import numpy as np
import dlib
from google.colab.patches import cv2_imshow


# + id="HUemp2wholSF" colab={"base_uri": "https://localhost:8080/", "height": 229} outputId="9c74b39b-7b67-401b-9e57-dd55be99b646"
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("drive/My Drive/Colab Notebooks/shape_predictor_68_face_landmarks.dat")

def createBox(img, points, scale = 5, masked= False, cropped = True):
  if masked:
    mask = np.zeros_like(img)
    mask = cv.fillPoly(mask, [points], (255,255,255))
    img = cv.bitwise_and(img, mask)
  
  if cropped:
    cropBox = cv.boundingRect(points)
    x,y,w,h = cropBox
    imgCrop = img[y:y+h,x:x+w] 
    imgCrop = cv.resize(imgCrop, (0,0), None, scale, scale)
    return imgCrop
  
  else:
    return mask

img = cv.imread("drive/My Drive/Memories/myPic.jpg", 1)
img = cv.resize(img, (0,0), None, 2, 2)
imgOriginal= img.copy()
imgGray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
faces = detector(imgGray)

for face in faces:
  x1,y1 = face.left(),face.top()
  x2,y2 = face.right(),face.bottom()
  cv.rectangle(imgOriginal, (x1,y1),(x2,y2), (0,255,0))
  landmarks = predictor(imgGray, face) 
  myPoints = []

  for n in range(68):
    x = landmarks.part(n).x
    y = landmarks.part(n).y
    myPoints.append([x,y])
    #cv.circle(imgOriginal, (x,y), 3, (255,255,255), cv.FILLED)
    cv.putText(imgOriginal, str(n), (x,y-4), cv.FONT_HERSHEY_COMPLEX_SMALL, 0.6, (0,0,0), 1)

  print(myPoints)
  myPoints= np.array(myPoints)

  lips = createBox(img, myPoints[42:48], masked = True, cropped = False)
  colorLips = np.zeros_like(lips)
  colorLips[:] = 0,0,255
  colorLips = cv.bitwise_and(lips, colorLips)
  colorLips = cv.GaussianBlur(colorLips, (7,7), 50)
  colorLips = cv.addWeighted(img, 1, colorLips, 0.4, 0)
  cv2_imshow(colorLips)

cv2_imshow(result)
cv2_imshow(img)
cv2_imshow(imgOriginal)


StarcoderdataJupyterScriptsDedupFiltered