0
0
Drone Programmingprogramming~10 mins

Camera stream access with OpenCV in Drone Programming - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to open the default camera stream using OpenCV.

Drone Programming
import cv2

cap = cv2.VideoCapture([1])

if not cap.isOpened():
    print("Cannot open camera")
Drag options to blanks, or click blank then click option'
A"camera"
B1
C0
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 instead of 0 for the default camera index.
Passing a string instead of an integer.
2fill in blank
medium

Complete the code to read a frame from the camera stream.

Drone Programming
ret, frame = cap.[1]()

if not ret:
    print("Failed to grab frame")
Drag options to blanks, or click blank then click option'
Acapture
Bread
Cget
Dfetch
Attempts:
3 left
💡 Hint
Common Mistakes
Using get() which only retrieves properties.
Using capture() or fetch() which do not exist.
3fill in blank
hard

Fix the error in the code to release the camera resource properly.

Drone Programming
cap.[1]()
cv2.destroyAllWindows()
Drag options to blanks, or click blank then click option'
Aclose
Bend
Cstop
Drelease
Attempts:
3 left
💡 Hint
Common Mistakes
Using close() which does not exist for VideoCapture.
Using stop() or end() which are invalid.
4fill in blank
hard

Fill both blanks to convert the captured frame to grayscale and display it.

Drone Programming
gray = cv2.[1](frame, cv2.[2])
cv2.imshow('Gray Frame', gray)
Drag options to blanks, or click blank then click option'
AcvtColor
BCOLOR_BGR2GRAY
CCOLOR_RGB2GRAY
DconvertColor
Attempts:
3 left
💡 Hint
Common Mistakes
Using convertColor which is not a valid function.
Using COLOR_RGB2GRAY when the frame is in BGR format.
5fill in blank
hard

Fill all three blanks to create a loop that reads frames, shows them, and breaks on pressing 'q'.

Drone Programming
while True:
    ret, frame = cap.[1]()
    if not ret:
        break
    cv2.imshow('Frame', frame)
    if cv2.waitKey([2]) & 0xFF == ord([3]):
        break
cap.release()
cv2.destroyAllWindows()
Drag options to blanks, or click blank then click option'
Aread
B1
C'q'
Dwait
Attempts:
3 left
💡 Hint
Common Mistakes
Using wait() instead of waitKey().
Using a wrong key character or missing quotes around 'q'.
Not breaking the loop properly on key press.