Example #1
0
    def __init__(self, config):
        # host = unrealcv.HOST
        # port = unrealcv.PORT
        #
        # if 'endpoint' in config:
        #     host, port = config['endpoint']
        # if 'port' in config:
        #     port = config['port']
        # if 'hostname' in config:
        #     host = config['hostname']

        host = '127.0.0.1'
        port = 9234

        self.opencv_bridge = cv_bridge.CvBridge()

        self._client_lock = threading.Lock()
        self._client = unrealcv.Client(('127.0.0.1', 9234))
        self._client.connect()
        if not self._client.isconnected():
            raise RuntimeError(
                "Could not connect to unrealcv simulator, is it running?")

        res = self._client.request('vget /unrealcv/status')
        print(res)

        # Store the declare services
        self._services = []
Example #2
0
def test_client_release(server):
    '''
    If the previous client release the connection, further connection should be accepted. This will also test the server code
    '''
    # server = MessageServer((localhost, echo_port))
    # server.start()
    client = unrealcv.Client((localhost, echo_port))

    num_release_trial = 3
    logger.info('Try to release client %d times' % num_release_trial)
    for i in range(num_release_trial):
        msg = 'Running trial %d' % i
        client.connect()
        assert client.isconnected() == True, \
            'Client is not successfully connected in trial %d' % i

        # 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, \
            'Client is not successfully disconnected in trial %d' % i
        # Make sure the server can detect client connection also
        time.sleep(0.5)  # Wait for the server to detect the disconnection
        assert server.get_client_socket() == None, \
            'Server can not detect client disconnect event.'
        logger.info('Trial %d is finished.' % i)
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()
Example #4
0
    def __init__(self,
                 cam_id=0,
                 ip='127.0.0.1',
                 targets=None,
                 env='/home/zfw/Documents/Realistic5'):
        global client
        if ip == '127.0.0.1':
            self.docker = False
        else:
            self.docker = True
        client = unrealcv.Client((ip, 9000))
        self.cam_id = cam_id
        self.envdir = env

        self.cam = dict(
            id=cam_id,
            position=[0, 0, 0],
            x=0,
            y=0,
            z=0,
            roll=0,
            yaw=0,
            pitch=0,
        )

        self.init_unrealcv()
        self.pitch = 0  #-30
        self.color_dict = dict()
        if targets:
            self.color_dict = self.target_color_dic(targets)
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()
Example #6
0
 def setUpClass(cls):
     print cls.port
     cls.test_cmd = 'vset /mode/depth'
     unrealcv.client = unrealcv.Client(('localhost', cls.port))
     unrealcv.client.connect()
     if not unrealcv.client.isconnected():
         raise Exception('Fail to connect to UE4CVServer, check whether server is running and whether a connection exists')
Example #7
0
def test_multi_connection(server):
    '''
    Only one client is allowed for the server
    Make a second connection to the server, when one connection exists
    '''
    # server = MessageServer((localhost, echo_port))
    # 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()
Example #8
0
def test_request(server):
    ''' Simple test for basic functions '''
    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
Example #9
0
 def test_request_timeout(self):
     '''
     In which case the request will timeout?
     If the server did not respond with a correct reply.
     '''
     client = unrealcv.Client((self.host, self.null_port))
     client.connect()
     self.assertEqual(client.isconnected(), True)
     response = client.request('hi', timeout = 1)
     self.assertEqual(response, None)
Example #10
0
 def test_multiple_connection(self):
     unrealcv.client.disconnect()
     unrealcv.client.connect()
     self.assertTrue(unrealcv.client.isconnected())
     response = unrealcv.client.request(self.test_cmd)
     self.assertEqual(response, 'ok')
     for i in range(10):
         client = unrealcv.Client((self.host, self.port))
         client.connect()
         # print client.connect()
         self.assertEqual(client.isconnected(), False)
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_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
Example #13
0
    def test_no_server(self):
        # Suppress error message

        client = unrealcv.Client((self.host, self.no_port), None)
        client.connect()
        self.assertEqual(client.isconnected(), False)
        tasks = [
            ['hi', None],
            ['hello', None],
        ]

        run_tasks(self, client, tasks)
