如何實(shí)現(xiàn)以頭部姿勢(shì)移動(dòng)鼠標(biāo)?
介紹在本文中,將會(huì)詳細(xì)介紹如何使用卷積神經(jīng)網(wǎng)絡(luò)(深度學(xué)習(xí))和名為pyautogui的鼠標(biāo)/鍵盤(pán)自動(dòng)化庫(kù)來(lái)創(chuàng)建Python程序,實(shí)現(xiàn)以頭部姿勢(shì)移動(dòng)鼠標(biāo)。我們之所以使用pyautogui,是因?yàn)樗呛?jiǎn)單易用的Python庫(kù),以編程方式控制計(jì)算機(jī)的各種組件。下面將介紹有關(guān)如何使用此庫(kù)的更多詳細(xì)信息。首先,我們必須創(chuàng)建一個(gè)深度學(xué)習(xí)模型,該模型可以將你當(dāng)前的頭部姿勢(shì)分為5種不同的類(lèi)別,即中(未指示方向),左,右,上和下。Google Teachable MachineGoogle Teachable Machine是Google免費(fèi)的無(wú)代碼深度學(xué)習(xí)模型創(chuàng)建網(wǎng)絡(luò)平臺(tái)。使用該平臺(tái)你可以構(gòu)建模型來(lái)對(duì)圖像,音頻甚至姿勢(shì)進(jìn)行分類(lèi)。完成上述操作后,你可以下載經(jīng)過(guò)訓(xùn)練的模型并將其用于你的應(yīng)用程序。你可以使用Tensorflow或PyTorch之類(lèi)的框架來(lái)構(gòu)建自定義的卷積神經(jīng)網(wǎng)絡(luò),或者如果你想要一種簡(jiǎn)單的無(wú)代碼方式進(jìn)行此操作,則可以使用 Google Teachable Machine(https://teachablemachine.withgoogle.com/)平臺(tái)為你完成相同的操作。
選擇“Image Project”并命名示例,然后記錄你的照片與相應(yīng)的頭部姿勢(shì)。你可以設(shè)置默認(rèn)的超參數(shù),然后進(jìn)行模型訓(xùn)練。
提示:嘗試記錄相對(duì)于攝像機(jī)的不同深度和位置的頭部姿勢(shì)照片,以免造成數(shù)據(jù)過(guò)擬合,導(dǎo)致預(yù)測(cè)不佳。接下來(lái),我們可以使用同一頁(yè)面的“Preview”部分來(lái)查看已訓(xùn)練模型的性能,并決定是否將其用于程序或使用樣本訓(xùn)練更健壯的模型。
完成上述步驟后,下載模型權(quán)重。權(quán)重將以“ keras_model.h5”的格式下載。程序現(xiàn)在,讓我們將其與能夠移動(dòng)鼠標(biāo)的程序結(jié)合起來(lái)。讓我們看一下代碼:# Python program to control mouse based on head position # Import necessary modules
import numpy as np
import cv2
from time import sleep
import tensorflow.keras
from keras.preprocessing import image
import tensorflow as tf
import pyautogui
# Using laptop’s webcam as the source of video
cap = cv2.VideoCapture(0)
# Labels — The various outcome possibilities
labels = [‘Left’,’Right’,’Up’,’Down’,’Neutral’]
# Loading the model weigths we just downloaded
model = tensorflow.keras.models.load_model(‘keras_model.h5’)
while True:
success, image = cap.read()
if success == False:
break
# Necessary to avoid conflict between left and right
image = cv2.flip(image,1)
cv2.imshow(“Frame”,image)
# The model takes an image of dimensions (224,224) as input so let’s
# reshape our image to the same.
img = cv2.resize(image,(224,224))
# Convert the image to a numpy array
img = np.a(chǎn)rray(img,dtype=np.float32)
img = np.expand_dims(img,axis=0)
# Normalizing input image
img = img/255
# Predict the class
prediction = model.predict(img)
# Necessary to avoid conflict between left and right
image = cv2.flip(image,1)
cv2.imshow(“Frame”,image)
# The model takes an image of dimensions (224,224) as input so let’s
# reshape our image to the same.
img = cv2.resize(image,(224,224))
# Convert the image to a numpy array
img = np.a(chǎn)rray(img,dtype=np.float32)
img = np.expand_dims(img,axis=0)
# Normalizing input image
img = img/255
# Predict the class
prediction = model.predict(img)
# Close all windows if one second has passed and ‘q’ is pressed
if cv2.waitKey(1) & 0xFF == ord(‘q’):
break
# Release open connections
cap.
# Close all windows if one second has passed and ‘q’ is pressed
if cv2.waitKey(1) & 0xFF == ord(‘q’):
break
# Release open connections
cap.release()
cv2.destroyAllWindo
release()
cv2.destroyAllWindows()
你還可以在此處找到代碼和權(quán)重。https://github.com/Sharan-Babu/Control-Mouse-with-Head-Pose-pyautogui函數(shù)的說(shuō)明:pyautogui.moveTo(current_x-80, currently, duration=1)
上面的代碼使鼠標(biāo)從當(dāng)前位置向左移動(dòng)80個(gè)像素,并花費(fèi)1秒鐘的時(shí)間進(jìn)行相同操作。如果未設(shè)置duration 參數(shù),則鼠標(biāo)指針將立即移動(dòng)到新點(diǎn),從而消除了移動(dòng)鼠標(biāo)的影響。current_pos = pyautogui.position()
current_x = current_pos.x
current_y = current_pos.y
第一行獲取鼠標(biāo)的x和y坐標(biāo)值。最后,釋放打開(kāi)的連接。本文我們實(shí)現(xiàn)了一個(gè)端到端的深度學(xué)習(xí)模型,該模型可以從用戶那里獲取輸入視頻,程序在讀取視頻時(shí),會(huì)根據(jù)你的頭部姿勢(shì)對(duì)每個(gè)圖像進(jìn)行分類(lèi),并返回相應(yīng)的預(yù)測(cè),使用此預(yù)測(cè),我們可以采取適當(dāng)?shù)牟僮,將鼠?biāo)移至頭部指向的方向。你可以通過(guò)添加自定義功能來(lái)單擊鼠標(biāo),而無(wú)需實(shí)際觸摸計(jì)算機(jī)的鼠標(biāo),從而進(jìn)一步改進(jìn)我們剛剛構(gòu)建的項(xiàng)目,同時(shí)你也可以在Google Teachable Machine上為此訓(xùn)練另一種深度學(xué)習(xí)模型。
發(fā)表評(píng)論
請(qǐng)輸入評(píng)論內(nèi)容...
請(qǐng)輸入評(píng)論/評(píng)論長(zhǎng)度6~500個(gè)字
最新活動(dòng)更多
-
即日-10.29立即報(bào)名>> 2024德州儀器嵌入式技術(shù)創(chuàng)新發(fā)展研討會(huì)
-
10月31日立即下載>> 【限時(shí)免費(fèi)下載】TE暖通空調(diào)系統(tǒng)高效可靠的組件解決方案
-
即日-11.13立即報(bào)名>>> 【在線會(huì)議】多物理場(chǎng)仿真助跑新能源汽車(chē)
-
11月14日立即報(bào)名>> 2024工程師系列—工業(yè)電子技術(shù)在線會(huì)議
-
12月19日立即報(bào)名>> 【線下會(huì)議】OFweek 2024(第九屆)物聯(lián)網(wǎng)產(chǎn)業(yè)大會(huì)
-
即日-12.26火熱報(bào)名中>> OFweek2024中國(guó)智造CIO在線峰會(huì)
推薦專題
- 1 Intel宣布40年來(lái)最重大轉(zhuǎn)型:年底前裁員15000人、拋掉2/3房產(chǎn)
- 2 因美封殺TikTok,字節(jié)股價(jià)骨折!估值僅Meta1/5
- 3 宏山激光重磅發(fā)布行業(yè)解決方案,助力智能制造產(chǎn)業(yè)新飛躍
- 4 國(guó)產(chǎn)AI芯片公司破產(chǎn)!白菜價(jià)拍賣(mài)
- 5 具身智能火了,但規(guī)模落地還需時(shí)間
- 6 三次錯(cuò)失風(fēng)口!OpenAI前員工殺回AI編程賽道,老東家捧金相助
- 7 國(guó)產(chǎn)英偉達(dá)們,抓緊沖刺A股
- 8 英特爾賦能智慧醫(yī)療,共創(chuàng)數(shù)字化未來(lái)
- 9 英偉達(dá)的麻煩在后頭?
- 10 將“網(wǎng)紅”變成“商品”,AI“爆改”實(shí)力拉滿
- 高級(jí)軟件工程師 廣東省/深圳市
- 自動(dòng)化高級(jí)工程師 廣東省/深圳市
- 光器件研發(fā)工程師 福建省/福州市
- 銷(xiāo)售總監(jiān)(光器件) 北京市/海淀區(qū)
- 激光器高級(jí)銷(xiāo)售經(jīng)理 上海市/虹口區(qū)
- 光器件物理工程師 北京市/海淀區(qū)
- 激光研發(fā)工程師 北京市/昌平區(qū)
- 技術(shù)專家 廣東省/江門(mén)市
- 封裝工程師 北京市/海淀區(qū)
- 結(jié)構(gòu)工程師 廣東省/深圳市