แ„‚ ๐Ÿ˜„ [16 ์ผ์ฐจ] : Assignment - EX03

๋ฐฑ๊ฑดยท2022๋…„ 1์›” 21์ผ
0

๊ณ ์–‘์ด ์ˆ˜์—ผ ๋ถ™์ด๊ธฐ

๊ณ ์–‘์ด ์ˆ˜์—ผ ์Šคํ‹ฐ์ปค ๋งŒ๋“ค๊ธฐ

๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ถˆ๋Ÿฌ์˜ค๊ธฐ

import cv2
import matplotlib.pyplot as plt
import numpy as np
import dlib
my_image_path = './camera_sticker/images/image.png'
img_bgr = cv2.imread(my_image_path)  # OpenCV๋กœ ์ด๋ฏธ์ง€๋ฅผ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค
img_show = img_bgr.copy()            # ์ถœ๋ ฅ์šฉ ์ด๋ฏธ์ง€๋ฅผ ๋”ฐ๋กœ ๋ณด๊ด€ํ•ฉ๋‹ˆ๋‹ค
plt.imshow(img_bgr)                  # ์ด๋ฏธ์ง€ ์ถœ๋ ฅํ•˜๊ธฐ
plt.show()                           

์•„- ํŒŒ๋ž˜ํŒŒ๋ž˜

img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
plt.imshow(img_rgb)
plt.show()

# detector๋ฅผ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค
detector_hog = dlib.get_frontal_face_detector()
img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
dlib_rects = detector_hog(img_rgb, 1)   # (image, num of image pyramid)
print(dlib_rects)   

for dlib_rect in dlib_rects:
    l = dlib_rect.left()
    t = dlib_rect.top()
    r = dlib_rect.right()
    b = dlib_rect.bottom()

    cv2.rectangle(img_show, (l,t), (r,b), (0,255,0), 2, lineType=cv2.LINE_AA)

img_show_rgb =  cv2.cvtColor(img_show, cv2.COLOR_BGR2RGB)
plt.imshow(img_show_rgb)
plt.show()
rectangles[[(141, 201) (409, 468)]]

# ๋žœ๋“œ๋งˆํฌ ๋ชจ๋ธ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ

model_path = './camera_sticker/models/shape_predictor_68_face_landmarks.dat'
landmark_predictor = dlib.shape_predictor(model_path)

๋‚ด ์–ผ๊ตด์ด ์‚๋šค์–ด์กŒ๋‚˜..

list_landmarks = []
# ์–ผ๊ตด ์˜์—ญ ๋ฐ•์Šค ๋งˆ๋‹ค face landmark๋ฅผ ์ฐพ์•„๋ƒ…๋‹ˆ๋‹ค
for dlib_rect in dlib_rects:
    points = landmark_predictor(img_rgb, dlib_rect)
    # face landmark ์ขŒํ‘œ๋ฅผ ์ €์žฅํ•ด๋‘ก๋‹ˆ๋‹ค
    list_points = list(map(lambda p: (p.x, p.y), points.parts()))
    list_landmarks.append(list_points)

print(len(list_landmarks[0]))
68
# landmark ์  ์ฐ๊ธฐ
for landmark in list_landmarks:
    for point in landmark:
        cv2.circle(img_show, point, 2, (0, 255, 255), -1)

img_show_rgb = cv2.cvtColor(img_show, cv2.COLOR_BGR2RGB)
plt.imshow(img_show_rgb)
plt.show()

for dlib_rect, landmark in zip(dlib_rects, list_landmarks):
    print (landmark[30]) # ์ฝ”์˜ index๋Š” 30 ์ž…๋‹ˆ๋‹ค
    x = landmark[30][0]
    y = landmark[30][1]
    w = h = dlib_rect.width()
    
    print("(x, y) : ({}, {})".format(x, y))
    print("(w, h) : ({}, {})".format(w, h))
(295, 339)
(x, y) : (295, 339)
(w, h) : (269, 269)
# ์Šคํ‹ฐ์ปค ์ด๋ฏธ์ง€๋ฅผ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค
sticker_path = './camera_sticker/images/cat-whiskers.png'
img_sticker = cv2.imread(sticker_path) 
# ์ด๋ฏธ์ง€ Resize
img_sticker = cv2.resize(img_sticker, (w, h))
print (img_sticker.shape)
(269, 269, 3)
# RGB ์ด๋ฏธ์ง€๋กœ ๋ฐ”๊พธ๊ธฐ
img_sticker_rgb = cv2.cvtColor(img_sticker, cv2.COLOR_BGR2RGB)
plt.imshow(img_sticker_rgb)
print (img_sticker_rgb.shape)
(269, 269, 3)

# ์Šคํ‹ฐ์ปค ์ด๋ฏธ์ง€ ์‹œ์ž‘ ์ขŒํ‘œ (์œ„์ขŒ)
refined_x = x - w // 2  # left
refined_y = y - h // 2  # top
print("(x, y) : ({}, {})".format(refined_x, refined_y))
(x, y) : (161, 205)
# ์Šคํ‹ฐ์ปค๊ฐ€ ์›๋ณธ ์ด๋ฏธ์ง€์˜ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚ ๋•Œ
if refined_x < 0:
    img_sticker = img_sticker[:, -refined_x:]
    refined_x = 0
if refined_y < 0:
    img_sticker = img_sticker[-refined_y:, :]
    refined_y = 0

print('(x, y): (%d, %d)' %(refined_x, refined_y))
(x, y): (161, 205)
# img_show[from: to] ํ˜•์‹

