000
00
000
00
0000
A
L
randomrandint#隨機取0~9之間的整數=rrandint09
micrecordfilename="1.wav",dur=25,TH=0.02,SilenceNum=50
foon,m=1
nodeobject__init__selfa=1.selfaa
v
q
e
==linebreak
True
i
5
5
0
5
123
11
9
45
whereisthemouse.whereisthemousestart
whereisthemouse滑鼠定位工具下載https://nlacsoft.net/whereisthemouse
time1010True#每隔2秒取得鼠標位置=XYend="\n"XYtime.sleep2
time.sleep0.5
#取得螢幕擷圖,存檔至 1.png, 擷圖區域左上座標(0,0)長300寬200
=img"1.png",region=(0,0,300,200)
#取得鼠標位置=xy
1010
up
pyautogui.write123456789
10100.3
#取得(20,20)處像素RGB值
=
img
=pix
img2020
1010
#定位 1.png 於螢幕上之位置
=xy1.png
#定位 1.png 於螢幕上之位置,設定圖片比對有5%的容錯(confidence=0.95)=XY1.png"1.png"confidence0.95
#pyautogui無法控制或反應速度太慢的狀況可改試pydirectinputpydirectinput#外掛滑鼠點擊指定座標.pydirectinputclick200300#外掛按鍵控制.pydirectinputpressx
keyboard.is_pressed'p'
keyboardTruekeyboardis_pressedeend="\n"按下了 e 鍵pass
keyboard#監聽 hotkey : 'p'鍵按下時觸發下列函式def on_hotkey_triggered():passkeyboard.add_hotkey'p'on_hotkey_triggeredkeyboard.wait
#colab範例連結如下(Note:20240708發現pytube有問題暫時切換至pytubefix)#https://colab.research.google.com/drive/17t9mJhrGt54heYSscWvDxo3cEXmT54gG?usp=sharing
#youtube mp3下載os=cwdosgetcwdpytubefixYouTube=ytYouTube此處貼上Youtube網址end="\n"下載中…ytstreamsfilter().get_audio_only()downloadfilename1.mp3end="\n"已下載至下列位置end="\n"+cwd+/1.mp3
#youtube mp4下載os=cwdosgetcwdpytubefixYouTube=ytYouTube此處貼上Youtube網址end="\n"下載中…ytstreams.filter()get_highest_resolution()downloadfilename1.mp4end="\n"已下載至下列位置end="\n"+cwd+/1.mp4
pyMicVoiceDetectionmic#偵測音量,有聲段自動編號儲存,5秒後結束錄音,能量門檻值0.08#能量門檻值0.08#連續10個音框之能量小於門檻值時才做音訊切割micrecordSerialAndPlotdur=5,TH=0.08,SilenceNum=10
#連續語音辨識pyMicVoiceDetectionmicspeech_recognitionsrwhile True:#偵測音量,有聲段存至 1.wavmicrecordfilename="1.wav",dur=25,TH=0.02,SilenceNum=50srRecognizerr#由1.wav 讀出待辨識 audiowithsr.WavFile("1.wav") as source=audio.rrecordsource#取得google語音有中文辨識結果=result.rrecognize_googleaudio,language='zh-TW'end="\n"resultepass
#使用gtts產生語音gttsgTTS=ttsgTTS好的請稍後lang='zh-TW'.ttssavetemp.mp3
timetemp.mp3#播放函式,unblockedplay().timesleep5#釋放mixer物件,關閉已開啟之音檔pygame.mixer.quit
keyboard1.mp3#監聽 hotkey : 'p'鍵按下時觸發下列函式play()def on_hotkey_triggered():#如果音樂正在播放就暫停,否則繼續pause()unpause()keyboard.add_hotkey'p'on_hotkey_triggeredkeyboard.wait
#瀏覽器螢幕截圖driverget_screenshot_as_filescreenshot.png'
'''selenium 開啟 google 首頁使用XPATH找到搜尋輸入框後使用send_keys輸入pclearn0915網頁元素XPATH的取得方法可參考下列網址https://ithelp.ithome.com.tw/articles/10191811?sc=rss.iron'''#載入 selenium chromedriver 各種工具包seleniumwebdriverselenium.webdriver.chrome.service as ServiceChromeServicewebdriver_manager.chromeChromeDriverManager#載入必要項目selenium.webdriver.supportexpected_conditions as ECselenium.webdriver.support.uiWebDriverWaitselenium.webdriver.common.byBy#取得chromedriver自動開啟瀏器driverwebdriver.ChromeserviceChromeServiceChromeDriverManager().install()drivergethttps://www.google.comxpath//*[@id="APjFqb"]funcEC.visibility_of_element_locatedBy.XPATHxpathelementWebDriverWaitdriver5untilfunc#輸入123456elementsend_keyspclearn0915
#透過OCR網站做文字辨識pypercliptimeos#載入 selenium chromedriver 各種工具包seleniumwebdriverselenium.webdriver.chrome.service as ServiceChromeServicewebdriver_manager.chromeChromeDriverManager#載入必要項目selenium.webdriver.support.uiWebDriverWaitselenium.webdriver.common.byByOCRimFullPath':' not in imFullPathimFullPath+os.getcwd()+/imFullPathdriverwebdriver.ChromeserviceChromeServiceChromeDriverManager().install()drivergethttps://www.gaitubao.com/hk/ocrelementdriver.find_elementBy.XPATH//*[@id="file-upload"]elementsend_keysimFullPath.timesleep3elementdriver.find_elementBy.XPATH//*[@id="btn-copy"]elementclickdrivercloseOCRcode.pngtemppyperclip.paste()#去除不必要字元如.*,空白temp"".join(c for c in temp if c.isalnum())end="\n"temp
requestswebbrowser#search_youtube函式:傳入關鍵字,回傳youtube搜尋到的第一個網址defsearch_youtubekeyword=params"search_query":keyword=res.requestsget"https://www.youtube.com/results",params=i.restext.find/watch?v=0=j.restext.find\u0026i=vid.restext0i0j+"https://www.youtube.com"vidwebbrowseropensearch_youtube五月天的突然好想你
webbrowserwebbrowseropenhttps://github.com/ai-ky/blocks
PILImage#載入圖片=imgImageopenscreenshot.png#rect=left,top,right,bottomrect1010200200#圖片裁切(Crop)=imgimgcroprect#裁切後圖片儲存.imgsavecrop.png
#載入teachablemachine imgage模型並做測試0cv2.CAP_DSHOWmodelkeras_model.h5labels.txtclass_names#webcam影像貼圖迴圈while True:retimg=img1img#調整圖片大小並做正規化=fvcv2.INTER_AREAimg224224=fv-/fv1271#*特徵向量要多一個維度,因為predict的功能是同時預測多個特徵向量=fvfv12242243#取得辨識結果class_namepredictionmodelfvverbose0class_nameclass_names0prediction#顯示文字cv2.FONT_HERSHEY_SIMPLEX1(0,0,255)imgclass_name3030==50qbreakimg
0cv2.CAP_DSHOWmodelkeras_model.h5labels.txtclass_names#webcam影像貼圖迴圈while True:retimg=img1img#調整圖片大小並做正規化=fvcv2.INTER_AREAimg224224=fv-/fv1271#*特徵向量要多一個維度,因為predict的功能是同時預測多個特徵向量=fvfv12242243#取得辨識結果class_namepredictionmodelfvverbose0class_nameclass_names0prediction#顯示文字cv2.FONT_HERSHEY_SIMPLEX1(0,0,255)imgclass_name3030==50qbreakimg
1010
200
#畫矩形框(0,0,255)img0050502
img
img
img
img
img
255
#由(0,0) 到 (99,99)畫一條紅線寬度為3cv2lineimg009999255003
#顯示文字cv2.FONT_HERSHEY_SIMPLEX1(0,0,255)img00
img
50
50
img
img
50
50
=imgimg
numpy練習與webcam影像處理參考下列colab連結 https://colab.research.google.com/drive/1WUlLJkMW493Usn9bS7OgmOZt0YWVbSYq?usp=sharing
TeachableMachine示範參考下列colab連結 https://colab.research.google.com/drive/1Gb5JStf-UQxUaNd2wJPzQcefUSbZf8yY?usp=sharing
pyFaceTrace Colab 範例搭配 KNN 做性別辨識https://colab.research.google.com/drive/1VKhaYcqjcjcVpidEKFwGFZnPH90ZnjOs?usp=sharing
#Step1#下載各種照片樣本至TRAIN資料夾pyFaceTraceftftdownloadImageSamples
#Step2#開啟webcam視窗將辨識到的人臉ID標示出來PIL ImageFontpyFaceTraceft#至train資料夾載入樣本,欲辨識之目標圖片可放入 train資料夾ftloadDBFONTImageFonttruetypekaiu.ttf50index00cv2.CAP_DSHOW#webcam影像貼圖迴圈while True:retimg=img1imgrectsftdetectorimg1rectrectsfvftgetFeatureVectorimgrecttagdistftpredictFromDBfv#畫人臉矩形框(0,0,255)img0rect.left()0rect.top()50rect.right()50rect.bottom()3#顯示文字:人臉IDimgftaddText2Img_cv2imgtagFONTposition0rect.left()0MINUS1rect.top()1FONT.size==50qbreakimg
0cv2.CAP_DSHOWTrueretimg=img1img=imgcv2.COLOR_BGR2RGBimgimgFalseresultimgimgTrue=imgcv2.COLOR_RGB2BGRimgmulti_hand_landmarksresulthand_landmarksmulti_hand_landmarksresultabchand_landmarksy88取 index finger y座標imghand_landmarks==50qbreakmediapipe hands demoimg
img
img
result
img
result
hand_landmarks
result
0
img
hand_landmarks
0cv2.CAP_DSHOWTrueretimgret=img1imgimgFalseposeResultimgimgTrueposeLandmarksposeResultposeLandmarksposeLandmarksimgabcposeLandmarksy16EQ50qbreakimg
img
0
img
tkinter*mouseDowneventisClick=isClickTruemouseUpevent=isClickFalsemouseMoveeventisClickroot.geometry+{event.x_root - 20}+{event.y_root - 20}=rootTkrootwithdraw#窗體置頂rootwm_attributes-topmost1=imgPhotoImagefile欲顯示圖片之檔案路徑#圖片Label初始設定=lab1Labelrootimageimgbd0lab1placex0y0lab1bind<Button-1>mouseDownlab1bind<ButtonRelease-1>mouseUplab1bind<B1-Motion>mouseMove#文字Label初始設定=lab2Labelroottext顯示文字bd0bg#FFFAFAlab2placex100y0#root初始設定:視窗無標題欄rootoverrideredirectTrue#root初始設定:白色為透明色rootwm_attributes-transparentcolor#FFFFFFrootupdaterootgeometry200x300+100+400rootconfigurebg#FFFFFFrootwm_deiconifyrootmainloop
def thread_job10101thread1thread_jobthread1
thread2pyautogui.pressUpthread2
lock
lock
asyncioasync def task_a():end="\n"task_ai10end="\n"i await.asynciosleep1async def task_b():end="\n"task_b await.asynciosleep4end="\n"task_b結束=event_loop.asyncioget_event_loop.asyncioensure_futuretask_a()loopevent_loop.asyncioensure_futuretask_b()loopevent_loop.event_looprun_forever
檔名encodingutf-8
=fopen檔名encodingutf-8fclose
serialPort
COM3
9600
serialPort
serialPort
serialPort
serialPort
apdu
time#開啟COM埠serialPortCOM39600=i0WHILETrueGTserialPort0#讀取序列埠傳來之資料解碼後並印出=line'utf-8'serialPortend="\n"linereq:line#讀取序列埠傳來之資料包含 "req:" 時才回應serialPort#回應資料為 "res:" + str(i) + "\n" 編碼後送出serialPort'utf-8'res:{ i }\ntimesleep0.1+=i11
#開啟COM埠,遂次讀取一行由COM埠傳來的訊息每隔20serialPortCOM39600=i0WHILETrueGTserialPort0#每隔20行,顯示一次讀入之訊息以免顯示速度跟不上傳送速度=lineserialPort==%i200end=""line=iADD1i1
0.0.0.0
0.0.0.0
adhoc
#httpServer Hello World!!appFlask__name__#定義瀏覽根目錄 "/" 時觸發的事件@app.route/hellohelloWorld!!800.0.0.0
value#可先使用瀏覽器做測試 ex: http://192.168.0.1/999#上述通訊 httpReq 中之value=999end="\n"valueres:value800.0.0.0
X
Y
0
model
0
0
model
dense
model
model
x_train
y_train
80
64
loss
accuracy
model
x_test
y_test
model
model
x_test
y_train
5
x_train
y_train
x_test
L
A
A
A
A
A 00
A000
A
0
0
0
0
A
img
0
255
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
A
0
L
compressXML.compressXMLstart
#遊戲把手usb中介伺服器,先確認連線哪個COMtime#偵測Xp,Yp所在位置的像素內容XpYp1448898#開啟COM埠serialPortCOM39600#設定欲偵測的顏色D_COLOR25500=vMotorPow0=value1,1#副程式:根據http客戶端(遊戲把手)傳來的按鈕btn 及 tt馬達轉動發電大小roll決定按鍵輸出handleKeyboardbtnroll=rollroll==roll0keyDown('Down')keyUp('Down')#按鈕 PIN設定為INPUT_PULLUP 輸出 0 時代表有按下==btn0keyDown('e')keyUp('e')>roll500keyDown('3')keyUp('1')keyUp('2')keyDown('Up')>roll300keyDown('2')keyUp('3')keyUp('1')keyDown('Up')>roll200keyDown('1')keyUp('3')keyUp('2')keyDown('Up')keyUp('1')keyUp('2')keyUp('3')keyUp('Up')eend="\n"evMotorPow0#副程式:執行緒工作def thread_jobvMotorPowvalue#副程式:定義http伺服器接收訊息後如何回應TrueGTserialPort0#讀取序列埠傳來之資料解碼後並印出=value'utf-8'serialPort#回應資料為 vMotorPow轉換為字串後加上換行符號 編碼後送出serialPortserialPort'utf-8'ADD1vMotorPow1\nepass#創造並啟動執行緒,讓http伺服端平行執行thread1thread_jobthread1#偵測Xp,Yp所在的像素內容並輸出振動馬達振動的大小 vMotorPowtimeTrue#取得硬體端傳來的按鈕訊息及旋轉發電量 btn,rollbtnroll","valueend="\n"btnrollvMotorPow#另開執行緒處理鍵盤外掛thread2handleKeyboardbtnrollthread2#偵測Xp,Yp所在的像素內容並輸出振動馬達振動的大小 vMotorPow==1448Xp898YpD_COLORvMotorPow255vMotorPow0time.sleep0.01
#遊戲把手http中介伺服器,執行後可得url位置>>開ArduinoIDE設定並燒錄#偵測Xp,Yp所在位置的像素內容XpYp1448898#設定偵測目標顏色 D_COLORD_COLOR25500#副程式:根據http客戶端(遊戲把手)傳來的按鈕btn 及 tt馬達轉動發電大小roll決定按鍵輸出handleKeyboardbtnroll=rollroll==roll0keyDown('Down')keyUp('Down')#按鈕 PIN設定為INPUT_PULLUP 輸出 0 時代表有按下==btn0keyDown('e')keyUp('e')>roll500keyDown('3')keyUp('1')keyUp('2')keyDown('Up')>roll300keyDown('2')keyUp('3')keyUp('1')keyDown('Up')>roll200keyDown('1')keyUp('3')keyUp('2')keyDown('Up')keyUp('1')keyUp('2')keyUp('3')keyUp('Up')eend="\n"evMotorPow0V1,1#副程式:執行緒工作def thread_jobvMotorPowV#副程式:定義http伺服器接收訊息後如何回應valuevMotorPowV in ,valueVvalue0vMotorPow#啟動http伺服端800.0.0.0#創造並啟動執行緒,讓http伺服端平行執行thread1thread_jobthread1#偵測Xp,Yp所在的像素內容並輸出振動馬達振動的大小 vMotorPowtimeTruebtnroll','Vthread2handleKeyboardbtnrollthread2end="\n"btnrollvMotorPow==1448Xp898YpD_COLORvMotorPow255vMotorPow0time.sleep0.1eend="\n"e