📖 Scenario: You are programming a drone to follow a red ball in a room. The drone uses a camera to see colors and moves to keep the red ball centered in its view.
🎯 Goal: Build a simple program that detects the red color in the camera feed and decides the drone's movement direction to follow the red ball.
📋 What You'll Learn
Create a dictionary called camera_feed with pixel colors as keys and their counts as values
Create a variable called red_threshold to decide when red is dominant
Use a for loop with variables color and count to iterate over camera_feed.items()
Create a variable called red_count to sum counts of red pixels
Print the drone's movement direction based on red detection
💡 Why This Matters
🌍 Real World
Drones use color tracking to follow objects like balls or markers in sports, delivery, or search and rescue.
💼 Career
Understanding color-based tracking helps in robotics, drone programming, and computer vision jobs.
Progress0 / 4 steps
1
DATA SETUP: Create the camera feed data
Create a dictionary called camera_feed with these exact entries: 'red': 15, 'green': 5, 'blue': 10, 'yellow': 3
Drone Programming
Hint
Use curly braces to create a dictionary with the exact colors and counts.
2
CONFIGURATION: Set the red detection threshold
Create a variable called red_threshold and set it to 10
Drone Programming
Hint
Just assign the number 10 to the variable red_threshold.
3
CORE LOGIC: Count red pixels in the camera feed
Create a variable called red_count and set it to 0. Then use a for loop with variables color and count to iterate over camera_feed.items(). Inside the loop, add count to red_count only if color is equal to 'red'.
Drone Programming
Hint
Use an if statement inside the loop to check if color is 'red' and add count to red_count.
4
OUTPUT: Print the drone's movement direction
Write a print statement that prints "Move forward" if red_count is greater than red_threshold. Otherwise, print "Search for red ball".
Drone Programming
Hint
Use an if-else statement to decide what to print based on red_count and red_threshold.
Practice
(1/5)
1. What is the main purpose of color-based tracking in drone programming?
easy
A. To make the drone follow an object by detecting its color
B. To control the drone's altitude using GPS
C. To improve the drone's battery life
D. To send commands to the drone via remote control
Solution
Step 1: Understand color-based tracking concept
Color-based tracking means the drone uses color detection to follow an object.
Step 2: Identify the main goal
The goal is to detect and follow an object by its color, not altitude or battery.
Final Answer:
To make the drone follow an object by detecting its color -> Option A
Quick Check:
Color-based tracking = follow object by color [OK]
Hint: Focus on color detection as the tracking method [OK]
Common Mistakes:
Confusing color tracking with GPS control
Thinking it manages battery or remote commands
Mixing color tracking with altitude control
2. Which of the following code snippets correctly initializes a color filter for red in a drone's tracking system?
A. SyntaxError due to incorrect dictionary keys; fix keys to 'low' and 'high'
B. TypeError because object_center is None; fix by assigning a tuple to object_center
C. IndexError because green_filter keys are missing; add keys
D. No error; code runs correctly
Solution
Step 1: Identify the error cause
object_center is None, so accessing object_center[0] causes TypeError.
Step 2: Fix the error
Assign a tuple like (x, y) to object_center before using it in condition.
Final Answer:
TypeError because object_center is None; fix by assigning a tuple to object_center -> Option B
Quick Check:
None used as index causes TypeError [OK]
Hint: Check variable types before indexing [OK]
Common Mistakes:
Ignoring NoneType indexing error
Changing dictionary keys incorrectly
Assuming code runs without error
5. You want the drone to follow a yellow object but only if its detected center's y-coordinate is above 100. Which code snippet correctly implements this condition using color-based tracking?
A. if yellow_filter['lower'][0] <= object_center[0] <= yellow_filter['upper'][0] and object_center[1] > 100:
print('Following yellow object')
B. if yellow_filter['lower'][1] <= object_center[1] <= yellow_filter['upper'][1] and object_center[0] > 100:
print('Following yellow object')
C. if yellow_filter['lower'][0] <= object_center[1] <= yellow_filter['upper'][0] and object_center[0] > 100:
print('Following yellow object')
D. if yellow_filter['lower'][1] <= object_center[0] <= yellow_filter['upper'][1] and object_center[1] > 100:
print('Following yellow object')
Solution
Step 1: Understand HSV and coordinate roles
yellow_filter keys 'lower' and 'upper' define hue range at index 0; object_center[0] is x, object_center[1] is y.
Step 2: Check condition for hue and y-coordinate
Hue check uses object_center[0] between lower and upper hue; y-coordinate must be > 100.
Final Answer:
if yellow_filter['lower'][0] <= object_center[0] <= yellow_filter['upper'][0] and object_center[1] > 100:
print('Following yellow object') -> Option A
Quick Check:
Hue range on x and y > 100 condition = if yellow_filter['lower'][0] <= object_center[0] <= yellow_filter['upper'][0] and object_center[1] > 100:
print('Following yellow object') [OK]
Hint: Match hue with x, check y > 100 separately [OK]
Common Mistakes:
Mixing x and y coordinates in conditions
Using wrong indices for hue or saturation
Checking y-coordinate against hue range
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.