# ์›๋ณธ ์ด๋ฏธ์ง€์—์„œ ์Šคํ‹ฐ์ปค ์ ์šฉํ•  ์œ„์น˜ cropํ•œ ์ด๋ฏธ์ง€
sticker_area = img_show[refined_y: refined_y+img_sticker.shape[0],
                       refined_x: refined_x+img_sticker.shape[1]]
img_show[refined_y: refined_y+img_sticker.shape[0],
        refined_x: refined_x+img_sticker.shape[1]] = \
    np.where(img_sticker==0, img_sticker, sticker_area).astype(np.uint8)

plt.imshow(cv2.cvtColor(img_show, cv2.COLOR_BGR2RGB))
plt.show()

์˜ค ์ž˜ ๋ถ™์—ˆ์–ด

# ๊น”๋”ํ•˜๊ฒŒ ์ •๋ฆฌ
# img_show ๋Œ€์‹  img_rbg ์ด์šฉํ•ด๋ณด๊ธฐ

sticker_area = img_bgr[refined_y: refined_y+img_sticker.shape[0],
                       refined_x: refined_x+img_sticker.shape[1]]
img_bgr[refined_y: refined_y+img_sticker.shape[0],
        refined_x: refined_x+img_sticker.shape[1]] = \
    np.where(img_sticker==0, img_sticker, sticker_area).astype(np.uint8)

plt.imshow(cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB))
plt.show()

์—ฌ๋Ÿฌ๊ฐ€์ง€ ํ…Œ์ŠคํŠธ

## ๊ฐ๋„ ๋ฐ”๊ฟ”๋ณด๊ธฐ
my_image_path = './camera_sticker/images/image2.png'
img_bgr = cv2.imread(my_image_path) # OpenCV๋กœ ์ด๋ฏธ์ง€ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
img_show = img_bgr.copy() # ์ถœ๋ ฅ ์ด๋ฏธ์ง€ ๋”ฐ๋กœ ๋ณด๊ด€

# RGB ๋ณ€ํ˜•
img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
plt.imshow(img_rgb)
plt.show()

์™œ ํ™”๋‚ฌ์ง€?

# detector ์„ ์–ธ
detector_hog = dlib.get_frontal_face_detector()

# detector_hog๋ฅผ ์ด์šฉํ•ด ์–ผ๊ตด์˜ bounding box ์ถ”์ถœ
img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
dlib_rects = detector_hog(img_rgb, 1) # (image, # of image pyramid)

# ์ฐพ์€ ์–ผ๊ตด ์˜์—ญ ๋ฐ•์Šค ๋ฆฌ์ŠคํŠธ
print(dlib_rects)

for dlib_rect in dlib_rects:
    l = dlib_rect.left() # ์™ผ์ชฝ ์•„๋ž˜
    t = dlib_rect.top() # ์™ผ์ชฝ ์œ„
    r = dlib_rect.right() # ์˜ค๋ฅธ์ชฝ ์œ„
    b = dlib_rect.bottom() # ์˜ค๋ฅธ์ชฝ ์•„๋ž˜
    
    cv2.rectangle(img_show, (l, t), (r, b), (0, 255, 0), 2, lineType=cv2.LINE_AA)

img_show_rgb = cv2.cvtColor(img_show, cv2.COLOR_BGR2RGB)
plt.imshow(img_show_rgb)
plt.show()
rectangles[[(134, 206) (455, 527)]]

์˜ค ๋˜๋„ค????

# ์ €์žฅํ•œ landmark ๋ชจ๋ธ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
landmark_predictor = dlib.shape_predictor(model_path)

list_landmarks = []

# ์–ผ๊ตด ์˜์—ญ ๋ฐ•์Šค๋งˆ๋‹ค face landmark๋ฅผ ์ฐพ์•„๋‚ด๊ธฐ
for dlib_rect in dlib_rects:
    points = landmark_predictor(img_rgb, dlib_rect) # (RGB, rectangle)
    # face landmark ์ขŒํ‘œ ์ €์žฅํ•˜๊ธฐ
    list_points = list(map(lambda p: (p.x, p.y), points.parts()))
    list_landmarks.append(list_points)

print(len(list_landmarks[0]))

# landmark ์  ์ฐ๊ธฐ
for landmark in list_landmarks:
    for point in landmark:
        cv2.circle(img_show, point, 2, (0, 255, 255), -1)

img_show_rgb = cv2.cvtColor(img_show, cv2.COLOR_BGR2RGB)
plt.imshow(img_show_rgb)
plt.show()
68

# ์ขŒํ‘œ ํ™•์ธํ•˜๊ธฐ
for dlib_rect, landmark in zip(dlib_rects, list_landmarks):
    print(landmark[33]) # ์ฝ” ๋์˜ index = 33
    x = landmark[33][0]
    y = landmark[33][1]
    w = h = dlib_rect.width()
    print('์ฝ”์˜ ์œ„์น˜ (x, y) : (%d, %d)' %(x, y))
    print('box ํฌ๊ธฐ (w, h) : (%d, %d)' %(w, h))

    
# ์Šคํ‹ฐ์ปค ์ด๋ฏธ์ง€ ์‹œ์ž‘ ์ขŒํ‘œ (top-left ์ขŒํ‘œ)
refined_x = x - w//2 # left
refined_y = y - h//2 # top

if refined_x < 0:
    img_sticker = img_sticker[:, -refined_x:]
    refined_x = 0
if refined_y < 0:
    img_sticker = img_sticker[-refined_y:, :]
    refined_y = 0

