def displayCards(frame, rider, deckSize, discard, played): subFrames = Frames(frame) displayRider(subFrames.new(), rider) framesLine = subFrames.newLine(3) displayDeck(framesLine[0], deckSize) displayDiscard(framesLine[1], discard, rider.color) displayPlayed(framesLine[2], sorted(played))
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_train_non_face_data(k = 12,write_to_disk = 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 = [] root = 'F:\\Datasets\\image_data_sets\\non-faces' pattern = "*.jpg" 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] non_face = shuffle(fr.split_frame(img,wshape=(k,k)),random_state=42)[:25] for e in non_face: X.append(fr.frame_to_vect(e)) X = sp.array(X) y = sp.zeros(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 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 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_train_non_face_data(k=12, write_to_disk=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 = [] root = 'F:\\Datasets\\image_data_sets\\non-faces' pattern = "*.jpg" 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] non_face = shuffle(fr.split_frame(img, wshape=(k, k)), random_state=42)[:25] for e in non_face: X.append(fr.frame_to_vect(e)) X = sp.array(X) y = sp.zeros(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 main(): window = tk.Tk() window.title("Library") frames = Frames(window) reliefs(frames.new()) showColors(frames.new()) window.bind("<space>", lambda e: window.destroy()) window.mainloop()
def astropy_fits(filename_in, filename_out): frame = cvtColor(Frames.read_image(filename_in), COLOR_RGB2BGR) cv2.imshow('Example - Show image in window', frame) cv2.waitKey(0) cv2.destroyAllWindows() Frames.save_image(filename_out, cvtColor(frame, COLOR_BGR2RGB), color=True)
def apply_12_net(image, iname, nn, p_level=16, k=1.18, debug=1): py = fr.get_frame_pyramid(image, k=k, t=p_level) suff = nn.nn_name[:2] patch_size = int(suff) rows, cols, d = image.shape s = time() rbb = [] ## check last k levels k = 1 t_level = p_level - k for m, frame in enumerate(py): if m < t_level: continue H, W, dim = frame.shape X_test = [] pnt = [] for u in range(0, H - patch_size + 1, 4): for v in range(0, W - patch_size + 1, 4): subframe = fr.get_patch(frame, u, v, (patch_size, patch_size)) #subframe = frame[y:y+h,x:x+w] X_test.append(fr.frame_to_vect(subframe)) pnt.append((v, u)) pnt = sp.array(pnt) X_test = sp.array(X_test) pred = nn.predict(X_test) pnt = pnt[pred == 1] if len(pred[pred == 1]) == 0: print('no detection on', str(m)) continue else: print('detection on', str(m)) bb = [] for p in pnt: i = p[0] j = p[1] if debug: cv2.rectangle(frame, (i, j), (i + patch_size, j + patch_size), (255, 0, 255), 1) bb.append([i, j, i + patch_size, j + patch_size]) for e in bb: x1, y1, x2, y2 = e ox1 = math.floor(image.shape[1] * (x1 / frame.shape[1])) oy1 = math.floor(image.shape[0] * (y1 / frame.shape[0])) ox2 = math.floor(image.shape[1] * (x2 / frame.shape[1])) oy2 = math.floor(image.shape[0] * (y2 / frame.shape[0])) if m >= p_level - k: rbb.append([ox1, oy1, ox2, oy2]) if debug: cv2.imwrite( iname[:-4] + '_pipeline_' + 'lev' + str(m) + '_' + nn.nn_name + '_.jpg', frame) f = time() print('time is:', f - s) return sp.array(rbb)
def __init__(self): """ Set all variables to its default values """ self.total_inst = 0 self.stat_vars = 0 self.instructions = [] self.labels = [] self.calls = [] self.names_pattern = re.compile("[^A-ZÁ-Ža-zá-ž0-9\-\*\$%_&]") self.frames = Frames() self.variables_factory = VariablesFactory(self.frames)
def get_train_wider_calib_data(n=None, k=12): ''' for calibration net return X - features y - labels cnt - count of examples ''' X, y = [], [] sn = (0.83, 0.91, 1.0, 1.10, 1.21) xn = (-0.17, 0.0, 0.17) yn = (-0.17, 0.0, 0.17) prod = [e for e in itertools.product(sn, xn, yn)] inv_calib = lambda i, j, h, w, n: [ round(i - (-prod[n][1] * w / (prod[n][0]**-1))), round(j - (-prod[n][2] * h / (prod[n][0]**-1))), round(h / prod[n][0]**-1), round(w / prod[n][0]**-1) ] suff = str(k) X_name = 'train_data_icalib_' + suff + '.npy' y_name = 'labels_icalib_' + suff + '.npy' root = 'F:\\Datasets\\image_data_sets\\faces\\WIDERFace\\' pattern = "*.jpg" bboxs = Datasets.load_wider_face( os.path.join(root, 'wider_face_split', 'wider_face_train_v7.mat')) for path, subdirs, files in os.walk(root, 'WIDER_train'): for iname in files: if fnmatch(iname, pattern): ipath = os.path.join(path, iname) img = fr.get_frame(ipath) H, W = img.shape[:2] bbox_list = bboxs[iname[:-4]] for bbox in bbox_list: label = sp.random.randint(0, 45) i, j, h, w = [ int(e) for e in inv_calib(bbox[1], bbox[0], bbox[2], bbox[3], label) ] face = fr.get_patch(img, i, j, (h, w)) face_r, good_example = Datasets.sample_resize(face, k) if good_example: #print('orig:',bbox[1],bbox[0],bbox[2],bbox[3]) #print('inv_calib:',i,j,h,w) vec_icalib = fr.frame_to_vect(face_r) X.append(vec_icalib) y.append(label) print('face calib:', label, ipath) y = sp.array(y) sp.save(y_name, y) X = sp.array(X) sp.save(X_name, X) return X, y
def save_version(self): """ save the result as 16bit png, tiff or fits file at the standard location. :return: - """ Frames.save_image(self.postproc_data_object.file_name_processed, self.postproc_data_object.versions[ self.postproc_data_object.version_selected].image, color=self.postproc_data_object.color, avoid_overwriting=False, header=self.pss_version)
def apply_12_net(image,iname,nn,p_level = 16,k = 1.18,debug = 1): py = fr.get_frame_pyramid(image,k=k,t=p_level) suff = nn.nn_name[:2] patch_size = int(suff) rows,cols,d = image.shape s = time() rbb = [] ## check last k levels k = 1 t_level = p_level-k for m,frame in enumerate(py): if m < t_level: continue H,W,dim = frame.shape X_test = [] pnt = [] for u in range(0,H - patch_size + 1,4): for v in range(0,W - patch_size + 1,4): subframe = fr.get_patch(frame,u,v,(patch_size,patch_size)) #subframe = frame[y:y+h,x:x+w] X_test.append(fr.frame_to_vect(subframe)) pnt.append((v,u)) pnt = sp.array(pnt) X_test = sp.array(X_test) pred = nn.predict(X_test) pnt = pnt[pred==1] if len(pred[pred==1]) == 0: print('no detection on',str(m)) continue else: print('detection on',str(m)) bb = [] for p in pnt: i = p[0] j = p[1] if debug: cv2.rectangle(frame, (i, j), (i+patch_size, j+patch_size), (255,0,255), 1) bb.append([i,j,i+patch_size,j+patch_size]) for e in bb: x1,y1,x2,y2 = e ox1 = math.floor(image.shape[1] * (x1/frame.shape[1])) oy1 = math.floor(image.shape[0] * (y1/frame.shape[0])) ox2 = math.floor(image.shape[1] * (x2/frame.shape[1])) oy2 = math.floor(image.shape[0] * (y2/frame.shape[0])) if m >= p_level - k: rbb.append([ox1,oy1,ox2,oy2]) if debug: cv2.imwrite(iname[:-4]+'_pipeline_' + 'lev'+str(m) + '_' + nn.nn_name + '_.jpg',frame) f = time() print('time is:',f-s) return sp.array(rbb)
def apply_12_net(image,frame,iname,m,nn,debug = 0): suff = nn.nn_name[:2] patch_size = int(suff) if debug: s = time() rbb = [] H,W,dim = frame.shape X_test = [] pnt = [] for u in range(0,H - patch_size + 1,4): for v in range(0,W - patch_size + 1,4): subframe = fr.get_patch(frame,u,v,(patch_size,patch_size)) X_test.append(fr.frame_to_vect(subframe)) pnt.append((v,u)) pnt = sp.array(pnt) X_test = sp.array(X_test) proba = nn.predict_proba(X_test) pred = nn.predict(X_test) pnt = pnt[pred==1] pos_proba = proba[pred==1][:,1] if debug: if len(pnt) == 0: print('no detection on',str(m)) else: print('detection on',str(m),frame.shape) bb = [] for p in pnt: i = p[0] j = p[1] bb.append([i,j,i+patch_size,j+patch_size]) bb = sp.array(bb) for e in bb: x1,y1,x2,y2 = e ratio_x = image.shape[1]/frame.shape[1] ratio_y = image.shape[0]/frame.shape[0] ox1 = int(round(x1 * ratio_x)) oy1 = int(round(y1 * ratio_y)) ox2 = int(round(x2 * ratio_x)) oy2 = int(round(y2 * ratio_y)) rbb.append([ox1,oy1,ox2,oy2]) if debug: f = time() print('Finish... time is:',f-s) return (sp.array(rbb),pos_proba)
def get_train_calib_data(k = 12): ''' for calibration net return X - features y - labels cnt - count of examples ''' sp.random.seed(42) X_data,y_data = [],[] suff = str(k) c = 0 X_name = 'train_data_icalib_'+ suff + '.npz' y_name = 'labels_icalib_'+ suff + '.npz' label = -1 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) 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)) #fr.write_frame('F:\\1\\' + str(c) + 'orig',face) c += 1 for ((new_y,new_x,new_w,new_h),label) in [(util.calib(y,x,w,h,k),k) for k in sp.random.randint(0,45,5)]: face = fr.get_patch(frame,new_y,new_x,(new_w,new_h)) no_neg_calib = sp.all(sp.array([new_x,new_y,new_h,new_w]) > 0) face_r,good_example = Datasets.sample_resize(face,k,k) if good_example and no_neg_calib: #fr.write_frame('F:\\1\\' + str(c) + 'calib_'+str(label) ,face) print('face:',fpath,label) vec = fr.frame_to_vect(face_r) X_data.append(vec) y_data.append(label) y_data = sp.array(y_data) sp.savez(y_name,y_data) X_data = sp.array(X_data) sp.savez(X_name,X_data) return X_data,y_data
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 execute_save_postprocessed_image(self, postprocessed_image): # The signal payload is None only if the editor was left with "cancel" in interactive mode. # In this case, skip saving the result and proceed with the next job. if postprocessed_image is not None: self.set_status_bar_processing_phase("saving result") # Save the image as 16bit int (color or mono). if self.configuration.global_parameters_protocol_level > 0: Miscellaneous.protocol( "+++ Start saving the postprocessed image +++", self.attached_log_file) self.my_timer.create_no_check('Saving the postprocessed image') Frames.save_image( self.postprocessed_image_name, postprocessed_image, color=(len(postprocessed_image.shape) == 3), avoid_overwriting=False, header=self.configuration.global_parameters_version) self.my_timer.stop('Saving the postprocessed image') if self.configuration.global_parameters_protocol_level > 1: Miscellaneous.protocol( " The postprocessed image was written to: " + self.postprocessed_image_name, self.attached_log_file, precede_with_timestamp=False) if self.configuration.global_parameters_protocol_level > 1: Miscellaneous.print_postproc_parameters( self.configuration.postproc_data_object.versions[ self.configuration.postproc_data_object. version_selected].layers, self.attached_log_file) self.work_next_task_signal.emit("Next job") # Print timing info for this job. self.my_timer.stop('Execution over all') if self.configuration.global_parameters_protocol_level > 0: self.my_timer.protocol(self.attached_log_file) if self.attached_log_file: self.attached_log_file.close() # If the attached log name was defined for a stacking job, rename it to include # parameter information. if self.attached_log_name_new and self.attached_log_name_new != self.attached_log_name: try: remove(self.attached_log_name_new) except: pass rename(self.attached_log_name, self.attached_log_name_new) self.attached_log_name = self.attached_log_name_new
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]
class VisualTester(): def __init__(self, window): self.frames = Frames(window) window.bind("<space>", lambda e: window.destroy()) def __before__(self): self.frame = self.frames.new()
def main(): iname = 'img/test/1226.jpg' nn12 = Cnnl('12-net').load_model('12-net_lasagne_.pickle') nn_calib12 = Cnnl('12-calib_net').load_model( '12-calib_net_lasagne_.pickle') nn24 = Cnnl(nn_name='24-net', subnet=nn12).load_model('24-net_lasagne_.pickle') nn_calib24 = Cnnl('24-calib_net').load_model( '24-calib_net_lasagne_.pickle') nn48 = Cnnl(nn_name='48-net', subnet=nn24).load_model('48-net_lasagne_.pickle') nn_calib48 = Cnnl('48-calib_net').load_model( '48-calib_net_lasagne_.pickle') image = fr.get_frame(iname) p = 16 k = 1.18 bb = apply_12_net(image=image, iname=iname, nn=nn12, p_level=p, k=k, debug=0) bb = apply_calib_net(image=image, iname=iname, bb=bb, nn=nn_calib12) bb = util.nms(bb, T=0.8) bb = apply_24_net(image=image, iname=iname, bb=bb, nn=nn24) bb = apply_calib_net(image=image, iname=iname, bb=bb, nn=nn_calib24) bb = util.nms(bb, T=0.8) bb = apply_48_net(image=image, iname=iname, bb=bb, nn=nn48) bb = util.nms(bb, T=0.3) bb = apply_calib_net(image=image, iname=iname, bb=bb, nn=nn_calib48) draw_bboxes(iname, bb)
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 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 get_train_face_wider_data(k=12, write_to_disk=False): ''' cut faces (positive examples) by bboxes from all images in dataset return X - features y - labels cnt - count of examples ''' X, y = [], [] root = 'F:\\Datasets\\image_data_sets\\faces\\WIDERFace\\' pattern = "*.jpg" bboxs = Datasets.load_wider_face( os.path.join(root, 'wider_face_split', 'wider_face_train_v7.mat')) for path, subdirs, files in os.walk(root, 'WIDER_train'): for indx, iname in enumerate(files): if fnmatch(iname, pattern): ipath = os.path.join(path, iname) print('face:', ipath) img = fr.get_frame(ipath) H, W, dim = img.shape bbox_list = bboxs[iname[:-4]] for bbox in bbox_list: face = fr.get_patch(img, bbox[1], bbox[0], (bbox[2], bbox[3])) #fr.write_frame('F:\\1\\' + str(c),face) face_r, good_example = Datasets.sample_resize( face, k, k) if good_example: vec = fr.frame_to_vect(face_r) X.append(vec) y.append(1) face_r_flip = fr.flip_frame(face_r) vec = fr.frame_to_vect(face_r_flip) X.append(vec) y.append(1) X = sp.array(X) y = sp.array(y) #y = sp.ones(len(X)) return X, y
def get_train_wider_calib_data(n = None,k = 12): ''' for calibration net return X - features y - labels cnt - count of examples ''' X,y = [],[] sn = (0.83, 0.91, 1.0, 1.10, 1.21) xn = (-0.17, 0.0, 0.17) yn = (-0.17, 0.0, 0.17) prod = [e for e in itertools.product(sn,xn,yn)] inv_calib = lambda i,j,h,w,n: [ round(i-(-prod[n][1]*w/(prod[n][0]**-1))),round(j-(-prod[n][2]*h/(prod[n][0]**-1))),round(h/prod[n][0]**-1),round(w/prod[n][0]**-1) ] suff = str(k) X_name = 'train_data_icalib_'+ suff + '.npy' y_name = 'labels_icalib_'+ suff + '.npy' root = 'F:\\Datasets\\image_data_sets\\faces\\WIDERFace\\' pattern = "*.jpg" bboxs = Datasets.load_wider_face(os.path.join(root,'wider_face_split','wider_face_train_v7.mat')) for path, subdirs, files in os.walk(root,'WIDER_train'): for iname in files: if fnmatch(iname, pattern): ipath = os.path.join(path, iname) img = fr.get_frame(ipath) H,W = img.shape[:2] bbox_list = bboxs[iname[:-4]] for bbox in bbox_list: label = sp.random.randint(0,45) i,j,h,w = [int(e) for e in inv_calib(bbox[1],bbox[0],bbox[2],bbox[3],label)] face = fr.get_patch(img,i,j,(h,w)) face_r,good_example = Datasets.sample_resize(face,k) if good_example: #print('orig:',bbox[1],bbox[0],bbox[2],bbox[3]) #print('inv_calib:',i,j,h,w) vec_icalib = fr.frame_to_vect(face_r) X.append(vec_icalib) y.append(label) print('face calib:',label,ipath) y = sp.array(y) sp.save(y_name,y) X = sp.array(X) sp.save(X_name,X) return X,y
def __init__(self, program_list): # seznam instrukci vstupniho programu self.program_list = program_list # pocet vsech vykonanych instrukci # (muze byt vyssi nez pocet instrukci ve vstupnim programu) self.ins_cntr = 0 self.prog_cntr = 0 # poradi prave prochazene instrukce od 0 # seznam slovniku promennych v ramci GF, # docasne tam budou vsechny promenne (TF i LF) # slovnik promennych - jmeno:hodnota self.frames = Frames(self.prog_cntr) self.label_dict = {} # jmeno : poradi_instrukce self.data_stack = [] # stack pro POPS a PUSHS self.call_stack = [] # promenna pro skoky # - interpret projde znovu program od mista, kam se skoci self.again = True
def save_version_as(self): """ save the result as 16bit png, tiff or fits file at a location selected by the user. :return: - """ options = QtWidgets.QFileDialog.Options() filename, extension = QtWidgets.QFileDialog.getSaveFileName(self, "Save result as 16bit png, tiff or fits image", self.postproc_data_object.file_name_processed, "Image Files (*.png *.tiff *.fits)", options=options) if filename and extension: Frames.save_image(filename, self.postproc_data_object.versions[ self.postproc_data_object.version_selected].image, color=self.postproc_data_object.color, avoid_overwriting=False, header=self.pss_version)
def draw_bboxes(iname,bb): debug = 1 image = fr.get_frame(iname) marked_image = image.copy() for box in bb: x1,y1,x2,y2 = box cv2.rectangle(marked_image, (x1, y1), (x2, y2), (64,0,192), 1) if debug: cv2.imwrite('result'+'_pipeline_rescale_image_' + '.jpg',marked_image) else: sns.plt.imshow(marked_image)
def get_train_face_wider_data(k = 12,write_to_disk = False): ''' cut faces (positive examples) by bboxes from all images in dataset return X - features y - labels cnt - count of examples ''' X,y = [],[] root = 'F:\\Datasets\\image_data_sets\\faces\\WIDERFace\\' pattern = "*.jpg" bboxs = Datasets.load_wider_face(os.path.join(root,'wider_face_split','wider_face_train_v7.mat')) for path, subdirs, files in os.walk(root,'WIDER_train'): for indx,iname in enumerate(files): if fnmatch(iname, pattern): ipath = os.path.join(path, iname) print('face:',ipath) img = fr.get_frame(ipath) H,W,dim = img.shape bbox_list = bboxs[iname[:-4]] for bbox in bbox_list: face = fr.get_patch(img,bbox[1],bbox[0],(bbox[2],bbox[3])) #fr.write_frame('F:\\1\\' + str(c),face) face_r,good_example = Datasets.sample_resize(face,k,k) if good_example: vec = fr.frame_to_vect(face_r) X.append(vec) y.append(1) face_r_flip = fr.flip_frame(face_r) vec = fr.frame_to_vect(face_r_flip) X.append(vec) y.append(1) X = sp.array(X) y = sp.array(y) #y = sp.ones(len(X)) return X,y
def start(self, video, input_path, out_path, frames_video_path, t, k, h): """ :param video: Ruta del vídeo :param input_path: Ruta donde se generarán los fotogramas del video :param out_path: Ruta donde se añadirán los fotogramas clave del video :param frames_video_path: Ruta donde se añadirán los fotogramas para generar el video resumen :param t: Número de fotogramas a saltar :param k: Número de grupos para k-means :param h: Tamaño del histográma generado """ # Inicializamos la clase Frames frames = Frames() # Obtenemos los fotogramas del video frames.get_frames_from_video(video, input_path) # Obtenemos fotogramas clave y fotogramas para generar el video key_frames, frames_video = self.__get_key_frames(input_path, t, k, h) # Movemos los fotogramas a sus respectivas carpetas frames.move_key_frames(input_path, out_path, key_frames) frames.move_key_frames(input_path, frames_video_path, frames_video) # Generamos el video resumen frames.get_video_from_frames(frames_video_path)
def draw_bboxes(iname, bb): debug = 1 image = fr.get_frame(iname) marked_image = image.copy() for box in bb: x1, y1, x2, y2 = box cv2.rectangle(marked_image, (x1, y1), (x2, y2), (64, 0, 192), 1) if debug: cv2.imwrite('result' + '_pipeline_rescale_image_' + '.jpg', marked_image) else: sns.plt.imshow(marked_image)
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 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 draw_bboxes(iname,bb,c = 42,debug=0): image = fr.get_frame(iname) marked_image = image.copy() for box in bb: x1,y1,x2,y2 = box cv2.rectangle(marked_image, (x1, y1), (x2, y2), (0,0,255), 1) if debug: cv2.imwrite('pipeline_' + str(c) + '.jpg',marked_image) else: marked_image = marked_image[...,::-1] sns.plt.grid(False) sns.plt.imshow(marked_image)
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)
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 __init__(self, datadir, addiliRecords=True, addWictionary=True): """ The Germanet object is initialized with the directory where the Germanet data is stored. The data is loaded when Germanet is initialized. :param datadir: [String] The path to the directory where the Germanet data is stored :param addiliRecords: a boolean, denotes whether the iliRecords should also be loaded into the Germanet object, default: True :param addWictionary: a boolean, denotes whether the wictionary files should also be loaded into the Germanet object, default: True """ self.datadir = datadir self.addiliRecords = addiliRecords self.addWictionary = addWictionary # Dictionary: lexunit id - lexunit object self._lexunits = {} # Dictionary: synset id - synset object self._synsets = {} # Dictionary: any orthform (all variants) - lexunit id self._orthform2lexid = defaultdict(set) # Dictionary: main orthform - lexunit id self._mainOrtform2lexid = defaultdict(set) # Dictionary: lower cased orht form (all variants) - lexunit id self._lowercasedform2lexid = defaultdict(set) # Dictionary: Wordcategory - set of lexunit ids self._wordcat2lexid = defaultdict(set) # Dictionary: Wordclass - set of lexunit ids self._wordclass2lexid = defaultdict(set) # Set if synsets (that are compounds) self._compounds = set() # Dictionary: Frame - Lexunit objects self._frames2lexunits = defaultdict(set) # List: wictionary entries self._wiktionary_entries = [] # List: ili Records self._ili_records = [] self.load_data() self._frames = Frames(self._frames2lexunits)
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 __init__(self, manufacturer): # the names list contains model from Specialized bikes as I am a huge fan of its bikes (I own one! :-)) names = [ "Pitch", "Epic", "Source", "Enduro", "Fatboy", "Status", "Demo", "Tarmac", "Allez", "Venge", "Shiv", "Roubaix", "Secteur", "Diverge", "Awol", "Crux", "Langster", "Sirrus", "Daily", "Crosstail", "CossRoads", "Expedition" ] self.frame = Frames(1) self.wheels = Wheels(1) self.manufacturer = manufacturer self.model_name = choice(names) self.weight = (self.frame.frame_weight + 2 * self.wheels.wheel_weight) / 1000.0 self.cost = self.frame.frame_cost + 2 * self.wheels.wheel_cost
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 data_augmentation(frame,y,x,w,h): face = fr.get_patch(frame,y,x,(w,h)) face_flip = fr.flip_frame(face) t1 = sp.random.randint(0,3) t2 = sp.random.randint(0,3) face_narrow = fr.get_patch(frame,y,x,(w-t2,h-t2)) face_wide = fr.get_patch(frame,y,x,(w+t2,h+t2)) t1 = sp.random.randint(0,3) t2 = sp.random.randint(0,3) face_shift1 = fr.get_patch(frame,y+t1,x+t1,(w+t2,h+t2)) face_shift2 = fr.get_patch(frame,y-t1,x-t1,(w-t2,h-t2)) th = float((1 if sp.random.randint(0,2) % 2 == 0 else -1) * sp.random.randint(45,90)) face_rot = fr.frame_rotate(face,theta = th) faces_list = filter(lambda x: x != None,[face,face_flip,face_narrow,face_wide,face_shift1,face_shift2,face_rot]) return faces_list
def __init__(self, db_conn, db_name): from pymongo import Connection print "Opening MongoDB connection" self.conn = Connection(host=db_conn) self.db = self.conn[db_name] # Open subcollections self.knowledge = Knowledge(self) self.frames = Frames(self) #self.map = Map(self) self.geo = Geo(self) # Logging from wifistalker import Log header = 'DB' self.log = Log(self, use_stdout=True, header=header) # Log collection self._log = self['log'] self._log.ensure_index('stamp_utc', expireAfterSeconds=60*60)
def main(): iname = 'img/test/1226.jpg' nn12 = Cnnl('12-net').load_model('12-net_lasagne_.pickle') nn_calib12 = Cnnl('12-calib_net').load_model('12-calib_net_lasagne_.pickle') nn24 = Cnnl(nn_name = '24-net',subnet=nn12).load_model('24-net_lasagne_.pickle') nn_calib24 = Cnnl('24-calib_net').load_model('24-calib_net_lasagne_.pickle') nn48 = Cnnl(nn_name = '48-net',subnet=nn24).load_model('48-net_lasagne_.pickle') nn_calib48 = Cnnl('48-calib_net').load_model('48-calib_net_lasagne_.pickle') image = fr.get_frame(iname) p = 16 k = 1.18 bb = apply_12_net(image = image,iname = iname,nn = nn12,p_level=p,k=k,debug=0) bb = apply_calib_net(image = image, iname = iname,bb=bb,nn=nn_calib12) bb = util.nms(bb,T=0.8) bb = apply_24_net(image = image, iname = iname, bb = bb,nn = nn24 ) bb = apply_calib_net(image = image, iname = iname,bb = bb,nn=nn_calib24) bb = util.nms(bb,T=0.8) bb = apply_48_net(image = image, iname = iname, bb = bb,nn = nn48 ) bb = util.nms(bb,T=0.3) bb = apply_calib_net(image = image, iname = iname,bb=bb,nn=nn_calib48) draw_bboxes(iname,bb)
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 main(): iname = 'img/test/p1.jpg' nn12 = Cnnl('12-net').__load_model_old__('12-net_lasagne_.pickle') nn24 = Cnnl(nn_name = '24-net',subnet=nn12).__load_model_old__('24-net_lasagne_.pickle') nn48 = Cnnl(nn_name = '48-net',subnet=nn24).__load_model_old__('48-net_lasagne_.pickle') nn_calib12 = Cnnl('12-calib_net').__load_model_old__('12-calib_net_lasagne_.pickle') nn_calib24 = Cnnl('24-calib_net').__load_model_old__('24-calib_net_lasagne_.pickle') #nn_calib48 = Cnnl('48-calib_net').__load_model_old__('48-calib_net_lasagne_.pickle') image = fr.get_frame(iname) p = 11 k = 1.41 t_level = 0 gbb = [] gp = [] print('Start....') s = time() py = fr.get_frame_pyramid(image,scale=k,steps=p) for m,frame in enumerate(py): if m < t_level: continue bb,p = apply_12_net(image = image,frame=frame,iname = iname,nn = nn12,m=m,debug=0) keep = util.nms(bb,p,T=0.5) bb = bb[keep] if keep != [] else bb p = p[keep] if keep != [] else p cnt_detect = len(bb) if cnt_detect == 0: continue #draw_bboxes(iname,bb,c='#-r_12--' + str(m)) for b in bb: gbb.append(b) for pp in p: gp.append(pp) gbb = sp.array(gbb) gp = sp.array(gp) #draw_bboxes(iname,gbb,c='0-r_12') bb = gbb p = gp bb = apply_calib_net(image = image, iname = iname,bb=bb,nn=nn_calib12) cnt_detect = len(bb) #draw_bboxes(iname,bb,c='1-r_calib_12') keep = util.nms(bb,p,T=0.3) bb = bb[keep] if keep != [] else bb p = p[keep] if keep != [] else p #draw_bboxes(iname,bb,c='2-r_nms_12') bb,p = apply_24_48_net(image = image, iname = iname, bb = bb,nn = nn24,debug=0 ) cnt_detect = len(bb) #draw_bboxes(iname,bb,c='3-r_24') bb = apply_calib_net(image = image, iname = iname,bb = bb,nn=nn_calib24) cnt_detect = len(bb) #draw_bboxes(iname,bb,c='4-r_calib_24') keep = util.nms(bb,p,T=0.3) bb = bb[keep] if keep != [] else bb p = p[keep] if keep != [] else p #draw_bboxes(iname,bb,c='5-r_nms_24') cnt_detect = len(bb) bb,p = apply_24_48_net(image = image, iname = iname, bb = bb,nn = nn48,debug=1) cnt_detect = len(bb) draw_bboxes(iname,bb,c='6-r_48',debug=1) # bb = apply_calib_net(image = image,iname = iname,bb=bb,nn=nn_calib48) # draw_bboxes(iname,bb,c='6-r_calib_48') # # bb = util.nms(bb,T=0.2) # draw_bboxes(iname,bb,c='7-r_nms_48_final') print('Finish...',time()-s)