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

請輸入評論內容...
請輸入評論/評論長度6~500個字
最新活動更多
推薦專題
-
10 月之暗面,絕地反擊
- 1 UALink規(guī)范發(fā)布:挑戰(zhàn)英偉達AI統(tǒng)治的開始
- 2 北電數智主辦酒仙橋論壇,探索AI產業(yè)發(fā)展新路徑
- 3 降薪、加班、裁員三重暴擊,“AI四小龍”已折戟兩家
- 4 “AI寒武紀”爆發(fā)至今,五類新物種登上歷史舞臺
- 5 國產智駕迎戰(zhàn)特斯拉FSD,AI含量差幾何?
- 6 光計算迎來商業(yè)化突破,但落地仍需時間
- 7 東陽光:2024年扭虧、一季度凈利大增,液冷疊加具身智能打開成長空間
- 8 地平線自動駕駛方案解讀
- 9 封殺AI“照騙”,“淘寶們”終于不忍了?
- 10 優(yōu)必選:營收大增主靠小件,虧損繼續(xù)又逢關稅,能否乘機器人東風翻身?