|
|
@@ -56,27 +56,13 @@ def parse_args():
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
|
-def predict(img, model, test_transforms):
|
|
|
- model.arrange_transforms(transforms=test_transforms, mode='test')
|
|
|
- img, im_info = test_transforms(img.astype('float32'))
|
|
|
- img = np.expand_dims(img, axis=0)
|
|
|
- result = model.exe.run(model.test_prog,
|
|
|
- feed={'image': img},
|
|
|
- fetch_list=list(model.test_outputs.values()))
|
|
|
- score_map = result[1]
|
|
|
- score_map = np.squeeze(score_map, axis=0)
|
|
|
- score_map = np.transpose(score_map, (1, 2, 0))
|
|
|
- return score_map, im_info
|
|
|
-
|
|
|
-
|
|
|
def recover(img, im_info):
|
|
|
- for info in im_info[::-1]:
|
|
|
- if info[0] == 'resize':
|
|
|
- w, h = info[1][1], info[1][0]
|
|
|
- img = cv2.resize(img, (w, h), cv2.INTER_LINEAR)
|
|
|
- elif info[0] == 'padding':
|
|
|
- w, h = info[1][0], info[1][0]
|
|
|
- img = img[0:h, 0:w, :]
|
|
|
+ if im_info[0] == 'resize':
|
|
|
+ w, h = im_info[1][1], im_info[1][0]
|
|
|
+ img = cv2.resize(img, (w, h), cv2.INTER_LINEAR)
|
|
|
+ elif im_info[0] == 'padding':
|
|
|
+ w, h = im_info[1][0], im_info[1][0]
|
|
|
+ img = img[0:h, 0:w, :]
|
|
|
return img
|
|
|
|
|
|
|
|
|
@@ -84,8 +70,7 @@ def video_infer(args):
|
|
|
resize_h = args.image_shape[1]
|
|
|
resize_w = args.image_shape[0]
|
|
|
|
|
|
- test_transforms = transforms.Compose(
|
|
|
- [transforms.Resize((resize_w, resize_h)), transforms.Normalize()])
|
|
|
+ test_transforms = transforms.Compose([transforms.Normalize()])
|
|
|
model = pdx.load_model(args.model_dir)
|
|
|
if not args.video_path:
|
|
|
cap = cv2.VideoCapture(0)
|
|
|
@@ -118,9 +103,21 @@ def video_infer(args):
|
|
|
while cap.isOpened():
|
|
|
ret, frame = cap.read()
|
|
|
if ret:
|
|
|
- score_map, im_info = predict(frame, model, test_transforms)
|
|
|
- cur_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
|
|
- cur_gray = cv2.resize(cur_gray, (resize_w, resize_h))
|
|
|
+ im_shape = frame.shape
|
|
|
+ im_scale_x = float(resize_w) / float(im_shape[1])
|
|
|
+ im_scale_y = float(resize_h) / float(im_shape[0])
|
|
|
+ im = cv2.resize(
|
|
|
+ frame,
|
|
|
+ None,
|
|
|
+ None,
|
|
|
+ fx=im_scale_x,
|
|
|
+ fy=im_scale_y,
|
|
|
+ interpolation=cv2.INTER_LINEAR)
|
|
|
+ image = im.astype('float32')
|
|
|
+ im_info = ('resize', im_shape[0:2])
|
|
|
+ pred = model.predict(image)
|
|
|
+ score_map = pred['score_map']
|
|
|
+ cur_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
|
|
|
score_map = 255 * score_map[:, :, 1]
|
|
|
optflow_map = postprocess(cur_gray, score_map, prev_gray, prev_cfd, \
|
|
|
disflow, is_init)
|
|
|
@@ -146,8 +143,21 @@ def video_infer(args):
|
|
|
while cap.isOpened():
|
|
|
ret, frame = cap.read()
|
|
|
if ret:
|
|
|
- score_map, im_info = predict(frame, model, test_transforms)
|
|
|
- cur_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
|
|
+ im_shape = frame.shape
|
|
|
+ im_scale_x = float(resize_w) / float(im_shape[1])
|
|
|
+ im_scale_y = float(resize_h) / float(im_shape[0])
|
|
|
+ im = cv2.resize(
|
|
|
+ frame,
|
|
|
+ None,
|
|
|
+ None,
|
|
|
+ fx=im_scale_x,
|
|
|
+ fy=im_scale_y,
|
|
|
+ interpolation=cv2.INTER_LINEAR)
|
|
|
+ image = im.astype('float32')
|
|
|
+ im_info = ('resize', im_shape[0:2])
|
|
|
+ pred = model.predict(image)
|
|
|
+ score_map = pred['score_map']
|
|
|
+ cur_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
|
|
|
cur_gray = cv2.resize(cur_gray, (resize_w, resize_h))
|
|
|
score_map = 255 * score_map[:, :, 1]
|
|
|
optflow_map = postprocess(cur_gray, score_map, prev_gray, prev_cfd, \
|