|
2 | 2 | # -*- encoding: utf-8 -*- |
3 | 3 | # @File: coco_visual.py |
4 | 4 | import json |
| 5 | +import random |
5 | 6 | import os |
6 | 7 | import argparse |
7 | 8 |
|
8 | 9 | import cv2 |
9 | 10 |
|
10 | 11 |
|
11 | 12 | def visualization_bbox(num_image, json_path, img_path): |
12 | | - # 需要画的第num副图片, 对应的json路径和图片路径 |
13 | 13 | with open(json_path) as annos: |
14 | 14 | annotation_json = json.load(annos) |
15 | 15 |
|
16 | 16 | print('the annotation_json num_key is:',len(annotation_json)) # 统计json文件的关键字长度 |
17 | 17 | print('the annotation_json key is:', annotation_json.keys()) # 读出json文件的关键字 |
18 | 18 | print('the annotation_json num_images is:', len(annotation_json['images'])) # json文件中包含的图片数量 |
19 | 19 |
|
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)] |
22 | 26 |
|
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: |
29 | 37 | 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) |
33 | 38 |
|
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] |
35 | 42 |
|
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) |
39 | 60 |
|
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) |
43 | 63 | cv2.imshow(image_name, image) |
44 | 64 | cv2.waitKey(0) |
45 | 65 |
|
46 | 66 |
|
47 | 67 | if __name__ == "__main__": |
48 | 68 | parser = argparse.ArgumentParser() |
| 69 | + parser.add_argument('--vis_num', type=int, default=1, |
| 70 | + help="可视化哪一张") |
49 | 71 | parser.add_argument('--json_path', type=str, required=True) |
50 | 72 | parser.add_argument('--img_dir', type=str, required=True) |
51 | 73 | args = parser.parse_args() |
52 | 74 |
|
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