3344import argparse
55import json
6- import platform
76import random
87from pathlib import Path
8+ from typing import List , Tuple
99
1010import cv2
1111import numpy as np
@@ -17,30 +17,25 @@ def __init__(
1717 ):
1818 self .font_size = 0.7
1919
20- def __call__ (self , img_id : int , json_path , img_path ):
21- with open (json_path , "r" , encoding = "utf-8" ) as annos :
22- anno_dict = json .load (annos )
23-
20+ def __call__ (self , img_id : int , json_path : str , img_path : str ):
21+ anno_dict = self .read_json (json_path )
2422 anno_imgs = anno_dict .get ("images" , None )
2523 if anno_imgs is None :
2624 raise ValueError (f"The images of { json_path } cannot be empty." )
2725
28- print ("The anno_dict num_key is:" , len (anno_dict ))
29- print ("The anno_dict key is:" , anno_dict .keys ())
30- print ("The anno_dict num_images is:" , len (anno_imgs ))
26+ print (f "The anno_dict num_key is: { len (anno_dict )} " )
27+ print (f "The anno_dict key is: { anno_dict .keys ()} " )
28+ print (f "The anno_dict num_images is: { len (anno_imgs )} " )
3129
3230 categories = anno_dict ["categories" ]
3331 categories_dict = {c ["id" ]: c ["name" ] for c in categories }
3432
3533 class_nums = len (categories_dict .keys ())
36- color = [
37- (random .randint (0 , 255 ), random .randint (0 , 255 ), random .randint (0 , 255 ))
38- for _ in range (class_nums )
39- ]
34+ color = self .get_class_color (class_nums )
4035
4136 img_info = anno_dict ["images" ][img_id - 1 ]
42- img_name = img_info .get ("file_name" )
4337
38+ img_name = img_info .get ("file_name" )
4439 img_full_path = Path (img_path ) / img_name
4540 image = cv2 .imread (str (img_full_path ))
4641
@@ -57,50 +52,83 @@ def __call__(self, img_id: int, json_path, img_path):
5752 class_name = categories_dict [class_id ]
5853 class_color = color [class_id - 1 ]
5954
60- # plot sgmentations
6155 segs = anno .get ("segmentation" , None )
6256 if segs is not None :
63- segs = np .array (segs ).reshape (- 1 , 2 )
64- cv2 .polylines (image , np .int32 ([segs ]), 2 , class_color )
65-
66- # plot rectangle
67- x , y , w , h = [round (v ) for v in anno ["bbox" ]]
68- cv2 .rectangle (
69- image , (int (x ), int (y )), (int (x + w ), int (y + h )), class_color , 2
70- )
71-
72- txt_size = cv2 .getTextSize (
73- class_name , cv2 .FONT_HERSHEY_SIMPLEX , self .font_size , 1
74- )[0 ]
75- cv2 .rectangle (
76- image ,
77- (x , y + 1 ),
78- (x + txt_size [0 ] + 5 , y - int (1.5 * txt_size [1 ])),
79- class_color ,
80- - 1 ,
81- )
82- cv2 .putText (
83- image ,
84- class_name ,
85- (x + 5 , y - 5 ),
86- cv2 .FONT_HERSHEY_SIMPLEX ,
87- self .font_size ,
88- (255 , 255 , 255 ),
89- 1 ,
90- )
91-
92- print ("The unm_bbox of the display image is:" , num_bbox )
93-
94- cur_os = platform .system ()
95- if cur_os == "Windows" :
96- cv2 .namedWindow (img_name , 0 )
97- cv2 .resizeWindow (img_name , 1000 , 1000 )
98- cv2 .imshow (img_name , image )
99- cv2 .waitKey (0 )
100- else :
101- save_path = f"vis_{ Path (img_name ).stem } .jpg"
102- cv2 .imwrite (save_path , image )
103- print (f"The { save_path } has been saved the current director." )
57+ self .plot_segmentations (image , segs , class_color )
58+ self .plot_text (image , segs [0 ][:2 ], class_color , class_name )
59+
60+ bbox = anno .get ("bbox" , None )
61+ if bbox is None :
62+ continue
63+
64+ self .plot_rectangle (image , bbox , class_color )
65+ self .plot_text (image , bbox , class_color , class_name )
66+
67+ print (f"The unm_bbox of the display image is: { num_bbox } " )
68+ save_path = f"vis_{ Path (img_name ).stem } .jpg"
69+ cv2 .imwrite (save_path , image )
70+ print (f"The { save_path } has been saved the current director." )
71+
72+ @staticmethod
73+ def read_json (json_path ):
74+ with open (json_path , "r" , encoding = "utf-8" ) as f :
75+ data = json .load (f )
76+ return data
77+
78+ @staticmethod
79+ def get_class_color (class_nums : int ) -> List [Tuple [int ]]:
80+ def random_color ():
81+ return random .randint (0 , 255 )
82+
83+ color = [
84+ (random_color (), random_color (), random_color ()) for _ in range (class_nums )
85+ ]
86+ return color
87+
88+ @staticmethod
89+ def plot_segmentations (
90+ image : np .ndarray , segs : List [List [float ]], class_color : Tuple [int ]
91+ ):
92+ segs = np .array (segs ).reshape (- 1 , 2 )
93+ cv2 .polylines (image , np .int32 ([segs ]), 2 , class_color )
94+
95+ @staticmethod
96+ def plot_rectangle (
97+ image : np .ndarray ,
98+ bbox : List [float ],
99+ class_color : Tuple [int ],
100+ thickness : int = 1 ,
101+ ):
102+ x , y , w , h = [round (v ) for v in bbox ]
103+ start_point = (int (x ), int (y ))
104+ end_point = (int (x + w ), int (y + h ))
105+ cv2 .rectangle (image , start_point , end_point , class_color , thickness )
106+
107+ def plot_text (
108+ self ,
109+ image : np .ndarray ,
110+ bbox : Tuple [float ],
111+ class_color : str ,
112+ class_name : str ,
113+ ):
114+ txt_size = cv2 .getTextSize (
115+ class_name , cv2 .FONT_HERSHEY_SIMPLEX , self .font_size , 1
116+ )[0 ]
117+
118+ x , y = [round (v ) for v in bbox [:2 ]]
119+ start_point = (x , y + 1 )
120+ end_point = (x + txt_size [0 ] + 5 , y - int (1.5 * txt_size [1 ]))
121+ cv2 .rectangle (image , start_point , end_point , class_color , - 1 )
122+
123+ cv2 .putText (
124+ image ,
125+ class_name ,
126+ (x + 5 , y - 5 ),
127+ cv2 .FONT_HERSHEY_SIMPLEX ,
128+ self .font_size ,
129+ (255 , 255 , 255 ),
130+ 1 ,
131+ )
104132
105133
106134def main ():
0 commit comments