print('์Šคํ‹ฐ์ปค ์ขŒํ‘œ (x, y): (%d, %d)' %(refined_x, refined_y))
(301, 421)
์ฝ”์˜ ์œ„์น˜ (x, y) : (301, 421)
box ํฌ๊ธฐ (w, h) : (322, 322)
์Šคํ‹ฐ์ปค ์ขŒํ‘œ (x, y): (140, 260)
img_sticker = cv2.imread(sticker_path) # ์Šคํ‹ฐ์ปค ์ด๋ฏธ์ง€ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
img_sticker = cv2.resize(img_sticker, (w, h)) # ์Šคํ‹ฐ์ปค resize

print(img_sticker.shape)
(322, 322, 3)
# (์ฝ”์˜) x, y ์ขŒํ‘œ ์กฐ์ • (top-left ์ขŒํ‘œ)
# ์Šคํ‹ฐ์ปค ์ด๋ฏธ์ง€ ์‹œ์ž‘ ์ขŒํ‘œ

refined_x = x - w//2# left
refined_y = y - h//2 # top

if refined_x < 0:
    img_sticker = img_sticker[:, -refined_x:]
    refined_x = 0
if refined_y < 0:
    img_sticker = img_sticker[-refined_y:, :]
    refined_y = 0

print('(x, y): (%d, %d)' %(refined_x, refined_y))
(x, y): (140, 260)
# img_show ๋Œ€์‹  img_rbg ์ด์šฉํ•ด๋ณด๊ธฐ

sticker_area = img_bgr[refined_y: refined_y+img_sticker.shape[0],
                       refined_x: refined_x+img_sticker.shape[1]]
img_bgr[refined_y: refined_y+img_sticker.shape[0],
        refined_x: refined_x+img_sticker.shape[1]] = \
    np.where(img_sticker==0, img_sticker, sticker_area).astype(np.uint8)

plt.imshow(cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB))
plt.show()

๋ถ™๊ธฐ๋Š” ๋ถ™๋„ค...

๋งˆ์Šคํฌ ์“ฐ๊ธฐ

my_image_path = './camera_sticker/images/image1.png'
img_bgr = cv2.imread(my_image_path) # OpenCV๋กœ ์ด๋ฏธ์ง€ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
img_show = img_bgr.copy() # ์ถœ๋ ฅ ์ด๋ฏธ์ง€ ๋”ฐ๋กœ ๋ณด๊ด€

# RGB ์ด๋ฏธ์ง€๋กœ ๋ณ€ํ˜•
img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
plt.imshow(img_rgb)
plt.show()

# detector ์„ ์–ธ
detector_hog = dlib.get_frontal_face_detector()

# detector_hog๋ฅผ ์ด์šฉํ•ด ์–ผ๊ตด์˜ bounding box ์ถ”์ถœ
img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
dlib_rects = detector_hog(img_rgb, 1) # (image, # of image pyramid)

# ์ฐพ์€ ์–ผ๊ตด ์˜์—ญ ๋ฐ•์Šค ๋ฆฌ์ŠคํŠธ
print(dlib_rects)

for dlib_rect in dlib_rects:
    l = dlib_rect.left() # ์™ผ์ชฝ ์•„๋ž˜
    t = dlib_rect.top() # ์™ผ์ชฝ ์œ„
    r = dlib_rect.right() # ์˜ค๋ฅธ์ชฝ ์œ„
    b = dlib_rect.bottom() # ์˜ค๋ฅธ์ชฝ ์•„๋ž˜
    
    cv2.rectangle(img_show, (l, t), (r, b), (0, 255, 0), 2, lineType=cv2.LINE_AA)

img_show_rgb = cv2.cvtColor(img_show, cv2.COLOR_BGR2RGB)
plt.imshow(img_show_rgb)
plt.show()
rectangles[[(141, 201) (409, 468)]]

์˜ค...์ธ์‹ํ•˜๋„ค???

# ์ €์žฅํ•œ landmark ๋ชจ๋ธ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
landmark_predictor = dlib.shape_predictor(model_path)

list_landmarks = []

# ์–ผ๊ตด ์˜์—ญ ๋ฐ•์Šค๋งˆ๋‹ค face landmark๋ฅผ ์ฐพ์•„๋‚ด๊ธฐ
for dlib_rect in dlib_rects:
    points = landmark_predictor(img_rgb, dlib_rect) # (RGB, rectangle)
    # face landmark ์ขŒํ‘œ ์ €์žฅํ•˜๊ธฐ
    list_points = list(map(lambda p: (p.x, p.y), points.parts()))
    list_landmarks.append(list_points)

print(len(list_landmarks[0]))

# landmark ์  ์ฐ๊ธฐ
for landmark in list_landmarks:
    for point in landmark:
        cv2.circle(img_show, point, 2, (0, 255, 255), -1)

img_show_rgb = cv2.cvtColor(img_show, cv2.COLOR_BGR2RGB)
plt.imshow(img_show_rgb)
plt.show()
68

์˜ค~ ์ ๋„ ์ฐํ˜€.

# ์ขŒํ‘œ ํ™•์ธํ•˜๊ธฐ
for dlib_rect, landmark in zip(dlib_rects, list_landmarks):
    print(landmark[33]) # ์ฝ” ๋์˜ index = 33
    x = landmark[33][0]
    y = landmark[33][1]
    w = h = dlib_rect.width()
    print('์ฝ”์˜ ์œ„์น˜ (x, y) : (%d, %d)' %(x, y))
    print('box ํฌ๊ธฐ (w, h) : (%d, %d)' %(w, h))

    
