Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
L
LearningEnvironment
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Armin Co
LearningEnvironment
Commits
2994f0c1
Commit
2994f0c1
authored
Feb 7, 2021
by
Armin Co
Browse files
Options
Downloads
Patches
Plain Diff
Camera update
parent
e88e8237
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
carla_environment.py
+40
-17
40 additions, 17 deletions
carla_environment.py
with
40 additions
and
17 deletions
carla_environment.py
+
40
−
17
View file @
2994f0c1
"""
Carla.org environment
"""
import
glob
import
os
import
sys
import
random
import
sys
import
numpy
as
np
import
pygame
import
math
import
atexit
from
steering_wheel
import
Controller
# find carla module
try
:
sys
.
path
.
append
(
glob
.
glob
(
'
/media/armin/Games/carla/PythonAPI/carla/dist/carla-*%d.%d-%s.egg
'
%
(
CARLA_PATH
=
'
/media/armin/Games/carla/PythonAPI/carla/dist/carla-*%d.%d-%s.egg
'
sys
.
path
.
append
(
glob
.
glob
(
CARLA_PATH
%
(
sys
.
version_info
.
major
,
sys
.
version_info
.
minor
,
'
win-amd64
'
if
os
.
name
==
'
nt
'
else
'
linux-x86_64
'
))[
0
])
...
...
@@ -20,31 +21,37 @@ except IndexError:
import
carla
class
Camera
:
"""
Add camera sensor to the carla world
"""
sensor
=
None
display
=
None
surface
=
None
display
=
None
width
=
None
height
=
None
surface
=
None
camera_type
=
None
def
__init__
(
self
,
world
,
width
=
64
0
,
height
=
48
0
):
def
__init__
(
self
,
world
,
camera_type
=
'
rgb
'
,
width
=
128
0
,
height
=
72
0
):
self
.
width
=
width
self
.
height
=
height
self
.
camera_type
=
camera_type
self
.
display
=
pygame
.
display
.
set_mode
(
(
width
,
height
),
pygame
.
HWSURFACE
|
pygame
.
DOUBLEBUF
)
self
.
display
.
fill
((
0
,
0
,
0
))
camera_blueprint
=
world
.
world
.
get_blueprint_library
().
find
(
'
sensor.camera.rgb
'
)
camera_transform
=
carla
.
Transform
(
carla
.
Location
(
x
=
1.5
,
z
=
2.4
))
camera_blueprint
=
world
.
world
.
get_blueprint_library
().
find
(
'
sensor.camera.
'
+
camera_type
)
camera_blueprint
.
set_attribute
(
'
image_size_x
'
,
f
'
{
self
.
width
}
'
)
camera_blueprint
.
set_attribute
(
'
image_size_y
'
,
f
'
{
self
.
height
}
'
)
camera_blueprint
.
set_attribute
(
'
fov
'
,
'
110
'
)
camera_blueprint
.
set_attribute
(
'
fov
'
,
'
95
'
)
camera_transform
=
carla
.
Transform
(
carla
.
Location
(
x
=-
3.0
,
z
=
2.2
))
self
.
sensor
=
world
.
world
.
spawn_actor
(
camera_blueprint
,
camera_transform
,
attach_to
=
world
.
player
)
self
.
sensor
.
listen
(
lambda
data
:
self
.
process_img
(
data
))
def
process_img
(
self
,
data
):
"""
Callback for rgb-camera sensor.
"""
if
self
.
camera_type
==
'
semantic_segmentation
'
:
data
.
convert
(
carla
.
ColorConverter
.
CityScapesPalette
)
array
=
np
.
frombuffer
(
data
.
raw_data
,
dtype
=
np
.
dtype
(
"
uint8
"
))
array
=
np
.
reshape
(
array
,
(
self
.
height
,
self
.
width
,
4
))
array
=
array
[:,
:,
:
3
]
...
...
@@ -52,10 +59,12 @@ class Camera:
self
.
surface
=
pygame
.
surfarray
.
make_surface
(
array
.
swapaxes
(
0
,
1
))
def
on_update
(
self
):
"""
Update the pygame display.
"""
if
self
.
surface
is
not
None
:
self
.
display
.
blit
(
self
.
surface
,
(
0
,
0
))
pygame
.
display
.
flip
()
class
World
:
"""
Wrapper for the carla environment, incl. player/vehicle
"""
player
=
None
...
...
@@ -70,47 +79,61 @@ class World:
self
.
reset
()
def
reset
(
self
):
"""
Remove and create new player/vehicle.
"""
self
.
destroy
()
self
.
spawn_player
()
def
spawn_player
(
self
):
"""
Add a vehicle to the world.
"""
while
self
.
player
is
None
:
blueprint
=
random
.
choice
(
self
.
blueprint_library
.
filter
(
'
vehicle
'
))
blueprint
=
random
.
choice
(
self
.
blueprint_library
.
filter
(
'
model3
'
))
position
=
random
.
choice
(
self
.
spawn_points
)
self
.
player
=
self
.
world
.
try_spawn_actor
(
blueprint
,
position
)
start_location
=
self
.
player
.
get_location
()
print
(
str
(
start_location
))
# start_location.x = 288.0
# start_location.y = 55.0
# self.player.set_location(start_location)
def
destroy
(
self
):
"""
Remove vehicle from the world.
"""
if
self
.
player
is
not
None
:
self
.
player
.
destroy
()
def
step
(
self
,
action
):
"""
Apply controls to vehicle.
"""
self
.
player
.
apply_control
(
action
)
# print(str(self.player.get_location()))
# print(str(self.player.get_velocity()))
class
CarlaEnvironment
:
world
=
None
client
=
None
def
__init__
(
self
,
host
=
"
127.0.0.1
"
,
port
=
2000
,
width
=
1280
,
height
=
720
):
def
__init__
(
self
,
host
=
"
127.0.0.1
"
,
port
=
2000
):
self
.
client
=
carla
.
Client
(
host
,
port
)
self
.
client
.
set_timeout
(
2.0
)
self
.
client
.
load_world
(
'
Town07
'
)
self
.
world
=
World
(
self
.
client
.
get_world
())
def
step
(
self
,
action
):
control
=
carla
.
VehicleControl
(
throttle
=
action
[
0
],
steer
=
action
[
2
],
brake
=
action
[
1
])
control
=
carla
.
VehicleControl
(
throttle
=
action
[
0
],
steer
=
action
[
2
],
brake
=
action
[
1
]
,
reverse
=
action
[
3
]
)
self
.
world
.
step
(
control
)
if
__name__
==
"
__main__
"
:
LOCALHOST
=
"
127.0.0.1
"
SERVER
=
"
192.168.188.20
"
pygame
.
init
()
clock
=
pygame
.
time
.
Clock
()
env
=
CarlaEnvironment
()
cam
=
Camera
(
env
.
world
)
env
=
CarlaEnvironment
(
host
=
LOCALHOST
)
cam
=
Camera
(
env
.
world
,
camera_type
=
'
semantic_segmentation
'
)
ctrl
=
Controller
()
running
=
True
while
ctrl
.
is_running
():
clock
.
tick
(
60
)
ctrl
.
on_update
()
env
.
step
(
ctrl
.
get_action
())
cam
.
on_update
()
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment