def RestTest(): l_2w = ReadFile(data_path + 'train_2w.txt') l_all = ReadFile(data_path + 'list.txt') te_out = open(data_path + 'test_rest.txt', 'w') tr_imgs_path = [] te_imgs_path = [] for ind, line in enumerate(l_2w): tr_imgs_path.append(line.split(' ')[0].split('/')[-1].split('.')[0]) print str(len(tr_imgs_path)) for l in l_all: if l + '_128' not in tr_imgs_path: im_path = img_path + l + '_128.jpg' p3_path = para_path + l + '_pose.mat' Euler = sio.loadmat(p3_path)['Euler_Para'][0] pitch = degrees(Euler[0]) yaw = degrees(Euler[1]) roll = degrees(Euler[2]) roll = roll - align_degree[prefix.index(l)] - disturb_degree[ prefix.index(l)] te_imgs_path.append(l) te_out.writelines(im_path + ' ' + str(pitch) + ' ' + str(yaw) + ' ' + str(roll)) te_out.write('\n') print str(len(te_imgs_path)) te_out.close()
def invFile(filenameIn, filenameOut): print " -- Checking if we need to invert?", # Find the amount of translation we need to do! data = ReadFile(filenameIn) dataLines = [l for l in data.split("\n") if l.strip() and not l.strip().startswith("#") ] CommentLines = [l for l in data.split("\n") if l.strip() and l.strip().startswith("#") ] xmin,xmax = None,None outData = CommentLines for l in dataLines: i,t,x,y,z,X,Y,Z,p = extractXYZLine(l) xmin = min( [xmin,x,X] ) if xmin != None else min( [x,X] ) xmax = max( [xmax,x,X] ) if xmax != None else max( [x,X] ) if not( xmin<=0 and xmax <=0): print " No, xmin,max:", xmin,xmax WriteFile(filenameOut, data) else: print " Yes, xmin,max:", xmin,xmax # Ok, we have no positive x's: outData = CommentLines for l in dataLines: i,t,x,y,z,X,Y,Z,p = extractXYZLine(l) x = x * -1 X = X * -1 outData.append( "\t".join( [str(g) for g in [i,t,x,y,z,X,Y,Z,p] ] ) ) WriteFile( filenameOut, "\n".join( outData) )
def nrnXYZToSWC(srcFilename, sinkFilename): datestr = datetime.datetime.now().strftime("%d-%B-%Y") print " -- Converting to SWC" # Find the amount of translation we need to do! data = ReadFile(srcFilename) dataLines = [l for l in data.split("\n") if l.strip() and not l.strip().startswith("#") ] CommentLines = [l for l in data.split("\n") if l.strip and l.strip().startswith("#") ] outData = CommentLines + ["# Converted from xyz to swc using nrnXYZtoSWC.py" + " on: %s" %datestr] #["# Translated by (%d,%d,%d) using nrnTranslate.py"%translation + " On: %s"%datestr] + ["# Corrected for shrinkage (x1.28) using nrnScaled.py" + " On: %s"%datestr] + ["# Files Concatenated by nrnConcatenate.py" + " on: %s"%datestr] + ["# Converted from xyz to swc using nrnXYZtoSWC.py" + " on: %s" %datestr] for l in dataLines: i,t,x,y,z,X,Y,Z,p = extractXYZLine(l) sX = (float(x)+float(X)) / 2.0 sY = (float(y)+float(Y)) / 2.0 sZ = (float(z)+float(Z)) / 2.0 dX = float(x) - float(X) dY = float(y) - float(Y) dZ = float(z) - float(Z) d = math.sqrt( dX**2 + dY**2 + dZ**2 ) r = d / 2.0 r = r if r > 0.15 else 0.15 outData.append( "\t".join( [str(g) for g in [i,t,sX,sY,sZ,r,p] ] ) ) WriteFile( sinkFilename, "\n".join( outData) )
def nrnScale(srcFilename, sinkFilename, scaleFactor): datestr = datetime.datetime.now().strftime("%d-%B-%Y") # Find the amount of translation we need to do! data = ReadFile(srcFilename) dataLines = [l for l in data.split("\n") if l.strip() and not l.strip().startswith("#") ] CommentLines = [l for l in data.split("\n") if l.strip() and l.strip().startswith("#") ] outData = CommentLines + ["# Corrected for shrinkage (x1.28) using nrnScaled.py" + " On: %s"%datestr] for l in dataLines: i,t,x,y,z,X,Y,Z,p = extractXYZLine(l) x *= scaleFactor y *= scaleFactor z *= scaleFactor X *= scaleFactor Y *= scaleFactor Z *= scaleFactor outData.append( "\t".join( [str(g) for g in [i,t,x,y,z,X,Y,Z,p] ] ) ) WriteFile( sinkFilename, "\n".join( outData) )
def nrnTranslate( nsFilenameSrc, nrnFilenameSrc,nsFilenameSink, nrnFilenameSink): print " -- Finding Translation" nsData = ReadFile(nsFilenameSrc) # Find the amount of translation we need to do! dataLines = [l for l in nsData.split("\n") if l.strip() and not l.strip().startswith("#") ] tPoint = None for l in dataLines: i,t,x,y,z,X,Y,Z,p = extractXYZLine(l) if p==-1 and t==9: assert not tPoint tPoint = (-1.0*x,-1.0*y,-1.0*z) assert tPoint print " -- Translating by", tPoint TranslateFile(nsFilenameSrc, nsFilenameSink, tPoint) TranslateFile(nrnFilenameSrc, nrnFilenameSink, tPoint)
def CheckInFile(file_name, Checker): lines = ReadFile(file_name) comment_lines = Checker.GetCommentLines(lines) single_line_comments = Checker.GetSingleLineComments(lines) block_comment_lines = Checker.GetBlockCommentLines(lines) total_block_comments = Checker.CountBlockComments(lines) total_TODOs = Checker.CountTODOs(lines) print("Total # of lines: ", len(lines)) print("Total # of comment lines: ", len(comment_lines)) print("Total # of single line comments: ", len(single_line_comments)) print("Total # of comment lines within block comments: ", len(block_comment_lines)) print("Total # of block line comments: ", total_block_comments) print("Total # of TODO's: ", total_TODOs)
def TranslateFile(filenameIn, filenameOut, translation): datestr = datetime.datetime.now().strftime("%d-%B-%Y") # Find the amount of translation we need to do! data = ReadFile(filenameIn) dataLines = [l for l in data.split("\n") if l.strip() and not l.strip().startswith("#") ] CommentLines = [l for l in data.split("\n") if l.strip() and l.strip().startswith("#") ] # copies comment lines and adds comment line showing the processing that's been done plus date. #outData = CommentLines + ["# Translated by (%d,%d,%d) using nrnTranslate.py"%translation] + ["# On: %s"%datestr] outData = CommentLines + ["# Translated by (%d,%d,%d) using nrnTranslate.py"%translation + " On: %s"%datestr] #outData = outData + ["# On: %s"%datestr] for l in dataLines: i,t,x,y,z,X,Y,Z,p = extractXYZLine(l) x = x + translation[0] y = y + translation[0] z = z + translation[0] X = X + translation[0] Y = Y + translation[0] Z = Z + translation[0] outData.append( "\t".join( [str(g) for g in [i,t,x,y,z,X,Y,Z,p] ] ) ) WriteFile( filenameOut, "\n".join( outData) )
def SplitTxt(): lines = ReadFile(data_path + 'test_3w6.txt') new_ind = np.random.permutation(len(lines)).tolist() new_lines = [] for ind in new_ind: new_lines.append(lines[ind]) tr_num = 20000 tr_lines = new_lines[:tr_num] te_lines = new_lines[tr_num:] out_tr = open(data_path + 'train_rest_2w.txt', 'w') out_te = open(data_path + 'test_rest_1w6.txt', 'w') for tr in tr_lines: out_tr.writelines(tr) out_tr.write('\n') out_tr.close() for te in te_lines: out_te.writelines(te) out_te.write('\n') out_te.close()
def nrnConcatenateXYZS( srcFilename1, srcFilename2, sinkFilename): datestr = datetime.datetime.now().strftime("%d-%B-%Y") print " -- Concatentating Files" D1 = ReadFile(srcFilename1) D2 = ReadFile(srcFilename2) dataLines1 = [l for l in D1.split("\n") if l.strip() and not l.strip().startswith("#") ] dataLines2 = [l for l in D2.split("\n") if l.strip() and not l.strip().startswith("#") ] CommentLines1 = [l for l in D1.split("\n") if l.strip() and l.strip().startswith("#") ] CommentLines2 = [l for l in D2.split("\n") if l.strip() and l.strip().startswith("#") ] # copies comment lines and adds comment line showing the processing that's been done plus date. #outData = [ "#Comment From File1: %s"%srcFilename1 ] + CommentLines1 + [ "#Comment From File2: %s"%srcFilename2 ] + CommentLines2 + ["# Files Concatenated by nrnConcatenate.py" + " on: %s"%datestr] outData = CommentLines1 + CommentLines2 + ["# Files Concatenated by nrnConcatenate.py" + " on: %s"%datestr] # Remap the id's # Write Datafile 1 idMap = {} for l in dataLines1: i,t,x,y,z,X,Y,Z,p = extractXYZLine(l) new_id = len(idMap) + 1 new_p = p if p ==-1 else idMap[ (0,p) ] outData.append( "\t".join( [str(g) for g in [new_id,t,x,y,z,X,Y,Z,new_p] ] ) ) idMap[ (0,i) ] = new_id #print srcFilename2 # Write Datafile 2 for l in dataLines2: #print l #print i,t,x,y,z,X,Y,Z,p = extractXYZLine(l) new_id = len(idMap) + 1 #print p new_p = p if p ==-1 else idMap[(1,p)] outData.append( "\t".join( [str(g) for g in [new_id,t,x,y,z,X,Y,Z,new_p] ] ) ) idMap[(1,i)] = new_id WriteFile( sinkFilename, "\n".join( outData) )
def WriteHdf5(setname): IMAGE_SIZE = (128, 128) MEAN_VALUE = 0 filename = data_path + setname + '.txt' lines = ReadFile(filename) np.random.shuffle(lines) sample_size = len(lines) imgs = np.zeros(( sample_size, 3, ) + IMAGE_SIZE, dtype=np.float32) poses = np.zeros((sample_size, 3), dtype=np.float32) h5_filename = '{}.h5'.format(setname) with h5py.File(h5_filename, 'w') as h: for i, line in enumerate(lines): image_name, pitch, yaw, roll = line.split() img = cv2.imread(image_name).astype(np.float32) img = img.transpose(2, 0, 1) img = img.reshape((1, ) + img.shape) img -= MEAN_VALUE imgs[i] = img poses[i] = [float(pitch), float(yaw), float(roll)] if (i + 1) % 1000 == 0: print('processed {} images!'.format(i + 1)) h.create_dataset('data', data=imgs) h.create_dataset('pose', data=poses) print "save h5 file" with open('{}_h5.txt'.format(setname), 'w') as f: f.write(h5_filename)
net.blobs['data'].reshape(*(image.shape)) forward_kwargs = {'data': image.astype(np.float32)} blobs_out = net.forward(**forward_kwargs) euler = net.blobs['poselayer'].data[0].tolist() return euler model_dir = '/Users/momo/Desktop/Euler/Model/' prototxt = model_dir + 'resSmallV2_noBN_Ridge_bigyawpitch_731.prototxt' caffemodel = prototxt.replace('prototxt', 'caffemodel') net = caffe.Net(prototxt, caffemodel, caffe.TEST) imgSize = 128 train_names = ReadFile('/Users/momo/Desktop/MoGaze/data/train/head/img.txt') train_names = np.random.permutation(train_names) ''' #test_names = ReadFile('/Users/momo/Desktop/MoGaze/data/train/head/img.txt') f = open('./test_head_lo.txt','w') for name in train_names: ori_img = cv2.imread(name) crop_img = ori_img[ori_img.shape[0] // 2 - 200:ori_img.shape[0] // 2 + 200, ori_img.shape[1] // 2 - 200:ori_img.shape[1] // 2 + 200] euler = TestAImage(crop_img, net) euler = np.asarray([euler[1], euler[0]]).astype(np.float32) f.writelines(name.split('/')[-1].strip('.png') + '\n') f.writelines("{:.3f}".format(euler[0]))
def Gen_txt(): ''' 生成Euler回归txt的list ''' pre_list = ReadFile(data_path + 'list_96.txt') tr_num = 36000 te_num = 1000 tr_out = open(data_path + '96_train_%d.txt' % (tr_num), 'w') te_out = open(data_path + '96_test_%d.txt' % (te_num), 'w') re_out = open(data_path + '96_test_rest.txt', 'w') tr_start_ind = np.random.randint(0, len(pre_list) - tr_num - te_num) new_ind = np.random.permutation(len(pre_list)) tr_ind = new_ind[tr_start_ind:tr_start_ind + tr_num] te_ind = new_ind[tr_start_ind + tr_num:tr_start_ind + tr_num + te_num] for ti in tr_ind: p = pre_list[ti] im_path = img_path + p + '_128.jpg' p3_path = para_path + p + '_pose.mat' Euler = sio.loadmat(p3_path)['Euler_Para'][0] pitch = degrees(Euler[0]) yaw = degrees(Euler[1]) roll = degrees(Euler[2]) # ***** validation roll ********* # img = cv2.imread(im_path.replace('/input_96/', '/img/').replace('_128.jpg', '.jpg')) # cv2.imshow("ori_img", img) # # small_img_ro = cv2.imread(im_path) # # M = cv2.getRotationMatrix2D((img.shape[1] / 2, img.shape[0] / 2), roll, 1) # big_img_ro = cv2.warpAffine(img, M, (img.shape[0], img.shape[1])) # # cv2.putText(small_img_ro, str(roll), (40, 20), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, # (200, 20, 200), 1) # # cv2.imshow("ori_img_ro", big_img_ro) # cv2.imshow("small_img_ro", small_img_ro) # cv2.waitKey() # ********************************* tr_out.writelines(im_path + ' ' + str(pitch) + ' ' + str(yaw) + ' ' + str(roll)) tr_out.write('\n') tr_out.close() for te in te_ind: p = pre_list[te] im_path = img_path + p + '_128.jpg' p3_path = para_path + p + '_pose.mat' Euler = sio.loadmat(p3_path)['Euler_Para'][0] pitch = degrees(Euler[0]) yaw = degrees(Euler[1]) roll = degrees(Euler[2]) te_out.writelines(im_path + ' ' + str(pitch) + ' ' + str(yaw) + ' ' + str(roll)) te_out.write('\n') te_out.close() cou = 0 for s in new_ind: if s not in tr_ind and s not in te_ind: p = pre_list[s] im_path = img_path + p + '_128.jpg' p3_path = para_path + p + '_pose.mat' Euler = sio.loadmat(p3_path)['Euler_Para'][0] pitch = degrees(Euler[0]) yaw = degrees(Euler[1]) roll = degrees(Euler[2]) re_out.writelines(im_path + ' ' + str(pitch) + ' ' + str(yaw) + ' ' + str(roll)) re_out.write('\n') else: cou += 1 re_out.close() print " the rest number of samples : " + str(len(pre_list) - cou) print " = " + str(len(pre_list) - len(tr_ind) - len(te_ind))