# ์Šคํ‹ฐ์ปค ์ด๋ฏธ์ง€ ์‹œ์ž‘ ์ขŒํ‘œ (top-left ์ขŒํ‘œ)
refined_x = x - w//2 # left
refined_y = y - h//2 # top

if refined_x < 0:
    img_sticker = img_sticker[:, -refined_x:]
    refined_x = 0
if refined_y < 0:
    img_sticker = img_sticker[-refined_y:, :]
    refined_y = 0

print('์Šคํ‹ฐ์ปค ์ขŒํ‘œ (x, y): (%d, %d)' %(refined_x, refined_y))
(284, 337)
์ฝ”์˜ ์œ„์น˜ (x, y) : (284, 337)
box ํฌ๊ธฐ (w, h) : (269, 269)
์Šคํ‹ฐ์ปค ์ขŒํ‘œ (x, y): (150, 203)
img_sticker = cv2.imread(sticker_path) # ์Šคํ‹ฐ์ปค ์ด๋ฏธ์ง€ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
img_sticker = cv2.resize(img_sticker, (w, h)) # ์Šคํ‹ฐ์ปค resize

print(img_sticker.shape)
(269, 269, 3)
# (์ฝ”์˜) x, y ์ขŒํ‘œ ์กฐ์ • (top-left ์ขŒํ‘œ)
# ์Šคํ‹ฐ์ปค ์ด๋ฏธ์ง€ ์‹œ์ž‘ ์ขŒํ‘œ

refined_x = x - w//2# left
refined_y = y - h//2 # top

if refined_x < 0:
    img_sticker = img_sticker[:, -refined_x:]
    refined_x = 0
if refined_y < 0:
    img_sticker = img_sticker[-refined_y:, :]
    refined_y = 0

print('(x, y): (%d, %d)' %(refined_x, refined_y))
(x, y): (150, 203)
# img_show ๋Œ€์‹  img_rbg ์ด์šฉํ•ด๋ณด๊ธฐ

sticker_area = img_bgr[refined_y: refined_y+img_sticker.shape[0],
                       refined_x: refined_x+img_sticker.shape[1]]
img_bgr[refined_y: refined_y+img_sticker.shape[0],
        refined_x: refined_x+img_sticker.shape[1]] = \
    np.where(img_sticker==0, img_sticker, sticker_area).astype(np.uint8)

plt.imshow(cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB))
plt.show()

์˜ค~ ๋งˆ์Šคํฌ ์“ด ๊ณ ์–‘์ด.

์‚ฌ์ง„์„ ๋ฉ€๋ฆฌ

my_image_path = './camera_sticker/images/image3.png'
img_bgr = cv2.imread(my_image_path) # OpenCV๋กœ ์ด๋ฏธ์ง€ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
img_show = img_bgr.copy() # ์ถœ๋ ฅ ์ด๋ฏธ์ง€ ๋”ฐ๋กœ ๋ณด๊ด€

# RGB ์ด๋ฏธ์ง€๋กœ ๋ณ€ํ˜•
img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
plt.imshow(img_rgb)
plt.show()

# detector ์„ ์–ธ
detector_hog = dlib.get_frontal_face_detector()

# detector_hog๋ฅผ ์ด์šฉํ•ด ์–ผ๊ตด์˜ bounding box ์ถ”์ถœ
img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
dlib_rects = detector_hog(img_rgb, 1) # (image, # of image pyramid)

# ์ฐพ์€ ์–ผ๊ตด ์˜์—ญ ๋ฐ•์Šค ๋ฆฌ์ŠคํŠธ
print(dlib_rects)

for dlib_rect in dlib_rects:
    l = dlib_rect.left() # ์™ผ์ชฝ ์•„๋ž˜
    t = dlib_rect.top() # ์™ผ์ชฝ ์œ„
    r = dlib_rect.right() # ์˜ค๋ฅธ์ชฝ ์œ„
    b = dlib_rect.bottom() # ์˜ค๋ฅธ์ชฝ ์•„๋ž˜
    
    cv2.rectangle(img_show, (l, t), (r, b), (0, 255, 0), 2, lineType=cv2.LINE_AA)

img_show_rgb = cv2.cvtColor(img_show, cv2.COLOR_BGR2RGB)
plt.imshow(img_show_rgb)
plt.show()
rectangles[[(544, 296) (619, 370)]]

# ์ €์žฅํ•œ landmark ๋ชจ๋ธ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
landmark_predictor = dlib.shape_predictor(model_path)

list_landmarks = []

# ์–ผ๊ตด ์˜์—ญ ๋ฐ•์Šค๋งˆ๋‹ค face landmark๋ฅผ ์ฐพ์•„๋‚ด๊ธฐ
for dlib_rect in dlib_rects:
    points = landmark_predictor(img_rgb, dlib_rect) # (RGB, rectangle)
    # face landmark ์ขŒํ‘œ ์ €์žฅํ•˜๊ธฐ
    list_points = list(map(lambda p: (p.x, p.y), points.parts()))
    list_landmarks.append(list_points)

print(len(list_landmarks[0]))

# landmark ์  ์ฐ๊ธฐ
for landmark in list_landmarks:
    for point in landmark:
        cv2.circle(img_show, point, 1, (0, 255, 255), -1)

img_show_rgb = cv2.cvtColor(img_show, cv2.COLOR_BGR2RGB)
plt.imshow(img_show_rgb)
plt.show()
68

