11import cv2 as cv
22import numpy as np
33import json
4+ import os
5+ import argparse
6+
7+ parser = argparse .ArgumentParser ()
8+ parser .add_argument ('--epsilon' , type = float , default = 2.5 , help = 'epsilon pixel to approximate the polygons' )
9+ parser .add_argument ('--input' , type = str , default = "images_test/Abbey_Road_watershed_mask.png" , help = 'image mask input to compute all polygons' )
10+ parser .add_argument ('--config' , type = str , default = "config.json" , help = 'config file content labels informations' )
11+ opt = parser .parse_args ()
412
513def to_categorical (y , num_classes = None ):
614 y = np .array (y , dtype = 'int' ).ravel ()
@@ -11,9 +19,9 @@ def to_categorical(y, num_classes=None):
1119 categorical [np .arange (n ), y ] = 1
1220 return categorical
1321
14- img = cv .imread ("images_test/Abbey_Road_watershed_mask.png" )
22+ img = cv .imread (opt . input )
1523gray = cv .cvtColor (img , cv .COLOR_BGR2GRAY )
16- config = json .load (open ("config.json" ))['labels' ]
24+ config = json .load (open (opt . config ))['labels' ]
1725
1826shape = (gray .shape [0 ],gray .shape [1 ],1 )
1927gray = gray .reshape (shape )
@@ -26,7 +34,7 @@ def to_categorical(y, num_classes=None):
2634data ["shapes" ] = []
2735data ["lineColor" ]= [0 ,255 ,0 ,128 ]
2836data ["fillColor" ]= [255 ,0 ,0 ,128 ]
29- data ["imagePath" ]= "Abbey_Road. jpg"
37+ data ["imagePath" ]= os . path . basename ( opt . input ). replace ( "_watershed_mask.png" , ". jpg")
3038data ["imageData" ]= None
3139data ["imageHeight" ] = img .shape [0 ]
3240data ["imageWidth" ] = img .shape [1 ]
@@ -39,11 +47,11 @@ def to_categorical(y, num_classes=None):
3947 im , contour , hierarchy = cv .findContours (person , cv .RETR_EXTERNAL , cv .CHAIN_APPROX_SIMPLE )
4048
4149 for i ,c in enumerate (contour ) :
42- contour [i ] = cv .approxPolyDP (c ,1.5 ,True )
50+ contour [i ] = cv .approxPolyDP (c ,opt . epsilon ,True )
4351 d = {}
4452 d ['label' ] = label
4553 d ["line_color" ] = None
46- d ["fill_color" ] = None
54+ d ["fill_color" ] = config [ label ][ 'color' ]
4755 if contour [i ].shape [0 ] < 3 :
4856 continue
4957 d ["points" ] = contour [i ].reshape (contour [i ].shape [0 ],contour [i ].shape [2 ]).tolist ()
0 commit comments