Cameras
Description
Cameras in the Open Worlds are used for observing the workspace from different views and capturing image data. Cameras are considered “actors”. The camera library controls the camera actors.
A camera must be initialized first by spawning the camera in the location (or attached to the parent actor of choice) in order to use it.
See the Camera Tutorial to get a better understanding of using cameras in Quanser Interactive Labs.
Camera Library
Constants
- QLabsFreeCamera.ID_FREE_CAMERA = 170
Class ID
Member Variables
- QLabsFreeCamera.actorNumber = None
The current actor number of this class to be addressed. This will be set by spawn methods and cleared by destroy methods. It will not be modified by the destroy all actors. This can be manually altered at any time to use one object to address multiple actors.
Methods
- QLabsFreeCamera.__init__(qlabs, verbose=False)[source]
Constructor Method
- Parameters
qlabs (object) – A QuanserInteractiveLabs object
verbose (boolean) – (Optional) Print error information to the console.
- QLabsFreeCamera.spawn(location=[0, 0, 0], rotation=[0, 0, 0], scale=[1, 1, 1], configuration=0, waitForConfirmation=True)
Spawns a new actor with the next available actor number within this class.
- Parameters
location (float array[3]) – (Optional) An array of floats for x, y and z coordinates
rotation (float array[3]) – (Optional) An array of floats for the roll, pitch, and yaw in radians
scale (float array[3]) – (Optional) An array of floats for the scale in the x, y, and z directions. Scale values of 0.0 should not be used.
configuration (uint32) – (Optional) Spawn configuration. See class library for configuration options.
waitForConfirmation (boolean) – (Optional) Make this operation blocking until confirmation of the spawn has occurred. Note that if this is False, the returned actor number will be invalid.
- Returns
status - 0 if successful, 1 class not available, 3 unknown error, -1 communications error.
actorNumber - An actor number to use for future references.
- Return type
int32, int32
Tip
Scale will not affect the camera, but will affect attached children.
- QLabsFreeCamera.spawn_degrees(location=[0, 0, 0], rotation=[0, 0, 0], scale=[1, 1, 1], configuration=0, waitForConfirmation=True)
Spawns a new actor with the next available actor number within this class.
- Parameters
location (float array[3]) – (Optional) An array of floats for x, y and z coordinates
rotation (float array[3]) – (Optional) An array of floats for the roll, pitch, and yaw in degrees
scale (float array[3]) – (Optional) An array of floats for the scale in the x, y, and z directions. Scale values of 0.0 should not be used.
configuration (uint32) – (Optional) Spawn configuration. See class library for configuration options.
waitForConfirmation (boolean) – (Optional) Make this operation blocking until confirmation of the spawn has occurred. Note that if this is False, the returned actor number will be invalid.
- Returns
status - 0 if successful, 1 class not available, 3 unknown error, -1 communications error.
actorNumber - An actor number to use for future references.
- Return type
int32, int32
Tip
Scale will not affect the camera, but will affect attached children.
- QLabsFreeCamera.spawn_id(actorNumber, location=[0, 0, 0], rotation=[0, 0, 0], scale=[1, 1, 1], configuration=0, waitForConfirmation=True)
Spawns a new actor.
- Parameters
actorNumber (uint32) – User defined unique identifier for the class actor in QLabs
location (float array[3]) – (Optional) An array of floats for x, y and z coordinates
rotation (float array[3]) – (Optional) An array of floats for the roll, pitch, and yaw in radians
scale (float array[3]) – (Optional) An array of floats for the scale in the x, y, and z directions. Scale values of 0.0 should not be used.
configuration (uint32) – (Optional) Spawn configuration. See class library for configuration options.
waitForConfirmation (boolean) – (Optional) Make this operation blocking until confirmation of the spawn has occurred.
- Returns
status - 0 if successful, 1 class not available, 2 actor number not available or already in use, 3 unknown error, -1 communications error
- Return type
int32
Tip
Scale will not affect the camera, but will affect attached children.
- QLabsFreeCamera.spawn_id_degrees(actorNumber, location=[0, 0, 0], rotation=[0, 0, 0], scale=[1, 1, 1], configuration=0, waitForConfirmation=True)
Spawns a new actor.
- Parameters
actorNumber (uint32) – User defined unique identifier for the class actor in QLabs
location (float array[3]) – (Optional) An array of floats for x, y and z coordinates
rotation (float array[3]) – (Optional) An array of floats for the roll, pitch, and yaw in radians
scale (float array[3]) – (Optional) An array of floats for the scale in the x, y, and z directions. Scale values of 0.0 should not be used.
configuration (uint32) – (Optional) Spawn configuration. See class library for configuration options.
waitForConfirmation (boolean) – (Optional) Make this operation blocking until confirmation of the spawn has occurred.
- Returns
status - 0 if successful, 1 class not available, 2 actor number not available or already in use, 3 unknown error, -1 communications error
- Return type
int32
Tip
Scale will not affect the camera, but will affect attached children.
- QLabsFreeCamera.spawn_id_and_parent_with_relative_transform(actorNumber, location=[0, 0, 0], rotation=[0, 0, 0], scale=[1, 1, 1], configuration=0, parentClassID=0, parentActorNumber=0, parentComponent=0, waitForConfirmation=True)
Spawns a new actor relative to an existing actor and creates a kinematic relationship.
- Parameters
actorNumber (uint32) – User defined unique identifier for the class actor in QLabs
location (float array[3]) – (Optional) An array of floats for x, y and z coordinates
rotation (float array[3]) – (Optional) An array of floats for the roll, pitch, and yaw in radians
scale (float array[3]) – (Optional) An array of floats for the scale in the x, y, and z directions. Scale values of 0.0 should not be used.
configuration (uint32) – (Optional) Spawn configuration. See class library for configuration options.
parentClassID (uint32) – See the ID variables in the respective library classes for the class identifier
parentActorNumber (uint32) – User defined unique identifier for the class actor in QLabs
parentComponent (uint32) – 0 for the origin of the parent actor, see the parent class for additional reference frame options
waitForConfirmation (boolean) – (Optional) Make this operation blocking until confirmation of the spawn has occurred.
- Returns
status - 0 if successful, 1 class not available, 2 actor number not available or already in use, 3 cannot find the parent actor, 4 unknown error, -1 communications error
- Return type
int32
- QLabsFreeCamera.spawn_id_and_parent_with_relative_transform_degrees(actorNumber, location=[0, 0, 0], rotation=[0, 0, 0], scale=[1, 1, 1], configuration=0, parentClassID=0, parentActorNumber=0, parentComponent=0, waitForConfirmation=True)
Spawns a new actor relative to an existing actor and creates a kinematic relationship.
- Parameters
actorNumber (uint32) – User defined unique identifier for the class actor in QLabs
location (float array[3]) – (Optional) An array of floats for x, y and z coordinates
rotation (float array[3]) – (Optional) An array of floats for the roll, pitch, and yaw in degrees
scale (float array[3]) – (Optional) An array of floats for the scale in the x, y, and z directions. Scale values of 0.0 should not be used.
configuration (uint32) – (Optional) Spawn configuration. See class library for configuration options.
parentClassID (uint32) – See the ID variables in the respective library classes for the class identifier
parentActorNumber (uint32) – User defined unique identifier for the class actor in QLabs
parentComponent (uint32) – 0 for the origin of the parent actor, see the parent class for additional reference frame options
waitForConfirmation (boolean) – (Optional) Make this operation blocking until confirmation of the spawn has occurred.
- Returns
status - 0 if successful, 1 class not available, 2 actor number not available or already in use, 3 cannot find the parent actor, 4 unknown error, -1 communications error
- Return type
int32
- QLabsFreeCamera.possess()[source]
Possess (take control of) a camera in QLabs.
- Returns
True if possessing the camera was successful, False otherwise
- Return type
boolean
- QLabsFreeCamera.set_camera_properties(fieldOfView, depthOfField, aperture, focusDistance)[source]
Sets the camera properties. When depthOfField is enabled, the camera will produce more realistic (and cinematic) results by adding some blur to the view at distances closer and further away from a given focal distance. For more blur, use a large aperture (small value) and a narrow field of view.
- Parameters
fieldOfView (int) – The field of view that the camera can see (range:5-150 degrees). When depthOfField is True, smaller values will increase focal blur at distances relative to the focusDistance.
depthOfField (boolean) – Enable or disable the depth of field visual effect
aperture (float) – The amount of light allowed into the camera sensor (range:2.0-22.0). Smaller values (larger apertures) increase the light and decrease the depth of field. This parameter is only active when depthOfField is True.
focusDistance (float) – The distance to the focal plane of the camera. (range:0.1-50.0 meters). This parameter is only active when depthOfField is True.
- Returns
True if setting the camera properties was successful, False otherwise
- Return type
boolean
- QLabsFreeCamera.set_transform(location, rotation)[source]
Change the location and rotation of a spawned camera in radians
- Parameters
location (array[3]) – An array of floats for x, y and z coordinates
rotation (array[3]) – An array of floats for the roll, pitch, yaw in radians
- Returns
True if spawn was successful, False otherwise
- Return type
boolean
- QLabsFreeCamera.set_transform_degrees(location, rotation)[source]
Change the location and rotation of a spawned camera in degrees
- Parameters
location (array[3]) – An array of floats for x, y and z coordinates
rotation (array[3]) – An array of floats for the roll, pitch, yaw in degrees
- Returns
True if spawn was successful, False otherwise
- Return type
boolean
- QLabsFreeCamera.destroy()
Find and destroy a specific actor. This is a blocking operation.
- Returns
numActorsDestroyed - The number of actors destroyed. -1 if failed.
- Return type
int32
- QLabsFreeCamera.destroy_all_actors_of_class()
Find and destroy all actors of this class. This is a blocking operation.
- Returns
numActorsDestroyed - The number of actors destroyed. -1 if failed.
- Return type
int32
- QLabsFreeCamera.ping()
Checks if the actor is still present in the environment. Note that if you did not spawn the actor with one of the spawn functions, you may need to manually set the actorNumber member variable.
- Returns
status - True if successful, False otherwise
- Return type
boolean
- QLabsFreeCamera.get_world_transform()
Get the location, rotation, and scale in world coordinates of the actor.
- Returns
status - True if successful, False otherwise
location
rotation
scale
- Return type
boolean, float array[3], float array[3], float array[3]
- QLabsFreeCamera.get_world_transform_degrees()
Get the location, rotation, and scale in world coordinates of the actor.
- Returns
status - True if successful, False otherwise
location
rotation
scale
- Return type
boolean, float array[3], float array[3], float array[3]
- QLabsFreeCamera.set_image_capture_resolution(width=640, height=480)[source]
Change the default width and height of image resolution for capture
- Parameters
width (uint32) – Must be an even number. Default 640
height (uint32) – Must be an even number. Default 480
- Returns
True if spawn was successful, False otherwise
- Return type
boolean
- QLabsFreeCamera.get_image()[source]
Request an image from the camera actor. Note, set_image_capture_resolution must be set once per camera otherwise this method will fail.
- Returns
Success, RGB image data
- Return type
boolean, byte array[variable]
- QLabsFreeCamera.parent_with_relative_transform(location=[0, 0, 0], rotation=[0, 0, 0], scale=[1, 1, 1], parentClassID=0, parentActorNumber=0, parentComponent=0, waitForConfirmation=True)
Parents one existing actor to another to create a kinematic relationship.
- Parameters
location (float array[3]) – (Optional) An array of floats for x, y and z coordinates
rotation (float array[3]) – (Optional) An array of floats for the roll, pitch, and yaw in radians
scale (float array[3]) – (Optional) An array of floats for the scale in the x, y, and z directions. Scale values of 0.0 should not be used.
parentClassID (uint32) – See the ID variables in the respective library classes for the class identifier
parentActorNumber (uint32) – User defined unique identifier for the class actor in QLabs
parentComponent (uint32) – 0 for the origin of the parent actor, see the parent class for additional reference frame options
waitForConfirmation (boolean) – (Optional) Make this operation blocking until confirmation of the spawn has occurred.
- Returns
status - 0 if successful, 1 cannot find this actor, 2 cannot find the parent actor, 3 unknown error, -1 communications error
- Return type
int32
- QLabsFreeCamera.parent_with_relative_transform_degrees(location=[0, 0, 0], rotation=[0, 0, 0], scale=[1, 1, 1], parentClassID=0, parentActorNumber=0, parentComponent=0, waitForConfirmation=True)
Parents one existing actor to another to create a kinematic relationship.
- Parameters
location (float array[3]) – (Optional) An array of floats for x, y and z coordinates
rotation (float array[3]) – (Optional) An array of floats for the roll, pitch, and yaw in degrees
scale (float array[3]) – (Optional) An array of floats for the scale in the x, y, and z directions. Scale values of 0.0 should not be used.
parentClassID (uint32) – (Optional) See the ID variables in the respective library classes for the class identifier
parentActorNumber (uint32) – (Optional) User defined unique identifier for the class actor in QLabs
parentComponent (uint32) – (Optional) 0 for the origin of the parent actor, see the parent class for additional reference frame options
waitForConfirmation (boolean) – (Optional) Make this operation blocking until confirmation of the spawn has occurred.
- Returns
status - 0 if successful, 1 cannot find this actor, 2 cannot find the parent actor, 3 unknown error, -1 communications error
- Return type
int32
- QLabsFreeCamera.parent_with_current_world_transform(parentClassID=0, parentActorNumber=0, parentComponent=0, waitForConfirmation=True)
Parents one existing actor to another to create a kinematic relationship while preserving the current world transform of the child actor.
- Parameters
parentClassID (uint32) – See the ID variables in the respective library classes for the class identifier
parentActorNumber (uint32) – User defined unique identifier for the class actor in QLabs
parentComponent (uint32) – 0 for the origin of the parent actor, see the parent class for additional reference frame options
waitForConfirmation (boolean) – (Optional) Make this operation blocking until confirmation of the spawn has occurred.
- Returns
status - 0 if successful, 1 cannot find this actor, 2 cannot find the parent actor, 3 unknown error, -1 communications error
- Return type
int32
- QLabsFreeCamera.parent_break(waitForConfirmation=True)
Breaks any relationship with a parent actor (if it exists) and preserves the current world transform
- Parameters
waitForConfirmation (boolean) – (Optional) Make this operation blocking until confirmation of the spawn has occurred.
- Returns
status - 0 if successful, 1 cannot find this actor, -1 communications error
- Return type
int32
Configurations
There are no configuration options for the camera actor.
Connection Points
There are no connection points for the free camera actor.
Tip
There is a few easy steps to initializing a new camera in an environment using the interface as well as the code:
Pick a Location for your camera using the Coordinate Helper section.
Copy the location and rotation desired.
Use spawn or spawn_degrees to initialize a new camera. Paste the copied location and rotation into their respective places.
Camera Tutorial
Python Tutorial
Raw to download this tutorial: Camera Tutorial (.py)
.
1"""
2Free Camera Library Example
3---------------------------
4
5.. note::
6
7 Make sure you have Quanser Interactive Labs open before running this
8 example. This example is designed to best be run in QCar Cityscape.
9
10"""
11# imports to important libraries
12import time
13import cv2
14
15from qvl.qlabs import QuanserInteractiveLabs
16from qvl.free_camera import QLabsFreeCamera
17from qvl.system import QLabsSystem
18
19def main():
20
21 # creates a server connection with Quanser Interactive Labs and manages
22 # the communications
23 qlabs = QuanserInteractiveLabs()
24
25
26
27 # initialize our desired variables
28 # note that you can use the coordinate helper to pick locations for your camera.
29 loc = [-5.631, -1.467, 2.198]
30 rot = [0, -2.386, -20.528]
31
32 # trying to connect to QLabs and open the instance we have created - program will end if this fails
33 print("Connecting to QLabs...")
34 try:
35 qlabs.open("localhost")
36 except:
37 print("Unable to connect to QLabs")
38 return
39
40 # destroy any spawned actors in our QLabs that currently exist
41 qlabs.destroy_all_spawned_actors()
42
43 title = QLabsSystem(qlabs)
44 title.set_title_string("Camera Tutorial")
45
46 # create a camera in this qlabs instance
47 camera = QLabsFreeCamera(qlabs)
48 # add a custom camera at a specified location and rotation using degrees
49 camera.spawn_degrees(location=loc, rotation=rot)
50 # to switch our view from our current camera to the new camera we just initialized
51 camera.possess()
52
53 time.sleep(3)
54
55 camera.set_camera_properties(60, 1, 2.0, 1.3)
56
57 [success, location, rotation, scale] = camera.get_world_transform()
58
59 ping = camera.ping()
60
61 # Take a image from the first camera angle
62 if ping:
63 camera.set_image_capture_resolution()
64 [success, image1] = camera.get_image()
65
66 if not success:
67 print("Image decoding failure")
68 # if success:
69 # cv2.imshow('image1',image1)
70 # else:
71 # print("Image decoding failure")
72
73
74 time.sleep(2)
75
76 # Initialize and spawn the second camera angle
77 loc2 = [-33.17276819, 13.50500671, 2.282]
78 rot2 = [0, 0.077, 0.564]
79 camera2 = QLabsFreeCamera(qlabs)
80 x = camera2.spawn_id(2, loc2, rot2)
81 camera2.possess()
82
83 time.sleep(2)
84
85 camera.destroy()
86
87 [success, location, rotation, scale] = camera2.get_world_transform()
88 camera2.set_camera_properties(40, True, 2.3, 0.6)
89
90 time.sleep(2)
91
92 # Focus the camera
93 for y in range(1, 52):
94 camera2.set_camera_properties(40, True, 2.3, (0.6 + ((y / 50) ** 3) * 23.7))
95
96 camera.set_image_capture_resolution()
97
98 # Take image of the second camera angle
99 [success, image2] = camera.get_image()
100 print(success)
101
102 time.sleep(2)
103
104 # Initialize and spawn the third camera angle
105 camera3 = QLabsFreeCamera(qlabs)
106 loc3 = [-21.456, 31.995, 3.745]
107 rot3 = [0, 18.814, 0.326]
108 camera3.spawn_degrees(loc3, rot3)
109 camera3.possess()
110
111 # Display a image of the first camera angle
112 cv2.imshow('image1',image1)
113 cv2.waitKey(0)
114
115 # Closing qlabs
116 qlabs.close()
117 print('Done!')
118
119if __name__ == "__main__":
120 main()
Matlab Tutorial
Raw to download this tutorial: Camera Tutorial (.m)
.
1% Free Camera Library Example
2% -------------------------
3%
4% .. note::
5%
6% Make sure you have Quanser Interactive Labs open before running this
7% example. This example is designed to best be run in QCar Cityscape.
8
9close all;
10clear all;
11clc;
12
13% --------------------------------------------------------------
14% Setting MATLAB Path for the libraries
15% Always keep at the start, it will make sure it finds the correct references
16newPathEntry = fullfile(getenv('QAL_DIR'), 'libraries', 'matlab', 'qvl');
17pathCell = regexp(path, pathsep, 'split');
18if ispc % Windows is not case-sensitive
19 onPath = any(strcmpi(newPathEntry, pathCell));
20else
21 onPath = any(strcmp(newPathEntry, pathCell));
22end
23
24if onPath == 0
25 path(path, newPathEntry)
26 savepath
27end
28% --------------------------------------------------------------
29
30fprintf('\n\n----------------- Communications -------------------\n\n');
31
32qlabs = QuanserInteractiveLabs();
33connection_established = qlabs.open('localhost');
34
35if connection_established == false
36 disp("Failed to open connection.")
37 return
38end
39
40
41disp('Connected')
42
43num_destroyed = qlabs.destroy_all_spawned_actors();
44
45fprintf('%d actors destroyed', num_destroyed);
46
47% Use hSystem to set the tutorial title on the qlabs display screen
48hSystem = QLabsSystem(qlabs);
49hSystem.set_title_string('Camera Tutorial')
50
51% Create a camera in this qlabs instance
52camera = QLabsFreeCamera(qlabs);
53
54% Add a custom camera at a specified location and rotation using degrees
55location = [-5.631, -1.467, 2.198];
56rotation = [0, -2.386, -20.528];
57camera.spawn_degrees(location, rotation);
58
59% Switch our view from our current camera to the new camera we just initialized
60camera.possess();
61
62pause(3);
63
64camera.set_camera_properties(60, 1, 2.0, 1.3);
65
66[success, location, rotation, scale] = camera.get_world_transform();
67
68ping = camera.ping();
69
70% Take a image from the first camera angle
71if ping
72 camera.set_image_capture_resolution();
73 [success, image1] = camera.get_image();
74
75 if ~success
76 disp('Image decoding failure');
77 end
78end
79
80pause(2);
81
82% Initialize and spawn the second camera angle
83loc2 = [-33.17276819, 13.50500671, 2.282];
84rot2 = [0, 0.077, 0.564];
85camera2 = QLabsFreeCamera(qlabs);
86x = camera2.spawn_id(2, loc2, rot2);
87camera2.possess();
88
89pause(2);
90
91camera.destroy();
92
93[success, location, rotation, scale] = camera2.get_world_transform();
94camera2.set_camera_properties(40, true, 2.3, 0.6);
95
96pause(2);
97
98% Focus the camera
99for y = 1:51
100 camera2.set_camera_properties(40, true, 2.3, (0.6 + ((y / 50)^3) * 23.7));
101end
102
103camera.set_image_capture_resolution();
104
105% Take image of the second camera angle
106[success, image2] = camera.get_image();
107disp(success);
108
109pause(2);
110
111% Initialize and spawn the third camera angle
112camera3 = QLabsFreeCamera(qlabs);
113loc3 = [-21.456, 31.995, 3.745];
114rot3 = [0, 18.814, 0.326];
115camera3.spawn_degrees(loc3, rot3);
116camera3.possess();
117
118% Display a image of the first camera angle
119imshow(image1);
120
121% Closing qlabs
122qlabs.close();
123disp('Done!');
124