• 沒有找到結果。

國中生的

N/A
N/A
Protected

Academic year: 2022

Share "國中生的"

Copied!
43
0
0

加載中.... (立即查看全文)

全文

(1)

國中生的Python課

電腦視覺與空拍機

詹照塘

新興 自造教育及科技中心

(2)

Agenda

1

TELLO EDU SDK2.0 介紹

2

Python 簡介

3

4

用python 程式控制TELLO EDU

5

TELLO EDU群飛程式操控

今日教案簡報:

7

Tello EDU 臉部偵測追蹤飛行

6

openCV人臉辨識 附錄

https://reurl.cc/Xe4lm3

(3)

Part 1

TELLO EDU SDK2.0 架 構

透過UDP建立 Tello 和 PC、Mac 或移動設備之間的 Wi-Fi 通信。

發送命令和接收回應

Tello IP:192.168.10.1 UDP PORT:8889 << - - >>

PC / Mac / Mobile

步驟 1:在 PC,Mac 或移動設備上設置 UDP 用戶端,向 Tello UDP 埠 8889 發送命令和接收回應。

步驟 2:在發送所有其他命令之前,發送“

command

” 向 Tello UDP 埠 8889 命令以啟動 Tello 的 SDK 模式。

接收 Tello 狀態

Tello IP:192.168.10.1 - >> PC / Mac / Mobile UDP Server:0.0.0.0 UDP PORT:8890

(4)

Part 1

TELLO EDU SDK2.0

步驟 3:先完成步驟 1 和 2 的操作。在 PC、Mac 或移動 設備上建立 UDP 伺服器,通過 UDP 埠

8890

從 IP

0.0.0.0 收聽消息。

* 詳細狀態資訊請查看 5. Tello 狀態。

接收 Tello 視頻流

Tello IP:192.168.10.1 - >> PC / Mac / Mobile UDP Server:0.0.0.0 UDP PORT:11111

步驟 4:在 PC,Mac 或移動設備上設置 UDP 伺服器,通 過伺服器 UDP 埠 11111 從 IP 0.0.0.0 收聽消息。

步驟 5:先進行步驟 1 和 2 的操作,然後向 Tello UDP 埠 8889 發送 “streamon” 命令,開始接受 Tello 視頻流。

(5)

Part 2

TELLO EDU Python 基礎簡介

位元組碼 機械碼

原始碼

•認識Python程式語言

Python

是一種物件、程序、函數導向,直譯式的跨平台電腦程式 語言; 可輕鬆完成很多常見的任務(例如:讀寫檔案、自然語言處 理、網路爬蟲、網站開發、機器學習等)

#include <stdio.h>

int main(){

printf(“Hello! World!\n”);

}

public class HelloWorld{

public static void main(String[]

args) {

System.out.println(“Hello!

World!”); }}

print(“Hello! World”)

C

python JAVA

Python 主要設計的原則和特色就在於簡潔。

(6)

Part 2

TELLO EDU Python 基礎簡介

•認識Python程式語言

#成績

grade = 90

“””成績>90: excellent

>60 : fair <60 NG”””

if grade >= 90:

print('Excellent!') elif grade >= 60:

print('FAIR!') else:

print('NG!')

# :註解

“””……””” : 多行註解,前 後連續3個雙引號 ”或 單引號’均可。 ’’’……’’’

縮排 indent

使用縮排來定義區塊 , 而不使用{ }大括弧。

(7)

Part 2

TELLO EDU Python 基礎簡介

•認識Python程式語言

變數 :基本型別有:int (整數)、float (浮點數)、str (字串)、布林。

宣告變數並不用先給定型別,依初始值來決定變數的型別

intV = 10 fV = 10.3

strV = 'hello world' bV = True

print(intV, fV, strV, bV)

print('型態: '+str(type(intV))) print('型態: '+str(type(fV))) print('型態: '+str(type(strV))) print(type(bV))

print(str(intV)+strV,'浮點 :'+str(fV)) print(intV+fV)

10 10.3 hello world True

型態: <class 'int'>

型態: <class 'float'>

型態: <class 'str'>

<class 'bool'>

10hello world 浮點 :10.3

20.3 執行結果

布林值除可直接指定: a=True ,

b=False外(T,F要大寫),下列之數值也 被視為False: 0,0.0,()空元組,[]空清單,

{}空字典, None

(8)

Part 2

TELLO EDU Python 基礎簡介

•認識Python程式語言 運算子:除了先乘除後加減,運算子的優先順序如下:

運算子 功能

