-
Notifications
You must be signed in to change notification settings - Fork 0
/
lipstickdigital.py
151 lines (109 loc) · 3.39 KB
/
lipstickdigital.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
'''
************************************************************
Projeto Lipstick Digital
Descrição: projeto final da disciplina Processamento Digital
de Imagens da Universidade Federal do Rio Grande do Norte
Autor: Kaíque Gomes Machado
15/09/2021
************************************************************
'''
import cv2
import dlib
import numpy as np
from tkinter import Tk
from tkinter.filedialog import askopenfilename
root = Tk()
root.withdraw()
webcam = False
setgray = False
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
path = askopenfilename()
color = 0,0,0
def aphrodite_color(*args):
global color
color =153,0,157
return color
def athena_color(*args):
global color
color = 255,20,80
return color
def hera_color(*args):
global color
color = 0,0,150
return color
def gray_color(*args):
global setgray
setgray = True
def colors(*args):
global setgray
setgray = False
def cam(*args):
global webcam
webcam = True
def save_color(*args):
cv2.imwrite('result.png',ImgColor)
return color
def cancel_color(*args):
global color
color = 0,0,0
return color
def button_exit(*args):
exit()
cv2.namedWindow("Lipstick Digital")
cv2.createButton("Aphrodite",aphrodite_color)
cv2.createButton("Athena",athena_color)
cv2.createButton("Hera",hera_color)
cv2.createButton("Gray Effect",gray_color)
cv2.createButton("Colors",colors)
cv2.createButton("Câmera",cam)
cv2.createButton("Save Image",save_color)
cv2.createButton("Cancel",cancel_color)
cv2.createButton("Exit",button_exit)
cap = cv2.VideoCapture(0)
def gray(img,setgray=False):
if setgray:
ImgOG = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ImgOG = cv2.cvtColor(ImgOG,cv2.COLOR_GRAY2BGR)
img = cv2.addWeighted(ImgOG,1,ImgColor,0.4,0)
return img
else:
img = cv2.addWeighted(ImgO,1,ImgColor,0.4,0)
return img
def createBox(img,points,scale=5,masked=False,cropped = True):
if masked:
mask = np.zeros_like(img)
mask = cv2.fillPoly(mask,[points],(255,255,255))
img = cv2.bitwise_and(img,mask)
if cropped:
bbox = cv2.boundingRect(points)
x,y,w,h = bbox
imgCrop = img[y:y+h,x:x+w]
imgCrop = cv2.resize(imgCrop,(0,0),None,scale,scale)
return imgCrop
else:
return mask
while True:
if webcam: success ,img = cap.read()
else: img = cv2.imread(path)
img = cv2.resize(img,(0,0),None,0.5,0.5)
ImgO = img.copy()
imgGray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces = detector(imgGray)
for face in faces:
landmarks = predictor (imgGray,face)
myPoints = []
for n in range(68):
x = landmarks.part(n).x
y = landmarks.part(n).y
myPoints.append([x,y])
#cv2.circle(ImgO,(x,y),2,(0,255,0),cv2.FILLED)
myPoints = np.array(myPoints)
imgLips = createBox(img,myPoints[48:61],3,True,False)
ImgColor = np.zeros_like(imgLips)
ImgColor [:] = color
ImgColor = cv2.bitwise_and(imgLips,ImgColor)
ImgColor = cv2.GaussianBlur(ImgColor,(7,7),10)
ImgColor = gray(ImgO,setgray)
cv2.imshow('Lipstick Digital',ImgColor)
cv2.waitKey(30)