# opencv의 rectangle()을 이용하여 시각화
# rectangle()은 이미지와 좌상단 좌표, 우하단 좌표, box컬러색, 두께등을 인자로 입력하면 원본 이미지에 box를 그려줌.
green_rgb = (125, 255, 51)
img_rgb_copy = img_rgb.copy()
for rect in cand_rects:
left = rect[0]
top = rect[1]
# rect[2], rect[3]은 너비와 높이이므로 우하단 좌표를 구하기 위해 좌상단 좌표에 각각을 더함.
right = left + rect[2]
bottom = top + rect[3]
img_rgb_copy = cv2.rectangle(img_rgb_copy, (left, top), (right, bottom), color=green_rgb, thickness=2)
plt.figure(figsize=(8, 8))
plt.imshow(img_rgb_copy)
plt.show()
selective search를 이용하여 Object Detection을 위한 Region Proposal 영역을 도출
selectivesearch를 설치하고 이미지를 로드
[1]
!pip install selectivesearch
Collecting selectivesearch
Downloading https://files.pythonhosted.org/packages/ad/f8/dac81bcfc2923144626e0361446caf92b9b7a79ff27ee7b01f762c364ba5/selectivesearch-0.4.tar.gz
Requirement already satisfied: numpy in /usr/local/lib/python3.7/dist-packages (from selectivesearch) (1.19.5)
Requirement already satisfied: scikit-image in /usr/local/lib/python3.7/dist-packages (from selectivesearch) (0.16.2)
Requirement already satisfied: matplotlib!=3.0.0,>=2.0.0 in /usr/local/lib/python3.7/dist-packages (from scikit-image->selectivesearch) (3.2.2)
Requirement already satisfied: imageio>=2.3.0 in /usr/local/lib/python3.7/dist-packages (from scikit-image->selectivesearch) (2.4.1)
Requirement already satisfied: networkx>=2.0 in /usr/local/lib/python3.7/dist-packages (from scikit-image->selectivesearch) (2.5.1)
Requirement already satisfied: pillow>=4.3.0 in /usr/local/lib/python3.7/dist-packages (from scikit-image->selectivesearch) (7.1.2)
Requirement already satisfied: scipy>=0.19.0 in /usr/local/lib/python3.7/dist-packages (from scikit-image->selectivesearch) (1.4.1)
Requirement already satisfied: PyWavelets>=0.4.0 in /usr/local/lib/python3.7/dist-packages (from scikit-image->selectivesearch) (1.1.1)
Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib!=3.0.0,>=2.0.0->scikit-image->selectivesearch) (1.3.1)
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib!=3.0.0,>=2.0.0->scikit-image->selectivesearch) (2.4.7)
Requirement already satisfied: python-dateutil>=2.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib!=3.0.0,>=2.0.0->scikit-image->selectivesearch) (2.8.1)
Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.7/dist-packages (from matplotlib!=3.0.0,>=2.0.0->scikit-image->selectivesearch) (0.10.0)
Requirement already satisfied: decorator<5,>=4.3 in /usr/local/lib/python3.7/dist-packages (from networkx>=2.0->scikit-image->selectivesearch) (4.4.2)
Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.7/dist-packages (from python-dateutil>=2.1->matplotlib!=3.0.0,>=2.0.0->scikit-image->selectivesearch) (1.15.0)
Building wheels for collected packages: selectivesearch
Building wheel for selectivesearch (setup.py) ... done
Created wheel for selectivesearch: filename=selectivesearch-0.4-cp37-none-any.whl size=4349 sha256=7812409756ac7b624ac4cc7f685f449e5eb6a2a11bfdf14f6954af1dac537963
Stored in directory: /root/.cache/pip/wheels/ab/a0/bd/a56e017d10730401fce75f62ff9364004368b0b96f12c026ba
Successfully built selectivesearch
Installing collected packages: selectivesearch
Successfully installed selectivesearch-0.4
!mkdir /content/data
!wget -o /content/data/audrey01.jpg https://raw.githubusercontent.com/chulminkw/DLCV/master/data/image/audrey01.jpg
import selectivesearch
import cv2
import matplotlib.pyplot as plt
import os
%matplotlib inline
### 오드리햅번 이미지를 cv2로 로드하고 matplotlib으로 시각화
img = cv2.imread('./data/audrey01.jpg')
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
print('img shape:',img.shape )
plt.figure(figsize=(8,8))
plt.imshow(img_rgb)
plt.show()
import selectivesearch
#selectivesearch.selective_search()는 이미지의 Region Proposal정보를 반환
_, regions = selectivesearch.selective_search(img_rgb, scale=100, min_size=2000)
print(type(regions), len(regions))
반환된 Region Proposal(후보 영역)에 대한 정보 보기. 반환된 regions 변수는 리스트 타입으로 세부 원소로 딕셔너리를 가지고 있음. 개별 딕셔너리내 KEY값별 의미
rect 키값은 x,y 시작 좌표와 너비, 높이 값을 가지며 이 값이 Detected Object 후보를 나타내는 Bounding box임. size는 Bounding box의 크기 labels는 해당 rect로 지정된 Bounding Box내에 있는 오브젝트들의 고유 ID 아래로 내려갈 수록 너비와 높이 값이 큰 Bounding box이며 하나의 Bounding box에 여러개의 오브젝트가 있을 확률이 커짐.
[11]
regions
[{'labels': [0.0], 'rect': (0, 0, 107, 167), 'size': 11166},
{'labels': [1.0], 'rect': (15, 0, 129, 110), 'size': 8771},
{'labels': [2.0], 'rect': (121, 0, 253, 133), 'size': 17442},
{'labels': [3.0], 'rect': (134, 17, 73, 62), 'size': 2713},
{'labels': [4.0], 'rect': (166, 23, 87, 176), 'size': 8639},
{'labels': [5.0], 'rect': (136, 53, 88, 121), 'size': 4617},
{'labels': [6.0], 'rect': (232, 79, 117, 147), 'size': 7701},
{'labels': [7.0], 'rect': (50, 91, 133, 123), 'size': 7042},
{'labels': [8.0], 'rect': (305, 97, 69, 283), 'size': 11373},
{'labels': [9.0], 'rect': (0, 161, 70, 46), 'size': 2363},
{'labels': [10.0], 'rect': (72, 171, 252, 222), 'size': 34467},
{'labels': [11.0], 'rect': (0, 181, 118, 85), 'size': 5270},
{'labels': [12.0], 'rect': (106, 210, 89, 101), 'size': 2868},
{'labels': [13.0], 'rect': (302, 228, 66, 96), 'size': 2531},
{'labels': [14.0], 'rect': (0, 253, 92, 134), 'size': 7207},
{'labels': [15.0], 'rect': (153, 270, 173, 179), 'size': 10360},
{'labels': [16.0], 'rect': (0, 305, 47, 139), 'size': 4994},
{'labels': [17.0], 'rect': (104, 312, 80, 71), 'size': 3595},
{'labels': [18.0], 'rect': (84, 360, 91, 67), 'size': 2762},
{'labels': [19.0], 'rect': (0, 362, 171, 87), 'size': 7705},
{'labels': [20.0], 'rect': (297, 364, 77, 85), 'size': 5164},
{'labels': [7.0, 11.0], 'rect': (0, 91, 183, 175), 'size': 12312},
{'labels': [4.0, 5.0], 'rect': (136, 23, 117, 176), 'size': 13256},
{'labels': [10.0, 15.0], 'rect': (72, 171, 254, 278), 'size': 44827},
{'labels': [4.0, 5.0, 3.0], 'rect': (134, 17, 119, 182), 'size': 15969},
{'labels': [8.0, 13.0], 'rect': (302, 97, 72, 283), 'size': 13904},
{'labels': [2.0, 6.0], 'rect': (121, 0, 253, 226), 'size': 25143},
{'labels': [7.0, 11.0, 9.0], 'rect': (0, 91, 183, 175), 'size': 14675},
{'labels': [0.0, 1.0], 'rect': (0, 0, 144, 167), 'size': 19937},
{'labels': [0.0, 1.0, 4.0, 5.0, 3.0],
'rect': (0, 0, 253, 199),
'size': 35906},
{'labels': [14.0, 16.0], 'rect': (0, 253, 92, 191), 'size': 12201},
{'labels': [14.0, 16.0, 7.0, 11.0, 9.0],
'rect': (0, 91, 183, 353),
'size': 26876},
{'labels': [10.0, 15.0, 19.0], 'rect': (0, 171, 326, 278), 'size': 52532},
{'labels': [10.0, 15.0, 19.0, 8.0, 13.0],
'rect': (0, 97, 374, 352),
'size': 66436},
{'labels': [17.0, 18.0], 'rect': (84, 312, 100, 115), 'size': 6357},
{'labels': [17.0, 18.0, 14.0, 16.0, 7.0, 11.0, 9.0],
'rect': (0, 91, 184, 353),
'size': 33233},
{'labels': [17.0, 18.0, 14.0, 16.0, 7.0, 11.0, 9.0, 12.0],
'rect': (0, 91, 195, 353),
'size': 36101},
{'labels': [17.0, 18.0, 14.0, 16.0, 7.0, 11.0, 9.0, 12.0, 2.0, 6.0],
'rect': (0, 0, 374, 444),
'size': 61244},
{'labels': [17.0,
18.0,
14.0,
16.0,
7.0,
11.0,
9.0,
12.0,
2.0,
6.0,
10.0,
15.0,
19.0,
8.0,
13.0],
'rect': (0, 0, 374, 449),
'size': 127680},
{'labels': [17.0,
18.0,
14.0,
16.0,
7.0,
11.0,
9.0,
12.0,
2.0,
6.0,
10.0,
15.0,
19.0,
8.0,
13.0,
20.0],
'rect': (0, 0, 374, 449),
'size': 132844},
{'labels': [17.0,
18.0,
14.0,
16.0,
7.0,
11.0,
9.0,
12.0,
2.0,
6.0,
10.0,
15.0,
19.0,
8.0,
13.0,
20.0,
0.0,
1.0,
4.0,
5.0,
3.0],
'rect': (0, 0, 374, 449),
'size': 168750}]
# rect정보만 출력해서 보기
cand_rects = [cand['rect'] for cand in regions]
print(cand_rects)
# opencv의 rectangle()을 이용하여 시각화
# rectangle()은 이미지와 좌상단 좌표, 우하단 좌표, box컬러색, 두께등을 인자로 입력하면 원본 이미지에 box를 그려줌.
green_rgb = (125, 255, 51)
img_rgb_copy = img_rgb.copy()
for rect in cand_rects:
left = rect[0]
top = rect[1]
# rect[2], rect[3]은 너비와 높이이므로 우하단 좌표를 구하기 위해 좌상단 좌표에 각각을 더함.
right = left + rect[2]
bottom = top + rect[3]
img_rgb_copy = cv2.rectangle(img_rgb_copy, (left, top), (right, bottom), color=green_rgb, thickness=2)
plt.figure(figsize=(8, 8))
plt.imshow(img_rgb_copy)
plt.show()
cand_rects = [cand['rect'] for cand in regions if cand['size'] > 10000]
green_rgb = (125, 255, 51)
img_rgb_copy = img_rgb.copy()
for rect in cand_rects:
left = rect[0]
top = rect[1]
# rect[2], rect[3]은 너비와 높이이므로 우하단 좌표를 구하기 위해 좌상단 좌표에 각각을 더함.
right = left + rect[2]
bottom = top + rect[3]
img_rgb_copy = cv2.rectangle(img_rgb_copy, (left, top), (right, bottom), color=green_rgb, thickness=2)
plt.figure(figsize=(8, 8))
plt.imshow(img_rgb_copy)
plt.show()