Skip to content

KinFu python samples #2981

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Sep 4, 2021
58 changes: 58 additions & 0 deletions modules/rgbd/samples/kinfu_demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import numpy as np
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

numpy import should go before cv2

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

import cv2 as cv
import sys

from argparse import ArgumentParser

def get_depth_list(folder):
f = open(folder + '/depth.txt', 'r')
rgb = [folder + '/' + s for s in f.read().split() if s.endswith('.png')]
return rgb

def kinfu_demo():
parser = ArgumentParser()
parser.add_argument(
"-i", "--input", help="Required. Path to folder with a input image file", required=True, type=str)
parser.add_argument(
"-t", "--large_kinfu", help="Required. Name of KinFu type", required=False, type=str)
parser.add_argument(
"-ocl", "--use_opencl", help="Required. Flag of OpenCL use", required=False, type=int, default=1)

args = parser.parse_args()
print("Args: ", args)

cv.ocl.setUseOpenCL(args.use_opencl)

if (args.large_kinfu == None or args.large_kinfu == "0"):
params = cv.kinfu_Params.defaultParams()
kf = cv.kinfu_KinFu.create(params)
elif (args.large_kinfu == "1"):
params = cv.kinfu_Params.hashTSDFParams(False)
kf = cv.kinfu_KinFu.create(params)
else:
raise ValueError("Incorrect kinfu type name")

depth_list = get_depth_list(args.input)
for path in depth_list:

image = cv.imread(path, cv.IMREAD_ANYDEPTH)
(height, width) = image.shape

cv.imshow('input', image)

size = height, width, 4
cvt8 = np.zeros(size, dtype=np.uint8)

if not kf.update(image):
kf.reset()
else:
kf.render(cvt8)
cv.imshow('render', cvt8)
cv.pollKey()
cv.waitKey(0)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we should have waitKey(0) after the loop before exit.
Otherwise app will just close in the end. User can't see the final 'render' frame.

Copy link
Member Author

@DumDereDum DumDereDum Sep 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you mean replacing pollKey() after the loop ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pollKey() must be called after each imshow() call to update GUI window content (handle UI redraw events). See imshow() documentation.

We need waitKey(0) for infinite wait after loop and before calling cv.destroyAllWindows().

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


if __name__ == '__main__':
print(__doc__)
kinfu_demo()
cv.destroyAllWindows()