@@ -39,7 +39,7 @@ def SendPieData(id, name, color='#3e95cd', flush=True):
3939 Log ("pie:" + str (id )+ ":" + str (name )+ ":" + str (color ), flush )
4040
4141#data contains an array or normalized inputs (from 0 to 1)
42- def SendImageData (id , data , width = 32 , height = 32 , name = "" , rgba = False , flush = True , invert = False , offset = 0 ):
42+ def SendImageData (id , data , width = 32 , height = 32 , name = "" , rgba = False , flush = True , invert = False , offset = 0 , resize = [] ):
4343 if EDITOR_MODE :
4444 img = Image .new ( 'RGBA' , (width ,height ), "white" )
4545 pixels = img .load ()
@@ -57,6 +57,8 @@ def SendImageData(id, data, width=32, height=32, name="", rgba=False, flush=True
5757 else :
5858 pixels [x ,y ] = (int (pixel [0 ]* 255 ), int (pixel [1 ]* 255 ), int (pixel [2 ]* 255 ), 255 ) # set the colour accordingly
5959
60+ if len (resize )> 0 :
61+ img = img .resize ((resize [0 ], resize [1 ]), Image .NEAREST )
6062
6163 tmpDir = tempfile .gettempdir ()
6264 imgPath = str (tmpDir )+ "/" + name + "_out_" + str (id )+ "_" + str (offset )+ ".png"
@@ -178,6 +180,27 @@ def CarveNumber(pixels, number, x, y, bg = (255, 255, 255, 255), color=(0, 0, 0,
178180 IOHelpers .CarveDigit (pixels , str_num [i ], x + loc_x , y , bg , color )
179181 loc_x += 4
180182
183+ def CreateImage (data , width = 32 , height = 32 , rgba = False , invert = False , resize = []):
184+ img = Image .new ( 'RGBA' , (width ,height ), "white" )
185+ pixels = img .load ()
186+
187+ for i in range (len (data )): # for every pixel:
188+ y = int (np .floor (i / width ))
189+ x = i - y * width
190+ #print("coord: "+str(x)+"_"+str(y)+":"+str(data[i]))
191+ if rgba :
192+ pixel = max (0 , data [i ])
193+ else :
194+ pixel = [max (0 , data [i ]), max (0 , data [i ]), max (0 , data [i ]), 1 ]
195+ if invert :
196+ pixels [x ,height - y - 1 ] = (int (pixel [0 ]* 255 ), int (pixel [1 ]* 255 ), int (pixel [2 ]* 255 ), 255 ) # set the colour accordingly
197+ else :
198+ pixels [x ,y ] = (int (pixel [0 ]* 255 ), int (pixel [1 ]* 255 ), int (pixel [2 ]* 255 ), 255 ) # set the colour accordingly
199+
200+ if len (resize )> 0 :
201+ img = img .resize ((resize [0 ], resize [1 ]), Image .NEAREST )
202+
203+ return img
181204
182205 def CarveDigit (pixels , digit , x , y , bg = (255 , 255 , 255 , 255 ), color = (0 , 0 , 0 , 255 )):
183206 #3x5 = 15pixels
@@ -732,7 +755,7 @@ def avg(data=[], steps=-1):
732755
733756 return _sum / divider
734757
735- def entropy (data = [], ground = 0.5 , multiplier = 3 ):
758+ def entropy (data = [], ground = 0 , multiplier = 3 ):
736759 _sum = 0
737760 _len = max (1 , len (data ))
738761 for v in data :
@@ -775,7 +798,22 @@ def normalize(sample):
775798 sample [i ] = sample [i ]/ _max
776799 return sample
777800
801+ def normalize2D (sample ):
802+ _max = 0
803+ for m in sample :
804+ for n in m :
805+ if abs (n )> _max :
806+ _max = abs (n )
807+
808+ for i in range (len (sample )):
809+ for j in range (len (sample [i ])):
810+ sample [i ][j ] = sample [i ][j ]/ _max
811+ return sample
812+
778813 def Spectrogram (samples , samplerate ):
779814 from scipy import signal
780815 frequencies , times , spectogram = signal .spectrogram (samples , len (samples ))
781- return spectogram
816+ return spectogram
817+
818+ def Sigmoid (data ):
819+ pass
0 commit comments