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 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(): 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 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 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 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 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_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()
cm.render_joints(skeletons, color1) skeletons = api.estimate_keypoints(color2, 192) cm.render_skeletons(skeletons, color2) cm.render_joints(skeletons, color2) skeletons = api.estimate_keypoints(color3, 192) cm.render_skeletons(skeletons, color3) cm.render_joints(skeletons, color3) skeletons = api.estimate_keypoints(color4, 192) cm.render_skeletons(skeletons, color4) cm.render_joints(skeletons, color4) h1 = np.hstack((color1, color2)) h2 = np.hstack((color3, color4)) images = np.vstack((h1, h2)) vid_writer.write(images) if cv2.waitKey(1) & 0xFF == ord('q'): break frame_count -= 1 finally: client.request(f'vrun slomo 1.0') client.disconnect() vid_writer.release() vid_writer_org.release() cv2.destroyAllWindows()
def client_disconnect(self): res = client.disconnect() return res
def reset_client(): # Free up unrealcv connection cleanly when shutting down client.disconnect() rospy.loginfo( 'On unreal_ros_client shutdown: disconnected unrealcv client.')
def __del__(self): client.disconnect()
def main(): client.connect() # Connect to the game if not client.isconnected(): # Check if the connection is successfully established print('UnrealCV server is not running.') sys.exit(-1) else: res = client.request('vget /unrealcv/status') # The image resolution and port is configured in the config file. print(res) traj_file = '../../docs/tutorials_source/camera_traj.json' camera_trajectory = json.load(open(traj_file)) idx = 1 loc, rot = camera_trajectory[idx] # Set position of the first camera client.request('vset /camera/0/location {x} {y} {z}'.format(**loc)) client.request('vset /camera/0/rotation {pitch} {yaw} {roll}'.format(**rot)) # Get image res = client.request('vget /camera/0/lit lit.png') print('The image is saved to %s' % res) im = read_png(res) # Generate Ground Truth res = client.request('vget /camera/0/object_mask object_mask.png') print('The image is saved to %s' % res) object_mask = read_png(res) res = client.request('vget /camera/0/normal normal.png') print('The image is saved to %s' % res) normal = read_png(res) # Generate Depth res = client.request('vget /camera/0/depth depth.png') print('The image is saved to %s' % res) # Get objects from the scene scene_objects = client.request('vget /objects').split(' ') print('Number of objects in this scene: ', len(scene_objects)) print('They are: ', scene_objects) # Map from object id to the labeling color id2color = {} for obj_id in scene_objects: color = Color(client.request('vget /object/%s/color' % obj_id)) id2color[obj_id] = color # print('%s : %s' % (obj_id, str(color))) id2mask = {} for obj_id in scene_objects: color = id2color[obj_id] mask = match_color(object_mask, [color.R, color.G, color.B], tolerance=3) if mask is not None: id2mask[obj_id] = mask obj_file = '../../docs/tutorials_source/object_category.json' with open(obj_file) as f: id2category = json.load(f) categories = set(id2category.values()) # Show statistics of this frame image_objects = id2mask.keys() print('Number of objects in this image:', len(image_objects)) print('%20s : %s' % ('Category name', 'Object name')) for category in categories: objects = [v for v in image_objects if id2category.get(v) == category] if len(objects) > 6: # Trim the list if too long objects[6:] = ['...'] if len(objects) != 0: print('%20s : %s' % (category, objects)) # Plot objs vase_instance = [v for v in image_objects if id2category.get(v) == 'Vase'] mask = sum(id2mask[v] for v in vase_instance) plt.figure() plt.imshow(mask, cmap="gray") plt.show() client.disconnect()