JapanKanji

Форк
0
/
formSubs.py 
384 строки · 13.1 Кб
1
import datetime
2
import sys, os
3

4
import cv2 as cv2
5
import numpy as np
6
import pandas as pd
7
import matplotlib.pyplot as plt
8

9
##########################################################################
10

11
def s(x) : return(1.0/(1.0+np.exp(np.clip(-x,-10000,50))))
12
def softmax(x, axis=-1):
13
    y = np.exp(x - np.max(x, axis, keepdims=True))
14
    return y / np.sum(y, axis, keepdims=True)
15

16
##########################################################################
17

18
def BBxyTLDR (lx4_, rr,cc, anchor_net, scale, grid_net, shape_in=None) :
19
    
20
    lx4      = lx4_.copy()
21
    
22
    lx4[0:2] = s(lx4[0:2])
23
    
24
    lx4[0]   = (lx4[0]+cc)*grid_net[1]     # center x  
25
    lx4[1]   = (lx4[1]+rr)*grid_net[0]     # center y
26
    
27
    lx4[2:4] = anchor_net*np.exp(lx4[2:4]) # size x, y
28
    
29
    tl       = np.round((lx4[0:2]-lx4[2:4]/2)/scale,0) # top-left   x,y point
30
    dr       = np.round((lx4[0:2]+lx4[2:4]/2)/scale,0) # down-right x,y point
31

32
    if shape_in is not None:
33
        tl       = np.clip(tl,0,(shape_in[1],shape_in[0]))
34
        dr       = np.clip(dr,0,(shape_in[1],shape_in[0]))
35
        
36
    return(tl.astype(np.int32),dr.astype(np.int32))
37

38

39
def BBxySizeHalf (lx4_, rr,cc, anchor_net, scale, grid_net) :
40
    
41
    lx4      = lx4_.copy()
42
    
43
    lx4[0:2] = s(lx4[0:2])
44
    
45
    lx4[0]   = (lx4[0]+cc)*grid_net[1]     # center x  
46
    lx4[1]   = (lx4[1]+rr)*grid_net[0]     # center y
47
    
48
    lx4[2:4] = anchor_net*np.exp(lx4[2:4]) # size x, y
49
    
50
    lx4      = np.round(lx4/scale,0)
51
    
52
    return(lx4.astype(np.int32))
53

54
def BBxyHalf (lx2_, rr,cc, scale, grid_net) :
55
    
56
    lx4      = lx2_.copy()
57
    
58
    lx4[0:2] = s(lx4[0:2])
59
    
60
    lx4[0]   = (lx4[0]+cc)*grid_net[1]     # center x  
61
    lx4[1]   = (lx4[1]+rr)*grid_net[0]     # center y
62
    
63
    lx4      = np.round(lx4/scale)
64
    
65
    return(lx4[0:2].astype(np.int32))
66

67

68
##########################################################################
69

70
def formSub (pSub, scale_all, shape_in_all, classes, anchors_net, grid_net, classesOK=True, **kwargs) :
71
    labels = []
72
    for ii, pSub1 in enumerate(pSub) :
73
        
74
        if classesOK :
75
            pSub1 = nms(pSub1, scale_all[ii], shape_in_all[ii], anchors_net, grid_net, 
76
                                     threshold2=0.5, debug=False)
77

78
        label1 = formSub1(pSub1, scale_all[ii], shape_in_all[ii], classes, grid_net, 
79
                          classesOK=classesOK, **kwargs)
80
        labels.append(label1)
81
        
82
    return(labels)
83
    
84
##########################################################################
85

86
def formSub1(pSub1, scale, shape_in, classes, grid_net,
87
             threshold=0.5, threshold2=0.5, classesOK=True, sigmoidOK=True, softmaxOK=False,
88
             **kwargs) :
89
    
90
    label1 = ''
91
    for ir,rr in enumerate(pSub1) :
92
        for ic,cc in enumerate(rr) :
93
            for ia,aa in enumerate(cc) :
94
                #print(aa.shape, s(aa[0]),len(threshold))
95
                
96
                paa = s(aa[0]) if sigmoidOK else aa[0].copy()
97
                if (paa>=threshold) :
98
                        
99
                    lx4    = BBxyHalf(aa[1:3],ir,ic,scale,grid_net)     
100
                    
101
                    lx4    = np.clip(lx4,0,(shape_in[1],shape_in[0]))