Example #14
0
    def __init__(self,
                 K_arr=np.array([[320, 0, 320], [0, 320, 240], [0, 0, 1]]),
                 init_pose: Pose3 = Pose3()):
        self.camera_K = np.float32(K_arr)
        self.init_pose = init_pose
        self.client = unrealcv.Client(('127.0.0.1', 9000))
        self.cam_id = 0
        self.cam = dict(position=[0, 0, 0],
                        rotation=[0, 0, 0])  # center 6D representation

        import tempfile
        self.temp_dir = tempfile.mkdtemp()
Example #15
0
 def setUpClass(cls):
     cls.echo_port = 9010
     # Message sent to here will be echoed back
     cls.null_port = 9011
     # Message sent to here will not get any reply
     cls.no_port = 9012
     # No server is running on this port
     cls.host = 'localhost'
     cls.echo_server = MessageServer((cls.host, cls.echo_port))
     cls.echo_server.start()
     cls.null_server = NullServer((cls.host, cls.null_port))
     cls.null_server.start()
     unrealcv.client = unrealcv.Client((cls.host, cls.echo_port))
Example #16
0
 def __init__(self,
              port=9000,
              cam_id=0,
              ip='127.0.0.1',
              message_handler=None):
     global client
     client = unrealcv.Client((ip, port), message_handler)
     self.cam_id = cam_id
     self.rotation = (0, 0, 0)
     self.position = [0, 0, 0]
     self.ip = ip
     self.direction = ' '
     self.init_unrealcv()
Example #17
0
 def test_multiple_connection(self):
     '''
     Only one client is allowed for the server
     Make a second connection to the server, when one connection exists
     '''
     unrealcv.client.disconnect()
     unrealcv.client.connect(timeout=0.5)
     self.assertTrue(unrealcv.client.isconnected())
     response = unrealcv.client.request('hi')
     self.assertEqual(response, 'hi')
     for i in range(10):
         client = unrealcv.Client((self.host, self.echo_port))
         client.connect(0.1)
         # print client.connect()
         self.assertEqual(client.isconnected(), False)
Example #18
0
def main():
    client = unrealcv.Client(('localhost', 9000))
    client.connect()
    util = UnrealcvUtil(client)

    util.DEBUG = False
    util.clear()
    mesh_folder = "/Game/ShapenetManual/"  # need to end with /
    meta_folder = os.path.abspath(data_dir + 'ShapenetManual_metadata/')

    mesh_ids = list_mesh_id()
    ims = []
    segs = []
    for mesh_id in tqdm(mesh_ids):
        util.clear()
        obj_id = 'demo_car'
        util.make_demo_car(obj_id)
        util.request(
            'vset /car/{obj_id}/mesh/folder {mesh_folder} {meta_folder}'.
            format(**locals()))
        util.request('vset /car/{obj_id}/mesh/id {mesh_id}'.format(**locals()))
        util.request('vset /car/{obj_id}/door/angles 30 30 30 30 30 30'.format(
            **locals()))
        util.request(
            'vset /object/{obj_id}/location 0 0 130'.format(**locals()))
        util.set_car_part_seg_color(
            obj_id,
            [0, 0, 0],
            [0, 0, 128],  # fl, fr
            [0, 128, 0],
            [0, 128, 128],  # bl, br 
            [128, 0, 0],
            [128, 0, 128],  # hood, trunk
            [128, 128, 0]  # body
        )
        util.cam_track_obj(obj_id, 45 + 180, 45, 300)

        ims.append(util.get_im())
        segs.append(util.get_seg())
        # imageio.imwrite(mesh_id + '_im.png', util.get_im())
        # imageio.imwrite(mesh_id + '_seg.png', util.get_seg())

    fig = ipynb_util.imshow_grid(ims, 3)
    fig.savefig('car_shapenet_im.png')
    fig = ipynb_util.imshow_grid(segs, 3)
    fig.savefig('car_shapenet_seg.png')
