def apply_24_net(image,iname,nn,bb): X24 = [] X12 = [] for c,e in enumerate(bb): x1,y1,x2,y2 = e sub = image[y1:y2,x1:x2] sub12 = fr.resize_frame(sub,(12,12)) sub24 = fr.resize_frame(sub,(24,24)) X12.append(fr.frame_to_vect(sub12)) X24.append(fr.frame_to_vect(sub24)) X12 = sp.array(X12) X24 = sp.array(X24) pred = nn.predict(X=X24,X12=X12) return bb[pred==1]
def apply_24_net(image, iname, nn, bb): X24 = [] X12 = [] for c, e in enumerate(bb): x1, y1, x2, y2 = e sub = image[y1:y2, x1:x2] sub12 = fr.resize_frame(sub, (12, 12)) sub24 = fr.resize_frame(sub, (24, 24)) X12.append(fr.frame_to_vect(sub12)) X24.append(fr.frame_to_vect(sub24)) X12 = sp.array(X12) X24 = sp.array(X24) pred = nn.predict(X=X24, X12=X12) return bb[pred == 1]
def apply_24_48_net(image,iname,nn,bb,debug=0): X = [] k = int(nn.nn_name[:2]) for c,e in enumerate(bb): x1,y1,x2,y2 = e sub = image[y1:y2,x1:x2] subk = fr.resize_frame(sub,(k,k)) X.append(fr.frame_to_vect(subk)) X = sp.array(X) pred = nn.predict(X=X) proba = nn.predict_proba(X=X) pos_proba = proba[pred==1][:,1] bb = bb[pred==1] pos_pred = pred[pred==1] if nn.nn_name[:2] == '48': bb = bb[pos_proba > 0.999998] pred = pred[pos_proba > 0.999998] pos_proba = pos_proba[pos_proba > 0.999998] if debug: j = 0 for e,lb,p in zip(bb,pos_pred,pos_proba): x1,y1,x2,y2 = e sub = image[y1:y2,x1:x2] fr.write_frame('F:\\2\\'+str(j)+ '-'+str(lb) + '-' +str(p) + '-',sub) j += 1 return (bb,pos_proba)
def apply_calib_net(image, iname, nn, bb): X_calib = [] for c, e in enumerate(bb): x1, y1, x2, y2 = e sub = image[y1:y2, x1:x2] v = int(nn.nn_name[:2]) subr = fr.resize_frame(sub, (v, v)) X_calib.append(fr.frame_to_vect(subr)) #fr.write_frame('F:\\1\\'+str(c),sub) X_calib = sp.array(X_calib) pred_proba = nn.predict_proba(X_calib) nbb = [] for e, lb in zip(bb, pred_proba): t = 0.3 x1, y1, x2, y2 = e sn, xn, yn = mean_pattern(lb, t) h = abs(y1 - y2) w = abs(x1 - x2) #ii,jj,hh,ww = [int(e) for e in calib(x1,y1,h,w,lb)] #if sp.any(sp.array([ii,jj,hh,ww]) < 0): # continue ii, jj, hh, ww = [ max(0, int(e)) for e in calib_apply(x1, y1, h, w, sn, xn, yn) ] #print(ii,jj,hh,ww) nbb.append([ii, jj, ii + ww, jj + hh]) for c, e in enumerate(nbb): x1, y1, x2, y2 = e sub = image[y1:y2, x1:x2] #fr.write_frame('F:\\1\\'+str(c) + 'calib',sub) return sp.array(nbb)
def get_aflw_face_data(k = 12): dbpath = 'F:\\datasets\\image_data_sets\\faces\\AFLW' dbpath = join(dbpath,'aflw.sqlite') rfpath = 'F:\\datasets\\image_data_sets\\faces\\AFLW\\img' conn = sqlite3.connect(dbpath) X = [] for file_id,x,y,h,w in conn.execute('SELECT file_id,x,y,h,w FROM Faces NATURAL JOIN FaceRect'): fpath = join(rfpath,file_id) frame = fr.get_frame(fpath) no_neg = sp.all(sp.array([x,y,h,w]) > 0) ## ignore a bad data in sql table if frame != None and no_neg: face = fr.get_patch(frame,y,x,(h,w)) face_r,good_example = Datasets.sample_resize(face,k) if good_example: print('face:',fpath) vec = fr.frame_to_vect(face_r) X.append(vec) face_flip = fr.flip_frame(face) face_flip_r = fr.resize_frame(face_flip,(k,k)) vec = fr.frame_to_vect(face_flip_r) X.append(vec) #fr.write_frame('F:\\1\\'+'flip'+str(file_id),face_flip) #fr.write_frame('F:\\1\\'+str(file_id),face) X = sp.array(X) y = sp.ones(len(X)) return X,y
def apply_calib_net(image,iname,nn,bb): X_calib = [] for c,e in enumerate(bb): x1,y1,x2,y2 = e sub = image[y1:y2,x1:x2] v = int(nn.nn_name[:2]) subr = fr.resize_frame(sub,(v,v)) X_calib.append(fr.frame_to_vect(subr)) #fr.write_frame('F:\\1\\'+str(c),sub) X_calib = sp.array(X_calib) pred_proba = nn.predict_proba(X_calib) nbb = [] for e,lb in zip(bb,pred_proba): t = 0.3 x1,y1,x2,y2 = e sn,xn,yn = mean_pattern(lb,t) h = abs(y1-y2) w = abs(x1-x2) #ii,jj,hh,ww = [int(e) for e in calib(x1,y1,h,w,lb)] #if sp.any(sp.array([ii,jj,hh,ww]) < 0): # continue ii,jj,hh,ww = [max(0,int(e)) for e in calib_apply(x1,y1,h,w,sn,xn,yn)] #print(ii,jj,hh,ww) nbb.append([ii,jj,ii+ww,jj+hh]) for c,e in enumerate(nbb): x1,y1,x2,y2 = e sub = image[y1:y2,x1:x2] #fr.write_frame('F:\\1\\'+str(c) + 'calib',sub) return sp.array(nbb)
def frames2batch(k=12, batch_size=1024, is_calib=False): pos = util.get_files(rootdir='F:\\train_data\\pos\\') neg = util.get_files(rootdir='F:\\train_data\\neg\\') pos = shuffle(pos) neg = shuffle(neg) total = pos + neg total = shuffle(total) batch = [] c = 0 bpath = 'F:\\train_data\\batch\\' for item_path in total: frame = fr.get_frame(item_path) frame_r = fr.resize_frame(frame, (k, k)) if frame_r == None: continue vec = fr.frame_to_vect(frame_r) label = 1 if item_path.split('\\')[-1].find('pos') > 0 else 0 print(item_path, label) batch.append((vec, label)) if len(batch) > 0 and len(batch) % batch_size == 0: batch = sp.array(batch) sp.savez( bpath + str(c) + '_' + str(k) + ('_' if not is_calib else '_calib-') + 'net', batch) batch = [] c += 1 if len(batch) > 0 and len(batch) % batch_size == 0: batch = sp.array(batch) sp.savez( bpath + str(c) + '_' + str(k) + ('_' if not is_calib else '_calib') + '-net', batch) batch = [] c += 1
def get_aflw_face_data(k=12): dbpath = 'F:\\datasets\\image_data_sets\\faces\\AFLW' dbpath = join(dbpath, 'aflw.sqlite') rfpath = 'F:\\datasets\\image_data_sets\\faces\\AFLW\\img' conn = sqlite3.connect(dbpath) X = [] for file_id, x, y, h, w in conn.execute( 'SELECT file_id,x,y,h,w FROM Faces NATURAL JOIN FaceRect'): fpath = join(rfpath, file_id) frame = fr.get_frame(fpath) no_neg = sp.all( sp.array([x, y, h, w]) > 0) ## ignore a bad data in sql table if frame != None and no_neg: face = fr.get_patch(frame, y, x, (h, w)) face_r, good_example = Datasets.sample_resize(face, k) if good_example: print('face:', fpath) vec = fr.frame_to_vect(face_r) X.append(vec) face_flip = fr.flip_frame(face) face_flip_r = fr.resize_frame(face_flip, (k, k)) vec = fr.frame_to_vect(face_flip_r) X.append(vec) #fr.write_frame('F:\\1\\'+'flip'+str(file_id),face_flip) #fr.write_frame('F:\\1\\'+str(file_id),face) X = sp.array(X) y = sp.ones(len(X)) return X, y
def frames2batch(k = 12,batch_size = 1024, is_calib = False): pos = util.get_files(rootdir = 'F:\\train_data\\pos\\') neg = util.get_files(rootdir = 'F:\\train_data\\neg\\') pos = shuffle(pos) neg = shuffle(neg) total = pos + neg total = shuffle(total) batch = [] c = 0 bpath = 'F:\\train_data\\batch\\' for item_path in total: frame = fr.get_frame(item_path) frame_r = fr.resize_frame(frame,(k,k)) if frame_r == None: continue vec = fr.frame_to_vect(frame_r) label = 1 if item_path.split('\\')[-1].find('pos') > 0 else 0 print(item_path,label) batch.append((vec,label)) if len(batch) > 0 and len(batch) % batch_size == 0: batch = sp.array(batch) sp.savez(bpath + str(c) + '_' + str(k) + ('_' if not is_calib else '_calib-') + 'net',batch) batch = [] c += 1 if len(batch) > 0 and len(batch) % batch_size == 0: batch = sp.array(batch) sp.savez(bpath + str(c) + '_' + str(k) + ('_' if not is_calib else '_calib') + '-net',batch) batch = [] c += 1
def sample_resize(subframe,k = 12): ''' return resized frame or reject it if any of side less than k ''' H,W,dim = subframe.shape if H >= k and W >= k: return (fr.resize_frame(subframe,(k,k)),True) else: return (subframe,False)
def sample_resize(subframe, k=12): ''' return resized frame or reject it if any of side less than k ''' H, W, dim = subframe.shape if H >= k and W >= k: return (fr.resize_frame(subframe, (k, k)), True) else: return (subframe, False)
def sample_resize(subframe, k_check=12, k_res=12): ''' return resized frame or reject it if any of side less than k ''' H, W, dim = subframe.shape # there are negative coord's in datasets :( if H <= 0 or W <= 0: return (subframe, False) #print(H,W,dim) if H >= k_check or W >= k_check: return (fr.resize_frame(subframe, (k_res, k_res)), True) else: return (subframe, False)
def sample_resize(subframe,k_check = 12, k_res = 12): ''' return resized frame or reject it if any of side less than k ''' H,W,dim = subframe.shape # there are negative coord's in datasets :( if H <= 0 or W <= 0: return (subframe,False) #print(H,W,dim) if H >= k_check or W >= k_check: return (fr.resize_frame(subframe,(k_res,k_res)),True) else: return (subframe,False)
def apply_calib_net(image,iname,nn,bb,T=0.3): X_calib = [] for c,e in enumerate(bb): x1,y1,x2,y2 = e sub = image[y1:y2,x1:x2] v = int(nn.nn_name[:2]) subr = fr.resize_frame(sub,(v,v)) X_calib.append(fr.frame_to_vect(subr)) #fr.write_frame('F:\\1\\'+str(c),sub) X_calib = sp.array(X_calib) pred_proba = nn.predict_proba(X_calib) nbb = [] for e,lb in zip(bb,pred_proba): t = 1/45 x1,y1,x2,y2 = e ds, dx, dy = mean_pattern(lb,t) h = abs(y1-y2) w = abs(x1-x2) ii,jj,hh,ww = [int(max(0,e)) for e in util.calib_apply(x1,y1,h,w,ds,dx,dy)] nbb.append([ii,jj,ii+ww,jj+hh]) return sp.array(nbb)