1- # !/usr/bin/env python
21# -*- encoding: utf-8 -*-
32# @File: coco_visual.py
43import argparse
@@ -14,23 +13,18 @@ def visualization_bbox(num_image, json_path, img_path):
1413 with open (json_path ) as annos :
1514 annotation_json = json .load (annos )
1615
17- # 统计json文件的关键字长度
1816 print ('The annotation_json num_key is:' , len (annotation_json ))
1917
20- # 读出json文件的关键字
2118 print ('The annotation_json key is:' , annotation_json .keys ())
2219
23- # json文件中包含的图片数量
2420 print ('The annotation_json num_images is:' , len (annotation_json ['images' ]))
2521
26- # 获取所有类别数
2722 categories = annotation_json ['categories' ]
2823 categories_dict = {c ['id' ]: c ['name' ] for c in categories }
2924 class_nums = len (categories_dict .keys ())
3025 color = [(random .randint (0 , 255 ), random .randint (0 , 255 ),
3126 random .randint (0 , 255 )) for _ in range (class_nums )]
3227
33- # 读取图像
3428 image_name = annotation_json ['images' ][num_image - 1 ]['file_name' ]
3529 img_id = annotation_json ['images' ][num_image - 1 ]['id' ]
3630 image_path = os .path .join (img_path , str (image_name ).zfill (5 ))
@@ -46,12 +40,11 @@ def visualization_bbox(num_image, json_path, img_path):
4640 class_name = categories_dict [class_id ]
4741 class_color = color [class_id - 1 ]
4842
49- # 绘制边框
5043 x , y , w , h = list (map (int , anno ['bbox' ]))
5144 cv2 .rectangle (image , (int (x ), int (y )),
5245 (int (x + w ), int (y + h )),
5346 class_color , 2 )
54- # 绘制文本
47+
5548 font_size = 0.7
5649 txt_size = cv2 .getTextSize (class_name , cv2 .FONT_HERSHEY_SIMPLEX ,
5750 font_size , 1 )[0 ]
@@ -71,7 +64,6 @@ def visualization_bbox(num_image, json_path, img_path):
7164 cv2 .imshow (image_name , image )
7265 cv2 .waitKey (0 )
7366 else :
74- # 保存可视化图即可
7567 save_path = f'visul_{ num_image } .jpg'
7668 cv2 .imwrite (save_path , image )
7769 print (f'The { save_path } has been saved the current director.' )
@@ -81,7 +73,6 @@ def visualization_bbox(num_image, json_path, img_path):
8173 parser = argparse .ArgumentParser ()
8274 parser .add_argument ('--vis_num' , type = int , default = 1 ,
8375 help = "visual which one" )
84-
8576 parser .add_argument ('--json_path' , type = str , required = True )
8677 parser .add_argument ('--img_dir' , type = str , required = True )
8778 args = parser .parse_args ()
0 commit comments