--- title: 0812 內部課程 tags: 內部研習 --- <style> .navbar-brand:after { content: " × Web:AI"; } .a { color: red; font-weight:bold } </style> > [上傳區](https://hackmd.io/@webduino/0812_stu) > [範例程式 github](https://github.com/janice880624/webai) ## 講義 👉 [python 語法-1](https://www.canva.com/design/DAEmI26MV6U/BsmO6Vo7w2KHlacwFu-BlQ/view) 👉 [python 語法-2](https://www.canva.com/design/DAEmN54xf5g/OV1B0c5_0QZM9uvfqSD65A/view) 👉 [拍攝影像、文字與繪圖](https://www.canva.com/design/DAEmT7qXUeo/mmRWoMOXJiEZK8sKWCGDMw/view) ## 字典 ### 建立字典 ```python fruit= {'apple':40, 'banana':30, 'grapes':50} print(fruit) ``` ### 查詢項目 ```python fruit= {'apple':40, 'banana':30, 'grapes':50} print(fruit['apple']) ``` ### 新增或更新 ```python fruit= {'apple':40, 'banana':30, 'grapes':50} fruit['apple'] = 100 fruit['lemon'] = 20 print(fruit) ``` ### 刪除 ```python fruit= {'apple':40, 'banana':30, 'grapes':50} del fruit['banana'] print(fruit) ``` --- ## 函式 ```python a = 5 b = 10 def add_sum(a, b): return a+b print(add_sum(a, b)) ``` ### <span class='a'> 函式練習 </span> ```python # 請你設計一個簡易計算機,分別定義以下函式 # add_sum( )、sub_sum( )、mult_sum( )、div_sum( ) # 當使用者輸入兩個數 a, b 時,計算出答案 # print(add_sum(a, b)) # print(sub_sum(a, b)) # print(mult_sum(a, b)) # print(div_sum(a, b)) ``` --- ## 拍攝影像 ### 建立監聽程序 ```python def click(name, state): print(name, state) webai.addBtnListener(click) ``` ### 取得按鈕狀態 ```python while True: L = webai.btnL.btn.value() R = webai.btnR.btn.value() print(L, R) time.sleep(0.2) ``` ### 啟動鏡頭 ```python while True: webai.img = webai.snapshot() webai.show(img = webai.img) time.sleep(0.2) ``` ### 儲存照片 ```python webai.img = webai.snapshot() webai.img.save('photo.jpg') webai.show(file = 'photo.jpg') ``` ### 自拍機 ```python def click(name, state): if name == 'btnL' and state == 1: webai.img.save('photo.jpg') webai.addBtnListener(click) while True: if webai.btnR.btn.value() == 0: webai.show(file = 'photo.jpg') else: webai.img = webai.snapshot() webai.show(img = webai.img) ``` ## 影像處理 ### 查看韌體 ```python webai.fw.get() ``` ### 切換韌體 ```python webai.fw.set('std') ``` ### 3D 變形 ```python webai.img = webai.res.loadImg('photo.jpg') webai.img.rotation_corr(y_rotation=25, x_translation=15, zoom=0.8) webai.show(img=webai.img) ``` ### <span class='a'> 讓照片轉起來</span> ```python # 想一下怎麼讓照片轉起來 for i in range(0, 361, 2): # 0,2,4,6,...,360 webai.img = webai.res.loadImg('photo.jpg') # 載入開發板中的圖片成為Image物件 webai.img.rotation_corr(y_rotation=i, x_translation=0, zoom=1) # 圖像3D變形 webai.show(img=webai.img) # 圖像顯示在LCD ``` ### 卡通特效 ```python while True: webai.img = webai.snapshot() webai.img.cartoon(seed_threshold=0.2) webai.show(img=webai.img) ``` ### 猜看看是什麼特效-1 ```python while True: webai.img = webai.snapshot() webai.img.to_rainbow() webai.show(img=webai.img) ``` ### 猜看看是什麼特效-2 ```python while True: webai.img = webai.snapshot() webai.img.chrominvar() webai.show(img=webai.img) ``` ### 猜看看是什麼特效-3 ```python while True: webai.img = webai.snapshot().negate() webai.show(img=webai.img) ``` --- ## 文字與繪圖 ### 長寬 ```python print('lcd widht : {}'.format(webai.lcd.width())) print('lcd height : {}'.format(webai.lcd.height())) ``` ### 線 ```python webai.img = image.Image() webai.img.draw_line(10, 10, 100, 100) webai.show(img=webai.img) ``` ### 三角形 ```python webai.img = image.Image() webai.img.draw_line(10, 10, 100, 100) webai.img.draw_line(100, 100, 100, 10) webai.img.draw_line(100, 10, 10, 10) webai.show(img=webai.img) ``` ### 圓形 ```python webai.img = image.Image() webai.img.draw_circle(160, 120, 40) webai.show(img=webai.img) ``` ### 圓形之玩玩特效 ```python while True: for i in range(10, 100, 5): webai.clear() webai.img.draw_circle(160, 120, i) webai.show(img=webai.img) ``` ### 矩行 ```python webai.img = image.Image() webai.img.draw_rectangle(10, 10, 300, 220, thickness=250) webai.show(img=webai.img) ``` ### 橢圓形 ```python webai.img = image.Image() webai.img.draw_ellipse(160, 120, 40, 80) webai.show(img=webai.img) ``` ### 動ㄘ動ㄘ ```python while True: for i in range(40, 80, 5): webai.clear() webai.img.draw_ellipse(160, 120, 40, i) webai.show(img=webai.img) for i in range(40, 80, 5): webai.clear() webai.img.draw_ellipse(160, 120, i, 40) webai.show(img=webai.img) ``` ### 文字 ```python webai.clear() webai.draw_string(100, 100, '哈囉', scale=2) ``` ### <span class='a'> 跑馬燈</span> ```python # 剛剛教你顯示文字了,試著結合之前的迴圈,做出跑馬燈 webai.img = None while True: for i in range(320, -320, -5): webai.draw_string(i, 100, '哈囉哈囉', scale=2) ``` :::warning 超過五個字最後面加空格 ::: ### 箭頭 ```python webai.clear() webai.img = image.Image() webai.img.draw_arrow(10, 10, 150, 150) webai.show(img=webai.img) ``` ### 十字 ```python webai.clear() webai.img = image.Image() webai.img.draw_cross(10, 10) webai.show(img=webai.img) ``` ### 眼睛 ```python webai.clear() webai.img = image.Image() webai.img.draw_ellipse(100, 120, 50, 80, 0, (255, 255, 255), fill=True) webai.img.draw_ellipse(220, 120, 50, 80, 0, (255, 255, 255), fill=True) webai.img.draw_circle(76, 120, 25, (139, 69, 19),fill=True) webai.img.draw_circle(74, 117, 22, (0, 0, 0),fill=True) webai.img.draw_circle(62, 115, 6, (255, 255, 255),fill=True) webai.img.draw_circle(196, 120, 25, (139, 69, 19),fill=True) webai.img.draw_circle(194, 117, 22, (0, 0, 0),fill=True) webai.img.draw_circle(182, 115, 6, (255, 255, 255),fill=True) webai.show(img=webai.img) ``` ### <span class='a'> 讓眼睛動起來 </span> ```python # 試試看吧 ``` ### 花 ```python j = 20 webai.clear() webai.img = image.Image() webai.img.draw_line(160, 123-j, 160, 240, (139, 71, 38), 10) webai.img.draw_ellipse(190, 205, 15, 37, 50, (0, 205, 0), fill=True) webai.img.draw_ellipse(130, 190, 15, 37, 130, (0, 205, 0), fill=True) for i in range(0, 360, 30): webai.img.draw_ellipse(160, 120-j, 20, 80, i+15, (255, 255, 0), fill=True) webai.img.draw_circle(160, 120-j, 55, fill=True) webai.img.draw_circle(160, 120-j, 52, (205, 149, 12), fill=True) webai.img.draw_line(160, 80-j, 200, 120-j, (0, 0, 0), 3) webai.img.draw_line(145, 85-j, 195, 135-j, (0, 0, 0), 3) webai.img.draw_line(130, 90-j, 190, 150-j, (0, 0, 0), 3) webai.img.draw_line(125, 105-j, 175, 155-j, (0, 0, 0), 3) webai.img.draw_line(120, 120-j, 160, 160-j, (0, 0, 0), 3) webai.img.draw_line(145, 155-j, 195, 105-j, (0, 0, 0), 3) webai.img.draw_line(175, 85-j, 125, 135-j, (0, 0, 0), 3) webai.img.draw_line(190, 90-j, 130, 150-j, (0, 0, 0), 3) webai.img.draw_line(120, 120-j, 160, 80-j, (0, 0, 0), 3) webai.img.draw_line(160, 160-j, 200, 120-j, (0, 0, 0), 3) webai.show(img=webai.img) ``` ### 紅綠燈 ```python i = (211, 211, 211) j = (0, 0, 0) k = 90 r = (255, 0, 0) y = (255, 255, 0) g = (0, 255, 0) t = 2 webai.clear() webai.img = image.Image() webai.img.draw_rectangle(10, 80, 300, 80, i, fill=True) webai.img.draw_rectangle(20, 70, 280, 100, i, fill=True) webai.img.draw_circle(20, 80, 10, i, fill=True) webai.img.draw_circle(300, 80, 10, i, fill=True) webai.img.draw_circle(20, 160, 10, i, fill=True) webai.img.draw_circle(300, 160, 10, i, fill=True) webai.img.draw_rectangle(20, 75, 280, 90, j, fill=True) webai.img.draw_rectangle(15, 80, 290, 80, j, fill=True) webai.img.draw_circle(20, 80, 5, j, fill=True) webai.img.draw_circle(300, 80, 5, j, fill=True) webai.img.draw_circle(20, 160, 5, j, fill=True) webai.img.draw_circle(300, 160, 5, j, fill=True) webai.img.draw_circle(160-k, 120, 37, r, fill=True) webai.img.draw_circle(160, 120, 37, y, fill=True) webai.img.draw_circle(160+k, 120, 37, g, fill=True) webai.show(img=webai.img) ``` ### 爆閃燈 ```python webai.img = image.Image() def Flashing(Light): for i in range(2): if('L' in Light): webai.img.draw_rectangle(0,80,140,80,(0,0,255),fill=True) if('R' in Light): webai.img.draw_rectangle(180,80,140,80,(255,0,0),fill=True) webai.show(img=webai.img) time.sleep(0.1) webai.img.clear() webai.show(img=webai.img) time.sleep(0.1) while True: for i in range(4): Flashing('L') Flashing('R') time.sleep(0.2) for i in range(3): Flashing('LR') time.sleep(0.2) ``` ### 紅綠燈 ```python i = (211, 211, 211) j = (0, 0, 0) k = 90 r = (255, 0, 0) y = (255, 255, 0) g = (0, 255, 0) t = 2 webai.clear() webai.img = image.Image() webai.img.draw_rectangle(10, 80, 300, 80, i, fill=True) webai.img.draw_rectangle(20, 70, 280, 100, i, fill=True) webai.img.draw_circle(20, 80, 10, i, fill=True) webai.img.draw_circle(300, 80, 10, i, fill=True) webai.img.draw_circle(20, 160, 10, i, fill=True) webai.img.draw_circle(300, 160, 10, i, fill=True) webai.img.draw_rectangle(20, 75, 280, 90, j, fill=True) webai.img.draw_rectangle(15, 80, 290, 80, j, fill=True) webai.img.draw_circle(20, 80, 5, j, fill=True) webai.img.draw_circle(300, 80, 5, j, fill=True) webai.img.draw_circle(20, 160, 5, j, fill=True) webai.img.draw_circle(300, 160, 5, j, fill=True) while True: webai.img.draw_circle(160-k, 120, 37, r, fill=True) webai.img.draw_circle(160, 120, 37, j, fill=True) webai.img.draw_circle(160+k, 120, 37, j, fill=True) webai.show(img=webai.img) time.sleep(2) webai.img.draw_circle(160-k, 120, 37, j, fill=True) webai.img.draw_circle(160, 120, 37, y, fill=True) webai.img.draw_circle(160+k, 120, 37, j, fill=True) webai.show(img=webai.img) time.sleep(2) webai.img.draw_circle(160-k, 120, 37, j, fill=True) webai.img.draw_circle(160, 120, 37, j, fill=True) webai.img.draw_circle(160+k, 120, 37, g, fill=True) webai.show(img=webai.img) time.sleep(2) ``` ### <span class='a'> 讓紅綠燈動起來 </span> ```python # 讓紅綠燈動起來 # 加入秒數 # 讓他看起來像路上的紅綠燈 ``` --- ## 雲端圖片 ### ```python webai.img = webai.snapshot() webai.show(img=webai.img) webai.cloud.putImg(webai.img, 'pic.jpg') ``` ### 下載圖片 ```python webai.img = webai.cloud.getImg('pic.jpg') webai.show(img=webai.img) ``` ### 顯示 ```python webai.cloud.download("https://drive.google.com/uc?id=16MG4W1UsfnxvO79OB90W80Zv65zFrJoj",resize=320,filename="test.jpg") webai.show(file="test.jpg") ``` :::warning 將分享的網址轉換如下即可取得圖片連結 原網址:`https://drive.google.com/file/d/字串/view` 轉換網址:`https://drive.google.com/uc?id=字串` :::