# ์ขŒํ‘œ ํ™•์ธํ•˜๊ธฐ
for dlib_rect, landmark in zip(dlib_rects, list_landmarks):
    print(landmark[33]) # ์ฝ” ๋์˜ index = 33
    x = landmark[33][0]
    y = landmark[33][1]
    w = h = dlib_rect.width()
    print('์ฝ”์˜ ์œ„์น˜ (x, y) : (%d, %d)' %(x, y))
    print('box ํฌ๊ธฐ (w, h) : (%d, %d)' %(w, h))

    
# ์Šคํ‹ฐ์ปค ์ด๋ฏธ์ง€ ์‹œ์ž‘ ์ขŒํ‘œ (top-left ์ขŒํ‘œ)
refined_x = x - w//2 # left
refined_y = y - h//2 # top

if refined_x < 0:
    img_sticker = img_sticker[:, -refined_x:]
    refined_x = 0
if refined_y < 0:
    img_sticker = img_sticker[-refined_y:, :]
    refined_y = 0

print('์Šคํ‹ฐ์ปค ์ขŒํ‘œ (x, y): (%d, %d)' %(refined_x, refined_y))
(579, 338)
์ฝ”์˜ ์œ„์น˜ (x, y) : (579, 338)
box ํฌ๊ธฐ (w, h) : (76, 76)
์Šคํ‹ฐ์ปค ์ขŒํ‘œ (x, y): (541, 300)
img_sticker = cv2.imread(sticker_path) # ์Šคํ‹ฐ์ปค ์ด๋ฏธ์ง€ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
img_sticker = cv2.resize(img_sticker, (w, h)) # ์Šคํ‹ฐ์ปค resize

print(img_sticker.shape)
(76, 76, 3)
# (์ฝ”์˜) x, y ์ขŒํ‘œ ์กฐ์ • (top-left ์ขŒํ‘œ)
# ์Šคํ‹ฐ์ปค ์ด๋ฏธ์ง€ ์‹œ์ž‘ ์ขŒํ‘œ

refined_x = x - w//2# left
refined_y = y - h//2 # top

if refined_x < 0:
    img_sticker = img_sticker[:, -refined_x:]
    refined_x = 0
if refined_y < 0:
    img_sticker = img_sticker[-refined_y:, :]
    refined_y = 0

print('(x, y): (%d, %d)' %(refined_x, refined_y))
(x, y): (541, 300)
# img_show ๋Œ€์‹  img_rbg ์ด์šฉํ•ด๋ณด๊ธฐ

sticker_area = img_bgr[refined_y: refined_y+img_sticker.shape[0],
                       refined_x: refined_x+img_sticker.shape[1]]
img_bgr[refined_y: refined_y+img_sticker.shape[0],
        refined_x: refined_x+img_sticker.shape[1]] = \
    np.where(img_sticker==0, img_sticker, sticker_area).astype(np.uint8)

plt.imshow(cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB))
plt.show()

์‚ฌ์ง„์„ ์–ด๋‘ก๊ฒŒ.

my_image_path = './camera_sticker/images/image4.png'
img_bgr = cv2.imread(my_image_path) # OpenCV๋กœ ์ด๋ฏธ์ง€ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
img_show = img_bgr.copy() # ์ถœ๋ ฅ ์ด๋ฏธ์ง€ ๋”ฐ๋กœ ๋ณด๊ด€

# RGB ์ด๋ฏธ์ง€๋กœ ๋ณ€ํ˜•
img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
plt.imshow(img_rgb)
plt.show()

# detector ์„ ์–ธ
detector_hog = dlib.get_frontal_face_detector()

# detector_hog๋ฅผ ์ด์šฉํ•ด ์–ผ๊ตด์˜ bounding box ์ถ”์ถœ
img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
dlib_rects = detector_hog(img_rgb, 1) # (image, # of image pyramid)

# ์ฐพ์€ ์–ผ๊ตด ์˜์—ญ ๋ฐ•์Šค ๋ฆฌ์ŠคํŠธ
print(dlib_rects)

for dlib_rect in dlib_rects:
    l = dlib_rect.left() # ์™ผ์ชฝ ์•„๋ž˜
    t = dlib_rect.top() # ์™ผ์ชฝ ์œ„
    r = dlib_rect.right() # ์˜ค๋ฅธ์ชฝ ์œ„
    b = dlib_rect.bottom() # ์˜ค๋ฅธ์ชฝ ์•„๋ž˜
    
    cv2.rectangle(img_show, (l, t), (r, b), (0, 255, 0), 2, lineType=cv2.LINE_AA)

img_show_rgb = cv2.cvtColor(img_show, cv2.COLOR_BGR2RGB)
plt.imshow(img_show_rgb)
plt.show()
rectangles[[(167, 242) (390, 464)]]

# ์ €์žฅํ•œ landmark ๋ชจ๋ธ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
landmark_predictor = dlib.shape_predictor(model_path)

list_landmarks = []

# ์–ผ๊ตด ์˜์—ญ ๋ฐ•์Šค๋งˆ๋‹ค face landmark๋ฅผ ์ฐพ์•„๋‚ด๊ธฐ
for dlib_rect in dlib_rects:
    points = landmark_predictor(img_rgb, dlib_rect) # (RGB, rectangle)
    # face landmark ์ขŒํ‘œ ์ €์žฅํ•˜๊ธฐ
    list_points = list(map(lambda p: (p.x, p.y), points.parts()))
    list_landmarks.append(list_points)

print(len(list_landmarks[0]))

# landmark ์  ์ฐ๊ธฐ
for landmark in list_landmarks:
    for point in landmark:
        cv2.circle(img_show, point, 2, (0, 255, 255), -1)

