11# !/usr/bin/env python
22# -*- encoding: utf-8 -*-
33# @File: coco_visual.py
4+ import argparse
45import json
5- import random
66import os
7- import argparse
7+ import platform
8+ import random
89
910import cv2
1011
@@ -13,13 +14,18 @@ def visualization_bbox(num_image, json_path, img_path):
1314 with open (json_path ) as annos :
1415 annotation_json = json .load (annos )
1516
16- print ('the annotation_json num_key is:' ,len (annotation_json )) # 统计json文件的关键字长度
17- print ('the annotation_json key is:' , annotation_json .keys ()) # 读出json文件的关键字
18- print ('the annotation_json num_images is:' , len (annotation_json ['images' ])) # json文件中包含的图片数量
17+ # 统计json文件的关键字长度
18+ print ('the annotation_json num_key is:' , len (annotation_json ))
19+
20+ # 读出json文件的关键字
21+ print ('the annotation_json key is:' , annotation_json .keys ())
22+
23+ # json文件中包含的图片数量
24+ print ('the annotation_json num_images is:' , len (annotation_json ['images' ]))
1925
2026 # 获取所有类别数
2127 categories = annotation_json ['categories' ]
22- categories_dict = {c ['id' ]:c ['name' ] for c in categories }
28+ categories_dict = {c ['id' ]: c ['name' ] for c in categories }
2329 class_nums = len (categories_dict .keys ())
2430 color = [(random .randint (0 , 255 ), random .randint (0 , 255 ),
2531 random .randint (0 , 255 )) for _ in range (class_nums )]
@@ -33,7 +39,7 @@ def visualization_bbox(num_image, json_path, img_path):
3339 annotations = annotation_json ['annotations' ]
3440 num_bbox = 0
3541 for anno in annotations :
36- if anno ['image_id' ] == img_id :
42+ if anno ['image_id' ] == img_id :
3743 num_bbox = num_bbox + 1
3844
3945 class_id = anno ['category_id' ]
@@ -58,16 +64,24 @@ def visualization_bbox(num_image, json_path, img_path):
5864
5965 print ('The unm_bbox of the display image is:' , num_bbox )
6066
61- cv2 .namedWindow (image_name , 0 )
62- cv2 .resizeWindow (image_name , 1000 , 1000 )
63- cv2 .imshow (image_name , image )
64- cv2 .waitKey (0 )
67+ cur_os = platform .system ()
68+ if cur_os == 'Windows' :
69+ cv2 .namedWindow (image_name , 0 )
70+ cv2 .resizeWindow (image_name , 1000 , 1000 )
71+ cv2 .imshow (image_name , image )
72+ cv2 .waitKey (0 )
73+ else :
74+ # 保存可视化图即可
75+ save_path = f'visul_{ num_image } .jpg'
76+ cv2 .imwrite (save_path , image )
77+ print (f'The { save_path } has been saved the current director.' )
6578
6679
6780if __name__ == "__main__" :
6881 parser = argparse .ArgumentParser ()
6982 parser .add_argument ('--vis_num' , type = int , default = 1 ,
70- help = "可视化哪一张" )
83+ help = "visual which one" )
84+
7185 parser .add_argument ('--json_path' , type = str , required = True )
7286 parser .add_argument ('--img_dir' , type = str , required = True )
7387 args = parser .parse_args ()
0 commit comments