如何實現(xiàn)以頭部姿勢移動鼠標(biāo)?
介紹在本文中,將會詳細(xì)介紹如何使用卷積神經(jīng)網(wǎng)絡(luò)(深度學(xué)習(xí))和名為pyautogui的鼠標(biāo)/鍵盤自動化庫來創(chuàng)建Python程序,實現(xiàn)以頭部姿勢移動鼠標(biāo)。我們之所以使用pyautogui,是因為它是簡單易用的Python庫,以編程方式控制計算機的各種組件。下面將介紹有關(guān)如何使用此庫的更多詳細(xì)信息。首先,我們必須創(chuàng)建一個深度學(xué)習(xí)模型,該模型可以將你當(dāng)前的頭部姿勢分為5種不同的類別,即中(未指示方向),左,右,上和下。Google Teachable MachineGoogle Teachable Machine是Google免費的無代碼深度學(xué)習(xí)模型創(chuàng)建網(wǎng)絡(luò)平臺。使用該平臺你可以構(gòu)建模型來對圖像,音頻甚至姿勢進行分類。完成上述操作后,你可以下載經(jīng)過訓(xùn)練的模型并將其用于你的應(yīng)用程序。你可以使用Tensorflow或PyTorch之類的框架來構(gòu)建自定義的卷積神經(jīng)網(wǎng)絡(luò),或者如果你想要一種簡單的無代碼方式進行此操作,則可以使用 Google Teachable Machine(https://teachablemachine.withgoogle.com/)平臺為你完成相同的操作。
選擇“Image Project”并命名示例,然后記錄你的照片與相應(yīng)的頭部姿勢。你可以設(shè)置默認(rèn)的超參數(shù),然后進行模型訓(xùn)練。
提示:嘗試記錄相對于攝像機的不同深度和位置的頭部姿勢照片,以免造成數(shù)據(jù)過擬合,導(dǎo)致預(yù)測不佳。接下來,我們可以使用同一頁面的“Preview”部分來查看已訓(xùn)練模型的性能,并決定是否將其用于程序或使用樣本訓(xùn)練更健壯的模型。
完成上述步驟后,下載模型權(quán)重。權(quán)重將以“ keras_model.h5”的格式下載。程序現(xiàn)在,讓我們將其與能夠移動鼠標(biāo)的程序結(jié)合起來。讓我們看一下代碼:# 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ù)的說明:pyautogui.moveTo(current_x-80, currently, duration=1)
上面的代碼使鼠標(biāo)從當(dāng)前位置向左移動80個像素,并花費1秒鐘的時間進行相同操作。如果未設(shè)置duration 參數(shù),則鼠標(biāo)指針將立即移動到新點,從而消除了移動鼠標(biāo)的影響。current_pos = pyautogui.position()
current_x = current_pos.x
current_y = current_pos.y
第一行獲取鼠標(biāo)的x和y坐標(biāo)值。最后,釋放打開的連接。本文我們實現(xiàn)了一個端到端的深度學(xué)習(xí)模型,該模型可以從用戶那里獲取輸入視頻,程序在讀取視頻時,會根據(jù)你的頭部姿勢對每個圖像進行分類,并返回相應(yīng)的預(yù)測,使用此預(yù)測,我們可以采取適當(dāng)?shù)牟僮,將鼠?biāo)移至頭部指向的方向。你可以通過添加自定義功能來單擊鼠標(biāo),而無需實際觸摸計算機的鼠標(biāo),從而進一步改進我們剛剛構(gòu)建的項目,同時你也可以在Google Teachable Machine上為此訓(xùn)練另一種深度學(xué)習(xí)模型。

請輸入評論內(nèi)容...
請輸入評論/評論長度6~500個字
最新活動更多
-
8月5日立即報名>> 【在線會議】CAE優(yōu)化設(shè)計:醫(yī)療器械設(shè)計的應(yīng)用案例與方案解析
-
8月14日立即報名>> 【在線研討會】解析安森美(onsemi)高精度與超低功耗CGM系統(tǒng)解決方案
-
精彩回顧立即查看>> 《2024智能制造產(chǎn)業(yè)高端化、智能化、綠色化發(fā)展藍(lán)皮書》
-
精彩回顧立即查看>> 7月30日- 8月1日 2025全數(shù)會工業(yè)芯片與傳感儀表展
-
精彩回顧立即查看>> 全數(shù)會2025(第六屆)機器人及智能工廠展
-
精彩回顧立即查看>> OFweek 2025 具身機器人動力電池技術(shù)應(yīng)用大會
推薦專題
- 1 AI產(chǎn)業(yè)的新高度!英偉達成為全球首家市值破4萬億美元的公司
- 2 傳魏建軍與賈躍亭合作,長城汽車出海美國
- 3 一文讀懂:到底什么是 “具身智能” ?
- 4 黃仁勛:與雷軍長期合作,共探AI智駕
- 5 具身智能泡沫爭議下,華映資本尋找「穿越周期者」
- 6 中國平安們欲靠AI守“陣地”
- 7 官宣:智元機器人借殼上市,A股人形機器人第一股!
- 8 華為讓渡“三界”銷售主導(dǎo)權(quán),智界高管:終于能全力奔跑了
- 9 借仿生手實現(xiàn)突圍,國產(chǎn)靈巧手破局“不可能三角”
- 10 DeepSeek R2加持,中國AI與芯片產(chǎn)業(yè)迎來新一輪協(xié)同進化