-
Notifications
You must be signed in to change notification settings - Fork 430
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
179 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
from unrealcv.automation import UE4Binary | ||
from unrealcv.util import read_png, read_npy | ||
from unrealcv import client | ||
# import matplotlib.pyplot as plt | ||
import imageio | ||
import numpy as np | ||
import argparse, os | ||
|
||
class Logger: | ||
def __init__(self, client, log_filename, output_folder): | ||
self.client = client | ||
self.log_filename = log_filename | ||
self.output_folder = output_folder | ||
self.log_file = open(log_filename, 'w') | ||
|
||
def writelines(self, text): | ||
self.log_file.writelines(str(text)) | ||
|
||
def request(self, cmd): | ||
self.writelines('Request: ' + cmd + '\n') | ||
res = client.request(cmd) | ||
self.writelines('Response: ' + str(res[:50]) + '\n') | ||
# Trancate too long value | ||
return res | ||
|
||
def save_image(self, filename, img): | ||
abs_filename = os.path.join(self.output_folder, filename) | ||
# plt.imsave(abs_filename, img) | ||
imageio.imwrite(abs_filename, img) | ||
|
||
def normalize(src): | ||
normalized = (src - src.min()) / (src.max() - src.min()) | ||
return normalized | ||
|
||
def run_commands_demo(logger): | ||
logger.client.connect() | ||
logger.request('vget /unrealcv/status') | ||
res = logger.request('vget /camera/0/lit png') | ||
img = read_png(res) | ||
# plt.imshow(img) | ||
|
||
res = logger.request('vget /camera/0/location') | ||
res = logger.request('vget /camera/0/rotation') | ||
|
||
# res = logger.request('vset /camera/0/location -162 -126 90') | ||
# res = logger.request('vset /camera/0/rotation 348 60 0') | ||
|
||
res = logger.request('vget /camera/0/lit png') | ||
img = read_png(res) | ||
res = logger.request('vget /camera/0/depth npy') | ||
depth = read_npy(res) | ||
clip_far = np.median(depth) * 5 # use median instead of mean | ||
print('Before clip, max=%f, mean=%f' % (depth.max(), depth.mean())) | ||
depth[depth > clip_far] = clip_far | ||
print('Before clip, max=%f, mean=%f' % (depth.max(), depth.mean())) | ||
# Trancate the depth of the window and sky to make it easier to see | ||
|
||
res = logger.request('vget /camera/0/normal npy') | ||
normal = read_npy(res) | ||
|
||
res = logger.request('vget /camera/0/object_mask png') | ||
object_mask = read_png(res) | ||
|
||
logger.save_image("img.png", img) | ||
logger.save_image("depth.png", normalize(depth)) | ||
logger.save_image("surface_normal.png", normalize(normal)) | ||
logger.save_image("object_mask.png", object_mask) | ||
|
||
res = logger.request('vget /camera/0/lit lit.png') | ||
res = logger.request('vget /camera/0/depth depth.png') | ||
res = logger.request('vget /camera/0/object_mask object_mask.png') | ||
|
||
res = logger.request('vget /objects') | ||
object_names = res.split(' ') | ||
logger.writelines(object_names[:5]) | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument('binary_path', nargs='?', help = 'The UE4 binary to run, if this is empty, connect to the editor') | ||
parser.add_argument('--output_folder', help = 'Output folder for the log of this script') | ||
args = parser.parse_args() | ||
|
||
binary_path = args.binary_path | ||
# Use .exe file for windows. | ||
|
||
binary = None | ||
if binary_path: | ||
binary_name = os.path.basename(binary_path).split('.')[0] | ||
binary = UE4Binary(binary_path) # UE4Binary can support exe, linux and mac binary | ||
else: | ||
binary_name = 'editor' | ||
|
||
if args.output_folder: | ||
output_folder = args.output_folder | ||
else: | ||
output_folder = binary_name | ||
|
||
if os.path.isdir(output_folder): | ||
print('Output folder %s already exists' % output_folder) | ||
return | ||
os.mkdir(output_folder) | ||
log_filename = os.path.join(output_folder, 'output.txt') | ||
|
||
logger = Logger(client, log_filename, output_folder) | ||
if binary: | ||
with binary: | ||
run_commands_demo(logger) | ||
else: | ||
run_commands_demo(logger) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,34 @@ | ||
import subprocess | ||
import subprocess, os | ||
|
||
binaries = [ | ||
dict( | ||
binary_path = 'D:/unrealcv/Binaries/ArchinteriorsVol2Scene1/WindowsNoEditor/ArchinteriorsVol2Scene1.exe', | ||
output_folder = 'output/arch1' | ||
), | ||
dict( | ||
binary_path = 'D:/unrealcv/Binaries/ArchinteriorsVol2Scene2/WindowsNoEditor/ArchinteriorsVol2Scene2.exe', | ||
output_folder = 'output/arch2' | ||
), | ||
dict( | ||
binary_path = | ||
'D:/unrealcv/Binaries/ArchinteriorsVol2Scene3/WindowsNoEditor/ArchinteriorsVol2Scene3.exe', | ||
output_folder = 'output/arch3' | ||
), | ||
dict( | ||
binary_path = 'D:/unrealcv/Binaries/UrbanCity/WindowsNoEditor/UrbanCity.exe', | ||
output_folder = 'output/urbancity' | ||
), | ||
dict( | ||
binary_path = 'D:/unrealcv/Binaries/RealisticRendering/WindowsNoEditor/RealisticRendering.exe', | ||
output_folder = 'output/rr' | ||
) | ||
'D:/unrealcv/Binaries/RealisticRendering/WindowsNoEditor/RealisticRendering.exe', | ||
'D:/unrealcv/Binaries/ArchinteriorsVol2Scene1/WindowsNoEditor/ArchinteriorsVol2Scene1.exe', | ||
'D:/unrealcv/Binaries/ArchinteriorsVol2Scene2/WindowsNoEditor/ArchinteriorsVol2Scene2.exe', | ||
'D:/unrealcv/Binaries/ArchinteriorsVol2Scene3/WindowsNoEditor/ArchinteriorsVol2Scene3.exe', | ||
'D:/unrealcv/Binaries/UrbanCity/WindowsNoEditor/UrbanCity.exe', | ||
] | ||
|
||
linux_binary_path = './UE4Binaries/{project_name}/LinuxNoEditor/{project_name}/Binaries/Linux/{project_name}' | ||
project_names = [ | ||
'RealisticRendering', 'ArchinteriorsVol2Scene1', 'ArchinteriorsVol2Scene2', | ||
'ArchinteriorsVol2Scene3', 'UrbanCity', | ||
] | ||
|
||
binaries += [linux_binary_path.format(project_name = v) for v in project_names] | ||
|
||
if __name__ == '__main__': | ||
for binary in binaries: | ||
subprocess.call(['python', 'docs/reference/commands_demo.py', | ||
binary['binary_path'], '--output', binary['output_folder'] | ||
if not os.path.isdir('output'): | ||
os.path.mkdir('output') | ||
|
||
for binary_path in binaries: | ||
project_name = os.path.basename(binary_path).split('.')[0] | ||
output_folder = os.path.join('output', project_name) | ||
if not os.path.isfile(binary_path): | ||
print('Can not find binary %s, skip' % binary_path) | ||
continue | ||
|
||
print('Testing %s ...' % binary_path) | ||
subprocess.call([ | ||
'python', 'docs/reference/commands_demo.py', | ||
binary_path, '--output', output_folder | ||
]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
[tox] | ||
envlist = py27 | ||
envlist = py27,py35 | ||
skipsdist = True | ||
|
||
[testenv] | ||
deps = | ||
pytest | ||
unrealcv | ||
commands = py.test test/client/ | ||
[pytest] | ||
norecursedirs = client | ||
deps = | ||
--editable=file:///{toxinidir}/client/python | ||
numpy | ||
Pillow | ||
; matplotlib | ||
imageio | ||
|
||
commands = python examples/commands_demo.py --output tox_output_{envname} |