-
Notifications
You must be signed in to change notification settings - Fork 0
/
facial_landmark.py
82 lines (66 loc) · 2.32 KB
/
facial_landmark.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# -*- coding: utf-8 -*-
"""Facial Landmark.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1tN-L-1swVETv4PBv-ZxH4iS-pKrtt6kU
"""
import cv2
import urllib.request as urlreq
import os
import matplotlib.pyplot as plt
from pylab import rcParams
#pics_url = "https://static.wikia.nocookie.net/e27ce036-1a4a-46c9-877e-e02392ee656c"
pics_url = "https://d2eohwa6gpdg50.cloudfront.net/wp-content/uploads/sites/6/2021/04/26213829/Friends-5.jpeg"
pic = "image.jpg"
if(pic in os.listdir(os.curdir)):
print("Pictures exists")
os.remove("image.jpg")
print("removed...")
urlreq.urlretrieve(pics_url, pic)
print("picture downloaded")
else:
urlreq.urlretrieve(pics_url, pic)
print("picture downloaded")
image = cv2.imread(pic)
plt.imshow(image)
print (image)
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
plt.imshow(image_rgb)
#x,y,width,depth = 50, 300, 800, 600
#image_cropped = image_rgb[y:(y+depth), x: (x+width)]
image_cropped = image_rgb
image_template = image_cropped.copy()
rcParams['figure.figsize'] = 10,15
plt.imshow(image_cropped)
gray = cv2.cvtColor(image_cropped, cv2.COLOR_BGR2GRAY)
plt.imshow(gray, cmap='gray')
haarcascade_url = "https://raw.githubusercontent.com/opencv/opencv/master/data/haarcascades/haarcascade_frontalface_alt2.xml"
haarcascade = "frontface.xml"
if(haarcascade in os.listdir(os.curdir)):
print("file exists")
else:
urlreq.urlretrieve(haarcascade_url, haarcascade)
print("file downloaded")
detector = cv2.CascadeClassifier(haarcascade)
faces = detector.detectMultiScale(gray)
print("faces:\n ", faces)
for face in faces:
(x,y,w,d) = face
cv2.rectangle(image_template,(x,y),(x+w, y+d), (255,255,255),2)
plt.imshow(image_template)
LBFmodel_url = "https://raw.githubusercontent.com/kurnianggoro/GSOC2017/master/data/lbfmodel.yaml"
LBFmodel = "LFBmodel.yaml"
if(LBFmodel in os.listdir(os.curdir)):
print("file exists")
else:
urlreq.urlretrieve(LBFmodel_url, LBFmodel)
print("file downloaded")
landmark_detector = cv2.face.createFacemarkLBF()
landmark_detector.loadModel(LBFmodel)
_,landmarks = landmark_detector.fit(gray, faces)
print("landmarks", landmarks)
for landmark in landmarks:
for x,y in landmark[0]:
cv2.circle(image_cropped,(x,y),1,(255,255,255),1)
plt.axis("off")
plt.imshow(image_cropped)