{"id":1149,"date":"2023-08-30T02:25:11","date_gmt":"2023-08-29T18:25:11","guid":{"rendered":"http:\/\/bversion.com\/WordPress\/?p=1149"},"modified":"2023-09-17T20:20:35","modified_gmt":"2023-09-17T12:20:35","slug":"drone-programming-video-capturing","status":"publish","type":"post","link":"https:\/\/bversion.com\/WordPress\/2023\/08\/30\/drone-programming-video-capturing\/","title":{"rendered":"Drone Programming &#8211; Video Capturing"},"content":{"rendered":"\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Program Tello with Python #2 - Video Capturing\" width=\"604\" height=\"340\" src=\"https:\/\/www.youtube.com\/embed\/WhzW-2MVAbs?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>We introduced Drone movement in the last post, we are going to try the video capture. It is also very simple with the help of DJITELLOPY and CV2 API. With the basic movement control and video capture, we can start face detection and face tracking very soon. Let&#8217;s show you how to capture video from the Drone Tello.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">CV2<\/h2>\n\n\n\n<p>CV2, stands for OpenCV (Open Source Computer Vision Library), which is a popular open-source computer vision and machine learning software library. It provides a wide range of functions and tools for various computer vision tasks, image and video processing, machine learning, and more. OpenCV is widely used in the fields of computer vision, robotics, image processing, and artificial intelligence. <\/p>\n\n\n\n<p>As we mentioned in the previous post, CV2 already installed when we installing the DJITELLOPY 2.50 package. We just need to import the CV2 library when start the program.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Threading<\/h2>\n\n\n\n<p>As we need to capture and display the video from drone by the same time it is flying, we need parallel progressing. THREADING is basic and common used Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How&#8217;s the program working<\/h2>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.75rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#575279;--cbp-line-number-width:13.203125px;line-height:1rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#faf4ed\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"# Before you run this program, ensure to connect Tello with the WIFI\n\n# Import Tello class from djitellopy library\nfrom djitellopy import Tello\n\n# Import additional library CV2 - OpenCV for image processing, threading for multi-tasking\nimport cv2\nimport threading\n\n# Assign tello to the Tello class\ntello = Tello()\n\n# def a video capture and display function\ndef capturing_video(tello):\n    while True:\n        frame = tello.get_frame_read().frame\n        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)\n        cv2.imshow('Tello Video Stream', frame)\n        cv2.moveWindow('Tello Video Stream',0,0)\n        cv2.waitKey(1)\n\n# Connect to the drone via WIFI\ntello.connect()\n\n# Instrust Tello to start video stream and ensure first frame read\ntello.streamon()\nwhile True:\n            frame = tello.get_frame_read().frame\n            if frame is not None:\n                break\n\n# Start the video capture thread when the drone is flying\nvideo_thread = threading.Thread(target=capturing_video, args=(tello,), daemon=True)\nvideo_thread.start()\n\n# Take off the drone\ntello.takeoff()\n\n# Do combo action such as move up &amp; down and rotating\ntello.move_up(30)\ntello.move_down(30)\ntello.move_up(30)\ntello.move_down(30)\n\ntello.rotate_counter_clockwise(30)\ntello.rotate_clockwise(60)\ntello.rotate_counter_clockwise(30)\n\n# Landing the drone\ntello.land()\n\n# Stop the video stream\ntello.streamoff()\n\n# Show the battery level before ending the program\nprint(&quot;Battery :&quot;, tello.get_battery())\n\n# Stop the connection with the drone\ntello.end()\" style=\"color:#575279;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki rose-pine-dawn\" style=\"background-color: #faf4ed\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #797593; font-style: italic\">#<\/span><span style=\"color: #9893A5; font-style: italic\"> Before you run this program, ensure to connect Tello with the WIFI<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #797593; font-style: italic\">#<\/span><span style=\"color: #9893A5; font-style: italic\"> Import Tello class from djitellopy library<\/span><\/span>\n<span class=\"line\"><span style=\"color: #286983\">from<\/span><span style=\"color: #575279\"> djitellopy <\/span><span style=\"color: #286983\">import<\/span><span style=\"color: #575279\"> Tello<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #797593; font-style: italic\">#<\/span><span style=\"color: #9893A5; font-style: italic\"> Import additional library CV2 - OpenCV for image processing, threading for multi-tasking<\/span><\/span>\n<span class=\"line\"><span style=\"color: #286983\">import<\/span><span style=\"color: #575279\"> cv2<\/span><\/span>\n<span class=\"line\"><span style=\"color: #286983\">import<\/span><span style=\"color: #575279\"> threading<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #797593; font-style: italic\">#<\/span><span style=\"color: #9893A5; font-style: italic\"> Assign tello to the Tello class<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">tello <\/span><span style=\"color: #286983\">=<\/span><span style=\"color: #575279\"> Tello<\/span><span style=\"color: #797593\">()<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #797593; font-style: italic\">#<\/span><span style=\"color: #9893A5; font-style: italic\"> def a video capture and display function<\/span><\/span>\n<span class=\"line\"><span style=\"color: #286983\">def<\/span><span style=\"color: #575279\"> <\/span><span style=\"color: #D7827E\">capturing_video<\/span><span style=\"color: #797593\">(<\/span><span style=\"color: #907AA9; font-style: italic\">tello<\/span><span style=\"color: #797593\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">    <\/span><span style=\"color: #286983\">while<\/span><span style=\"color: #575279\"> <\/span><span style=\"color: #D7827E\">True<\/span><span style=\"color: #797593\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">        frame <\/span><span style=\"color: #286983\">=<\/span><span style=\"color: #575279\"> tello<\/span><span style=\"color: #797593\">.<\/span><span style=\"color: #575279\">get_frame_read<\/span><span style=\"color: #797593\">().<\/span><span style=\"color: #575279\">frame<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">        frame <\/span><span style=\"color: #286983\">=<\/span><span style=\"color: #575279\"> cv2<\/span><span style=\"color: #797593\">.<\/span><span style=\"color: #575279\">cvtColor<\/span><span style=\"color: #797593\">(<\/span><span style=\"color: #575279\">frame<\/span><span style=\"color: #797593\">,<\/span><span style=\"color: #575279\"> cv2<\/span><span style=\"color: #797593\">.<\/span><span style=\"color: #286983\">COLOR_BGR2RGB<\/span><span style=\"color: #797593\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">        cv2<\/span><span style=\"color: #797593\">.<\/span><span style=\"color: #575279\">imshow<\/span><span style=\"color: #797593\">(<\/span><span style=\"color: #EA9D34\">&#39;Tello Video Stream&#39;<\/span><span style=\"color: #797593\">,<\/span><span style=\"color: #575279\"> frame<\/span><span style=\"color: #797593\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">        cv2<\/span><span style=\"color: #797593\">.<\/span><span style=\"color: #575279\">moveWindow<\/span><span style=\"color: #797593\">(<\/span><span style=\"color: #EA9D34\">&#39;Tello Video Stream&#39;<\/span><span style=\"color: #797593\">,<\/span><span style=\"color: #D7827E\">0<\/span><span style=\"color: #797593\">,<\/span><span style=\"color: #D7827E\">0<\/span><span style=\"color: #797593\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">        cv2<\/span><span style=\"color: #797593\">.<\/span><span style=\"color: #575279\">waitKey<\/span><span style=\"color: #797593\">(<\/span><span style=\"color: #D7827E\">1<\/span><span style=\"color: #797593\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #797593; font-style: italic\">#<\/span><span style=\"color: #9893A5; font-style: italic\"> Connect to the drone via WIFI<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">tello<\/span><span style=\"color: #797593\">.<\/span><span style=\"color: #575279\">connect<\/span><span style=\"color: #797593\">()<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #797593; font-style: italic\">#<\/span><span style=\"color: #9893A5; font-style: italic\"> Instrust Tello to start video stream and ensure first frame read<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">tello<\/span><span style=\"color: #797593\">.<\/span><span style=\"color: #575279\">streamon<\/span><span style=\"color: #797593\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #286983\">while<\/span><span style=\"color: #575279\"> <\/span><span style=\"color: #D7827E\">True<\/span><span style=\"color: #797593\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">            frame <\/span><span style=\"color: #286983\">=<\/span><span style=\"color: #575279\"> tello<\/span><span style=\"color: #797593\">.<\/span><span style=\"color: #575279\">get_frame_read<\/span><span style=\"color: #797593\">().<\/span><span style=\"color: #575279\">frame<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">            <\/span><span style=\"color: #286983\">if<\/span><span style=\"color: #575279\"> frame <\/span><span style=\"color: #286983\">is<\/span><span style=\"color: #575279\"> <\/span><span style=\"color: #286983\">not<\/span><span style=\"color: #575279\"> <\/span><span style=\"color: #D7827E\">None<\/span><span style=\"color: #797593\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">                <\/span><span style=\"color: #286983\">break<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #797593; font-style: italic\">#<\/span><span style=\"color: #9893A5; font-style: italic\"> Start the video capture thread when the drone is flying<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">video_thread <\/span><span style=\"color: #286983\">=<\/span><span style=\"color: #575279\"> threading<\/span><span style=\"color: #797593\">.<\/span><span style=\"color: #575279\">Thread<\/span><span style=\"color: #797593\">(<\/span><span style=\"color: #907AA9; font-style: italic\">target<\/span><span style=\"color: #286983\">=<\/span><span style=\"color: #575279\">capturing_video<\/span><span style=\"color: #797593\">,<\/span><span style=\"color: #575279\"> <\/span><span style=\"color: #907AA9; font-style: italic\">args<\/span><span style=\"color: #286983\">=<\/span><span style=\"color: #797593\">(<\/span><span style=\"color: #575279\">tello<\/span><span style=\"color: #797593\">,),<\/span><span style=\"color: #575279\"> <\/span><span style=\"color: #907AA9; font-style: italic\">daemon<\/span><span style=\"color: #286983\">=<\/span><span style=\"color: #D7827E\">True<\/span><span style=\"color: #797593\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">video_thread<\/span><span style=\"color: #797593\">.<\/span><span style=\"color: #575279\">start<\/span><span style=\"color: #797593\">()<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #797593; font-style: italic\">#<\/span><span style=\"color: #9893A5; font-style: italic\"> Take off the drone<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">tello<\/span><span style=\"color: #797593\">.<\/span><span style=\"color: #575279\">takeoff<\/span><span style=\"color: #797593\">()<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #797593; font-style: italic\">#<\/span><span style=\"color: #9893A5; font-style: italic\"> Do combo action such as move up &amp; down and rotating<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">tello<\/span><span style=\"color: #797593\">.<\/span><span style=\"color: #575279\">move_up<\/span><span style=\"color: #797593\">(<\/span><span style=\"color: #D7827E\">30<\/span><span style=\"color: #797593\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">tello<\/span><span style=\"color: #797593\">.<\/span><span style=\"color: #575279\">move_down<\/span><span style=\"color: #797593\">(<\/span><span style=\"color: #D7827E\">30<\/span><span style=\"color: #797593\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">tello<\/span><span style=\"color: #797593\">.<\/span><span style=\"color: #575279\">move_up<\/span><span style=\"color: #797593\">(<\/span><span style=\"color: #D7827E\">30<\/span><span style=\"color: #797593\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">tello<\/span><span style=\"color: #797593\">.<\/span><span style=\"color: #575279\">move_down<\/span><span style=\"color: #797593\">(<\/span><span style=\"color: #D7827E\">30<\/span><span style=\"color: #797593\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #575279\">tello<\/span><span style=\"color: #797593\">.<\/span><span style=\"color: #575279\">rotate_counter_clockwise<\/span><span style=\"color: #797593\">(<\/span><span style=\"color: #D7827E\">30<\/span><span style=\"color: #797593\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">tello<\/span><span style=\"color: #797593\">.<\/span><span style=\"color: #575279\">rotate_clockwise<\/span><span style=\"color: #797593\">(<\/span><span style=\"color: #D7827E\">60<\/span><span style=\"color: #797593\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">tello<\/span><span style=\"color: #797593\">.<\/span><span style=\"color: #575279\">rotate_counter_clockwise<\/span><span style=\"color: #797593\">(<\/span><span style=\"color: #D7827E\">30<\/span><span style=\"color: #797593\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #797593; font-style: italic\">#<\/span><span style=\"color: #9893A5; font-style: italic\"> Landing the drone<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">tello<\/span><span style=\"color: #797593\">.<\/span><span style=\"color: #575279\">land<\/span><span style=\"color: #797593\">()<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #797593; font-style: italic\">#<\/span><span style=\"color: #9893A5; font-style: italic\"> Stop the video stream<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">tello<\/span><span style=\"color: #797593\">.<\/span><span style=\"color: #575279\">streamoff<\/span><span style=\"color: #797593\">()<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #797593; font-style: italic\">#<\/span><span style=\"color: #9893A5; font-style: italic\"> Show the battery level before ending the program<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B4637A; font-style: italic\">print<\/span><span style=\"color: #797593\">(<\/span><span style=\"color: #EA9D34\">&quot;Battery :&quot;<\/span><span style=\"color: #797593\">,<\/span><span style=\"color: #575279\"> tello<\/span><span style=\"color: #797593\">.<\/span><span style=\"color: #575279\">get_battery<\/span><span style=\"color: #797593\">())<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #797593; font-style: italic\">#<\/span><span style=\"color: #9893A5; font-style: italic\"> Stop the connection with the drone<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">tello<\/span><span style=\"color: #797593\">.<\/span><span style=\"color: #575279\">end<\/span><span style=\"color: #797593\">()<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#faf4ed;color:#625c88;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Video Stream from Tello<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.75rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#575279;--cbp-line-number-start:25;--cbp-line-number-width:13.203125px;line-height:1rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#faf4ed\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"# Instrust Tello to start video stream and ensure first frame read\ntello.streamon()\nwhile True:\n            frame = tello.get_frame_read().frame\n            if frame is not None:\n                break\" style=\"color:#575279;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki rose-pine-dawn\" style=\"background-color: #faf4ed\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #797593; font-style: italic\">#<\/span><span style=\"color: #9893A5; font-style: italic\"> Instrust Tello to start video stream and ensure first frame read<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">tello<\/span><span style=\"color: #797593\">.<\/span><span style=\"color: #575279\">streamon<\/span><span style=\"color: #797593\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #286983\">while<\/span><span style=\"color: #575279\"> <\/span><span style=\"color: #D7827E\">True<\/span><span style=\"color: #797593\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">            frame <\/span><span style=\"color: #286983\">=<\/span><span style=\"color: #575279\"> tello<\/span><span style=\"color: #797593\">.<\/span><span style=\"color: #575279\">get_frame_read<\/span><span style=\"color: #797593\">().<\/span><span style=\"color: #575279\">frame<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">            <\/span><span style=\"color: #286983\">if<\/span><span style=\"color: #575279\"> frame <\/span><span style=\"color: #286983\">is<\/span><span style=\"color: #575279\"> <\/span><span style=\"color: #286983\">not<\/span><span style=\"color: #575279\"> <\/span><span style=\"color: #D7827E\">None<\/span><span style=\"color: #797593\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">                <\/span><span style=\"color: #286983\">break<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#faf4ed;color:#625c88;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<p>With DJITELLOPY to achieve the video stream is very simple, we use streamon() to instruct Tello to enable the video stream and get_frame_read() to read the existing video frame. <\/p>\n\n\n\n<p>However, there may be a delay after calling streamon()<strong> <\/strong>in the DJITELLOPY library. When we call streamon(), we are initiating the video streaming from the Tello drone&#8217;s camera. The drone needs some time to establish the streaming connection and start sending video frames.<\/p>\n\n\n\n<p>So, we setup a while loop to ensure the camera is ready and the first frame is being read before we proceed to the next step.<\/p>\n\n\n\n<p>The get_frame_read() method in the djitellopy library returns a VideoFrame object that provides access to the current video frame from the Tello drone&#8217;s camera. Apart from the frame attribute, which contains the video frame data as a NumPy array, the VideoFrame object has other attributes that provide information about the frame. These attributes include:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>frame: The actual video frame data as a NumPy array.<\/li>\n\n\n\n<li>time: The timestamp of the frame in milliseconds.<\/li>\n\n\n\n<li>frame_number: The sequential number of the frame.<\/li>\n\n\n\n<li>h: The height of the video frame.<\/li>\n\n\n\n<li>w: The width of the video frame.<\/li>\n\n\n\n<li>channel: The number of color channels in the frame (usually 3 for RGB).<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Video Capture and display<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.75rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#575279;--cbp-line-number-start:13;--cbp-line-number-width:13.203125px;line-height:1rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#faf4ed\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"# def a video capture and display function\ndef capturing_video(tello):\n    while True:\n        frame = tello.get_frame_read().frame\n        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)\n        cv2.imshow('Tello Video Stream', frame)\n        cv2.moveWindow('Tello Video Stream',0,0)\n        cv2.waitKey(1)\" style=\"color:#575279;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki rose-pine-dawn\" style=\"background-color: #faf4ed\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #797593; font-style: italic\">#<\/span><span style=\"color: #9893A5; font-style: italic\"> def a video capture and display function<\/span><\/span>\n<span class=\"line\"><span style=\"color: #286983\">def<\/span><span style=\"color: #575279\"> <\/span><span style=\"color: #D7827E\">capturing_video<\/span><span style=\"color: #797593\">(<\/span><span style=\"color: #907AA9; font-style: italic\">tello<\/span><span style=\"color: #797593\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">    <\/span><span style=\"color: #286983\">while<\/span><span style=\"color: #575279\"> <\/span><span style=\"color: #D7827E\">True<\/span><span style=\"color: #797593\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">        frame <\/span><span style=\"color: #286983\">=<\/span><span style=\"color: #575279\"> tello<\/span><span style=\"color: #797593\">.<\/span><span style=\"color: #575279\">get_frame_read<\/span><span style=\"color: #797593\">().<\/span><span style=\"color: #575279\">frame<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">        frame <\/span><span style=\"color: #286983\">=<\/span><span style=\"color: #575279\"> cv2<\/span><span style=\"color: #797593\">.<\/span><span style=\"color: #575279\">cvtColor<\/span><span style=\"color: #797593\">(<\/span><span style=\"color: #575279\">frame<\/span><span style=\"color: #797593\">,<\/span><span style=\"color: #575279\"> cv2<\/span><span style=\"color: #797593\">.<\/span><span style=\"color: #286983\">COLOR_BGR2RGB<\/span><span style=\"color: #797593\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">        cv2<\/span><span style=\"color: #797593\">.<\/span><span style=\"color: #575279\">imshow<\/span><span style=\"color: #797593\">(<\/span><span style=\"color: #EA9D34\">&#39;Tello Video Stream&#39;<\/span><span style=\"color: #797593\">,<\/span><span style=\"color: #575279\"> frame<\/span><span style=\"color: #797593\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">        cv2<\/span><span style=\"color: #797593\">.<\/span><span style=\"color: #575279\">moveWindow<\/span><span style=\"color: #797593\">(<\/span><span style=\"color: #EA9D34\">&#39;Tello Video Stream&#39;<\/span><span style=\"color: #797593\">,<\/span><span style=\"color: #D7827E\">0<\/span><span style=\"color: #797593\">,<\/span><span style=\"color: #D7827E\">0<\/span><span style=\"color: #797593\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">        cv2<\/span><span style=\"color: #797593\">.<\/span><span style=\"color: #575279\">waitKey<\/span><span style=\"color: #797593\">(<\/span><span style=\"color: #D7827E\">1<\/span><span style=\"color: #797593\">)<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#faf4ed;color:#625c88;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<p>We define a function to read the frame from the drone and show as image in a separated window by CV2. There are two highlights,<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>The frame we capture from Tello is in RGB colors but cv2 processing image as &#8216;BGR&#8217; order. If we show the image directly, it will result as something bluish (Smurfs?). We need to convert this from RGB to BGR before we can show this properly.<\/li>\n\n\n\n<li>cv2.waitKey() must be excuted after the cv2.imshow(), otherwise, the image will not be displayed. <\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Parallel Processing<\/h3>\n\n\n\n<p>Since we need to capture the frame and display during the drone flying, we need parallel processing for capture_video().<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.75rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#575279;--cbp-line-number-start:32;--cbp-line-number-width:13.203125px;line-height:1rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#faf4ed\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"# Start the video capture thread when the drone is flying\nvideo_thread = threading.Thread(target=capturing_video, args=(tello,), daemon=True)\nvideo_thread.start()\" style=\"color:#575279;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki rose-pine-dawn\" style=\"background-color: #faf4ed\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #797593; font-style: italic\">#<\/span><span style=\"color: #9893A5; font-style: italic\"> Start the video capture thread when the drone is flying<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">video_thread <\/span><span style=\"color: #286983\">=<\/span><span style=\"color: #575279\"> threading<\/span><span style=\"color: #797593\">.<\/span><span style=\"color: #575279\">Thread<\/span><span style=\"color: #797593\">(<\/span><span style=\"color: #907AA9; font-style: italic\">target<\/span><span style=\"color: #286983\">=<\/span><span style=\"color: #575279\">capturing_video<\/span><span style=\"color: #797593\">,<\/span><span style=\"color: #575279\"> <\/span><span style=\"color: #907AA9; font-style: italic\">args<\/span><span style=\"color: #286983\">=<\/span><span style=\"color: #797593\">(<\/span><span style=\"color: #575279\">tello<\/span><span style=\"color: #797593\">,),<\/span><span style=\"color: #575279\"> <\/span><span style=\"color: #907AA9; font-style: italic\">daemon<\/span><span style=\"color: #286983\">=<\/span><span style=\"color: #D7827E\">True<\/span><span style=\"color: #797593\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #575279\">video_thread<\/span><span style=\"color: #797593\">.<\/span><span style=\"color: #575279\">start<\/span><span style=\"color: #797593\">()<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#faf4ed;color:#625c88;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<p>We started the video capture thread just before the drone takeoff, so that we can see the video window when the drone take off and flying.<\/p>\n\n\n\n<p>Again, that&#8217;s cool and easy, we just add few more lines to the last program and make it fly with video capturing. We believe that we can start doing face detection and tracking now.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We introduced Drone movement in the last post, we are going to try the video capture. It is also very simple with the help of DJITELLOPY and CV2 API. With the basic movement control and video capture, we can start face detection and face tracking very soon. Let&#8217;s show you how to capture video from &hellip; <a href=\"https:\/\/bversion.com\/WordPress\/2023\/08\/30\/drone-programming-video-capturing\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Drone Programming &#8211; Video Capturing<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":6,"featured_media":1157,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"cybocfi_hide_featured_image":"yes","footnotes":""},"categories":[14],"tags":[],"class_list":["post-1149","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-drone_control"],"_links":{"self":[{"href":"https:\/\/bversion.com\/WordPress\/wp-json\/wp\/v2\/posts\/1149","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bversion.com\/WordPress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bversion.com\/WordPress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bversion.com\/WordPress\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/bversion.com\/WordPress\/wp-json\/wp\/v2\/comments?post=1149"}],"version-history":[{"count":8,"href":"https:\/\/bversion.com\/WordPress\/wp-json\/wp\/v2\/posts\/1149\/revisions"}],"predecessor-version":[{"id":1174,"href":"https:\/\/bversion.com\/WordPress\/wp-json\/wp\/v2\/posts\/1149\/revisions\/1174"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/bversion.com\/WordPress\/wp-json\/wp\/v2\/media\/1157"}],"wp:attachment":[{"href":"https:\/\/bversion.com\/WordPress\/wp-json\/wp\/v2\/media?parent=1149"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bversion.com\/WordPress\/wp-json\/wp\/v2\/categories?post=1149"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bversion.com\/WordPress\/wp-json\/wp\/v2\/tags?post=1149"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}