import os
import urllib
import cv2
import numpy as np
from pixellib.semantic import semantic_segmentation
from matplotlib import pyplot as plt
print('์=3')
# ๋ณธ์ธ์ด ์ ํํ ์ด๋ฏธ์ง์ ๊ฒฝ๋ก์ ๋ง๊ฒ ๋ฐ๊ฟ ์ฃผ์ธ์.
img_path = '.human_segmentation/images/my_image.png'
img_orig = cv2.imread(img_path)
print(img_orig.shape)
plt.imshow(cv2.cvtColor(img_orig, cv2.COLOR_BGR2RGB))
plt.show()
๋ฅ๋ฌ๋์ด์ ์ ์ด๋ฏธ์ง ์ธ๊ทธ๋ฉํ ์ด์ ๋ฐฉ๋ฒ
DeepLab V3+: Encoder-Decoder with Atrous Separable Convolution for Semantic Image Segmentation
# ์ ์ฅํ ํ์ผ ์ด๋ฆ์ ๊ฒฐ์ ํฉ๋๋ค
model_dir = os.getenv('HOME')+'/aiffel/human_segmentation/models'
model_file = os.path.join(model_dir, 'deeplabv3_xception_tf_dim_ordering_tf_kernels.h5')
# PixelLib๊ฐ ์ ๊ณตํ๋ ๋ชจ๋ธ์ url์
๋๋ค
model_url = 'https://github.com/ayoolaolafenwa/PixelLib/releases/download/1.1/deeplabv3_xception_tf_dim_ordering_tf_kernels.h5'
# ๋ค์ด๋ก๋๋ฅผ ์์ํฉ๋๋ค
urllib.request.urlretrieve(model_url, model_file)
model = semantic_segmentation()
model.load_pascalvoc_model(model_file)
segvalues, output = model.segmentAsPascalvoc(img_path)
LABEL_NAMES = [
'background', 'aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus',
'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike',
'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tv'
]
len(LABEL_NAMES)
21
background๋ฅผ ์ ์ธํ๋ฉด 20๊ฐ์ ํด๋์ค
20 ์ ์๋ฏธ๋ tv
plt.imshow(output)
plt.show()
segvalues
{'class_ids': array([ 0, 9, 15]),
'masks': array([[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
...,
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False]])}
for class_id in segvalues['class_ids']:
print(LABEL_NAMES[class_id])
background
chair
person
# ์๋ ์ฝ๋๋ฅผ ์ดํดํ์ง ์์๋ ์ข์ต๋๋ค
# PixelLib์์ ๊ทธ๋๋ก ๊ฐ์ ธ์จ ์ฝ๋์
๋๋ค
# ์ฃผ๋ชฉํด์ผ ํ ๊ฒ์ ์์ ์ฝ๋ ๊ฒฐ๊ณผ๋ฌผ์ด์์!
colormap = np.zeros((256, 3), dtype = int)
ind = np.arange(256, dtype=int)
for shift in reversed(range(8)):
for channel in range(3):
colormap[:, channel] |= ((ind >> channel) & 1) << shift
ind >>= 3
colormap[:20]
array([[ 0, 0, 0],
[128, 0, 0],
[ 0, 128, 0],
[128, 128, 0],
[ 0, 0, 128],
[128, 0, 128],
[ 0, 128, 128],
[128, 128, 128],
[ 64, 0, 0],
[192, 0, 0],
[ 64, 128, 0],
[192, 128, 0],
[ 64, 0, 128],
[192, 0, 128],
[ 64, 128, 128],
[192, 128, 128],
[ 0, 64, 0],
[128, 64, 0],
[ 0, 192, 0],
[128, 192, 0]])
PixelLib์ ๋ฐ๋ฅด๋ฉด ์์ ๊ฐ์ ์์ ์ฌ์ฉ
colormap[15]
array([192, 128, 128])
seg_color = (128,128,192)
# output์ ํฝ์
๋ณ๋ก ์์์ด seg_color์ ๊ฐ๋ค๋ฉด 1(True), ๋ค๋ฅด๋ค๋ฉด 0(False)์ด ๋ฉ๋๋ค
seg_map = np.all(output==seg_color, axis=-1)
print(seg_map.shape)
plt.imshow(seg_map, cmap='gray')
plt.show()
(731, 579)
img_show = img_orig.copy()
# True๊ณผ False์ธ ๊ฐ์ ๊ฐ๊ฐ 255๊ณผ 0์ผ๋ก ๋ฐ๊ฟ์ค๋๋ค
img_mask = seg_map.astype(np.uint8) * 255
# 255์ 0์ ์ ๋นํ ์์์ผ๋ก ๋ฐ๊ฟ๋ด
๋๋ค
color_mask = cv2.applyColorMap(img_mask, cv2.COLORMAP_JET)
# ์๋ณธ ์ด๋ฏธ์ง์ ๋ง์คํธ๋ฅผ ์ ๋นํ ํฉ์ณ๋ด
๋๋ค
# 0.6๊ณผ 0.4๋ ๋ ์ด๋ฏธ์ง๋ฅผ ์๋ ๋น์จ์
๋๋ค.
img_show = cv2.addWeighted(img_show, 0.6, color_mask, 0.4, 0.0)
plt.imshow(cv2.cvtColor(img_show, cv2.COLOR_BGR2RGB))
plt.show()
์ ํ์ธ์ด ์๋์ OCEAN์ผ๋ก ๋ณ๊ฒฝ
# (13,13)์ blurring kernel size๋ฅผ ๋ปํฉ๋๋ค
# ๋ค์ํ๊ฒ ๋ฐ๊ฟ๋ณด์ธ์
img_orig_blur = cv2.blur(img_orig, (13,13))
plt.imshow(cv2.cvtColor(img_orig_blur, cv2.COLOR_BGR2RGB))
plt.show()
img_mask_color = cv2.cvtColor(img_mask, cv2.COLOR_GRAY2BGR)
img_bg_mask = cv2.bitwise_not(img_mask_color)
img_bg_blur = cv2.bitwise_and(img_orig_blur, img_bg_mask)
plt.imshow(cv2.cvtColor(img_bg_blur, cv2.COLOR_BGR2RGB))
plt.show()
๋ฐ์ ๋ ์ธ๊ทธ๋ฉํ ์ด์ ๊ฒฐ๊ณผ๋ฅผ ์ด์ฉํด์ ์ด๋ฏธ์ง์ bitwise_and ์ฐ์ฐ์ ์ํํ๋ฉด ๋ฐฐ๊ฒฝ๋ง ์๋ ์์์ ์ป์ ์ ์์ต๋๋ค. 0๊ณผ ์ด๋ค ์๋ฅผ bitwise_and ์ฐ์ฐ์ ํด๋ 0์ด ๋๊ธฐ ๋๋ฌธ์ ์ฌ๋์ด 0์ธ ๊ฒฝ์ฐ์๋ ์ฌ๋์ด ์๋ ๋ชจ๋ ํฝ์ ์ด 0์ด ๋ฉ๋๋ค. ๊ฒฐ๊ตญ ์ฌ๋์ด ์ฌ๋ผ์ง๊ฒ ๋๋ ๊ฑฐ์ฃ .
img_concat = np.where(img_mask_color==255, img_orig, img_bg_blur)
plt.imshow(cv2.cvtColor(img_concat, cv2.COLOR_BGR2RGB))
plt.show()
์ธ๊ทธ๋ฉํ ์ด์ ๋ง์คํฌ๊ฐ 255์ธ ๋ถ๋ถ๋ง ์๋ณธ ์ด๋ฏธ์ง ๊ฐ์ ๊ฐ์ง๊ณ ์ค๊ณ ์๋ ์์ญ์ ๋ธ๋ฌ๋ ์ด๋ฏธ์ง ๊ฐ์ ์ฌ์ฉ
np.where(์กฐ๊ฑด, ์ฐธ์ผ๋, ๊ฑฐ์ง์ผ๋)์ ํ์์ ์ฝ๋๋ฅผ ์ฌ์ฉ
์๋ณธ | ๋ณํ |
---|---|
![]() | ![]() |