반응형
이 자료는 한국인공지능협회에서 진행한 2021년 사업주 직업능력 개발훈련으로 BASIC AI(32시간)-박성주 강사의 온라인 교육내용을 참고한 것입니다.
2021.12.16 - [그냥, AI] - GAN실습 - 환경구축 & BeautyGAN(1)
# tensorflow의 낮은 버전으로 아래와 같은 오류가 나올 수 있으나 그냥 무시하면 된다
In [1]:
import dlib
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import tensorflow as tf
import numpy as np
C:\ProgramData\Anaconda3\envs\p36t19_beauty\lib\site-packages\tensorflow\python\framework\dtypes.py:523: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_qint8 = np.dtype([("qint8", np.int8, 1)])
C:\ProgramData\Anaconda3\envs\p36t19_beauty\lib\site-packages\tensorflow\python\framework\dtypes.py:524: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_quint8 = np.dtype([("quint8", np.uint8, 1)])
C:\ProgramData\Anaconda3\envs\p36t19_beauty\lib\site-packages\tensorflow\python\framework\dtypes.py:525: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_qint16 = np.dtype([("qint16", np.int16, 1)])
C:\ProgramData\Anaconda3\envs\p36t19_beauty\lib\site-packages\tensorflow\python\framework\dtypes.py:526: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_quint16 = np.dtype([("quint16", np.uint16, 1)])
C:\ProgramData\Anaconda3\envs\p36t19_beauty\lib\site-packages\tensorflow\python\framework\dtypes.py:527: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_qint32 = np.dtype([("qint32", np.int32, 1)])
C:\ProgramData\Anaconda3\envs\p36t19_beauty\lib\site-packages\tensorflow\python\framework\dtypes.py:532: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
np_resource = np.dtype([("resource", np.ubyte, 1)])
Load Models¶
In [2]:
detector = dlib.get_frontal_face_detector()
sp = dlib.shape_predictor('models/shape_predictor_5_face_landmarks.dat')
Load Images¶
In [32]:
img = dlib.load_rgb_image('imgs/test4.jpg')
plt.figure(figsize=(16, 10))
plt.imshow(img)
Out[32]:
<matplotlib.image.AxesImage at 0x1f202564b38>
Find Faces¶
In [33]:
img_result = img.copy()
dets = detector(img, 1)
if len(dets) == 0:
print('cannot find faces!')
fig, ax = plt.subplots(1, figsize=(16, 10))
for det in dets:
x, y, w, h = det.left(), det.top(), det.width(), det.height()
rect = patches.Rectangle((x, y), w, h, linewidth=2, edgecolor='r', facecolor='none')
ax.add_patch(rect)
ax.imshow(img_result)
Out[33]:
<matplotlib.image.AxesImage at 0x1f201516780>
Find Landmarks 5points¶
In [34]:
fig, ax = plt.subplots(1, figsize=(16, 10))
objs = dlib.full_object_detections()
for detection in dets:
s = sp(img, detection)
objs.append(s)
for point in s.parts():
circle = patches.Circle((point.x, point.y), radius=3, edgecolor='r', facecolor='r')
ax.add_patch(circle)
ax.imshow(img_result)
Out[34]:
<matplotlib.image.AxesImage at 0x1f2019ab1d0>
Align Faces¶
In [35]:
faces = dlib.get_face_chips(img, objs, size=256, padding=0.3)
fig, axes = plt.subplots(1, len(faces)+1, figsize=(20, 16))
axes[0].imshow(img)
for i, face in enumerate(faces):
axes[i+1].imshow(face)
Functionalize¶
In [38]:
def align_faces(img):
dets = detector(img, 1)
objs = dlib.full_object_detections()
for detection in dets:
s = sp(img, detection)
objs.append(s)
faces = dlib.get_face_chips(img, objs, size=256, padding=0.35)
return faces
# test
test_img = dlib.load_rgb_image('imgs/test5.jpg')
test_faces = align_faces(test_img)
fig, axes = plt.subplots(1, len(test_faces)+1, figsize=(20, 16))
axes[0].imshow(test_img)
for i, face in enumerate(test_faces):
axes[i+1].imshow(face)
Load BeautyGAN Pretrained¶
In [8]:
sess = tf.Session()
sess.run(tf.global_variables_initializer())
saver = tf.train.import_meta_graph('models/model.meta')
saver.restore(sess, tf.train.latest_checkpoint('models'))
graph = tf.get_default_graph()
X = graph.get_tensor_by_name('X:0') # source
Y = graph.get_tensor_by_name('Y:0') # reference
Xs = graph.get_tensor_by_name('generator/xs:0') # output
INFO:tensorflow:Restoring parameters from models\model
Preprocess and Postprocess Functions¶
In [9]:
def preprocess(img):
return img.astype(np.float32) / 127.5 - 1.
def postprocess(img):
return ((img + 1.) * 127.5).astype(np.uint8)
Load Images¶
In [40]:
img1 = dlib.load_rgb_image('imgs/test2.jpg')
img1_faces = align_faces(img1)
img2 = dlib.load_rgb_image('imgs/makeup/jolly.png')
img2_faces = align_faces(img2)
fig, axes = plt.subplots(1, 2, figsize=(16, 10))
axes[0].imshow(img1_faces[0])
axes[1].imshow(img2_faces[0])
Out[40]:
<matplotlib.image.AxesImage at 0x1f2035e5ac8>
Run¶
In [41]:
src_img = img1_faces[0]
ref_img = img2_faces[0]
X_img = preprocess(src_img)
X_img = np.expand_dims(X_img, axis=0)
Y_img = preprocess(ref_img)
Y_img = np.expand_dims(Y_img, axis=0)
output = sess.run(Xs, feed_dict={
X: X_img,
Y: Y_img
})
output_img = postprocess(output[0])
fig, axes = plt.subplots(1, 3, figsize=(20, 10))
axes[0].set_title('Source')
axes[0].imshow(src_img)
axes[1].set_title('Reference')
axes[1].imshow(ref_img)
axes[2].set_title('Result')
axes[2].imshow(output_img)
Out[41]:
<matplotlib.image.AxesImage at 0x1f201e13710>
In [ ]:
반응형
'그냥, 코딩' 카테고리의 다른 글
[개발자를 위한 머신러닝&딥러닝] 텐서플로(Tensorflow) 소개 (0) | 2022.10.04 |
---|---|
OpenCV 라이브러리를 jupyter에 설치 및 실습 (0) | 2021.12.16 |
GAN실습 - 환경구축 & BeautyGAN(1) (0) | 2021.12.16 |
GAN 알고리즘 소개 및 활용범위 (0) | 2021.12.16 |
티처블머신(Teachable Machine) X 인공지능키트(AIIT,에이토) (0) | 2021.12.15 |