Given the following function that processes a camera frame to detect a line's position, what will be the returned value when the line is centered?
def detect_line_position(frame): # frame is a 2D list representing pixel brightness width = len(frame[0]) line_pixels = [x for x in range(width) if frame[len(frame)//2][x] > 200] if not line_pixels: return None position = sum(line_pixels) / len(line_pixels) center = width / 2 offset = position - center return offset # Example frame: center line pixels at positions 4,5,6 with brightness 255 frame = [[0]*10 for _ in range(10)] for x in [4,5,6]: frame[5][x] = 255 print(detect_line_position(frame))
Think about how the average position of the bright pixels compares to the center of the frame.
The bright pixels are at positions 4, 5, and 6. Their average is (4+5+6)/3 = 5. The center is 10/2 = 5. The offset is 5 - 5 = 0.0.
For a drone following a line on the ground using a camera, which type of data is most important to extract from the camera feed?
Think about what helps the drone stay on the line.
The drone needs to know where the line is relative to its center to adjust its path. The position of the line is key.
Consider this code snippet for line following. It crashes with an error. What is the cause?
def follow_line(frame): width = len(frame[0]) line_pos = None for x in range(width): if frame[len(frame)//2][x] > 200: line_pos += x return line_pos
Check the initial value of line_pos and how it is used inside the loop.
line_pos starts as None, and adding an integer to None causes a TypeError.
Fix the syntax error in this dictionary comprehension that maps pixel positions to brightness if brightness is above 200:
{x: frame[5][x] if frame[5][x] > 200 for x in range(10)}Remember the correct order of for and if in comprehensions.
The if filter must come after the for clause in comprehensions.
Consider this drone control loop that adjusts steering based on line position offsets detected from the camera. How many times will the drone adjust its steering if the offsets list is [0.1, -0.2, 0.0, 0.3, -0.1]?
offsets = [0.1, -0.2, 0.0, 0.3, -0.1] adjustments = 0 for offset in offsets: if abs(offset) > 0.05: adjustments += 1 print(adjustments)
Count how many offsets have an absolute value greater than 0.05.
Offsets 0.1, -0.2, 0.3, and -0.1 are greater than 0.05 in absolute value, so 4 adjustments.