Skip to content

Commit 2d747bd

Browse files
committed
merge yolo and scsfm
1 parent c707d55 commit 2d747bd

26 files changed

+337
-4988
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,4 @@ res/*
133133
*.mp4
134134
!res/dataset*
135135
*.pt
136+
*.tar

adabins/infer.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def __init__(self, dataset='nyu', device='cuda:0'):
7272
self.max_depth = 10
7373
self.saving_factor = 1000 # used to save in 16 bit
7474
model = UnetAdaptiveBins.build(n_bins=256, min_val=self.min_depth, max_val=self.max_depth)
75-
pretrained_path = "./res/weights/adabins_nyu.pt"
75+
pretrained_path = "./data/weights/adabins_nyu.pt"
7676
elif dataset == 'kitti':
7777
raise ValueError("dataset can be only 'nyu' {}".format(dataset))
7878
self.min_depth = 1e-3

camera_calibration.py

+84-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
# %%
2-
import random
3-
import torch
4-
from math import pi, tan
2+
import scsfm
3+
import argparse
4+
import time
55
import cv2
66
import numpy as np
7-
from numpy.linalg import inv
8-
import matplotlib.pyplot as plt
9-
import pandas as pd
7+
import torch
8+
from skimage.transform import resize as imresize
9+
import random
10+
from math import pi, tan
11+
# from numpy.linalg import inv
12+
# import matplotlib.pyplot as plt
13+
# import pandas as pd
1014

1115
###########################
1216
# VISUALIZATION FUNCTIONS #
@@ -31,10 +35,12 @@ def draw(frame, imgpts):
3135
# corner = corners[0].ravel()
3236
imgpts = np.int32(imgpts).reshape(-1, 2)
3337
# draw ground floor in green
34-
frame = cv2.drawContours(frame, [imgpts[:4]], -1, (0, 255, 0), -3)
38+
frame = cv2.drawContours(frame, [imgpts[:4]],
39+
-1, (0, 255, 0), -3)
3540
# draw pillars in blue color
3641
for i, j in zip(range(4), range(4, 8)):
37-
frame = cv2.line(frame, tuple(imgpts[i]), tuple(imgpts[j]), (255), 1)
42+
frame = cv2.line(frame, tuple(imgpts[i]),
43+
tuple(imgpts[j]), (255), 1)
3844
# draw top layer in red color
3945
frame = cv2.drawContours(frame, [imgpts[4:]], -1, (0, 0, 255), 1)
4046
return frame
@@ -43,7 +49,7 @@ def draw(frame, imgpts):
4349
####################
4450
# FRAME PARAMETERS #
4551
####################
46-
frame = cv2.imread('data/blob/blob21.jpg')
52+
frame = cv2.imread('data/data.png')
4753
height, width, channels = frame.shape
4854
print(f'width: {width} height: {height} channels: {channels}')
4955
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
@@ -112,8 +118,8 @@ def draw(frame, imgpts):
112118

113119
# Filter by Area.
114120
blobParams.filterByArea = True
115-
blobParams.minArea = 10 # minArea may be adjusted to suit for your experiment
116-
blobParams.maxArea = 100 # maxArea may be adjusted to suit for your experiment
121+
blobParams.minArea = 1
122+
blobParams.maxArea = 100
117123

118124
# Filter by Circularity
119125
blobParams.filterByCircularity = True
@@ -303,3 +309,70 @@ def find_board(keypoints):
303309
[225, 255, 255], thickness=tf, lineType=cv2.LINE_AA)
304310

305311
cv2.imwrite('res/blobs.png', blob_frame)
312+
313+
314+
device = torch.device(
315+
"cuda") if torch.cuda.is_available() else torch.device("cpu")
316+
317+
318+
def load_tensor_image(img, resize=(256, 320)):
319+
320+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
321+
img = img.astype(np.float32)
322+
323+
if resize:
324+
img = imresize(img, resize)
325+
326+
img = np.transpose(img, (2, 0, 1))
327+
tensor_img = ((torch.from_numpy(img).unsqueeze(
328+
0) / 255 - 0.45) / 0.225).to(device)
329+
print(f'tensor shape {tensor_img.shape}')
330+
return tensor_img
331+
332+
333+
def prediction_to_visual(output, shape=(360, 640)):
334+
pred_disp = output.cpu().numpy()[0, 0]
335+
img = 1 / pred_disp
336+
img = imresize(img, shape).astype(np.float32)
337+
return img
338+
339+
340+
@torch.no_grad()
341+
def predict_depth():
342+
parser = argparse.ArgumentParser()
343+
parser.add_argument('--source', type=str,
344+
default='res/dataset1.mp4', help='source')
345+
parser.add_argument('--output', type=str,
346+
default='res/scfm.csv', help='source')
347+
opt = parser.parse_args()
348+
print(opt)
349+
350+
################
351+
# Load DispNet #
352+
################
353+
disp_net = scsfm.DispResNet(18, False).to(device)
354+
weights = torch.load('data/weights/scfm-nyu2-test.pth.tar')
355+
disp_net.load_state_dict(weights['state_dict'])
356+
disp_net.eval()
357+
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
358+
359+
# cap = cv2.VideoCapture('data/data2.mp4')
360+
# while True:
361+
then = time.time()
362+
frame_rgb = frame_rgb[:, 30:510]
363+
tgt_img = load_tensor_image(frame_rgb.copy())
364+
print_duration(then, 'capture and convert')
365+
then = time.time()
366+
output = disp_net(tgt_img)
367+
print_duration(then, 'inference')
368+
369+
cv2.imshow('frame', frame_rgb)
370+
cv2.imshow('depth', prediction_to_visual(output))
371+
cv2.waitKey(-1)
372+
373+
374+
def print_duration(then, prefix=''):
375+
print(prefix, 'took %.2f ms' % ((time.time() - then) * 1000))
376+
377+
378+
predict_depth()

cpopservice/depth/__init__.py

Whitespace-only changes.

cpopservice/depth/scsfm.py

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# %%
2+
import argparse
3+
import time
4+
import cv2
5+
import numpy as np
6+
import torch
7+
from skimage.transform import resize as imresize
8+
import models.scsfm as models
9+
10+
device = torch.device(
11+
"cuda") if torch.cuda.is_available() else torch.device("cpu")
12+
13+
14+
def load_tensor_image(img, resize=(256, 320)):
15+
img = img[:, 30:510]
16+
if resize:
17+
resized_img = imresize(img, resize)
18+
cv2.imshow('resized', resized_img)
19+
20+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
21+
img = img.astype(np.float32)
22+
23+
if resize:
24+
img = imresize(img, resize)
25+
26+
img = np.transpose(img, (2, 0, 1))
27+
tensor_img = ((torch.from_numpy(img).unsqueeze(
28+
0) / 255 - 0.45) / 0.225).to(device)
29+
print(f'tensor shape {tensor_img.shape}')
30+
return tensor_img
31+
32+
33+
def prediction_to_visual(output, shape=(360, 640)):
34+
pred_disp = output.cpu().numpy()[0, 0]
35+
img = 1 / pred_disp
36+
# img = imresize(img, shape).astype(np.float32)
37+
return img
38+
39+
40+
@torch.no_grad()
41+
def main():
42+
parser = argparse.ArgumentParser()
43+
parser.add_argument('--source', type=str,
44+
default='res/dataset1.mp4', help='source')
45+
parser.add_argument('--output', type=str,
46+
default='res/scfm.csv', help='source')
47+
48+
opt = parser.parse_args()
49+
print(opt)
50+
51+
disp_net = models.DispResNet(18, False).to(device)
52+
weights = torch.load('data/weights/scfm-nyu2-test.pth.tar')
53+
disp_net.load_state_dict(weights['state_dict'])
54+
disp_net.eval()
55+
56+
if opt.source == '0':
57+
source = 0
58+
else:
59+
source = opt.source
60+
61+
62+
63+
64+
def print_duration(then, prefix=''):
65+
print(prefix, 'took %.2f ms' % ((time.time() - then) * 1000))
66+
67+
68+
if __name__ == '__main__':
69+
main()
70+
71+
# %%

0 commit comments

Comments
 (0)