訂閱
糾錯(cuò)
加入自媒體

如何實(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í)模型。

聲明: 本文由入駐維科號(hào)的作者撰寫(xiě),觀點(diǎn)僅代表作者本人,不代表OFweek立場(chǎng)。如有侵權(quán)或其他問(wèn)題,請(qǐng)聯(lián)系舉報(bào)。

發(fā)表評(píng)論

0條評(píng)論,0人參與

請(qǐng)輸入評(píng)論內(nèi)容...

請(qǐng)輸入評(píng)論/評(píng)論長(zhǎng)度6~500個(gè)字

您提交的評(píng)論過(guò)于頻繁,請(qǐng)輸入驗(yàn)證碼繼續(xù)

  • 看不清,點(diǎn)擊換一張  刷新

暫無(wú)評(píng)論

暫無(wú)評(píng)論

人工智能 獵頭職位 更多
掃碼關(guān)注公眾號(hào)
OFweek人工智能網(wǎng)
獲取更多精彩內(nèi)容
文章糾錯(cuò)
x
*文字標(biāo)題:
*糾錯(cuò)內(nèi)容:
聯(lián)系郵箱:
*驗(yàn) 證 碼:

粵公網(wǎng)安備 44030502002758號(hào)