Bebop 2无人机视频流获取问题(Python、OpenCV与FFmpeg)
Hey there! Let's work through your Bebop 2 video stream problems—whether you want to use OpenCV directly or leverage pyparrot's built-in tools, we've got solutions.
1. Fixing OpenCV's Protocol Whitelist Error
Your initial OpenCV error happens because by default, FFmpeg (which OpenCV uses under the hood) blocks rtp and udp protocols. You can manually add these to the whitelist when initializing the VideoCapture:
import cv2 # Initialize capture with FFmpeg and protocol whitelist cap = cv2.VideoCapture("./bebop.sdp", cv2.CAP_FFMPEG) cap.set(cv2.CAP_PROP_FFMPEG_OPTS, "protocol_whitelist=file,rtp,udp") while True: ret, frame = cap.read() if not ret: print("Couldn't grab frame—check stream connection!") break # Display the frame cv2.imshow("Bebop 2 Live Feed", frame) # Exit on 'q' press if cv2.waitKey(1) & 0xFF == ord('q'): break # Cleanup cap.release() cv2.destroyAllWindows()
This explicitly tells FFmpeg to allow the protocols needed for the Bebop's RTP video stream, fixing the assertion error you saw.
2. Improving FFmpeg Image Extraction Quality
Your original FFmpeg command used a fixed bitrate (-b:v 800k) which can lead to poor quality, especially with fast-moving drone footage. Instead, use the Constant Rate Factor (-crf) parameter for consistent quality:
ffmpeg -protocol_whitelist "file,rtp,udp" -i ./bebop.sdp -r 30 -c:v libx264 -crf 20 -pix_fmt yuv420p image_%03d.png
-crf 20: Balances quality and file size (values 18-28 work well; lower = better quality)-c:v libx264: Uses the efficient H.264 codec for better compression-pix_fmt yuv420p: Ensures compatibility with most image viewers/processors
3. Simplify with pyparrot's Built-In Video Streaming
Since you're using pyparrot to control the drone, skip manual SDP handling entirely—pyparrot has built-in methods to grab video frames directly:
from pyparrot.Bebop import Bebop import cv2 def process_video_frame(frame): # Convert pyparrot's RGB frame to OpenCV's BGR format cv2_frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) cv2.imshow("Bebop 2 Stream", cv2_frame) # Return False to stop the stream when 'q' is pressed if cv2.waitKey(1) & 0xFF == ord('q'): return False return True # Connect to the drone bebop = Bebop() print("Connecting to Bebop 2...") connection_success = bebop.connect(5) if connection_success: print("Starting video stream...") bebop.start_video_stream() # Set up the callback to process each frame bebop.set_video_callback(process_video_frame) # Add your drone control logic here! # Example: bebop.move_forward(10) or bebop.turn_right(20) # Keep running until user presses 'q' while True: if cv2.waitKey(1) & 0xFF == ord('q'): break # Cleanup bebop.stop_video_stream() bebop.disconnect() cv2.destroyAllWindows() else: print("Failed to connect to the drone—check Wi-Fi!")
This method is far more reliable than manual SDP parsing, as pyparrot handles the stream connection and frame decoding for you. You can seamlessly add your control commands right alongside the video processing.
内容的提问来源于stack exchange,提问作者Seb A