() 括號

** 指數

+,- 正負號

*,/,//,% 算數 : 乘,除,整數除法,取餘數

+,- 算數 : 加,減

<,<=,>,>=,

!=,==

小於,小於等於,大於,大於等於,

不等於,等於

not,and,or 邏輯: 否、且、或

(9)

Part 2

TELLO EDU Python 基礎簡介

•認識Python程式語言 條件判斷: if ; if/else if/elif/else:

a= int (input('請輸入目前 氣溫 : '))

if a>30:

print('打開門窗')

print('目前氣溫: '+str(a)+' 度')

請輸入目前氣溫 : 26 目前氣溫: 26度

請輸入目前氣溫 : 35 打開門窗

目前氣溫: 35度

執行結果

注意

•單選: if

•二選一: if /else

•多選一: if /elif/else if:

(10)

Part 2

TELLO EDU Python 基礎簡介

•認識Python程式語言 條件判斷: if ; if/else if/elif/else:

a= int (input('請輸入目前 氣溫 : '))

if a>26:

print('請穿短袖') else:

print('請穿長袖')

請輸入目前氣溫 : 27 請穿短袖

請輸入目前氣溫 : 22 請穿長袖

執行結果 注意

•單選: if

•二選一: if /else

•多選一: if /elif/else If / else:

(11)

Part 2

TELLO EDU Python 基礎簡介

•認識Python程式語言 條件判斷: if ; if/else if/elif/else:

a= int (input('請輸入目前 氣溫 : '))

if a>26:

print('請穿短袖') elif a<20:

print('請穿外套') else:

print('請穿長袖')

請輸入目前氣溫 : 31 請穿短袖

請輸入目前氣溫 : 19 請穿外套

請輸入目前氣溫 : 24 請穿長袖

執行結果 注意

•單選: if

•二選一: if /else

•多選一: if /elif/else If / elif/ else:

(12)

Part 2

TELLO EDU Python 基礎簡介

•認識Python程式語言 迴圈for & while::

sum=0

for i in range(5):

sum=sum+i

print('累積: '+str(sum))

累積: 0 累積: 1 累積: 3 累積: 6 累積: 10

執行結果 注意

for (通常用在已知的迴圈數)

(13)

Part 2

TELLO EDU Python 基礎簡介

•認識Python程式語言 range函數: range() : range(start, end, step)

start:預設0; step:預設1

range( )函式 實際範圍

range(5) 0,1,2,3,4 range(1,5) 1,2,3,4 range(1,10,2) 1,3,5,7,9 range(1,11,2) 1,3,5,7,9

range(0,-10,-2) 0,-2, -4 , -6 -8 range(? ,? ,?) 4,3,2,1,0

(14)

Part 2

TELLO EDU Python 基礎簡介

•認識Python程式語言 迴圈for & while::

sum=0 i=1

while i <5:

sum=sum+i

print('累積: '+str(sum)) i=i+1

累積: 0 累積: 1 累積: 3 累積: 6 累積: 10

執行結果

注意

while (要自己處理迴圈數的變化)

(15)

Part 2

TELLO EDU Python 基礎簡介

•認識Python程式語言 容器資料型態:

執行結果

元組(Tuple) :唯讀不能新增,刪除或者更新 tuple 的元素

清單[List] :可以容納不同的變數類型與資料結構,類似陣 列(Array),可新增、插入、刪除或者更新

字典{dictionary

}

: dictionary 是帶有鍵值(key)的 list

tp =(3,4,5,6) tp[0]=3

tp[-1]=6

lst =[3,4,5,6]

>>> ani={'cat':4,'dog':4,'bird':2,'turtle':4}

>>> for poi,legs in ani.items():

print('動物%s 有%d 隻腳' %(poi,legs))

動物cat 有4 隻腳 動物dog 有4 隻腳 動物bird 有2 隻腳 動物turtle 有4 隻腳

集合{dictionary

}

: 是一種沒有順序的元素集合,如同list的功能,但每一個元素都是唯一的, S={‘turtle’,’dog’,’cat’,python’}

(16)

Part 3

TELLO EDU 四軸飛行器 單機程控

✓ 打開手機Wi-Fi

連接自己的飛行器, (SSID在機身上面)

 連結完成

(17)

Part 3

TELLO EDU 單機程控

使用Python程式語言

把電腦直接連到Tello進行程控

附件檔案連結 https://reurl.cc/Xe4lm3 在附件的檔案中包含:

tello_command.py

: SDK指令操控

4corner.py

: 程控TELLO飛一個方形後降落