img_show_rgb = cv2.cvtColor(img_show, cv2.COLOR_BGR2RGB)
plt.imshow(img_show_rgb)
plt.show()
68

# ์ขŒํ‘œ ํ™•์ธํ•˜๊ธฐ
for dlib_rect, landmark in zip(dlib_rects, list_landmarks):
    print(landmark[33]) # ์ฝ” ๋์˜ index = 33
    x = landmark[33][0]
    y = landmark[33][1]
    w = h = dlib_rect.width()
    print('์ฝ”์˜ ์œ„์น˜ (x, y) : (%d, %d)' %(x, y))
    print('box ํฌ๊ธฐ (w, h) : (%d, %d)' %(w, h))

    
# ์Šคํ‹ฐ์ปค ์ด๋ฏธ์ง€ ์‹œ์ž‘ ์ขŒํ‘œ (top-left ์ขŒํ‘œ)
refined_x = x - w//2 # left
refined_y = y - h//2 # top

if refined_x < 0:
    img_sticker = img_sticker[:, -refined_x:]
    refined_x = 0
if refined_y < 0:
    img_sticker = img_sticker[-refined_y:, :]
    refined_y = 0

print('์Šคํ‹ฐ์ปค ์ขŒํ‘œ (x, y): (%d, %d)' %(refined_x, refined_y))
(293, 383)
์ฝ”์˜ ์œ„์น˜ (x, y) : (293, 383)
box ํฌ๊ธฐ (w, h) : (224, 224)
์Šคํ‹ฐ์ปค ์ขŒํ‘œ (x, y): (181, 271)
img_sticker = cv2.imread(sticker_path) # ์Šคํ‹ฐ์ปค ์ด๋ฏธ์ง€ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
img_sticker = cv2.resize(img_sticker, (w, h)) # ์Šคํ‹ฐ์ปค resize

print(img_sticker.shape)
(224, 224, 3)
# (์ฝ”์˜) x, y ์ขŒํ‘œ ์กฐ์ • (top-left ์ขŒํ‘œ)
# ์Šคํ‹ฐ์ปค ์ด๋ฏธ์ง€ ์‹œ์ž‘ ์ขŒํ‘œ

refined_x = x - w//2# left
refined_y = y - h//2 # top

if refined_x < 0:
    img_sticker = img_sticker[:, -refined_x:]
    refined_x = 0
if refined_y < 0:
    img_sticker = img_sticker[-refined_y:, :]
    refined_y = 0

print('(x, y): (%d, %d)' %(refined_x, refined_y))
(x, y): (181, 271)
# img_show ๋Œ€์‹  img_rbg ์ด์šฉํ•ด๋ณด๊ธฐ

sticker_area = img_bgr[refined_y: refined_y+img_sticker.shape[0],
                       refined_x: refined_x+img_sticker.shape[1]]
img_bgr[refined_y: refined_y+img_sticker.shape[0],
        refined_x: refined_x+img_sticker.shape[1]] = \
    np.where(img_sticker==0, img_sticker, sticker_area).astype(np.uint8)

plt.imshow(cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB))
plt.show()

ํšŒ๊ณ 

๊ธฐ๋ณธ

์ผ๋‹จ ์–ผ๊ตด์ด ์ธ์‹๋˜๋ฉด ์Šคํ‹ฐ์ปค๋ฅผ ๋ถ™์ด๋Š” ๊ฒƒ์€ ์–ด๋ ต์ง€ ์•Š์Œ.

  • ์‚ฌ์ง„ ํฌ๊ธฐ์— ๋”ฐ๋ผ์„œ ๋žœ๋“œ๋งˆํฌ ์ ์ด ์ž˜ ๋ณด์ด์ง€ ์•Š์•„ ์ฒ˜์Œ์—” ์—†๋Š” ์ค„ ์•Œ๊ณ  ๋งŽ์€ ์‹œ๊ฐ„์„ ํ—ค๋ฉค ใ…œใ…œ
  • ์ด ์  ํฌ๊ธฐ์— ๋Œ€ํ•œ ์„ค๋ช…์„ ๋‚ด๊ฐ€ ์ œ๋Œ€๋กœ ๋ชป๋ดค๋‚˜..ใ…กใ…œ
  • ์ƒ๊ฐ๋ณด๋‹ค ์–ผ๊ตด์ธ์‹์ด ์ž˜ ๋˜๋Š” ๊ฒƒ ๊ฐ™์•„์„œ ์‹ ๊ธฐํ–ˆ๋Š”๋ฐ
  • ๋‹ค๋ฅธ ๋ถ„๋“ค์˜ ์ด์•ผ๊ธฐ๋ฅผ ๋“ค์–ด๋ณด๋ฉด ์–ด๋‘์šด ๊ณณ์—์„œ๋Š” ์ธ์‹์ด ์•ˆ๋œ๋‹ค๊ณ  ํ–ˆ๋Š”๋ฐ ์ธ์‹์ด ์ž˜ ๋˜๋Š” ๋“ฏ.
  • ์š”์ฆ˜ ๋งˆ์Šคํฌ๋ฅผ ์“ฐ๋Š” ๊ฒƒ์ด ๋Œ€์„ธ๋ผ ๋งˆ์Šคํฌ๋ฅผ ์“ฐ๊ณ  ํ•ด๋ดค๋Š”๋ฐ ์ž˜๋˜๊ณ .
  • ์™•๊ด€ํ•  ๋•Œ๋Š” ์ด๋ฏธ์ง€ ์ž๋ฅด๋Š” ๋ถ€๋ถ„์ด ์กฐ๊ธˆ ํ—ท๊ฐˆ๋ ธ๋Š”๋ฐ ์ˆ˜์—ผ์€ ์™•๊ด€์ด ์•„๋‹ˆ๋ผ ์กฐ๊ธˆ ๋‹คํ–‰์ด๋ผ๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค.
  • ์–ผ๊ตด์˜ ๋ชจ์–‘์ด ์ด์ƒํ•œ ๊ฑด์ง€ ์–ผ๊ตด์ธ์‹์ด ์‚ฌ์ง„์— ๋”ฐ๋ผ ์กฐ๊ธˆ์”ฉ ์ขŒ์šฐ๋กœ ์ ๋ฆฌ๋Š”๋ฐ ์–ด๋–ค ๊ทœ์น™์„ฑ์ด ์žˆ๋Š”์ง€๋Š” ์•„์ง ํŒŒ์•…์ด ์•ˆ๋จ
  • ์ฒ˜์Œ์— ์ฝ”์˜ ์œ„์น˜๋ฅผ 30์œผ๋กœ ์žก์„์ง€ 33์œผ๋กœ ์žก์„ ์ง€ ๊ณ ๋ฏผํ–ˆ๋Š”๋ฐ 33์ด ๋” ์ž์—ฐ์Šค๋Ÿฌ์šด ๊ฒƒ ๊ฐ™์•„์„œ 33์œผ๋กœ ๊ฒฐ์ •.
  • ๊ฐœ๋ฐœ์ž๋“ค์ด ์ด ์ฝ”๋“œ๋ฅผ ๋‹ค ์™ธ์›Œ์„œ ์ฝ”๋”ฉํ•˜๋Š”์ง€ ๊ฐ‘์ž๊ธฐ ๊ถ๊ธˆํ•ด์ง.
  • ๋‹ค ํ•ด๋†“๊ณ  ๋ณด๋‹ˆ ์—ฌ๋Ÿฌ์‚ฌ๋žŒ์ด ์žˆ์„ ๋•Œ ์–ด๋–ป๊ฒŒ ๋‚˜์˜ฌ์ง€ ์กฐ๊ธˆ ๊ถ๊ธˆํ•ด์ง