102
                    
103
               
104
                    class1 = -1
105
                    try    : class1 = softmax(aa[5:]).argmax() if sigmoidOK else aa[5:].argmax()
106
                    except : class1 = -1
107
                    if class1>=0 :
108
                        pclass = s(aa[5+class1]) if sigmoidOK else aa[5+class1].copy()
109
                        if pclass<threshold2 : class1=-1
110
                    #print(class1, pclass)
111
                    if classesOK and class1>=0:
112
                            label1 = label1+(classes[class1] if class1>=0 else 'X+000000')
113
                            label1 = label1+' '+str(int(lx4[0]))+' '+str(int(lx4[1]))+' '
114
                    else :
115
                            label1 = label1+(classes[class1] if class1>=0 else 'X+000000')
116
                            label1 = label1+' '+str(int(lx4[0]))+' '+str(int(lx4[1]))+' '
117
    label1 = label1.strip()
118
    return(label1)
119

120
##########################################################################
121

122
def formSub5 (pSub, scale_all, shape_in_all, classes, grid_net, anchors_net,  
123
              delta=None, classesOK=True, nmsOK=True, **kwargs) :
124
    labels = []
125
    if delta is None : delta = [[0,0]]*len(pSub)
126

127
    for ii, pSub1 in enumerate(pSub) :
128
        
129
        label1 = formSub1x4(pSub1, scale_all[ii], shape_in_all[ii], classes, grid_net, anchors_net, delta[ii],
130
                          classesOK=classesOK, **kwargs)
131
        if nmsOK : label1 = NMSx4(label1, threshold=0.5, noClass=(not classesOK))
132
        labels.append(label1)
133
        
134
    return(labels)
135
    
136

137
##########################################################################
138

139
def formSub1x4 ( pSub1, scale, shape_in, classes, grid_net, anchors_net, delta1,
140
                 threshold =0.5, classesOK=True,
141
                 threshold2=0.2,
142
                 sigmoidOK=False, # 2019-09-13 change from True to False
143
                 debug=False) : #   **kwargs) :
144
    label1  = ''
145
    iCount = 0
146
    for ir,rr in enumerate(pSub1) :
147
        for ic,cc in enumerate(rr) :
148
            for ia,aa in enumerate(cc) :
149
                #print(aa.shape, s(aa[0]), threshold)
150
                
151
                paa = s(aa[0])
152
                if (paa>=threshold) :
153
                    
154
                    iCount = iCount + 1
155
                    lx4    = BBxySizeHalf(aa[1:5],ir,ic, anchors_net[ia], scale, grid_net)
156
                    
157
                    lx4[0:2] = np.clip(lx4[0:2]+(delta1[1]/scale,delta1[0]/scale),0,shape_in[::-1])
158
                    
159
                    class1 = -1
160
                    
161
                    if classesOK and len(aa)>5 and len(classes)>0 :
162
                    
163
                        try    : class1 = softmax(aa[5:]).argmax() if not sigmoidOK else s(aa[5:]).argmax()
164
                        except : class1 = -1
165
                        if class1>=0 :
166
                            pclass = s(aa[5+class1]) if sigmoidOK else softmax(aa[5+class1])
167
                            if pclass<threshold2 : class1=-1
168
                        #print(class1, pclass)
169
                        label1 = label1+(classes[class1] if class1>=0 else 'X+000000')
170
                        label1 = label1+' {l[0]:d} {l[1]:d} {l[2]:d} {l[3]:d} '.format(l=lx4)
171
                        
172
                    else :
173
                            label1 = label1+(classes[class1] if class1>=0 else 'X+000000')
174
                            label1 = label1+' {l[0]:d} {l[1]:d} {l[2]:d} {l[3]:d} '.format(l=lx4)
175
                #print(aa.shape, s(aa[0]),len(threshold))
176
                
177
    label1 = label1.strip()
178
    
179
    if debug : return(label1, iCount)
180
    return(label1)
181
#labels = formSub(ptest, scale_test, threshold=0.5)
182

183
##########################################################################
184

185
def nms(pSub1, scale, shape_in, anchors_net, grid_net, threshold=0.5, threshold2=0.5, debug=False, printOK=False) :
186
    
187
    bb1, code = [], 0
188
    for ir,rr in enumerate(pSub1) :
189
        for ic,cc in enumerate(rr) :
190
            for ia,aa in enumerate(cc) :
191
                paa = s(aa[0].copy())
192
                if (paa>=threshold) :
193
                    
194
                    tl,dr = BBxyTLDR(aa[1:5],ir,ic,anchors_net[ia],scale,grid_net,shape_in=shape_in)
195
                    
196
                    class1 = -1
197
                    try    : class1 = softmax(aa[5:]).argmax()
198
                    except : class1 = -1
199
                        
200
                    #print(class1, pclass)
201
                    if class1>=0 :
202
                        bb1.append([class1,tl[0],tl[1],dr[0],dr[1],ir,ic,ia,1])
203
                    else :
204
                        print(class1)
205
                        
206
    #print(len(bb1))
207
    bb1 = np.array(bb1);
208
    #print(bb1.shape)
209
    if len(bb1)>0 :
210
        bb1=bb1[bb1[:,0].argsort(),:]
211
        for ii in range(len(bb1)) :
212
            if bb1[ii,8]>0 :
213
                for jj in range(ii+1,len(bb1)) :
214
                    if bb1[ii,0]!=bb1[jj,0] : break
215
                    ttll = np.maximum(bb1[ii,1:3],bb1[jj,1:3])
216
                    ddrr = np.minimum(bb1[ii,3:5],bb1[jj,3:5])
217
                    if np.min(ddrr-ttll)<0 : break
218
                    iou1 = (ddrr-ttll)[0]*(ddrr-ttll)[1]
219
                    iou  = iou1/((bb1[ii,3]-bb1[ii,1])*(bb1[ii,4]-bb1[ii,2])+
220
                                 (bb1[jj,3]-bb1[jj,1])*(bb1[jj,4]-bb1[jj,2])-iou1)
221
                    if printOK :
222
                        print('---',np.round(bb1[ii,1:5],0),np.round(bb1[jj,1:5],0))
223
                        print('   ',iou, iou1)
224
                    if iou>=threshold2 : bb1[jj,8] = 0
225
    for jj in range(len(bb1)) :
226
        if bb1[jj,8]<=0 :
227
            pSub1[int(bb1[jj,5]),int(bb1[jj,6]),int(bb1[jj,7]),0]=-1000 # sigmoid=0
228
            code      = code+1
229
        
230
    if debug : return(pSub1,code,bb1)
231
    return pSub1
232

233
##########################################################################
234

235
def notIOUx12 (tldr1, tldr2, threshold=0.5, printOK=False) :
236
    
237
    ttll = np.maximum(tldr1[0:2],tldr2[0:2])
238
    ddrr = np.minimum(tldr1[2:4],tldr2[2:4])
239

240
    if  printOK and False:
241
        print('notIOUx12 1',tldr1,tldr2, np.min(ddrr-ttll))
242

243
    if np.min(ddrr-ttll)<=0 : return(True)
244

245
    iou12= (ddrr-ttll)[0]*(ddrr-ttll)[1]
246
    size1= (tldr1[2]-tldr1[0])*(tldr1[3]-tldr1[1])
247
    size2= (tldr2[2]-tldr2[0])*(tldr2[3]-tldr2[1])
248

249
    if  printOK :
250
        print('notIOUx12 2',
251
              tldr1,tldr2,'\t' ,
252
              iou12, size1, size2, 
253
              round(iou12/(size2-iou12),4),
254
              round(iou12/(size1+size2-iou12),4))
255
        
256
    if   iou12==size2                     : return(False)
257
    elif (iou12/(size2-iou12))>=threshold : return(False)
258
    else :
259
        iou  = iou12/(size1+size2-iou12)
260
        if iou>=threshold :  return(False)
261
                            
262
    return(True)
263
    
264

265
##########################################################################
266

267
def NMSx4(pSub1x4, threshold=0.5, noClass=False, debug=False, printOK=False) :
268
    
269
    count  = 0
270
    label1 = ''
271
    bb1  = np.array(pSub1x4.split()).reshape((-1,5))
272
    uni  = bb1[:,0].copy()
273
    tldr = bb1[:,1:5].astype(np.int32)
274
    mark = np.ones(len(uni)).astype(bool)
275
    uni0 = uni.copy()
276
    if printOK :
277
        print(uni[:5],uni0[:5])
278
    
279
    
280
    if noClass : 
281
        uni = np.ones(len(uni))
282
    
