def saveGraph(path, ckpt_file, img_height, img_width): tf.reset_default_graph() fh = open(path, 'rb') I = pil.open(fh) I = I.resize((img_width, img_height), pil.ANTIALIAS) I = np.array(I) sfm = SfMLearner() sfm.setup_inference(img_height, img_width, mode='depth') saver = tf.train.Saver([var for var in tf.model_variables()]) with tf.Session() as sess: saver.restore(sess, ckpt_file) pred = sfm.inference(I[None,:,:,:], sess, mode='depth') figure(figsize=(15,15)) fig = plt.figure(figsize=(15, 13)) # prep (x,y) for extra plotting xs = np.linspace(0, 2*np.pi, 60) # from 0 to 2pi ys = np.abs(np.sin(xs)) # absolute of sine ax = [] # create subplot and append to ax ax.append( fig.add_subplot(1, 2, 1) ) plt.imshow(I, alpha=1) # create subplot and append to ax ax.append( fig.add_subplot(1, 2, 2) ) plt.imshow(normalize_depth_for_display(pred['depth'][0,:,:,0]), alpha=1) plt.savefig(os.path.join('DepthMask',path)) # finally, render the plot # Clear the current axes. plt.cla() # Clear the current figure. plt.clf() # Closes all the figure windows. plt.close('all') fh.close()
def main(): basename = os.path.basename(FLAGS.ckpt_file) # output_file = FLAGS.output_dir + '/' + basename sfm = SfMLearner() sfm.setup_inference(img_height=FLAGS.img_height, img_width=FLAGS.img_width, batch_size=FLAGS.batch_size, mode='depth') saver = tf.train.Saver([var for var in tf.model_variables()]) config = tf.ConfigProto() config.gpu_options.allow_growth = True for files in test_files_txt: newfolder = os.path.splitext(files)[0] savepathimage= FLAGS.save_output_depthimg + '/' + newfolder #print(savepathimage) if not os.path.exists(savepathimage): os.makedirs(savepathimage) savepathdata= FLAGS.save_output_depthdata + '/' + newfolder if not os.path.exists(savepathdata): os.makedirs(savepathdata) print(files) with open(os.path.join(FLAGS.test_file_list,files), 'r') as f: test_files = f.readlines() test_files = [t[:-1] for t in test_files] # if not os.path.exists(FLAGS.output_dir): # os.makedirs(FLAGS.output_dir) with tf.Session(config=config) as sess: saver.restore(sess, FLAGS.ckpt_file) pred_all = [] for t in range(0, len(test_files), FLAGS.batch_size): if t % 100 == 0: print('processing %s: %d/%d' % (basename, t, len(test_files))) inputs = np.zeros( (FLAGS.batch_size, FLAGS.img_height, FLAGS.img_width, 3), dtype=np.uint8) for b in range(FLAGS.batch_size): idx = t + b print(len(test_files)) if idx >= len(test_files): break # fh = open(test_files[idx], 'r') # print(fh) # raw_im = Image.open(fh) # scaled_im = raw_im.resize((FLAGS.img_width, FLAGS.img_height), Image.ANTIALIAS) # inputs[b] = np.array(scaled_im) im = scipy.misc.imread(test_files[idx]) inputs[b] = scipy.misc.imresize(im, (FLAGS.img_height, FLAGS.img_width)) pred = sfm.inference(inputs, sess, mode='depth') for b in range(FLAGS.batch_size): idx = t + b if idx >= len(test_files): break dept= pred['depth'][b,:,:,0] imagesave = normalize_depth_for_display(dept) plt.imsave(os.path.join(savepathimage , newfolder + '_'+ str(idx) + '.png'), imagesave) pred_all.append(pred['depth'][b,:,:,0]) sio.savemat(os.path.join(savepathdata,newfolder + '_'+ str(idx)), {'D': dept.squeeze()})
from SfMLearner import SfMLearner from utils import normalize_depth_for_display img_height = 128 img_width = 416 ckpt_file = 'models/model-190532' fh = open('misc/sample.png', 'rb') I = pil.open(fh) I = I.resize((img_width, img_height), pil.ANTIALIAS) I = np.array(I) # In[2]: sfm = SfMLearner() sfm.setup_inference(img_height, img_width, mode='depth') # In[3]: saver = tf.train.Saver([var for var in tf.model_variables()]) with tf.Session() as sess: saver.restore(sess, ckpt_file) pred = sfm.inference(I[None, :, :, :], sess, mode='depth') # In[4]: figure(figsize=(15, 15)) subplot(1, 2, 1) imshow(I) subplot(1, 2, 2) imshow(normalize_depth_for_display(pred['depth'][0, :, :, 0]))
def save_image(self, img_name, image): print("Save File : {}".format(img_name)) plt.imsave(img_name, normalize_depth_for_display(image['depth'][0, :, :, 0]))
from tensorflow.compat.v1 import ConfigProto from tensorflow.compat.v1 import InteractiveSession config = ConfigProto() config.gpu_options.allow_growth = True session = InteractiveSession(config=config) # adding end img_height = 128 img_width = 416 ckpt_file = 'models/kitti_depth_model/model-190532' fh = open('misc/sample.png', 'rb') I = pil.open(fh) I = I.resize((img_width, img_height), pil.ANTIALIAS) I = np.array(I) sfm = SfMLearner() sfm.setup_inference(img_height, img_width, mode='depth') saver = tf.train.Saver([var for var in tf.model_variables()]) with tf.Session() as sess: saver.restore(sess, ckpt_file) pred = sfm.inference(I[None, :, :, :], sess, mode='depth') plt.figure() plt.subplot(121) cv2.imshow("source", I) plt.subplot(122) cv2.imshow("result", normalize_depth_for_display(pred['depth'][0, :, :, 0])) cv2.waitKey()
#cap2 = cv2.VideoCapture(0) cap.set(cv2.CAP_PROP_FPS, 60) # カメラFPSを60FPSに設定 cap.set(cv2.CAP_PROP_FRAME_WIDTH, 440) # カメラ画像の横幅を1280に設定 cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 180) # カメラ画像の縦幅を720に設定 while True: # VideoCaptureから1フレーム読み込む ret, frame = cap.read() # スクリーンショットを撮りたい関係で1/4サイズに縮小 frame = cv2.resize(frame, ( (int(416), int(128))) ) pred = sfm.inference(frame[None,:,:,:], sess, mode='depth') # 加工なし画像を表示する cv2.imshow('Raw Frame', frame) # depth画像表示 #cv2.imshow('Raw Frame222222222', normalize_depth_for_display(pred['depth'][0,:,:,0])) #img2 = cv2.imread(normalize_depth_for_display(pred['depth'][0,:,:,0]).astype(unit8)) cv2.imshow('Raw Frame33333333333333', normalize_depth_for_display(pred['depth'][0,:,:,0])) # キー入力を1ms待って、k が27(ESC)だったらBreakする k = cv2.waitKey(1) if k == 27: break # キャプチャをリリースして、ウィンドウをすべて閉じる cap.release() cv2.destroyAllWindows()