7import smutils.utils_os
as utils
13 설정한 수량만큼 컬러 테이블을 생성하는 기능
16 return (list[tuple]): 컬러 테이블
18 state = np.random.get_state()
20 palette = np.random.randint(0, 256, size=(n, 3))
21 np.random.set_state(state)
22 colors = [tuple(c)
for c
in palette]
27 Instance Segmentation 결과 시각화를 위한 기능
28 박스 또는 마스크 둘중 하나 이상은 입력되어야함
31 masks (list[np.array]): Instance Mask, 각 마스크의 shape은 이미지의 크기와 동일함
32 bboxes (np.array): 검출 객체의 박스, 순서는 마스크와 매칭되어야함
33 colors (list[tuple]): 컬러 테이블
34 return (np.array): Instance Segmentation 결과가 시각화된 이미지
35 TODO: 이미지를 리턴하지 않아도 됨, 리턴 제거
48 mask_img = np.zeros_like(image)
51 for mask, color
in zip(reversed(masks), colors):
52 mask_img[mask] = color
56 image[mask] = image[mask]*0.3 + mask_img[mask]*0.7
59 if bboxes
is not None:
60 for bbox, color
in zip(reversed(bboxes), colors):
61 image = draw_single_bbox_and_label(image, bbox, f
'{bbox[-1]:.3f}', color, 2, color, 2, 0.7, box_type=
'xyxy')
70 pose_results (list[dict] or dict): 스켈레톤 정보를 담고 'keypoints'가 포함된 데이터 리스트
71 return (np.array): coco 스켈레톤이 시각화된 이미지
75 if not isinstance(pose_results, list):
76 pose_results = [pose_results]
79 for pose
in pose_results:
80 kps = pose[
'keypoints']
if pose[
'keypoints'].ndim == 2
else pose[
'keypoints'][0]
88 한명에 대한 coco 스켈레톤을 시각화 하는 기능
91 keypoints (np.array): 한명에 대한 스켈레톤 좌표, shape (17, 3)
92 return (np.array): 스켈레톤이 시각화된 이미지
101 (0, 1), (0, 2), (1, 3), (2, 4),
104 (5, 6), (5, 11), (6, 12), (11, 12),
111 'face': (255, 210, 127),
112 'left_body': (0, 165, 255),
113 'right_body': (0, 255, 0),
114 'body': (255, 210, 127)
118 keypoint_colors = [colors[
'face']] * 5 + [colors[
'left_body'], colors[
'right_body']] * 6
121 pair_colors = [colors[
'face']] * 4 + [colors[
'left_body']] * 2 + [colors[
'right_body']] * 2 + \
122 [colors[
'body']] * 4 + [colors[
'left_body']] * 2 + [colors[
'right_body']] * 2
126 for i, pair
in enumerate(coco_pairs):
130 if keypoints[kp1, 2] > 0.5
and keypoints[kp2, 2] > 0.5:
131 cv2.line(image, (int(keypoints[kp1, 0]), int(keypoints[kp1, 1])),
132 (int(keypoints[kp2, 0]), int(keypoints[kp2, 1])), pair_colors[i], 2)
135 for i, keypoint
in enumerate(keypoints):
136 x, y, score = keypoint
140 cv2.circle(image, (int(x), int(y)), 5, keypoint_colors[i], thickness=-1, lineType=cv2.FILLED)
147 이미지에 박스 한개와 텍스트를 출력하는 기능
148 텍스트의 위치는 박스의 상단, 상단에 출력하지 못할것 같다고 판단되면 박스 하단에 출력
151 bbox (np.array): 박스, shape (4 or 5)
153 box_color (tuple): 박스 컬러
154 box_thk (int): 박스를 그리는 선의 두께
155 txt_color (tuple): 텍스트의 색상
156 txt_thk (int): 텍스트 선의 두께
157 txt_scale (float): 텍스트의 크기
158 box_type (str): 박스 구조, 'xyxy', 'xywh' 지원
159 return (np.array): 박스 및 텍스트가 그려진 이미지
163 txt_xy = (int(bbox[0])-30, int(bbox[1]-20))
164 if int(bbox[1]-20) < 40:
165 txt_xy = (int(bbox[0]-30), int(bbox[1]+bbox[3]+25))
168 box_color = tuple(map(int, box_color))
169 txt_color = tuple(map(int, txt_color))
172 if box_type ==
'xywh':
173 img = cv2.rectangle(img, (int(bbox[0]), int(bbox[1])), (int(bbox[0]+bbox[2]), int(bbox[1]+bbox[3])), box_color, box_thk)
174 elif box_type ==
'xyxy':
175 img = cv2.rectangle(img, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])), box_color, box_thk)
178 cv2.putText(img, label, txt_xy, cv2.FONT_HERSHEY_DUPLEX, txt_scale, txt_color, txt_thk, 1)
184 리스트 또는 np.array 타입의 박스를 이미지에 그리는 기능
186 image (np.array): 이미지
187 bboxes (list[np.array], np.array): 박스
188 box_color (tuple): 박스 컬러
189 box_thk (int): 박스를 그리는 선의 두께
190 box_type (str): 박스 구조, 'xyxy', 'xywh' 지원
192 return (np.array): 박스 및 텍스트가 그려진 이미지
197 labels = [
'']*len(bboxes)
200 for bbox, label
in zip(bboxes, labels):
202 if label ==
'' and bbox.shape[0] == 6:
203 label = str(int(bbox[0]))
207 image = draw_single_bbox_and_label(image, bbox, label, box_color, box_thk, (255, 255, 255), 2, 1.5, box_type=box_type)
212 딕셔너리 형태로 입력된 박스를 그리는 기능
215 image (np.array): 이미지
216 bboxes (dict[np.array]): 박스
217 box_color (tuple): 박스 컬러
218 box_thk (int): 박스를 그리는 선의 두께
219 box_type (str): 박스 구조, 'xyxy', 'xywh' 지원
220 return (np.array): 박스가 그려진 이미지
222 for track_id, bbox
in bboxes.items():
223 label = f
'{track_id}'
224 image = draw_single_bbox_and_label(image, bbox, label, box_color, box_thk, (255, 255, 255), 2, 1.5, box_type=box_type)
229 박스를 그리고 박스가 그려진 이미지를 저장하는 기능
231 image (str or np.array): 이미지 경로 또는 이미지
232 save_path (str): 이미지 저장 경로
233 bboxes (np.array or list[np.array] or dict): 박스
234 box_color (tuple): 박스 컬러
235 box_thk (int): 박스를 그리는 선의 두께
236 box_type (str): 박스 구조, 'xyxy', 'xywh' 지원
240 if isinstance(image, str):
241 image = cv2.imread(image)
244 if isinstance(bboxes, dict):
247 image = draw_bboxes_list(image, bboxes, box_color, box_thk, box_type)
250 cv2.imwrite(save_path, image)
254 박스 한개를 그리고 박스가 그려진 이미지를 저장하는 기능
256 image (str or np.array): 이미지 경로 또는 이미지
257 save_path (str): 이미지 저장 경로
258 bbox (np.array): 박스, shape (4 or 5 or 6)
260 box_color (tuple): 박스 컬러
261 box_thk (int): 박스를 그리는 선의 두께
262 box_type (str): 박스 구조, 'xyxy', 'xywh' 지원
266 if isinstance(image, str):
267 image = cv2.imread(image)
270 if bbox.shape[0] == 6:
274 image = draw_single_bbox_and_label(image, bbox, label, box_color, box_thk, (255, 255, 255), 2, 1.5, box_type=box_type)
277 cv2.imwrite(save_path, image)
279def make_video(folder_or_paths, save_path, fps=30, i_size='half'):
281 이미지를 기반으로 동영상을 생성하는 기능
282 TODO: utils_video.py의 make_video를 사용하도록 함, 제거 필요
284 folder_or_paths (str or list[str]): 동영상을 만들 이미지가 저장된 폴더 또는 이미지 경로 리스트
285 save_path (str): 동영상을 저장할 경로
286 fps (int): 생성할 동영상의 프레임 속도
287 i_size (str): 이미지의 크기를 절반으로 줄여서 저장할지 여부, 'half'입력했을 경우 영상을 절반 크기로 생성함
290 print(
'Warning: Please use make_video in utils_video.py.')
293 if isinstance(folder_or_paths, str):
294 _, path_list = utils.search_file(folder_or_paths,
'.jpg')
296 path_list = folder_or_paths
299 img = cv2.imread(path_list[0])
302 fourcc = cv2.VideoWriter_fourcc(*
'mp4v')
310 video_writer = cv2.VideoWriter(save_path, fourcc, fps, (w, h))
314 for i
in tqdm(range(N)):
316 img = cv2.imread(path)
319 img = cv2.resize(img, dsize=(w, h), interpolation=cv2.INTER_AREA)
321 video_writer.write(img)
325 video_writer.release()
draw_and_save_bboxes(image, save_path, bboxes, box_color, box_thk, box_type='xywh')
draw_bboxes_dict(image, bboxes, box_color, box_thk, box_type)
vis_pose_coco_skeleton(image, pose_results)
make_video(folder_or_paths, save_path, fps=30, i_size='half')
vis_pose_coco_skeleton_one_person(image, keypoints)
draw_bboxes_list(image, bboxes, box_color, box_thk, box_type, labels=None)
vis_instance_segmentation(image, masks=None, bboxes=None, colors=None)
draw_single_bbox_and_label(img, bbox, label, box_color, box_thk, txt_color, txt_thk, txt_scale, box_type='xywh')
draw_and_save_single_bbox(image, save_path, bbox, label, box_color, box_thk, box_type='xywh')