DenthDepth:深度估计(三维场景构建)——单目视觉挑战激光雷达
生活随笔
收集整理的這篇文章主要介紹了
DenthDepth:深度估计(三维场景构建)——单目视觉挑战激光雷达
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
網上看到這個
MAXIEYE周圣硯:欲靠單目視覺挑戰激光雷達
搜了一下,感覺應該就是這個工程,https://github.com/ialhashim/DenseDepth
宣傳的檢測效果
我網上搜一張圖片,運行一下效果大致如下:
?
檢測的準不準,自己判斷了,我進行了一點點修改,只是把數據源從單張圖片改為了從攝像頭獲取,
新建camera_test.py
import os
import glob
import argparse
import matplotlib
import cv2
import numpy as np
import matplotlib.pyplot as plt# Keras / TensorFlow
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '5'
from keras.models import load_model
from layers import BilinearUpSampling2D
from utils import predict, load_images, display_images, color_rescale
from matplotlib import pyplot as plt# Argument Parser
parser = argparse.ArgumentParser(description='High Quality Monocular Depth Estimation via Transfer Learning')
parser.add_argument('--model', default='nyu.h5', type=str, help='Trained Keras model file.')
args = parser.parse_args()# Custom object needed for inference and training
custom_objects = {'BilinearUpSampling2D': BilinearUpSampling2D, 'depth_loss_function': None}print('Loading model...')# Load model into GPU / CPU
model = load_model(args.model, custom_objects=custom_objects, compile=False)print('\nModel loaded ({0}).'.format(args.model))cap = cv2.VideoCapture(0)while True:# frame = cv2.imread("examples/626_image.png")ret, frame = cap.read()cv2.imshow("test", frame)frame = np.clip(np.asarray(frame[...,::-1], dtype=float) / 255, 0, 1)# Compute resultsoutputs = predict(model, frame[np.newaxis, :])cv2.imshow("depth", color_rescale(outputs[0]))if cv2.waitKey(1) & 0xFF == ord('q'):break
修改utils.py
import numpy as np
from PIL import Image
import cv2
import matplotlib.pyplot as plt
plasma = plt.get_cmap('plasma')def DepthNorm(x, maxDepth):return maxDepth / xdef predict(model, images, minDepth=10, maxDepth=1000, batch_size=2):# Support multiple RGBs, one RGB image, even grayscale if len(images.shape) < 3: images = np.stack((images,images,images), axis=2)if len(images.shape) < 4: images = images.reshape((1, images.shape[0], images.shape[1], images.shape[2]))# Compute predictionspredictions = model.predict(images, batch_size=batch_size)# Put in expected rangereturn np.clip(DepthNorm(predictions, maxDepth=maxDepth), minDepth, maxDepth) / maxDepthdef scale_up(scale, images):from skimage.transform import resizescaled = []for i in range(len(images)):img = images[i]output_shape = (scale * img.shape[0], scale * img.shape[1])scaled.append( resize(img, output_shape, order=1, preserve_range=True, mode='reflect', anti_aliasing=True ) )return np.stack(scaled)def load_images(image_files):loaded_images = []for file in image_files:x = np.clip(np.asarray(Image.open( file ), dtype=float) / 255, 0, 1)loaded_images.append(x)return np.stack(loaded_images, axis=0)def to_multichannel(i):if i.shape[2] == 3:return ii = i[:,:,0]print(i.shape)return np.stack((i,i,i), axis=2)def color_rescale(img):rescaled = img[:,:,0]print(np.min(rescaled))rescaled = rescaled - np.min(rescaled)print(np.max(rescaled))rescaled = rescaled / np.max(rescaled)return plasma(rescaled)[:,:,:3]def display_images(outputs, inputs=None, gt=None, is_colormap=True, is_rescale=True):import skimagefrom skimage.transform import resizeshape = (outputs[0].shape[0], outputs[0].shape[1], 3)all_images = []for i in range(outputs.shape[0]):imgs = []if isinstance(inputs, (list, tuple, np.ndarray)):x = to_multichannel(inputs[i])x = resize(x, shape, preserve_range=True, mode='reflect', anti_aliasing=True )imgs.append(x)if isinstance(gt, (list, tuple, np.ndarray)):x = to_multichannel(gt[i])x = resize(x, shape, preserve_range=True, mode='reflect', anti_aliasing=True )imgs.append(x)if is_colormap:imgs.append(color_rescale(outputs[i]))else:imgs.append(to_multichannel(outputs[i]))img_set = np.hstack(imgs)all_images.append(img_set)all_images = np.stack(all_images)return skimage.util.montage(all_images, multichannel=True, fill=(0,0,0))def save_images(filename, outputs, inputs=None, gt=None, is_colormap=True, is_rescale=False):montage = display_images(outputs, inputs, is_colormap, is_rescale)im = Image.fromarray(np.uint8(montage*255))im.save(filename)def load_test_data(test_data_zip_file='nyu_test.zip'):print('Loading test data...', end='')import numpy as npfrom data import extract_zipdata = extract_zip(test_data_zip_file)from io import BytesIOrgb = np.load(BytesIO(data['eigen_test_rgb.npy']))depth = np.load(BytesIO(data['eigen_test_depth.npy']))crop = np.load(BytesIO(data['eigen_test_crop.npy']))print('Test data loaded.\n')return {'rgb':rgb, 'depth':depth, 'crop':crop}def compute_errors(gt, pred):thresh = np.maximum((gt / pred), (pred / gt))a1 = (thresh < 1.25 ).mean()a2 = (thresh < 1.25 ** 2).mean()a3 = (thresh < 1.25 ** 3).mean()abs_rel = np.mean(np.abs(gt - pred) / gt)rmse = (gt - pred) ** 2rmse = np.sqrt(rmse.mean())log_10 = (np.abs(np.log10(gt)-np.log10(pred))).mean()return a1, a2, a3, abs_rel, rmse, log_10def evaluate(model, rgb, depth, crop, batch_size=6, verbose=False):N = len(rgb)bs = batch_sizepredictions = []testSetDepths = []for i in range(N//bs): x = rgb[(i)*bs:(i+1)*bs,:,:,:]# Compute resultstrue_y = depth[(i)*bs:(i+1)*bs,:,:]pred_y = scale_up(2, predict(model, x/255, minDepth=10, maxDepth=1000, batch_size=bs)[:,:,:,0]) * 10.0# Test time augmentation: mirror image estimatepred_y_flip = scale_up(2, predict(model, x[...,::-1,:]/255, minDepth=10, maxDepth=1000, batch_size=bs)[:,:,:,0]) * 10.0# Crop based on Eigen et al. croptrue_y = true_y[:,crop[0]:crop[1]+1, crop[2]:crop[3]+1]pred_y = pred_y[:,crop[0]:crop[1]+1, crop[2]:crop[3]+1]pred_y_flip = pred_y_flip[:,crop[0]:crop[1]+1, crop[2]:crop[3]+1]# Compute errors per image in batchfor j in range(len(true_y)):predictions.append( (0.5 * pred_y[j]) + (0.5 * np.fliplr(pred_y_flip[j])) )testSetDepths.append( true_y[j] )predictions = np.stack(predictions, axis=0)testSetDepths = np.stack(testSetDepths, axis=0)e = compute_errors(predictions, testSetDepths)if verbose:print("{:>10}, {:>10}, {:>10}, {:>10}, {:>10}, {:>10}".format('a1', 'a2', 'a3', 'rel', 'rms', 'log_10'))print("{:10.4f}, {:10.4f}, {:10.4f}, {:10.4f}, {:10.4f}, {:10.4f}".format(e[0],e[1],e[2],e[3],e[4],e[5]))return e
?
總結
以上是生活随笔為你收集整理的DenthDepth:深度估计(三维场景构建)——单目视觉挑战激光雷达的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 树莓派3b+目标检测: tflite 运
- 下一篇: deepspeech实时语音识别