skillFly.py

: 程控TELLO作順時鐘、逆時鐘、上下彈跳

starFlight.py

:程控TELLO飛一個星形後降落

(18)

Part 3

TELLO EDU 單機程控

使用Python程式語言

把電腦直接連到Tello進行程控

程式說明: 以tello_command.py 為例

# 此範例為如何用Python將SDK命令透過鍵盤程控Tello

# https://learn.droneblocks.io/p/tello-drone-programming-with-python/ 線上課程

import socket import threading import time

import sys

# IP and port of Tello

tello_address = ('192.168.10.1', 8889)

# IP and port of local computer

local_address = ('', 9000)

(19)

Part 3

TELLO EDU 單機程控

使用Python程式語言

把電腦直接連到Tello進行程控

# 建立一個UDP連結以發送指令

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# Bind to the local address and port sock.bind(local_address)

# 定義發送訊息給Tello的函數 def send(message):

# Try to send the message otherwise print the exception try:

sock.sendto(message.encode(), tello_address) print("Sending message: " + message)

except Exception as e:

print("Error sending: " + str(e))

(20)

Part 3

TELLO EDU 單機程控

使用Python程式語言 把電腦直接連到Tello進行程控

# 定義從Tello接收訊息的函數

def receive():

# 循環迴圈接收訊息

while True:

# Try to receive the message otherwise print the exception

try:

response, ip_address = sock.recvfrom(128)

print("Received message: " + response.decode(encoding='utf-8')) except Exception as e:

# If there's an error close the socket and break out of the loop

sock.close()

print("Error receiving: " + str(e)) break

(21)

Part 3

TELLO EDU 單機程控

使用Python程式語言 把電腦直接連到Tello進行程控

# 建立並在背景執行監聽緒

# 此執行緒使用上述之接收訊息的函數持續不斷的運行 receiveThread = threading.Thread(target=receive) receiveThread.daemon = True

receiveThread.start()

# 操作指示

print("請輸入Tello SDK指令,並按下'Enter'鍵.或輸入'quit'離開程 式.")

# 一直循環等待指令的輸入或是使用'quit'或ctrl-c退出 while True:

try:

# 讀取鍵盤輸入

(22)

Part 3

TELLO EDU 單機程控

使用Python程式語言 把電腦直接連到Tello進行程控

message = input('')

# 當收到

'quit'

指令,則結束程式並關閉連結 if 'quit' in message:

print("Program exited sucessfully") sock.close()

break

# 送出指令

send(message)

# 處理當使用ctrl-c時之例外處理 except KeyboardInterrupt as e:

sock.close() break

(23)

Part 4

TELLO EDU 四軸飛行器 群飛程控

✓ 打開手機Wi-Fi

連接自己的飛行器, (SSID在機身上面)

 連結完成

✓執行telloAP_assign.py 設成→ Station 模式

(24)

Part 3

TELLO EDU 四軸飛行器 群飛程控

# 將Tello設定成Station模式_1

# 將Tello設定成Station模式

# http://www.ryzerobotics.com/

import threading import socket

import time host = ''

port = 9000

locaddr = (host,port)

# Create a UDP socket

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

tello_address = ('192.168.10.1', 8889) sock.bind(locaddr)

(25)

Part 4

TELLO EDU 四軸飛行器

Tello EDU app

程式控制活動操作_挑戰卡說明

◆前往 XYZ M: 飛往設置 id 的挑戰卡坐標系的(x,y,z)座標點, M 空格可填入: 1~8 (挑戰卡上面的數字)

M 空格填入-1 : Tello Edu 內部演算法最快識別到的挑戰卡 M 空格填入-2 : 距離 Tello Edu 最近的挑戰卡

◆跳躍 XYZ M1 M2: Tello Edu 飛往 Mid1 坐標系的(x,y,z)點後 懸停, 識別 Mid2 的挑戰卡, 並向在 Mid2坐標系下(0,0,z) 的位 置旋轉到設置的 yaw 角度值 (Z>=50)

◆ 曲線飛行 P1,P2,M:飛弧線, 經過設置 mid 的挑戰卡坐標系 中的 P1(x,y,z)到 P2(x,y,z) 點, M 空格可填入: 1~8 (挑戰卡上 面的數位) ,速度 10~60cm/s , x,y: -500~500 , z: 50~500

M 空格填入-1 : Tello Edu 內部演算法最快識別到的挑戰卡 M 空格填入-2 : 距離 Tello Edu 最近的挑戰卡

(26)

Part 4