Example #19
0
    def __init__(self,
                 port=9000,
                 cam_id=0,
                 ip='127.0.0.1',
                 targets=None,
                 env='/home/zfw/Documents/Realistic5'):
        global client
        if ip == '127.0.0.1':
            self.docker = False
        else:
            self.docker = True
        client = unrealcv.Client((ip, port))
        self.cam_id = cam_id
        self.envdir = env
        self.ip = ip

        self.cam = dict(
            id=cam_id,
            position=[0, 0, 0],
            x=0,
            y=0,
            z=0,
            roll=0,
            yaw=0,
            pitch=0,
        )

        self.arm = dict(pose=np.zeros(5),
                        flag_pose=False,
                        grip=np.zeros(3),
                        flag_grip=False)

        self.init_unrealcv()
        self.pitch = 0  #-30
        self.color_dict = dict()
        self.targets = []
        if targets == 'all':
            self.targets = self.get_objects()
            self.color_dict = self.target_color_dic(self.targets)
        elif targets is not None:
            self.targets = targets
            self.color_dict = self.target_color_dic(self.targets)

        self.message = []
Example #20
0
def connect_client(host, port):

    print("Connecting to UnrealCV client...")
    client_ = unrealcv.Client((host, port))
    print("Client created...")
    client_.connect()

    if not client_.isconnected():

        print("Could not connect UnrealCV client with host " + host + ":" +
              str(port))
        sys.exit()

    else:

        print("Client connected!")
        res = client_.request('vget /unrealcv/status')
        print(res)

    return client_
