Skip to content

Commit

Permalink
Update model_zoo test scripts.
Browse files Browse the repository at this point in the history
  • Loading branch information
qiuwch committed Oct 19, 2017
1 parent 502ecf9 commit a40c89c
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 32 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ Binaries/
Intermediate/
Plugins/

# Ignore model_zoo binaries
UE4Binaries/

# Recursively ignore some cache folders
**/.cache/
**/__pycache__/
Expand Down
114 changes: 114 additions & 0 deletions examples/commands_demo.py
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()
24 changes: 24 additions & 0 deletions examples/model_zoo/build_binaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@
r'D:\workspace\uprojects\PhotorealisticCharacter\PhotorealisticCharacter2.uproject',
]

linux_uprojects = [
os.path.expanduser('~/workspace/uprojects/UE4RealisticRendering/RealisticRendering.uproject'),
os.path.expanduser('~/workspace/uprojects/UE4ArchinteriorsVol2Scene1/ArchinteriorsVol2Scene1.uproject'),
os.path.expanduser('~/workspace/uprojects/UE4ArchinteriorsVol2Scene2/ArchinteriorsVol2Scene2.uproject'),
os.path.expanduser('~/workspace/uprojects/UE4ArchinteriorsVol2Scene3/ArchinteriorsVol2Scene3.uproject'),
os.path.expanduser("~/workspace/uprojects/UE4UrbanCity/UrbanCity.uproject"),
]

uprojects = []

for uproject_path in win_uprojects:
Expand All @@ -25,8 +33,24 @@
),
)

for uproject_path in linux_uprojects:
uproject_name = os.path.basename(uproject_path).split('.')[0]
uprojects.append(
dict(
uproject_path = uproject_path,
ue4_path = ue4_linux,
log_file = 'log/linux_%s.log' % uproject_name
),
)


if __name__ == '__main__':
for uproject in uprojects:
uproject_path = uproject['uproject_path']
if not os.path.isfile(uproject_path):
print("Can not find uproject file %s, skip this project" % uproject_path)
continue

cmd = [
'python', 'build.py',
'--UE4', uproject['ue4_path'],
Expand Down
53 changes: 28 additions & 25 deletions examples/model_zoo/test_binaries.py
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
])
17 changes: 10 additions & 7 deletions tox.ini
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}

0 comments on commit a40c89c

Please sign in to comment.