def get_aflw_face_data(k = 12, on_drive = False): 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 = [] c = 0 for file_id,x,y,ra,rb,theta in conn.execute('SELECT file_id,x,y,ra,rb,theta FROM Faces NATURAL JOIN FaceEllipse'): fpath = join(rfpath,file_id) frame = fr.get_frame(fpath) x1,y1,x2,y2 = util.ellipse2bbox(a = ra, b = rb, angle = theta, cx = x, cy = y) x = x1 y = y1 h = abs(y2-y1) w = abs(x2-x1) no_neg = sp.all(sp.array([x,y,h,w]) > 0) ## ignore a bad data in sql table if frame != None and no_neg: y,x,w,h = [int(e) for e in (y,x,w,h)] face = fr.get_patch(frame,y,x,(w,h)) face_r,good_example = Datasets.sample_resize(face,k,k) if good_example: print('face:',fpath) vec = fr.frame_to_vect(face_r) if not on_drive: X.append(vec) face_flip_r = fr.flip_frame(face_r) vec = fr.frame_to_vect(face_flip_r) X.append(vec) else: for item in Datasets.data_augmentation(frame,y,x,w,h): fr.write_frame('F:\\train_data\\pos\\' + str(c) + '_' + str(file_id)[:-4] + '_' + 'pos',item) c +=1 X = sp.array(X) y = sp.ones(len(X)) return X,y
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 get_fddb_face_data(k = 12, on_drive = False): root = 'F:\\datasets\\image_data_sets\\faces\\FDDB\\' iroot = os.path.join(root,'originalPics') eroot = os.path.join(root,'FDDB-folds') pattern = '-ellipseList.txt' c = 0 X,y = [],[] for path, subdirs, files in os.walk(eroot): for fname in files: if fname.find(pattern) > 0: fpath = os.path.join(path,fname) print(fpath) with open(fpath) as f: lines = sp.array(f.readlines()) paths_indx = sp.where([line.find('/') > 0 for line in lines])[0] counts_indx = paths_indx + 1 paths = sp.array([e.strip() for e in lines[paths_indx]]) ellipces = [] for i in counts_indx: cnt = int(lines[i]) ellipces.append(lines[i+1:i+cnt+1]) ellipces = [ [ [float(num) for num in line.split()[:-1]] for line in e] for e in ellipces] ellipces = sp.array(ellipces) for iname,ells in zip(paths[:],ellipces[:]): ppath = os.path.join(iroot,iname.replace('/','\\')) + '.jpg' file_id = iname.split('/')[-1] frame = fr.get_frame(ppath) for item in ells: ra,rb,theta,x,y = item x1,y1,x2,y2 = util.ellipse2bbox(a = ra, b = rb, angle = theta, cx = x, cy = y) x = x1 y = y1 h = abs(y2-y1) w = abs(x2-x1) print(file_id,(y,x,h,w)) non_neg = x > 0 and y > 0 if not non_neg: continue if on_drive: for item in Datasets.data_augmentation(frame,y,x,w,h): fr.write_frame('F:\\train_data\\pos\\' + str(c) + '_' + str(file_id) + '_pos',item) c +=1 else: pass X = sp.array(X) y = sp.ones(len(X)) return X,y
def get_aflw_face_data(k=12, on_drive=False): 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 = [] c = 0 for file_id, x, y, ra, rb, theta in conn.execute( 'SELECT file_id,x,y,ra,rb,theta FROM Faces NATURAL JOIN FaceEllipse' ): fpath = join(rfpath, file_id) frame = fr.get_frame(fpath) x1, y1, x2, y2 = util.ellipse2bbox(a=ra, b=rb, angle=theta, cx=x, cy=y) x = x1 y = y1 h = abs(y2 - y1) w = abs(x2 - x1) no_neg = sp.all( sp.array([x, y, h, w]) > 0) ## ignore a bad data in sql table if frame != None and no_neg: y, x, w, h = [int(e) for e in (y, x, w, h)] face = fr.get_patch(frame, y, x, (w, h)) face_r, good_example = Datasets.sample_resize(face, k, k) if good_example: print('face:', fpath) vec = fr.frame_to_vect(face_r) if not on_drive: X.append(vec) face_flip_r = fr.flip_frame(face_r) vec = fr.frame_to_vect(face_flip_r) X.append(vec) else: for item in Datasets.data_augmentation( frame, y, x, w, h): fr.write_frame( 'F:\\train_data\\pos\\' + str(c) + '_' + str(file_id)[:-4] + '_' + 'pos', item) c += 1 X = sp.array(X) y = sp.ones(len(X)) return X, y
def get_train_non_face_data(k=12, patch_per_img=25, on_drive=False): ''' cut non-faces (negative examples) by pick random patch (if in not overlaped with any face bbox from all images in dataset return X - features y - labels cnt - count of examples ''' X = [] def yield_gen(data): data = shuffle(data)[:patch_per_img] for e in data: yield e root = 'F:\\Datasets\\image_data_sets\\non-faces' pattern = "*.jpg" c = 0 for path, subdirs, files in os.walk(root): for iname in files: if fnmatch(iname, pattern): ipath = os.path.join(path, iname) img = fr.get_frame(ipath) print('non_face:', ipath) if img == None: continue H, W = img.shape[:2] if not on_drive: for e in yield_gen(fr.split_frame(img, wshape=(k, k))): X.append(fr.frame_to_vect(e)) else: for e in yield_gen( fr.split_frame(img, wshape=(util.k_max, util.k_max))): fr.write_frame( 'F:\\train_data\\neg\\' + str(c) + '_' 'nonface' + '_neg', e) c += 1 X = sp.array(X) y = sp.zeros(len(X)) return X, y
def get_train_non_face_data(k = 12,patch_per_img = 25,on_drive = False): ''' cut non-faces (negative examples) by pick random patch (if in not overlaped with any face bbox from all images in dataset return X - features y - labels cnt - count of examples ''' X = [] def yield_gen(data): data = shuffle(data)[:patch_per_img] for e in data: yield e root = 'F:\\Datasets\\image_data_sets\\non-faces' pattern = "*.jpg" c = 0 for path, subdirs, files in os.walk(root): for iname in files: if fnmatch(iname, pattern): ipath = os.path.join(path, iname) img = fr.get_frame(ipath) print('non_face:',ipath) if img == None: continue H,W = img.shape[:2] if not on_drive: for e in yield_gen(fr.split_frame(img,wshape=(k,k))): X.append(fr.frame_to_vect(e)) else: for e in yield_gen(fr.split_frame(img,wshape=(util.k_max,util.k_max))): fr.write_frame('F:\\train_data\\neg\\' + str(c) + '_' 'nonface'+'_neg',e) c += 1 X = sp.array(X) y = sp.zeros(len(X)) return X,y
def get_fddb_face_data(k=12, on_drive=False): root = 'F:\\datasets\\image_data_sets\\faces\\FDDB\\' iroot = os.path.join(root, 'originalPics') eroot = os.path.join(root, 'FDDB-folds') pattern = '-ellipseList.txt' c = 0 X, y = [], [] for path, subdirs, files in os.walk(eroot): for fname in files: if fname.find(pattern) > 0: fpath = os.path.join(path, fname) print(fpath) with open(fpath) as f: lines = sp.array(f.readlines()) paths_indx = sp.where( [line.find('/') > 0 for line in lines])[0] counts_indx = paths_indx + 1 paths = sp.array( [e.strip() for e in lines[paths_indx]]) ellipces = [] for i in counts_indx: cnt = int(lines[i]) ellipces.append(lines[i + 1:i + cnt + 1]) ellipces = [[[float(num) for num in line.split()[:-1]] for line in e] for e in ellipces] ellipces = sp.array(ellipces) for iname, ells in zip(paths[:], ellipces[:]): ppath = os.path.join(iroot, iname.replace( '/', '\\')) + '.jpg' file_id = iname.split('/')[-1] frame = fr.get_frame(ppath) for item in ells: ra, rb, theta, x, y = item x1, y1, x2, y2 = util.ellipse2bbox(a=ra, b=rb, angle=theta, cx=x, cy=y) x = x1 y = y1 h = abs(y2 - y1) w = abs(x2 - x1) print(file_id, (y, x, h, w)) non_neg = x > 0 and y > 0 if not non_neg: continue if on_drive: for item in Datasets.data_augmentation( frame, y, x, w, h): fr.write_frame( 'F:\\train_data\\pos\\' + str(c) + '_' + str(file_id) + '_pos', item) c += 1 else: pass X = sp.array(X) y = sp.ones(len(X)) return X, y