๊ฐ๋„

  • ๊ฐ๋„์˜ ์˜ํ–ฅ์œผ๋กœ ์–ผ๊ตด์ธ์‹์ด ํฌ๊ฒŒ ๋‹ฌ๋ผ์ง€๋Š” ๊ฒƒ์€ ๋ชป ๋Š๊ผˆ์ง€๋งŒ
  • ๊ฐ๋„์—์„œ ์ด๋ชฉ๊ตฌ๋น„๊ฐ€ ์ •ํ™•ํžˆ ๋‚˜ํƒ€๋‚˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ์ธ์‹๋ฅ ์ด ๋งŽ์ด ๋–จ์–ด์งˆ ๊ฒƒ์ด๋ผ ์ƒ๊ฐ๋จ.
  • ์กฐ์›๋“ค ๋ณด๋‹ˆ ๊ฐ๋„์— ๋”ฐ๋ผ ์ธ์‹์ด ์•ˆ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•จ
  • ๊ฒฐ๋ก ์€ ์ ์ด ์ฐํžˆ๋Š” ํฌ์ธํŠธ๊ฐ€ ์ถฉ์กฑ ๋˜๋Š๋ƒ ์•„๋‹ˆ๋ƒ ํ•˜๋Š” ๋ถ€๋ถ„์ธ๋“ฏ.
  • ์ผ๋‹จ์€ ์ž˜ ๋‚˜์™€์„œ ์ข‹์€๋ฐ..
  • ์ด๊ฑธ ์•ฑ์œผ๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‚˜ ํ•˜๋Š” ๋ถ€๋ถ„์—์„œ ๋ง‰์—ฐํ•ด์ง.
  • ๊ทธ๋ž˜๋„ ์ผ๋‹จ ์ž˜ ๋ถ™์—ˆ์œผ๋‹ˆ ์ข‹๋‹ค.
  • ์‚ฌ์ง„์˜ ๋ฐฉํ–ฅ์„ ํ‹€์–ด์ฃผ๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์„๊ฑฐ๋ผ ์ƒ๊ฐํ•จ.

๋งˆ์Šคํฌ

  • ์•ž์„œ์˜ ๊ฒฐ๊ณผ์— ๋”ฐ๋ผ ๋งˆ์Šคํฌ๋ฅผ ์“ฐ๋ฉด ์ธ์‹์ด ์•ˆ๋  ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ–ˆ์Œ

  • ์™ ๊ฑธ...์™œ ์ธ์‹์ด ๋˜์ง€???

  • ๋งˆ์Šคํฌ๊ฐ€ ๋พฐ์กฑํ•ด์„œ ์ฝ”๋กœ ์ธ์‹ํ•˜๊ณ  ์ž… ๋ถ€๋ถ„ ์Œ์˜์ด ์ž…์œผ๋กœ ์ธ์‹์ด ๋˜์—ˆ๋‚˜ ํ•˜๋Š” ์ƒ๊ฐ์ด ๋“ฌ.

  • ๋งˆ์Šคํฌ ์ฝ”๋ถ€๋ถ„์˜ ๊ณ ์ •์‡  ๋ถ€๋ถ„์ด ํŠ€์–ด๋‚˜์™€์žˆ์–ด ์ฝ”๋กœ ์ธ์‹๋œ ๊ฒƒ ๊ฐ™์Œ.

  • ์ „์ฒด ์ ์˜ 70%์ •๋„๋งŒ ์ธ์‹์ด ๋˜๋ฉด ๋  ๊ฒƒ ๊ฐ™๋‹ค๋Š” ์ถ”์ธก.
    -> ์ด๊ฑธ ์–ด๋””์„œ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ์„๊ฒƒ ๊ฐ™์€๋ฐ..

  • ํ”„๋กœ์ ํŠธ๋Š” ํ”ผ๋“œ๋ฐฑ์ด ์—†์–ด์„œ ์•„์‰ฌ์›€

