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

請輸入評論內(nèi)容...
請輸入評論/評論長度6~500個字
最新活動更多
-
7月8日立即報名>> 【在線會議】英飛凌新一代智能照明方案賦能綠色建筑與工業(yè)互聯(lián)
-
7月22-29日立即報名>> 【線下論壇】第三屆安富利汽車生態(tài)圈峰會
-
7.30-8.1火熱報名中>> 全數(shù)會2025(第六屆)機器人及智能工廠展
-
7月31日免費預(yù)約>> OFweek 2025具身智能機器人產(chǎn)業(yè)技術(shù)創(chuàng)新應(yīng)用論壇
-
免費參會立即報名>> 7月30日- 8月1日 2025全數(shù)會工業(yè)芯片與傳感儀表展
-
即日-2025.8.1立即下載>> 《2024智能制造產(chǎn)業(yè)高端化、智能化、綠色化發(fā)展藍皮書》
推薦專題