def __init__(self, rc_x: 0, rc_y: 0): ue4.connect(timeout=5) self.connected = False if not ue4.isconnected(): print("UnrealCV server is not running.") else: self.connected = True print(ue4.request("vget /unrealcv/status")) # TODO: these are specific to the default maze # self.initial_x = 700 # self.initial_y = -700 self.initial_x = 1050 - (100 * rc_x) self.initial_y = -1050 + (100 * rc_y) self.initial_angle = 0 self.x = self.initial_x self.y = self.initial_y self.angle = self.initial_angle self.turn_speed = 5 self.walk_speed = 50 if self.connected: self.set_pose()
def test_random_operation(): ''' Randomly connect and disconnect the client, this is very likely to fail ''' echo_server = MessageServer((localhost, echo_port)) echo_server.start() client = unrealcv.Client((localhost, echo_port)) num_random_trial = 10 print('Try random operation %d times' % num_random_trial) for i in range(num_random_trial): msg = 'Trial %d' % i choice = random.randrange(2) if choice == 1: client.connect() time.sleep(0.1) assert client.isconnected() == True, msg elif choice == 0: client.disconnect() time.sleep(0.1) assert client.isconnected() == False, msg for i in range(10): client.connect() assert client.isconnected() == True for i in range(10): client.disconnect() assert client.isconnected() == False echo_server.shutdown()
def begin_deux(self): # Connect to the game from unrealcv import client client.connect() if not client.isconnected(): print('UnrealCV server is not running. Run the game downloaded ', 'from http://unrealcv.github.io first.') sys.exit(-1) # Make sure the connection works well res = client.request('vget /unrealcv/status') # The image resolution and port is configured in the config file. print(res) scene_objects = client.request('vget /objects').split(' ') print('Number of objects in this scene:', len(scene_objects)) # Creates a JSON file that maps each objects ID to a class # We use this to group stuff together obj_id_to_class = {} for obj_id in scene_objects: obj_id_parts = obj_id.split('_') class_name = obj_id_parts[1] obj_id_to_class[obj_id] = class_name # Write JSON file with open('../data/neighborhood_deux_object_ids.json', 'w') as outfile: json.dump(obj_id_to_class, outfile)
def main(data_dir, meta_dir, img_type = 'video'): client.connect() with open(os.path.join(meta_dir,'camera_parameter.json'), 'r') as f: camera_parameter = json.load(f) camera_parameter = camera_parameter[img_type] transform = [camera_parameter['PrincipalPoint'][0] - camera_parameter['ImageSize'][0]/2, camera_parameter['PrincipalPoint'][1] - camera_parameter['ImageSize'][1]/2] M = np.float32([[1,0,transform[0]],[0,1,transform[1]]]) with open(os.path.join(data_dir,'d3_pred.json'), 'r') as f: d3_pred = json.load(f) for file_name, preds in d3_pred.items(): img_dir = os.path.join(data_dir, 'imgs', os.path.splitext(file_name)[0] + '.jpg') img_raw = cv2.imread(img_dir) preds = preds['preds'] rows,cols = img_raw.shape[0:2] img_syn = visualizer(preds) img_syn = cv2.warpAffine(img_syn,M,(cols,rows)) img = img_raw/2+img_syn[:,:,0:3]/2 cv2.imwrite(os.path.join(data_dir, 'syn', os.path.splitext(file_name)[0] + '.jpg'), img) time.sleep(0.2)
def test_client_release(): ''' If the previous client release the connection, further connection should be accepted. This will also test the server code ''' echo_server = MessageServer((localhost, echo_port)) echo_server.start() client = unrealcv.Client((localhost, echo_port)) num_release_trial = 10 print('Try to release client %d times', num_release_trial) for i in range(num_release_trial): msg = 'Trial %d' % i client.connect() assert client.isconnected() == True, msg # Do something req = 'ok' res = client.request(req) assert req == res, msg client.disconnect( ) # Make sure the server can correctly handle the disconnection signal assert client.isconnected() == False, msg print('Trial %d is finished.' % i) echo_server.shutdown()
def main(): loc = None rot = None fig, ax = plt.subplots() img = np.zeros((480, 640, 4)) ax.imshow(img) def onpress(event): rot_offset = 10 # Rotate 5 degree for each key press loc_offset = 10 # Move 5.0 when press a key # Up and Down in cam-plane if event.key == 'w': loc[0] += loc_offset if event.key == 's': loc[0] -= loc_offset # Left and Right movement in cam-plane if event.key == 'a': loc[1] -= loc_offset if event.key == 'd': loc[1] += loc_offset # In and Out movement into cam-plane if event.key == 'q': loc[2] += loc_offset if event.key == 'e': loc[2] -= loc_offset # cmd = 'vset /camera/0/rotation %s' % ' '.join([str(v) for v in rot]) # client.request(cmd) cmd = 'vset /camera/0/moveto %s' % ' '.join([str(v) for v in loc]) client.request(cmd) print(client.request('vget /camera/0/location')) print(client.request('vget /camera/0/rotation')) res = client.request('vget /camera/2/lit png') img = read_png(res) ax.imshow(img) fig.canvas.draw() client.connect() if not client.isconnected(): print('UnrealCV server is not running. Run the game from http://unrealcv.github.io first.') return else: print(help_message) print("-------------------------------------------------------------------") print(client.request('vget /objects')) print("-------------------------------------------------------------------") print(client.request('vget /cameras')) print("-------------------------------------------------------------------") # print(client.request('vget /cameras')) # print("-------------------------------------------------------------------") init_loc = [float(v) for v in client.request('vget /camera/0/location').split(' ')] init_rot = [float(v) for v in client.request('vget /camera/0/rotation').split(' ')] loc = init_loc; rot = init_rot fig.canvas.mpl_connect('key_press_event', onpress) plt.title('Keep this window in focus, it will be used to receive key press event') plt.axis('off') plt.show() # Add event handler
def __init__(self): client.connect() if not client.isconnected(): print( 'UnrealCV server is not running. Run the game downloaded from http://unrealcv.github.io first.' ) sys.exit(-1)
def test_object_list(): client.connect() res = client.request('vget /objects') obj_ids = res.split(' ') assert checker.not_error(res) for obj_id in obj_ids: color = client.request('vget /object/%s/color' % obj_id) assert checker.not_error(color)
def test_color_mapping(): ''' Make sure the color for each object is identical ''' client.connect() scene_objects = client.request('vget /objects').split(' ') color_mapping = get_color_mapping(client, scene_objects) colors = color_mapping.values() assert len(colors) == len(set(colors)) print 'Number of objects %d, number of unique colors %d' % ( len(colors), len(set(colors))) print color_mapping
def test_exr_file(): cmds = [ 'vget /camera/0/depth test.exr', # This is very likely to fail in Linux ] client.connect() for cmd in cmds: res = client.request(cmd) assert checker.not_error(res) im = imread_file(res)
def test_npy_mode(): ''' Get data as a numpy array ''' client.connect() cmd = 'vget /camera/0/depth npy' res = client.request(cmd) assert checker.not_error(res) # Do these but without assert, if exception happened, this test failed arr = imread_npy(res)
def test_pause(): client.connect() cmds = [ 'vset /action/game/pause', 'vget /camera/0/lit', 'vget /camera/1/lit', 'vset /action/game/pause', ] for cmd in cmds: res = client.request(cmd) assert checker.not_error(res)
def test_camera_control(): client.connect() cmds = [ 'vget /camera/0/location', 'vget /camera/0/rotation', # 'vset /camera/0/location 0 0 0', # BUG: If moved out the game bounary, the pawn will be deleted, so that the server code will crash with a nullptr error. # 'vset /camera/0/rotation 0 0 0', ] for cmd in cmds: res = client.request(cmd) assert checker.not_error(res)
def test_no_server(): ''' What if server is not started yet? ''' no_port = 9012 client = unrealcv.Client((localhost, no_port), None) client.connect() assert client.isconnected() == False cmds = ['hi', 'hello'] for cmd in cmds: res = client.request(cmd) assert res == None
def test_npy_mode(): """ Get data as a numpy array """ client.connect() cmd = 'vget /camera/0/depth npy' res = client.request(cmd) assert checker.not_error(res) # Do these but without assert, if exception happened, this test failed arr = imread_npy(res)
def main(): loc = None rot = None fig, ax = plt.subplots() img = np.zeros((480, 640, 4)) ax.imshow(img) def onpress(event): rot_offset = 10 # Rotate 5 degree for each key press loc_offset = 10 # Move 5.0 when press a key if event.key == 'a': rot[1] -= rot_offset if event.key == 'd': rot[1] += rot_offset if event.key == 'q': loc[2] += loc_offset # Move up if event.key == 'e': loc[2] -= loc_offset # Move down if event.key == 'w': loc[1] -= loc_offset if event.key == 's': loc[1] += loc_offset if event.key == 'up': loc[1] -= loc_offset if event.key == 'down': loc[1] += loc_offset if event.key == 'left': loc[0] -= loc_offset if event.key == 'right': loc[0] += loc_offset cmd = 'vset /camera/0/rotation %s' % ' '.join([str(v) for v in rot]) client.request(cmd) cmd = 'vset /camera/0/location %s' % ' '.join([str(v) for v in loc]) client.request(cmd) res = client.request('vget /camera/0/lit png') img = read_png(res) # print(event.key) # print('Requested image %s' % str(img.shape)) ax.imshow(img) fig.canvas.draw() client.connect() if not client.isconnected(): print 'UnrealCV server is not running. Run the game from http://unrealcv.github.io first.' return else: print help_message init_loc = [float(v) for v in client.request('vget /camera/0/location').split(' ')] init_rot = [float(v) for v in client.request('vget /camera/0/rotation').split(' ')] loc = init_loc; rot = init_rot fig.canvas.mpl_connect('key_press_event', onpress) plt.title('Keep this window in focus, it will be used to receive key press event') plt.axis('off') plt.show() # Add event handler
def test_request_timeout(): ''' What if the server did not respond with a correct reply. ''' null_port = 9011 null_server = NullServer((localhost, null_port)) null_server.start() client = unrealcv.Client((localhost, null_port)) client.connect() assert client.isconnected() == True response = client.request('hi', timeout=1) assert response == None
def collect(self, count=400, preprocess=True): client.connect() if not client.isconnected(): raise RuntimeError("Could not connect to client. ") # First we prepare the Unreal Engine environment by preprocessing it if preprocess: PreProcessor(self.environment_folder).preprocess() # Then we build our dataset Builder(self.environment_folder).build(count) client.disconnect()
def test_request(): ''' Simple test for basic functions ''' server = MessageServer((localhost, echo_port)) server.start() client = unrealcv.Client((localhost, echo_port)) cmds = ['hi', 'hello', 'asdf' * 70] client.connect() assert client.isconnected() == True for cmd in cmds: res = client.request(cmd) assert res == cmd client.disconnect() # TODO: What if forgot to disconnect server.shutdown()
def ver(): client.connect() res = client.request('vget /unrealcv/version') client.connect() if res and res.startswith('error Can not find a handler'): return (0, 3, 0) # or earlier elif checker.is_error(res): print('Fail to connect to the game, make sure the game is running.') exit(-1) else: version = [int(v) for v in res.lstrip('v').split('.')] return tuple(version)
def test_png_mode(): ''' Get image as a png binary, make sure no exception happened ''' client.connect() cmds = [ 'vget /camera/0/lit png', 'vget /camera/0/object_mask png', 'vget /camera/0/normal png', ] for cmd in cmds: res = client.request(cmd) assert checker.not_error(res) im = imread_png(res)
def main(): client.connect() res = client.request('vget /unrealcv/help') # format response lines = res.strip().split('\n') uris = lines[0::2] shorthelp = lines[1::2] content = format_table(uris, shorthelp) help_file = 'help.md' with open(help_file, 'w') as f: f.write(content) print 'Help for UnrealCV commands is saved to %s' % help_file
def test_file_mode(): """ Save data to disk as image file """ client.connect() cmds = [ 'vget /camera/0/lit test.png', 'vget /camera/0/object_mask test.png', 'vget /camera/0/normal test.png', 'vget /camera/0/depth test.png', ] for cmd in cmds: res = client.request(cmd) assert checker.not_error(res) im = imread_file(res)
def test_png_mode(): """ Get image as a png binary, make sure no exception happened """ client.connect() cmds = [ 'vget /camera/0/lit png', 'vget /camera/0/object_mask png', 'vget /camera/0/normal png', ] for cmd in cmds: res = client.request(cmd) assert checker.not_error(res) im = imread_png(res)
def test_file_mode(): ''' Save data to disk as image file ''' client.connect() cmds = [ 'vget /camera/0/lit test.png', 'vget /camera/0/object_mask test.png', 'vget /camera/0/normal test.png', 'vget /camera/0/depth test.png', ] for cmd in cmds: res = client.request(cmd) assert checker.not_error(res) im = imread_file(res)
def main(): client.connect() if not client.isconnected(): print("LOG: UnrealCV server is not running") else: root = Tk() #size of the window root.geometry("1200x610") app = Window(root) root.mainloop() client.disconnect()
def main(): def log(msg): # no need to use logging module for such a simple use case print(msg) report_file.write(msg + '\n') parser = argparse.ArgumentParser() parser.add_argument('--task', default='benchmark_task.json', help='A json file to define the task') parser.add_argument('--report', default='benchmark_report.txt') args = parser.parse_args() json_task = args.task report_filename = args.report try: data = json.load(open(json_task)) except Exception as e: print('Failed to parse task file %s' % json_task) print(e) return tasks = data.get('Tasks') report_file = open(report_filename, 'w') log(sys_info()) client.connect() for task in tasks: task_text = str(task) if task.get('Cmd') == None or task.get('Skip') == True: continue if task.get('Duration'): duration = task.get('Duration') task_result, sample_output = run_duration(task['Cmd'], duration) if task.get('Count'): count = task.get('Count') task_result, sample_output = run_count(task['Cmd'], count) sample_output = sample_output.split('\n')[0][:256] log(task_text) log(task_result) log(sample_output) log('-' * 80) report_file.close()
def main(): total_nr_objects = 68 max_nr_objects = 68 locations, rotations = generate_camera_positions() ensure_dir(base_folder + "Points/gt/%s/" % (sub_scale_folder)) ensure_dir(base_folder + "Calibration/cal18/") for c in range(len(locations)): RT = position_to_transform(locations[c], rotations[c]) P = np.matmul(baseK, RT) output_file = base_folder + "Calibration/cal18/pos_%03d.txt" % (c + 1, ) f = open(output_file, 'w') f.write("%f %f %f %f\n" % (P[0, 0], P[0, 1], P[0, 2], P[0, 3])) f.write("%f %f %f %f\n" % (P[1, 0], P[1, 1], P[1, 2], P[1, 3])) f.write("%f %f %f %f\n" % (P[2, 0], P[2, 1], P[2, 2], P[2, 3])) f.close() try: if not client.connect(): print("Could not connect to client! Exiting.") return set_all_invisible(total_nr_objects) set_resolution() for i in tqdm(range(1, max_nr_objects + 1), desc="Scanning"): set_visible_object(i) scan_path = 'scan%d' % (i, ) capture_camera_positions(scan_path, locations, rotations) finally: client.disconnect()
def main(): def log(msg): # no need to use logging module for such a simple use case print(msg) report_file.write(msg + '\n') parser = argparse.ArgumentParser() parser.add_argument('--task', default='benchmark_task.json', help = 'A json file to define the task') parser.add_argument('--report', default='benchmark_report.txt') args = parser.parse_args() json_task = args.task report_filename = args.report try: data = json.load(open(json_task)) except Exception as e: print('Failed to parse task file %s' % json_task) print(e) return tasks = data.get('Tasks') report_file = open(report_filename, 'w') log(sys_info()) client.connect() for task in tasks: task_text = str(task) if task.get('Cmd') == None or task.get('Skip') == True: continue if task.get('Duration'): duration = task.get('Duration') task_result, sample_output = run_duration(task['Cmd'], duration) if task.get('Count'): count = task.get('Count') task_result, sample_output = run_count(task['Cmd'], count) sample_output = sample_output.split('\n')[0][:256] log(task_text) log(task_result) log(sample_output) log('-' * 80) report_file.close()
def build(): default_environment_folder = UnrealCollector( "testing_default").environment_folder client.connect() if not client.isconnected(): raise RuntimeError("Could not connect to client. ") builder = Builder(default_environment_folder) points = builder.get_random_points(400) builder.build_from_points(points) new_environment_folder = UnrealCollector("testing_new").environment_folder builder = Builder(new_environment_folder) PreProcessor(new_environment_folder).preprocess() builder.build_from_points(points) client.disconnect()
def test_camera_distance(): client.connect() for test_distance in [20, 40, 60]: res = client.request('vset /action/eyes_distance %d' % test_distance) assert checker.is_ok(res) for _ in range(5): client.request('vset /camera/0/rotation %s' % str(random_vec3())) actor_loc = Vec3(client.request('vget /actor/location')) loc1 = Vec3(client.request('vget /camera/0/location')) loc2 = Vec3(client.request('vget /camera/1/location')) print('%s %s %s' % (actor_loc, loc1, loc2)) actual_dist = (loc1 - loc2).l2norm() expect_dist = test_distance assert approx(actual_dist, expect_dist) actor_cam0_distance = (actor_loc - loc1).l2norm() assert approx(actor_cam0_distance, 0)
def test_multi_connection(): ''' Only one client is allowed for the server Make a second connection to the server, when one connection exists ''' echo_server = MessageServer((localhost, echo_port)) echo_server.start() client = unrealcv.Client((localhost, echo_port)) client.connect(timeout=0.1) assert client.isconnected() == True response = client.request('hi') assert response == 'hi' for i in range(10): client = unrealcv.Client((localhost, echo_port)) client.connect(0.1) # print client.connect() assert client.isconnected() == False client.disconnect() echo_server.shutdown()
def main(): client.connect() if not client.isconnected(): print 'UnrealCV server is not running. Run the game from http://unrealcv.github.io first.' return else: print help_message init_loc = [float(v) for v in client.request('vget /camera/0/location').split(' ')] init_rot = [float(v) for v in client.request('vget /camera/0/rotation').split(' ')] global rot, loc loc = init_loc; rot = init_rot image = np.zeros((300, 300)) fig, ax = plt.subplots() fig.canvas.mpl_connect('key_press_event', onpress) ax.imshow(image) plt.title('Keep this window in focus, used to receive key press event') plt.axis('off') plt.show() # Add event handler
def main(): binary_path = r'C:\qiuwch\workspace\unrealcv\Binaries\RealisticRendering.uproject\WindowsNoEditor\RealisticRendering.exe' binary = UE4Binary(binary_path) playback_seq = PlaybackSequence('./rr_573.json') camera = Camera(client, 0) with binary: client.connect() # res = client.request('vget /camera/0/lit rr.png') # print(res) # # res = client.request('vget /camera/0/lit png') # img = read_png(res) # plt.imsave('lit.png', img) for frameid in range(len(playback_seq)): print('Capture frame %d' % frameid) [loc, rot] = playback_seq.get(frameid) camera.set_location(loc) camera.set_rotation(rot) img = camera.capture_img() depth = camera.capture_depth()
def test_message_handler(): ''' Check message handler can correctly handle events from the server. And the thread is correctly handled that we can do something in the message_handler ''' echo_server = MessageServer((localhost, echo_port)) echo_server.start() client = unrealcv.Client((localhost, echo_port)) def handle_message(msg): print('Got server message %s' % repr(msg)) res = unrealcv.client.request('ok', 1) assert res == 'ok' print('Server response %s' % res) client.connect() assert client.isconnected() == True client.message_handler = handle_message res = client.request('ok') assert res == 'ok' echo_server.send('Hello from server') time.sleep(5) echo_server.shutdown()
def main(img_processor): def capture_frame(func=None): res = client.request('vget /camera/0/lit png') im = read_png(res) return im def updatefig(*args): frame = capture_frame(img_processor) img_processor(ax, frame) # im.set_array() fig.canvas.draw() return ax, fig = plt.figure() client.connect() # global im # im = plt.imshow(capture_frame(), animated=True) ani = animation.FuncAnimation(fig, updatefig, interval=50, blit=True) # plt.show() fig, ax = plt.subplots() plt.axis('off') plt.tight_layout() plt.show()
def test_connection(): client.connect() res = client.request('vget /unrealcv/status') assert checker.not_error(res)
def test_echo(): client.connect() res = client.request('vget /unrealcv/echo test') assert res == 'test'
import sys, atexit, argparse, json, time sys.path.append('..') from unrealcv import client trajectory = [] def message_handler(message): if message == 'clicked': rot = [float(v) for v in client.request('vget /camera/0/rotation').split(' ')] loc = [float(v) for v in client.request('vget /camera/0/location').split(' ')] trajectory.append(dict(rotation = rot, location = loc)) def save_to_file(filename): if len(trajectory) != 0: with open(filename, 'w') as f: json.dump(trajectory, f, indent = 4) if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('--filename', default='camera-trajectory.json') args = parser.parse_args() atexit.register(save_to_file, args.filename) client.connect() client.message_handler = message_handler if not client.isconnected(): print 'Can not connect to the game, please run the game downloaded from http://unrealcv.github.io first' else: time.sleep(60 * 60 * 24)
from unrealcv import client client.connect() # Connect to the game if not client.isconnected(): # Check if the connection is successfully established print 'UnrealCV server is not running. Run the game from http://unrealcv.github.io first.' else: filename = client.request('vget /camera/0/lit') print 'Image is saved to %s' % filename for gt_type in ['normal', 'object_mask']: filename = client.request('vget /camera/0/%s' % gt_type) print '%s is saved to %s' % (gt_type, filename) filename = client.request('vget /camera/0/depth depth.exr') print 'depth is saved to %s' % filename # Depth needs to be saved to HDR image to ensure numerical accuracy
def main(): client.connect() client.request('vexec ArmController_C_0 ToggleRandomPose')