์ž‘์€ ์‚ฌ์ง„

  • ํ”ฝ์…€์ด ์‹ฌ๊ฐํ•˜๊ฒŒ ๋ญ‰๊ฒŒ์ง€์ง€ ์•Š์œผ๋ฉด ์ธ์‹ํ•  ๊ฒƒ์ด๋ผ ์ถ”์ธก
  • ์‚ฌ์ง„์—์„œ ๋ฉ€๋ฆฌ ์žˆ์–ด๋„ ์ด๋ชฉ๊ตฌ๋น„๊ฐ€ ๋šœ๋ ธํ•˜๊ฒŒ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋„๋ก ์‚ฌ์ง„์„ ์ฐ์Œ
  • ๊ฑฐ๋ฆฌ์— ์žˆ์–ด์„œ ์ด๋ชฉ๊ตฌ๋น„ ๊ตฌ๋ถ„๋งŒ ์ž˜๋˜๋ฉด ์ธ์‹๋˜๋Š” ๊ฒƒ ๊ฐ™์Œ.
  • ์ด๋ฏธ์ง€ ์ธ์‹์€ ์—ญ์‹œ ์ฝ˜ํŠธ๋ผ์ŠคํŠธ๊ฐ€ ์ค‘์š”ํ•˜๋‹ค๊ณ  ํ™•์ธํ•จ.

์–ด๋‘์šด ์‚ฌ์ง„

  • ํ•˜...์ธ์‹ ๋จ...
  • ์„ฑ๋Šฅ์ด ๊ต‰์žฅํžˆ ์ข‹์€ ๊ฒƒ ๊ฐ™์Œ.
  • ์˜ˆ์ธก์€ ๋น—๋‚˜๊ฐ.
  • ๋‚ด๊ฐ€ ๋Œ€์ถฉ ์• ๋งคํ•œ๋ฐ ์–˜๋Š” ์ด๊ฑธ ๋˜ ์ธ์‹ํ•จ..
  • ๋œ ์–ด๋‘์› ๋‚˜..
  • ๋ญ ์ธ์‹์ด ์•ˆ๋œ๊ฒŒ ์žˆ์–ด์•ผ ๋ถ„์„์„ ํ•˜๋Š”๋ฐ..

๋ฌธ์ œ์ ๊ฐ™์ง€ ์•Š์€ ๋ฌธ์ œ์ ..ใ…œใ…œ

  • ์–ผ๊ตด ๊ฐ๋„์— ๋”ฐ๋ผ ์Šคํ‹ฐ์ปค๋ฅผ ํšŒ์ „์‹œํ‚ค๋Š” ๊ฒƒ๋„ ์ ์šฉํ•ด์•ผ ํ• ๋“ฏ.

  • ๋ฉ€๋ฆฌ์„œ ์ดฌ์˜ํ•˜๋ฉด ์™œ ์•ˆ๋ ๊นŒ์š”..

->> ๋๋Š”๋ฐ์š” ใ…œใ…œ

  • ์˜†์œผ๋กœ ๋ˆ„์›Œ์„œ ์ดฌ์˜ํ•˜๋ฉด ์™œ ์•ˆ๋ ๊นŒ์š”..

->> ๋๋Š”๋ฐ์š”.. ใ…œใ…œ

  • ์‹คํ–‰์†๋„๊ฐ€ ์™œ ์ค‘์š”ํ• ๊นŒ์š”?

->> ์‚ฌ์ง„์„ ์ธ์‹ํ•  ๋–„ ์‹คํ–‰์†๋„๊ฐ€ ๋Š๋ฆฌ๋ฉด ์‚ฌ์ง„์ด ์ œ๋Œ€๋กœ ์ž‘๋™ํ•˜์ง€ ์•Š๋Š” ๊ฒƒ๊ณผ ๊ฐ™์ง€ ์•Š์„๊นŒ?

  • ์ •ํ™•๋„ ์—ญ์‹œ

->> ์ธ์‹๋ฅ ์ด ์ข‹์•„์•ผ ๊ทธ ํšจ์œจ ๊ฐ€์น˜๊ฐ€ ์žˆ๋Š” ๊ฒƒ์ด๋‹ˆ..

๊ถ๊ธˆํ•ด์ง„ ์‚ฌํ•ญ์€

  • ๋‹ค์–‘ํ•œ ์‚ฌ์ง„ ์–ดํ”Œ์˜ ๊ฒฝ์šฐ ์–ด๋Š ์ •๋„ ํ•™์Šต์„ ์‹œํ‚ค๊ณ  ์ถœ์‹œํ•œ ๊ฒƒ์ผ๊นŒ?
profile
๋งˆ์ผ€ํŒ…์„ ์œ„ํ•œ ์ธ๊ณต์ง€๋Šฅ ์„ค๊ณ„์™€ ์Šคํƒ€ํŠธ์—… Log

0๊ฐœ์˜ ๋Œ“๊ธ€