Skip to content

Commit ee76fa6

Browse files
committed
optim visual effect
1 parent 1446c38 commit ee76fa6

File tree

2 files changed

+44
-21
lines changed

2 files changed

+44
-21
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
#### 可视化COCO格式标注格式
3737
```shell
38-
python coco_visual.py --json_path dataset/YOLOV5_COCO_format/annotations/instances_train2017.json \
38+
python coco_visual.py --vis_num 1 \
39+
--json_path dataset/YOLOV5_COCO_format/annotations/instances_train2017.json \
3940
--img_dir dataset/YOLOV5_COCO_format/train2017
4041
```

coco_visual.py

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,74 @@
22
# -*- encoding: utf-8 -*-
33
# @File: coco_visual.py
44
import json
5+
import random
56
import os
67
import argparse
78

89
import cv2
910

1011

1112
def visualization_bbox(num_image, json_path, img_path):
12-
# 需要画的第num副图片, 对应的json路径和图片路径
1313
with open(json_path) as annos:
1414
annotation_json = json.load(annos)
1515

1616
print('the annotation_json num_key is:',len(annotation_json)) # 统计json文件的关键字长度
1717
print('the annotation_json key is:', annotation_json.keys()) # 读出json文件的关键字
1818
print('the annotation_json num_images is:', len(annotation_json['images'])) # json文件中包含的图片数量
1919

20-
image_name = annotation_json['images'][num_image - 1]['file_name'] # 读取图片名
21-
id = annotation_json['images'][num_image - 1]['id'] # 读取图片id
20+
# 获取所有类别数
21+
categories = annotation_json['categories']
22+
categories_dict = {c['id']:c['name'] for c in categories}
23+
class_nums = len(categories_dict.keys())
24+
color = [(random.randint(0, 255), random.randint(0, 255),
25+
random.randint(0, 255)) for _ in range(class_nums)]
2226

23-
image_path = os.path.join(img_path, str(image_name).zfill(5)) # 拼接图像路径
24-
image = cv2.imread(image_path, 1) # 保持原始格式的方式读取图像
25-
num_bbox = 0 # 统计一幅图片中bbox的数量
26-
len_anno=len(annotation_json['annotations'][::])
27-
for i in range(len_anno):
28-
if annotation_json['annotations'][i]['image_id'] == id:
27+
# 读取图像
28+
image_name = annotation_json['images'][num_image - 1]['file_name']
29+
img_id = annotation_json['images'][num_image - 1]['id']
30+
image_path = os.path.join(img_path, str(image_name).zfill(5))
31+
image = cv2.imread(image_path, 1)
32+
33+
annotations = annotation_json['annotations']
34+
num_bbox = 0
35+
for anno in annotations:
36+
if anno['image_id'] == img_id:
2937
num_bbox = num_bbox + 1
30-
x, y, w, h = annotation_json['annotations'][i]['bbox'] # 读取边框
31-
image = cv2.rectangle(image, (int(x), int(y)),
32-
(int(x + w), int(y + h)), (0, 255, 255), 2)
3338

34-
print('The unm_bbox of the display image is:', num_bbox)
39+
class_id = anno['category_id']
40+
class_name = categories_dict[class_id]
41+
class_color = color[class_id-1]
3542

36-
# 显示方式1:用plt.imshow()显示
37-
# plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) #绘制图像,将CV的BGR换成RGB
38-
# plt.show() #显示图像
43+
# 绘制边框
44+
x, y, w, h = list(map(int, anno['bbox']))
45+
cv2.rectangle(image, (int(x), int(y)),
46+
(int(x + w), int(y + h)),
47+
class_color, 2)
48+
# 绘制文本
49+
font_size = 0.7
50+
txt_size = cv2.getTextSize(class_name, cv2.FONT_HERSHEY_SIMPLEX,
51+
font_size, 1)[0]
52+
cv2.rectangle(image, (x, y + 1),
53+
(x + txt_size[0] + 10, y - int(2 * txt_size[1])),
54+
class_color, -1)
55+
cv2.putText(image, class_name, (x + 5, y - 5),
56+
cv2.FONT_HERSHEY_SIMPLEX,
57+
font_size, (255, 255, 255), 1)
58+
59+
print('The unm_bbox of the display image is:', num_bbox)
3960

40-
# 显示方式2:用cv2.imshow()显示
41-
cv2.namedWindow(image_name, 0) # 创建窗口
42-
cv2.resizeWindow(image_name, 1000, 1000) # 创建500*500的窗口
61+
cv2.namedWindow(image_name, 0)
62+
cv2.resizeWindow(image_name, 1000, 1000)
4363
cv2.imshow(image_name, image)
4464
cv2.waitKey(0)
4565

4666

4767
if __name__ == "__main__":
4868
parser = argparse.ArgumentParser()
69+
parser.add_argument('--vis_num', type=int, default=1,
70+
help="可视化哪一张")
4971
parser.add_argument('--json_path', type=str, required=True)
5072
parser.add_argument('--img_dir', type=str, required=True)
5173
args = parser.parse_args()
5274

53-
visualization_bbox(1, args.json_path, args.img_dir)
75+
visualization_bbox(args.vis_num, args.json_path, args.img_dir)

0 commit comments

Comments
 (0)