{
  "source_file": "0930",
  "source_platform": "Dev.to",
  "author": "Mitansh Gor",
  "title_listed": "Carla Simulator 2 : Welcome to the Ride 🚗🏍️",
  "keywords": "",
  "domain": "大模型算法",
  "content_type": "技术教程",
  "is_paid_flag": "否",
  "url": "https://dev.to/mitanshgor/carla-simulator-2-welcome-to-the-ride-brian-cooper-here-4jbi",
  "publish_date": "2024-12-20",
  "corpus_stage": "",
  "notes": "",
  "id": "25ff2e9193cd",
  "collected_at": "2026-10-02T13:53:42",
  "title": "Carla Simulator 2 : Welcome to the Ride 🚗🏍️ - DEV Community",
  "body_markdown": "check\nBlog 1\nof Carla Simulator.\nCheck Video\nHey, family! It’s Brian here — yes, the Brian O’Conner, the gearhead who always has your back on the road or in a high-speed chase. Dom handed me the keys to this blog, saying it’s time to dive deep into some next-gen simulator tech that fuels the autonomous revolution. Thanks, Dom, for trusting me. I’ll try not to wreck it (not that I ever wreck anything, right? 😉).\nDom mentioned how the CARLA simulator lets us test and train autonomous vehicles in a realistic, open-source environment. It’s got the kind of details that make even Letty go, \"Damn, that’s sharp.\" Our mission here? To break it down for you like we would a turbocharged engine. You’ll know everything about CARLA’s actors, vehicles, and how to control them with the finesse of a perfectly-timed NOS burst. And if you stick with me, we’ll even set up a Flask API to bring this simulation to life. Let’s ride! 🏍️\nThe Plan for This Blog\nActors and Blueprints 101: Understand the essentials of what makes CARLA tick, from vehicles to walkers and how blueprints play a role.\nDive into Vehicle and Walker Management: Learn how actors are spawned, manipulated, and destroyed — think of it like choosing the perfect car for a street race and knowing when to swap gears.\nFlask API Integration: Build Flask APIs step-by-step to control CARLA’s simulation programmatically. By the end, you’ll be setting up car chases, random pedestrians, and traffic with just a few lines of code.\nHands-on Examples: Showcase how to populate the simulation with vehicles and walkers using our Flask API endpoints.\nReady to start? Let’s shift into gear! 🏎️\nActors and Blueprints 101 🎨\nIn CARLA, actors are the dynamic elements that breathe life into the simulation. This includes:\nVehicles: Cars, trucks, bikes — the heart of the simulation.\nWalkers: Pedestrians who roam the environment.\nTraffic Entities: Traffic lights and stop signs.\nSensors: Cameras, LiDAR, radar, and GPS for data collection.\nBlueprints: The DNA of Actors\nThink of blueprints as pre-configured settings for creating actors. CARLA’s blueprint library includes attributes like:\nVehicle Colors\nEngine Power\nWalker Speed\nSensor Ranges\nEach blueprint is customizable, letting you tweak the simulation to match real-world scenarios. It’s like choosing a Skyline’s body kit and tuning it for maximum horsepower.\nManaging Actors: Spawning, Interacting, and Destroying\nIn CARLA:\nSpawning: Use the spawn_actor API to bring vehicles and walkers into the world.\nInteracting: Control the physics, speed, and direction of actors.\nDestroying: Remove actors once their role in the simulation is done.\nVehicle Physics 🏎️⚡\nVehicles in CARLA come equipped with:\nDynamic Control: Accelerate, brake, and steer.\nCollision Response: Simulates crashes and obstacle impacts.\nEnvironmental Response: Handles weather and road conditions.\nWalker Dynamics 🚶‍♂️\nWalkers can:\nFollow predefined paths.\nReact dynamically to traffic.\nWalk at various speeds and animations.\nFlask API Integration: Building the Engine 🚒\nNow that we’ve got the basics, it’s time to build a Flask API to interact with CARLA. Here’s how:\nFlask API Endpoints\nSpawn Vehicles\n@app.route('/spawn_vehicle', methods=['POST'])\ndef spawn_vehicle():\n    data = request.get_json()\n    model = data.get('model', 'vehicle.tesla.model3')\n\n    blueprint_library = world.get_blueprint_library()\n    vehicle_bp = blueprint_library.filter(model)[0]\n\n    spawn_points = world.get_map().get_spawn_points()\n    if not spawn_points:\n        return jsonify({'error': 'No spawn points available'}), 400\n\n    spawn_point = random.choice(spawn_points)\n    vehicle = world.spawn_actor(vehicle_bp, spawn_point)\n\n    return jsonify({'message': 'Vehicle spawned', 'id': vehicle.id})\nEnter fullscreen mode\nExit fullscreen mode\n✨📸 Setting Up the Sensor Cameras\nTo capture images from a vehicle's top view and front view, two cameras are mounted on the car. Here's the setup:\nThe code retrieves camera blueprints (sensor.camera.rgb) from CARLA's blueprint library. Resolution settings (800x600) are defined for the cameras to ensure clear visuals. 📷\nTop Camera: Positioned directly above the car to capture a bird's-eye view.\nFront Camera: Placed at the front of the car for a forward-facing perspective\ncamera_spawn_pointTop = carla.Transform(\n    carla.Location(x=0.0, y=0.0, z=5.0),  # 5 meters above the car\n    carla.Rotation(pitch=-90.0)  # Looking straight down\n)\ncameraTop = world.spawn_actor(camera_bp1, camera_spawn_pointTop, attach_to=vehicle)\n\ncamera_spawn_pointFront = carla.Transform(\n    carla.Location(x=-5.0, y=0.0, z=3.0),  # 5 meters in front and 3 meters high\n    carla.Rotation(pitch=-15.0)  # Slight downward tilt\n)\ncameraFront = world.spawn_actor(camera_bp2, camera_spawn_pointFront, attach_to=vehicle)\nEnter fullscreen mode\nExit fullscreen mode\nEach camera is set up with a callback function (e.g., camera_callback1) to process the captured images. These images are saved as files and encoded in Base64 for easy transfer and storage.\ndef camera_callback1(image, id, vehicleId, pos):\n    array = image.raw_data\n    img = Image.frombytes(\"RGBA\", (image.width, image.height), bytes(array))\n    img = img.convert(\"RGB\")\n    img.save(f\"./images/{id}_{vehicleId}_{pos}.png\", format=\"PNG\")\nEnter fullscreen mode\nExit fullscreen mode\n🎥 Spectator Camera: Follow the Car\nTo make the simulation more immersive, a spectator camera is configured to follow the vehicle dynamically.\nA spectator camera is moved relative to the car's position using the set_transform method.\nIt stays a few meters behind and slightly above the car, aligned with its orientation.\ndef setup_spectator_camera(world, vehicle):\n    vehicle_transform = vehicle.get_transform()  # Get the car's position and rotation\n    spectator = world.get_spectator()  # Access the spectator camera\n\n    spectator_transform = carla.Transform(\n        vehicle_transform.location + carla.Location(x=-6, y=0, z=2),  # Offset behind and above the car\n        vehicle_transform.rotation  # Match the car's orientation\n    )\n    spectator.set_transform(spectator_transform)  # Update the spectator's position\nEnter fullscreen mode\nExit fullscreen mode\nThe spectator camera is continually updated in a loop, ensuring it tracks the car as it moves. This creates a realistic following effect.\nSpawn Walkers\n@app.route('/spawn_walker', methods=['POST'])\ndef spawn_walker():\n    data = request.get_json()\n    walker_count = data.get('count', 1)\n\n    walker_blueprints = world.get_blueprint_library().filter('walker.pedestrian.*')\n    spawn_points = [random.choice(world.get_map().get_spawn_points()) for _ in range(walker_count)]\n\n    walkers = []\n    for spawn_point in spawn_points:\n        walker_bp = random.choice(walker_blueprints)\n        walker = world.spawn_actor(walker_bp, spawn_point)\n        walkers.append(walker)\n\n    return jsonify({'message': f'{walker_count} walkers spawned', 'ids': [w.id for w in walkers]})\nEnter fullscreen mode\nExit fullscreen mode\nDestroy All Actors\n@app.route('/destroy_actors', methods=['POST'])\ndef destroy_actors():\n    actors = world.get_actors()\n    actors_to_destroy = [actor for actor in actors if actor.type_id.startswith('vehicle') or actor.type_id.startswith('walker')]\n\n    for actor in actors_to_destroy:\n        actor.destroy()\n\n    return jsonify({'message': 'All actors destroyed'})\nEnter fullscreen mode\nExit fullscreen mode\nAdd Random Vehicles and Walkers\n@app.route('/populate_simulation', methods=['POST'])\ndef populate_simulation():\n    data = request.get_json()\n    vehicle_count = data.get('vehicles', 10)\n    walker_count = data.get('walkers', 10)\n\n    # Spawn Vehicles\n    for _ in range(vehicle_count):\n        vehicle_bp = random.choice(world.get_blueprint_library().filter('vehicle.*'))\n        spawn_point = random.choice(world.get_map().get_spawn_points())\n        world.spawn_actor(vehicle_bp, spawn_point)\n\n    # Spawn Walkers\n    for _ in range(walker_count):\n        walker_bp = random.choice(world.get_blueprint_library().filter('walker.pedestrian.*'))\n        spawn_point = random.choice(world.get_map().get_spawn_points())\n        world.spawn_actor(walker_bp, spawn_point)\n\n    return jsonify({'message': f'{vehicle_count} vehicles and {walker_count} walkers added'})\nEnter fullscreen mode\nExit fullscreen mode\nWrapping Up 🏎️\nAnd that’s how you take control of CARLA with Flask! Whether you’re simulating a high-speed chase or a casual Sunday drive, these tools let you shape your virtual world. Stay tuned for more deep dives and, as always, drive safe — or at least make it look cool while you’re not. 🚗🚨",
  "metadata": {
    "title": "Carla Simulator 2 : Welcome to the Ride 🚗🏍️ - DEV Community",
    "description": "check Blog 1 of Carla Simulator.  Check Video  Hey, family! It’s Brian here — yes, the Brian... Tagged with carla, flask, python, simulation.",
    "keywords": "carla, flask, python, simulation, software, coding, development, engineering, inclusive, community",
    "author": null,
    "og:type": "article",
    "og:url": "https://dev.to/mitanshgor/carla-simulator-2-welcome-to-the-ride-brian-cooper-here-4jbi",
    "og:title": "Carla Simulator 2 : Welcome to the Ride 🚗🏍️",
    "og:description": "check Blog 1 of Carla Simulator.  Check Video  Hey, family! It’s Brian here — yes, the Brian...",
    "og:site_name": "DEV Community",
    "og:image": "https://media2.dev.to/dynamic/image/width=1000,height=500,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg6xqxy2to7fyzmscg1b0.jpg",
    "twitter:site": "@thepracticaldev",
    "twitter:creator": "@",
    "twitter:title": "Carla Simulator 2 : Welcome to the Ride 🚗🏍️",
    "twitter:description": "check Blog 1 of Carla Simulator.  Check Video  Hey, family! It’s Brian here — yes, the Brian...",
    "twitter:card": "summary_large_image",
    "twitter:widgets:new-embed-design": "on",
    "twitter:image:src": "https://media2.dev.to/dynamic/image/width=1000,height=500,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg6xqxy2to7fyzmscg1b0.jpg"
  },
  "paywall": {
    "listed_paid": false,
    "detected": false,
    "marker": "",
    "public_only": false,
    "policy": "付费/会员内容只保留页面公开可见部分，不绕过付费墙"
  },
  "crawl": {
    "success": true,
    "status_code": 200,
    "error": ""
  },
  "code_blocks": [
    {
      "file": "attachments/25ff2e9193cd/code/body-000.txt",
      "chars": 581,
      "language": "text"
    },
    {
      "file": "attachments/25ff2e9193cd/code/body-001.txt",
      "chars": 522,
      "language": "text"
    },
    {
      "file": "attachments/25ff2e9193cd/code/body-002.txt",
      "chars": 250,
      "language": "text"
    },
    {
      "file": "attachments/25ff2e9193cd/code/body-003.txt",
      "chars": 505,
      "language": "text"
    },
    {
      "file": "attachments/25ff2e9193cd/code/body-004.txt",
      "chars": 620,
      "language": "text"
    },
    {
      "file": "attachments/25ff2e9193cd/code/body-005.txt",
      "chars": 349,
      "language": "text"
    },
    {
      "file": "attachments/25ff2e9193cd/code/body-006.txt",
      "chars": 831,
      "language": "text"
    }
  ],
  "images": [
    {
      "status": "ok",
      "url": "https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq36abon970vjwbr4g3o0.png",
      "bytes": 113204,
      "reused": true,
      "local_path": "attachments/25ff2e9193cd/images/002.png",
      "src": "https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq36abon970vjwbr4g3o0.png",
      "alt": " ",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff25ucce8b8x4z4bmiev8.png",
      "bytes": 53766,
      "reused": true,
      "local_path": "attachments/25ff2e9193cd/images/003.png",
      "src": "https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff25ucce8b8x4z4bmiev8.png",
      "alt": " ",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnpvkt0fxm4xdyyggign6.png",
      "bytes": 179274,
      "reused": true,
      "local_path": "attachments/25ff2e9193cd/images/004.png",
      "src": "https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnpvkt0fxm4xdyyggign6.png",
      "alt": " ",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2bumatjat1bpb7y8jj32.png",
      "bytes": 263060,
      "reused": true,
      "local_path": "attachments/25ff2e9193cd/images/005.png",
      "src": "https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2bumatjat1bpb7y8jj32.png",
      "alt": " ",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu3pfcp0lm2zea7go9mar.png",
      "bytes": 33998,
      "reused": true,
      "local_path": "attachments/25ff2e9193cd/images/006.png",
      "src": "https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu3pfcp0lm2zea7go9mar.png",
      "alt": " ",
      "downloaded": true
    }
  ],
  "audio": [],
  "videos": [],
  "links": [
    {
      "href": "https://dev.to/mitanshgor/carla-simulator-1-how-to-set-up-carla-simulator-toretto-style-3800",
      "label": "Blog 1",
      "type": "a"
    },
    {
      "href": "https://drive.google.com/drive/home?dmr=1&ec=wgc-drive-hero-goto",
      "label": "Check Video",
      "type": "a"
    },
    {
      "href": "https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq36abon970vjwbr4g3o0.png",
      "label": "",
      "type": "a"
    },
    {
      "href": "https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff25ucce8b8x4z4bmiev8.png",
      "label": "",
      "type": "a"
    },
    {
      "href": "https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnpvkt0fxm4xdyyggign6.png",
      "label": "",
      "type": "a"
    },
    {
      "href": "https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2bumatjat1bpb7y8jj32.png",
      "label": "",
      "type": "a"
    },
    {
      "href": "https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu3pfcp0lm2zea7go9mar.png",
      "label": "",
      "type": "a"
    }
  ],
  "repair_version": 2,
  "repaired_at": "2026-10-14T10:48:49Z",
  "source_url": "https://dev.to/mitanshgor/carla-simulator-2-welcome-to-the-ride-brian-cooper-here-4jbi",
  "papers": [],
  "actual_video_files": [],
  "body_chars": 8615,
  "expected_body_images": 5,
  "discovery_status": "ok",
  "delivery_status": "ok",
  "missing_reason": "",
  "raw_html": "attachments/25ff2e9193cd/pages/source.html",
  "local_html": "attachments/25ff2e9193cd/pages/article.html",
  "body_base_path": "attachments/25ff2e9193cd/pages/",
  "body_markdown_path": "attachments/25ff2e9193cd/pages/article.md"
}