File size: 11,247 Bytes
752c636
 
 
 
 
8460a2a
752c636
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
572ea36
 
 
752c636
 
 
 
572ea36
752c636
 
 
572ea36
752c636
 
 
 
 
 
 
 
 
8f7b62d
 
 
 
752c636
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8460a2a
 
 
 
 
752c636
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8460a2a
752c636
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8f7b62d
752c636
 
 
 
 
 
 
 
b715caf
752c636
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8f7b62d
752c636
 
448ac8f
8f7b62d
 
752c636
8f7b62d
 
 
 
 
752c636
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9efc67a
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import cv2
import numpy as np
import matplotlib.pyplot as plt
import math
import copy
import importlib




def tiler(img,image_size,tiles=2,pad=0,):
    # pad is 0 to 0.9%
    h,w=img.shape[:2]

    tile_h=(h//tiles)
    tile_w=(w//tiles)
    
    pad=int(tile_h*pad)
    
    crops=[]
    coordinates=[]
    for i in range(tiles):
        for j in range(tiles):
            ymin=tile_h*i
            xmin=tile_w*j
        
            if i!=0:ymin=ymin-pad
            if j!=0:xmin=xmin-pad

            ymax=min(ymin+tile_h+pad,h)
            xmax=min(xmin+tile_w+pad,w)

            # crops.append(img[ymin:ymax,xmin:xmax])
            crops.append(cv2.resize(img[ymin:ymax,xmin:xmax],[image_size,image_size]))
            coordinates.append((xmin,ymin,xmax,ymax))
            # print(crops[-1].shape)
    return coordinates,np.array(crops)
    
class square_crop:
    def __call__(self,img):
        h,w=img.shape[:2]
        self.w_removed,self.h_removed=0,0
        if w>h:
            self.w_removed=(w-h)//2
            img=img[:,self.w_removed:w-self.w_removed]
        elif h>w:
            self.h_removed=(h-w)//2
            img=img[self.h_removed:h-self.h_removed,:]
        
        h,w=img.shape[:2]
        self.w_removed,self.h_removed=self.w_removed/w,self.h_removed/h
        return img
    def rescale(self,objs_found):
        raise NotImplementedError

class square_pad:
    def __init__(self,color=(0,0,0)):
        self.color=color
    def __call__(self,img):
        h,w=img.shape[:2]
        self.w_added,self.h_added=0,0
        if h>w:
            self.w_added=int((h-w)/2)
            padding=(np.ones([h,self.w_added,3])*np.array(self.color)[None,None,:]).astype("uint8")
            img=np.concatenate([padding,img,padding],axis=1)
        elif w>h:
            self.h_added=int((w-h)/2)
            padding=(np.ones([self.h_added,w,3])*np.array(self.color)[None,None,:]).astype("uint8")
            img=np.concatenate([padding,img,padding],axis=0)
        h,w=img.shape[:2]
        
        self.w_added,self.h_added=self.w_added/w,self.h_added/h
        
        return img
    def rescale(self,objs_found):
        
        for i in range(len(objs_found)):
            objs_found[i][2]=(objs_found[i][2]-self.w_added)/(1-2*self.w_added)
            objs_found[i][3]=(objs_found[i][3]-self.h_added)/(1-2*self.h_added)
            objs_found[i][4]=(objs_found[i][4])/(1-2*self.w_added)
            objs_found[i][5]=(objs_found[i][5])/(1-2*self.h_added)
        return objs_found


class face_detection:
    def __init__(self,model_path):
        ################### set num_anchors & tf_anchors ###################
        anchor_boxes=np.loadtxt(model_path+"/anchors.txt")
        num_anchors=anchor_boxes.shape[0]
        tf_anchors=K.reshape(K.variable(anchor_boxes),[1, 1, 1, num_anchors, 2])
        load_model_lib.num_anchors=num_anchors
        decode_model_lib.tf_anchors=tf_anchors
        ####################################################################
        
        self.model=load_model(model_path+"/model.h5")
        
        self.modes_available=["tiled","sized"]
        self.square_preprocessing=square_crop

        config_file_path='.'.join(model_path.split("/"))+".config"
        # print(config_file_path)
        self.model_config= importlib.import_module(config_file_path)

    def invoke_model(self,img,p,iou_threshold,batch_size):
        all_objs_found=[]
        
        for i in range(math.ceil(img.shape[0]/batch_size)):

            y_pred=self.model.predict(img[int(i*batch_size):int((i+1)*batch_size)].astype('float32'),verbose=0)
            # print(y_pred.shape)
            
            for i in range(y_pred.shape[0]):
                objs_found=get_objects(y_pred[i],p=p)
                all_objs_found.append(objs_found)
        
        return all_objs_found
    def get_tiled_output(self,img,p_thres,nms_thres,tiles,pad,image_size,save_tiles=None,batch_size=4): 
        # pad is 0 to 0.5%
        # pad=0.05       
        img=cv2.resize(img,[image_size*tiles,image_size*tiles])
    
        coordinates,crops=tiler(img,tiles=tiles,pad=pad,image_size=image_size)
        
        all_objs_found=self.invoke_model(crops,p_thres,nms_thres,batch_size)

        if save_tiles:
            fig=plt.figure(figsize=(3*tiles,3*tiles))
            plt.axis("off")
            plt.title(f"pad:{pad}")

            for i in range(int(tiles*tiles)): 
                fig.add_subplot(tiles,tiles,i+1)
                plt.axis("off")
                plt.imshow(pred_image(crops[i],all_objs_found[i]))
            # plt.show(block=False)
            plt.savefig(save_tiles+f"_{tiles}.jpg")
            plt.close()
        
        all_objs_found_joined=[]
        for i in range(int(tiles*tiles)): 
            xmin,ymin,xmax,ymax=coordinates[i]
            w,h=xmax-xmin,ymax-ymin
            for j in range(all_objs_found[i].__len__()):
                # all_objs_found[i][j]['xywh']=np.array(all_objs_found[i][j]['xywh'])/image_size
                all_objs_found[i][j]['xywh']=np.array(all_objs_found[i][j]['xywh'])*w
                all_objs_found[i][j]['xywh'][0]+=xmin
                all_objs_found[i][j]['xywh'][1]+=ymin
                all_objs_found[i][j]['xywh']=(all_objs_found[i][j]['xywh']/(image_size*tiles)).tolist()
            all_objs_found_joined.extend(all_objs_found[i])

        

        return all_objs_found_joined


    def set_mode(self,p_thres,nms_thres,batch_size=1,mode="sized",**kwargs):
        # mode : tiled or sized
        
        self.p_thres=p_thres
        self.nms_thres=nms_thres
        self.batch_size=batch_size

        if mode=="tiled":
            try:
                self.image_size=kwargs['image_size']
                if "tiles" not in kwargs:
                    self.tiles=[1]
                    self.pad=0
                else:
                    self.tiles=kwargs['tiles'] if(type(kwargs['tiles'])==type(list([1])) or type(kwargs['tiles'])==type(np.zeros([]))) else [kwargs['tiles']]
                    self.pad=kwargs['pad']
            except:
                
                raise ValueError(f"Not all tiled mode parameters passed.")
            
            self.save_tiles=kwargs["save_tiles"] if ("save_tiles" in kwargs) else None
            
        elif mode=="sized":
            try:
                # self.image_size=kwargs['image_size']
                self.image_size=kwargs['image_size'] if(type(kwargs['image_size'])==type(list([1])) or type(kwargs['image_size'])==type(np.zeros([]))) else [kwargs['image_size']]
                self.batch_size=1
            except:
                raise ValueError(f"Not all Sized mode parameters passed.")
        else:
            raise ValueError(f"Unavailable mode={mode} \nmode can only be one of:{self.modes_available}")
        
        self.mode=mode
        

    def predict_once(self,img):
        
        if (type(img)==str):
            img=cv2.cvtColor(cv2.imread(img),cv2.COLOR_BGR2RGB)
        elif (type(img)!=type(np.zeros([]))):
            raise TypeError(f"Inappropriate type of image={type(img)}")
        
        # if img.shape[0]!=img.shape[1]:  raise ValueError(f"The image should be squared")
        
    
        if  not hasattr(self,'mode'): raise ValueError(f"First call set_mode function to set mode using one of the following mode :{self.modes_available}")

        if self.mode=='tiled':
            if type(self.tiles)==type(list([1])) or type(self.tiles)==type(np.zeros([])):   raise TypeError("use advanced_predict function for inference on multiple tiles")
            objs_found=self.get_tiled_output(img,p_thres=self.p_thres,nms_thres=self.nms_thres,tiles=self.tiles,pad=self.pad,image_size=self.image_size,save_tiles=self.save_tiles)
        elif self.mode=='sized':
            if type(self.image_size)==type(list([1])) or type(self.image_size)==type(np.zeros([])):   raise TypeError("use advanced_predict function for inference on multiple sizes")
            resized_img=cv2.resize(img,[self.image_size,self.image_size])
            objs_found=self.invoke_model(resized_img[None,:,:,:],self.p_thres,self.nms_thres,batch_size=1)[0]

        return objs_found

    def predict(self,img):

        if (type(img)==str):
            img=cv2.cvtColor(cv2.imread(img),cv2.COLOR_BGR2RGB)
        elif (type(img)!=type(np.zeros([]))):
            raise TypeError(f"Inappropriate type of image={type(img)}")
        
        img=self.square_preprocessing(img)

        if  not hasattr(self,'mode'): raise ValueError(f"First call set_mode function to set mode using one of the following mode :{self.modes_available}")

        if self.mode=="tiled":
            all_objs_found=[]
            all_tiles=copy.deepcopy(self.tiles)
            for tiles in all_tiles:
                self.tiles=tiles
                _,objs_found=self.predict_once(img)
                all_objs_found.extend(objs_found)
            self.tiles=all_tiles

        elif self.mode=="sized":
            all_objs_found=[]
            all_image_size=copy.deepcopy(self.image_size)
            for image_size in all_image_size:
                self.image_size=image_size
                objs_found=self.predict_once(img)
                all_objs_found.extend(objs_found)
            self.image_size=all_image_size
        
        all_objs_found=np.array(all_objs_found)
        
        all_objs_found=nms(all_objs_found,self.nms_thres)
        all_objs_found=self.square_preprocessing.rescale(all_objs_found)   #rescale coordinates to original image's resolution
        for obj_found in all_objs_found: obj_found[1]=idx_to_class[obj_found[1]]
        # print(all_objs_found)
        
        return all_objs_found





if __name__=="__main__":
    import create_load_model as load_model_lib
    import decode_yolo_v2 as decode_model_lib
    from create_load_model import *
    from decode_yolo_v2 import *
    

    face_detector=face_detection("face_detection/Models/v1/")
    
    # test_img="C:\\Users\\Home\\Downloads\\WhatsApp Image 2023-01-04 at 6.58.40 PM.jpeg"
    test_img="C:\\Users\\Home\\Downloads\\family-52.jpg"

    p_thres=0.5
    nms_thres=0.3
    tiles=3
    pad=0

    # face_detector.set_mode(p_thres,nms_thres,mode="tiled",tiles=[1,2],pad=pad,image_size=416,save_tiles="tile")
    # face_detector.set_mode(p_thres=p_thres,nms_thres=nms_thres,image_size=608)
    # face_detector.set_mode(p_thres,nms_thres,mode="sized",image_size=256)
    # face_detector.set_mode(p_thres,nms_thres,mode="sized",image_size=352)
    # face_detector.set_mode(p_thres,nms_thres,mode="sized",image_size=608)
    face_detector.set_mode(p_thres,nms_thres,mode="sized",image_size=1024)
    # face_detector.set_mode(p_thres,nms_thres,mode="sized",image_size=[608,1024])
    img,objs_found=face_detector.predict(test_img)
    # img,objs_found=face_detector.advanced_predict(test_img)
    pred_img=pred_image(img,objs_found)
    plt.figure()
    plt.imshow(pred_img)
    plt.show()
    # cv2.imwrite("test_output.jpg",pred_img[:,:,::-1])
else:
    import face_detection.create_load_model as load_model_lib
    import face_detection.decode_yolo_v2 as decode_model_lib
    from face_detection.create_load_model import *
    from face_detection.decode_yolo_v2 import *