283
    size = -(tldr[:,2]*tldr[:,3])
284
    sort1= size.argsort()
285
    uni  = uni [sort1]
286
    uni0 = uni0[sort1]
287
    tldr = tldr[sort1,:]
288
    
289
    if printOK :
290
        print(uni[:5],uni0[:5])
291
    
292
    if False :
293
        mark = (np.abs(tldr[:,2:]-tldr[:,2:].mean(axis=0))<=2.0*tldr[:,2:].std(axis=0))
294
        mark = np.logical_and(mark[:,0],mark[:,1])
295
    
296
    temp = tldr.astype(np.int32)
297
    tldr[:,0:2] = np.floor(temp[:,0:2]-(temp[:,2:4]/2)).astype(np.int32)
298
    tldr[:,2:4] = np.ceil (temp[:,0:2]+(temp[:,2:4]/2)).astype(np.int32)
299
    
300
    if not noClass :
301
        sort2 = uni[:].argsort()
302
        tldr=tldr[sort2,:]
303
        mark=mark[sort2]
304
        uni =uni [sort2]
305
        uni0= uni[sort2]
306
        
307
    
308
    if len(uni)==0 :
309
        if debug : return(label1,count)
310
        return label1
311
    
312
    for ii in range(len(mark)) :
313
        if mark[ii] :
314
            for jj in range(ii+1,len(mark)) :
315
                if mark[jj] :
316
                    mark[jj] = notIOUx12(tldr[ii], tldr[jj], threshold=threshold) #, printOK=True)
317
                    
318
    tldr[:,2:4] = tldr[:,2:4]-tldr[:,0:2]
319
    tldr[:,0:2] = np.floor(tldr[:,0:2]+tldr[:,2:4]//2).astype(np.int32)
320
    
321
    for ii in range(len(mark)) :
322
        if mark[ii] :
323
            label1 = label1 + '{uni} {b[0]:d} {b[1]:d} {b[2]:d} {b[3]:d} '.format(uni=uni0[ii], b=tldr[ii])
324
            count  = count+1
325
        
326
    label1 = label1.strip()
327
    if debug : return(label1,count)
328
    return label1
329

330
##########################################################################
331

332
def drawSub1F (nameFile, pSub5, **kwargs) :
333
    
334
    img1 = cv2.imread(nameFile,0) #-1)
335
    
336
    drawSub1A (img1,pSub5,**kwargs)
337
    
338
    return
339

340
def drawSub1A (img1, pSub5, 
341
               iiNumber=-1, f1=-1, centerXY=True,
342
               sigmoidOK=False, show=True, printOK=False) :
343
    
344
    if show : plt.figure(figsize=(10,15))
345
    #####img1 = cv2.imread(nameFile,0) #-1)
346
    plt.imshow(img1.squeeze())
347
    shape_in = img1.shape[:2]
348
    
349
    bb1 = np.array(pSub5.split()).reshape((-1,5))
350
    
351
    iCount, rCount = len(bb1), 0
352
    
353
    #print(bb1[:,1])
354
    
355
    for ii, (uni,xc,yc,xs,ys) in enumerate(bb1) :
356
        
357
        marker = 'o' 
358
        color  = 'r' 
359
        
360
        lx4    = np.array([xc,yc,xs,ys],dtype=np.int32)
361

362
        if not centerXY : lx4[0:2] = lx4[0:2]+lx4[2:4]/2
363
        
364
        plt.scatter(lx4[0],lx4[1], color=color, marker=marker)
365
        
366
        tl, dr = np.floor(lx4[0:2]-lx4[2:4]/2), np.ceil(lx4[0:2]+lx4[2:4]/2)
367

368
        tl     = np.clip(tl,0,shape_in[::-1])
369
        dr     = np.clip(dr,0,shape_in[::-1])
370

371
        border = [tl,[tl[0],dr[1]],dr,[dr[0],tl[1]]]
372
        line   = plt.Polygon(border, closed=True, fill=None, edgecolor=color)
373
        plt.gca().add_line(line)
374

375
    plt.title(str(iCount)+' '+
376
              (' ('+str(iiNumber)+')' if iiNumber>=0 else '')+
377
              (' ('+str(f1)+')' if f1>=0 else ''))
378
    
379
    if show :
380
        plt.show()
381
        plt.close()
382
        print(iCount)
383
        
384
    return

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.