@@ -70,23 +70,59 @@ def get_bytes_batch(base64_img):
7070 return bytes_batch , response .SUCCESS
7171
7272 @staticmethod
73- def get_image_batch (model : ModelConfig , bytes_batch ):
73+ def get_image_batch (model : ModelConfig , bytes_batch , color = None ):
7474 # Note that there are two return objects here.
7575 # 1.image_batch, 2.response
7676
7777 response = Response ()
7878
79- def load_image (image_bytes ):
80- data_stream = io .BytesIO (image_bytes )
81- pil_image = PIL_Image .open (data_stream ).convert ('RGB' )
82- image = cv2 .cvtColor (np .asarray (pil_image ), cv2 .COLOR_RGB2GRAY )
79+ hsv_map = {
80+ "blue" : {
81+ "lower_hsv" : np .array ([100 , 128 , 46 ]),
82+ "high_hsv" : np .array ([124 , 255 , 255 ])
83+ },
84+ "red" : {
85+ "lower_hsv" : np .array ([0 , 128 , 46 ]),
86+ "high_hsv" : np .array ([5 , 255 , 255 ])
87+ },
88+ "yellow" : {
89+ "lower_hsv" : np .array ([15 , 128 , 46 ]),
90+ "high_hsv" : np .array ([34 , 255 , 255 ])
91+ },
92+ "green" : {
93+ "lower_hsv" : np .array ([35 , 128 , 46 ]),
94+ "high_hsv" : np .array ([77 , 255 , 255 ])
95+ },
96+ "black" : {
97+ "lower_hsv" : np .array ([0 , 0 , 0 ]),
98+ "high_hsv" : np .array ([180 , 255 , 46 ])
99+ }
100+ }
101+
102+ def separate_color (pil_image , color ):
103+ hsv = cv2 .cvtColor (np .asarray (pil_image ), cv2 .COLOR_BGR2HSV )
104+ lower_hsv = hsv_map [color ]['lower_hsv' ]
105+ high_hsv = hsv_map [color ]['high_hsv' ]
106+ mask = cv2 .inRange (hsv , lowerb = lower_hsv , upperb = high_hsv )
107+ return mask
108+
109+ def load_image (image_bytes , color = None ):
110+
111+ if color and color in ['red' , 'blue' , 'black' , 'green' , 'yellow' ]:
112+ image = np .asarray (bytearray (image_bytes ), dtype = "uint8" )
113+ image = cv2 .imdecode (image , - 1 )
114+ image = separate_color (image , color )
115+ else :
116+ data_stream = io .BytesIO (image_bytes )
117+ pil_image = PIL_Image .open (data_stream ).convert ('RGB' )
118+ image = cv2 .cvtColor (np .asarray (pil_image ), cv2 .COLOR_RGB2GRAY )
83119 image = preprocessing (image , model .binaryzation , model .smooth , model .blur ).astype (np .float32 )
84120 image = cv2 .resize (image , (model .resize [0 ], model .resize [1 ]))
85121 image = image .swapaxes (0 , 1 )
86122 return image [:, :, np .newaxis ] / 255.
87123
88124 try :
89- image_batch = [load_image (i ) for i in bytes_batch ]
125+ image_batch = [load_image (i , color = color ) for i in bytes_batch ]
90126 return image_batch , response .SUCCESS
91127 except OSError :
92128 return None , response .IMAGE_DAMAGE
0 commit comments