Example #21
0
def test_message_handler(server):
    ''' 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'

    server.send('Hello from server')
Example #22
0
    def __init__(self, port, ip, env, cam_id, resolution):

        if ip == '127.0.0.1':
            self.docker = False
        else:
            self.docker = True
        self.client = unrealcv.Client((ip, port))

        self.envdir = env
        self.ip = ip

        self.cam = dict()
        for i in range(3):
            self.cam[i] = dict(
                location=[0, 0, 0],
                rotation=[0, 0, 0],
            )

        self.init_unrealcv(cam_id, resolution)
        self.pitch = 0  #-30

        self.message = []
Example #23
0
    def __init__(self, port, ip, env, cam_id, resolution):

        if ip == '127.0.0.1':
            self.docker = False
        else:
            self.docker = True
        self.client = unrealcv.Client((ip, port))

        self.envdir = env
        self.ip = ip
        print(self.ip)
        self.cam = dict()
        for i in range(5):
            self.cam[i] = dict(
                location=[0, 0, 0],
                rotation=[0, 0, 0],
            )

        self.init_unrealcv(cam_id, resolution)
        self.pitch = 0
        self.resolution = resolution
        self.img_color = None
        self.img_depth = None
Example #24
0
def test_random_operation(server):
    """ Randomly connect and disconnect the client, this is very likely to fail """
    # server = MessageServer((localhost, echo_port))
    # server.start()
    client = unrealcv.Client((localhost, echo_port))

    num_random_trial = 3
    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()
            assert client.isconnected(), msg
        elif choice == 0:
            client.disconnect()
            assert not client.isconnected(), msg

    for i in range(3):
        client.connect()
        assert client.isconnected()
    for i in range(3):
        client.disconnect()
        assert not client.isconnected()
 def __init__(self, endpoint):
     self.client = unrealcv.Client(endpoint)
     self.logs = []
     self.DEBUG = False
     self.benchmark = True
def connect(ip='localhost', port=9000):
    global client
    client = unrealcv.Client((ip, port))
    client.connect()
Example #27
0
    def begin(self):
        """
        Start producing images.
        This will trigger any necessary startup code,
        and will allow get_next_image to be called.
        Return False if there is a problem starting the source.
        :return: True iff the simulator has started correctly.
        """
        # Start the simulator process
        if self._host == 'localhost':
            if self._client is not None:
                # Stop the client to free up the port
                self._client.disconnect()
                self._client = None
            self._store_config()
            start_simulator(self._executable)
            # Wait for the UnrealCV server to start, pulling lines from stdout to check
            time.sleep(
                2)  # Wait, we can't capture some of the output right now
#            while self._simulator_process.poll() is not None:
#                line = self._simulator_process.stdout.readline()
#                if 'listening on port {0}'.format(self._port) in line.lower():
#                    # Server has started, break
#                    break

# Create and connect the client
        if self._client is None:
            self._client = unrealcv.Client((self._host, self._port))
        if not self._client.isconnected():
            # Try and connect to the server 3 times, waiting 2s between tries
            for _ in range(10):
                self._client.connect()
                if self._client.isconnected():
                    break
                else:
                    time.sleep(2)
            if not self._client.isconnected():
                # Cannot connect to the server, shutdown completely.
                self.shutdown()

        # Set camera state from configuration
        if self._client is not None and self._client.isconnected():
            # Set camera properties
            self.set_field_of_view(self._fov)
            self.set_fstop(self._aperture)
            self.set_enable_dof(self._use_dof)
            if self._use_dof:  # Don't bother setting dof properties if dof is disabled
                if self._focus_distance is None:
                    self.set_autofocus(True)
                else:
                    self.set_focus_distance(self._focus_distance)

            # Set quality settings
            self._client.request(
                "vset /quality/texture-mipmap-bias {0}".format(
                    self._texture_mipmap_bias))
            self._client.request(
                "vset /quality/normal-maps-enabled {0}".format(
                    int(self._normal_maps_enabled)))
            self._client.request("vset /quality/roughness-enabled {0}".format(
                int(self._roughness_enabled)))
            self._client.request(
                "vset /quality/geometry-decimation {0}".format(
                    self._geometry_decimation))

            # Get the initial camera pose, which will be set by playerstart in the level
            location = self._client.request("vget /camera/0/location")
            location = location.split(' ')
            if len(location) == 3:
                rotation = self._client.request("vget /camera/0/rotation")
                rotation = rotation.split(' ')
                if len(rotation) == 3:
                    ue_trans = uetf.UnrealTransform(
                        location=(float(location[0]), float(location[1]),
                                  float(location[2])),
                        # Reorder to roll, pitch, yaw, Unrealcv order is pitch, yaw, roll
                        rotation=(float(rotation[2]), float(rotation[0]),
                                  float(location[1])))
                    self._current_pose = uetf.transform_from_unreal(ue_trans)
        return self._client is not None and self._client.isconnected()
    parser.add_argument('--out_dir', type=str, required=True)

    parser.add_argument('--Hz', type=float, default=10)
    parser.add_argument('--timeout', type=int, default=30)
    parser.add_argument('--save_pref', type=str, default='poses')
    args = parser.parse_args()

    assert os.path.exists(args.out_dir)
    ue_pose_fn = os.path.join(args.out_dir,
                              '{}_xyzpyr_ue.txt'.format(args.save_pref))
    Twc_fn = os.path.join(args.out_dir, '{}_Twc.txt'.format(args.save_pref))
    uepose_f = open(ue_pose_fn, 'w')
    Twc_f = open(Twc_fn, 'w')
    save_int_ms = int(1.0 / args.Hz * 1000)

    client = unrealcv.Client(('127.0.0.1', 9000))
    client.connect()
    assert client.isconnected()
    st = uu.getUnrealcvStatus(client)
    print(Fore.GREEN + st)

    print(Fore.YELLOW +
          "Going to read poses from client every {} ms and save to {}.".format(
              save_int_ms, ue_pose_fn))

    save_cnt = 0
    positions = []
    try:
        for i in xrange(args.timeout * 1000, 0, -1):
            time.sleep(1e-3)
            if i % save_int_ms == 0:
Example #29
0
# Weichao Qiu @ 2019
# Script to show all car appearance
import unrealcv
from unrealcv.util import read_png, read_npy
from unrealcv_util import request, clear, get_im, show_im
import imageio
from tqdm import tqdm
import os, sys

client = unrealcv.Client(('localhost', 9000))
client.connect()


def set_angle(fl, fr, bl, br, hood, trunk):
    client.request(
        'vset /car/0/door/angles {fl} {fr} {bl} {br} {hood} {trunk}'.format(
            **locals()))


def set_mesh_id(mesh_id):
    # The mesh_dir needs to end with /
    client.request('vset /car/0/mesh/id {mesh_id}'.format(**locals()))


def get_image(dist, az, el):
    client.request('vset /car/0/camera {dist} {az} {el}'.format(**locals()))
    lit = client.request('vget /car/0/image'.format(**locals()))
    lit = read_png(lit)
    return lit