TELLO EDU 四軸飛行器

Tello EDU app

程式控制活動操作_挑戰卡

◆ 跳躍 XYZ M1 M2 M3 M4

(27)

Part 5

TELLO EDU

openCV人臉辨識

人臉辨識

技術屬於電腦視覺(CV)的範疇。結合了

AI

的技術,讓 CV的效用提升,從影像分析,透過深度學習訓練,再利用演算法歸 納出其關聯來識別出結果。此概念類似的應用,不只用來識別人臉,

也可用來識別動物、水果等任何物品,只要具備有識別性的特徵,

電腦就能透過演算法進行分析及辨識。資料來源:

http://www.netadmin.com.tw/netadmin/zh-tw/technology/CDAFF5E13BEF4B0D8FF1199F43022D75

人臉辨識概要流程:

影像 輸入

經由模型 數據預測 身份辨識

人臉 擷取人臉

特徵數據 yes

人臉特徵 模型數據

收集資料 訓練、學習導出模型

(28)

Part 5

TELLO EDU

openCV人臉辨識 之一、資料收集 faceCreating.py

#從video stream攫取影像

camera=cv2.VideoCapture(0)

faceCascade=cv2.CascadeClassifier("haarcascade_frontallface_default.xml")

#haarcascadeface_default.xml 下載點:

https://github.com/KiLJ4EdeN/Flask_FacialRecognition_WebService/tree/mas ter/Flask_FacialRecognition_WebService

name=input(“What‘s his/her name?”)#建立類別名稱 .

. .

與openCV相關:

cv2.VideoCapture(0) :擷取攝影機的影像,0代表第一支攝影機 cv2.CascadeClassifier

gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)

faces=faceCascade.detectMultiScale(gray,1.5,5)#檢測 roiGray=gray[y:y+h,x:x+w]## 裁切圖片

cv2.imwrite(fileName,roiGray) cv2.imshow("face",roiGray)

(29)

Part 6

TELLO EDU

openCV人臉辨識 之二、訓練 faceTraining.py

import numpy as np import pickle

from PIL import Image

#python 3.x 後,要安裝pillow

faceCascade=cv2.CascadeClassifier("haarcascade_frontallface_default.xml")

#要安裝pip install opencv-contrib-python .

. 與openCV相關:

cv2.CascadeClassifier

cv2.face.LBPHFaceRecognizer_create()

Image.open(path).convert("L")#打開圖片並轉換成灰階圖 np.array(pilImage,"uint8")#將image形式轉換成array陣列 roi=imageArray[y:+y+h,x:x+w]

xTrain.append(roi)

pickle.dump(labelIds,f)

recognizer.train(xTrain,np.array(yLabels))

recognizer.save(“trainer.yml”)#訓練好的風格樣式

(30)

Part 5

TELLO EDU

openCV人臉辨識 之三、辨識偵測 faceRecognizing.py

#執行指令碼然後進入互動模式

os.environ['PYTHONINSPECT'] = 'TRUE' import numpy as np

import pickle

with open('labels', 'rb') as f:

dicti = pickle.load(f) f.close()

.與openCV相關:

recognizer.read("trainer.yml")

font = cv2.FONT_HERSHEY_SIMPLEX id_, conf = recognizer.predict(roiGray)

cv2.putText(frame, name, (x, y), font, 2, (0, 0 ,255), 2,cv2.LINE_AA)

#cv2.LINE_AA抗鋸齒

(31)

Part 6

TELLO EDU

臉部偵測追蹤飛行Main.py 電腦會依Tello EDU 與臉部的距離、高低及方向,自動調整 其飛行

import tello_drone as tello host = ''

port = 9000

local_address = (host, port)

#如果is_dummy =true,則會使用webcam

drone = tello.Tello(host, port, is_dummy=False)

def adjust_tello_position(offset_x, offset_y, offset_z):

“””offset_x: 中心點與臉部中心的X座標偏移值_修正角度 offset_y: 中心點與臉部中心的Y座標偏移值_修正高低 offset_z: 偵測到臉部的方形框的面積_修正遠近

其中角度的修正是以方框的面積大小成正比。

(32)

附錄: FAQ:

Tello 一直無法與電腦連線?

此情況最可能是,Tello尚未執行“啟動”,最大的可能是剛買的

新機第一次使用,這時請用

Tello

app(不是Tello EDU)去啟動並更新韌

體(firmware)即可正常操作。

(33)

附錄:

參考資料:

Tello空拍機入門/連宏城/台科大 CV+深度學習AI /謬鵬/深智數位 學AI真簡單 I、II / AI4kids / 全華 http://www.wiedu.com/telloedu/

https://www.ryzerobotics.com/zh-tw/tello http://kitsprout.logdown.com/posts/335383

https://edu.taiwandrone100.com/steam-school/drone-program-competition/

https://game.hlc.edu.tw/drone/plan2019.asp https://www.python.org/

Haar串聯分類器 https://github.com/opencv/opencv/tree/master/data/haarcascades https://en.wikipedia.org/wiki/Haar_wavelet

(34)

附錄:

四軸飛行器飛行的原理:

飛行器的螺旋槳配置示意圖如下圖的左上角所示,兩兩對角的螺旋槳轉向剛好 相反,透過這種配置,來互相抵銷旋轉上的力矩,在已經平衡的情況下,同時增加或 減少四個旋翼的推力的話,可以做出上升與下降的動作,同理:同時增加或減少相鄰或 對角的兩個旋翼的推力,可以做出前後左右或順逆時針旋轉運動的動作。

(35)

附錄:

2020,10,18 無人機競速飛行競賽_場地

(36)

附錄:

2020 無人機競速飛行競賽_遙控與編程(決賽)

(37)

TELLO EDU 四軸飛行器

新 興自 造 教 育 及 科 技 中 心

無人機操作安全守則:

1. 未說起飛時請勿起飛。

2. 請勿在飛行區域外練習。

3. 請勿在人員附近或上方飛行。

4. 請緊盯自己的飛機,危急時,務必提醒夥伴注意。

5. 指示降落時,務必要降落。

6. 請務必要設定起飛與降落的程式積木。

7. 確認好自己的程式後,經確認後才可以飛行。

罰則篇

1.處新臺幣三十萬元以上一百五十萬元以下罰鍰。

2.於禁航區、限航區及航空站或飛行場四周之一定距離範 圍內從事飛航活動。

3.逾距地表高度四百呎從事飛航活動。

附錄:

(38)

TELLO EDU 四軸飛行器

無人機操作場所限制:

1. 於禁航區要操作無人機時,其場所必須是四面有牆且 上方有屋頂。上方有屋頂,不飛出屋頂涵蓋範圍可。

2. 位於禁航區 ,即使無人機重量少於250克,也不能在 室外飛行。

3. 相關細則與規定,請參考民航局無人機專區。

https://www.caa.gov.tw/article.aspx?a=188&lang=1

4. 民航局飛航指南:

http://eaip.caa.gov.tw/eaip/history/2016-01- 21/html/eAIP/RC-ENR-5.1-zh-TW.html

5. 查詢禁航區APP

iOS 、Android : SkySentry - 無人機飛行安全的專家

附錄:

(39)

安裝Python

附錄:

i) 下載Python https://www.python.org/downloads/release/python-380/

ii)開始安裝Python

先點選Add Python 3.8 to Path 然後 選擇 Customize installation

(40)

安裝Python

附錄:

iii) Optional Features裡面全部勾選然後按 Next

iv) Advanced Options裡面 勾選Install for all users Customize install location路徑預設會變成 C:\Program Files\Python38

去掉Program Files 讓他在只在C槽底下把它改 成 C:\Python38

然後就等安裝完成

(41)

Win10 行動熱點

附錄:

(42)

Q&A fianl

FB

新興自造中心 LINE

新興自造中心

課後回饋單:

(43)

謝謝聆聽 請記得加入新興自造中心

FB粉絲專頁與 LINE群組

新興 自造教育及科技中心

參考文獻

相關文件

阿吉老師將簡介 micro:bit 開發程式環境,並分享 micro:bit 內建感測器程式編寫、數位/類比信號讀取及寫入,並了解 如何用 micro:bit 連接電子模組編寫程式進行機電整合。.

專案導向應用程式開發 階梯程式編輯畫面 狀態的監視與控制 階梯程式助憶碼輔助顯示 階梯程式註解功能

可程式控制器 (Programmable Logic Controller) 簡稱 PLC,是一種具有微處理機功能的數位電子 設備

數位計算機可用作回授控制系統中的補償器或控制

 MATLAB 程式使用 pass-by-value 的方 式,進行程式與函式間的溝通聯絡,當 程式呼叫函式時, MATLAB

藍牙 Android 遙控程式

[r]

進而能自行分析、設計與裝配各 種控制電路,並能應用本班已符 合機電整合術科技能檢定的實習 設備進行實務上的實習。本課程 可習得習得氣壓-機構連結控制