当手势识别和蓝牙小车制作完毕后,接下来要做的就是上位机和蓝牙小车的通信,所以尝试用Python发送指令控制小车
博主在安装pybluez库的时候一直报错
过程
sudo pip install pybluez
解决办法是运行以下命令安装依赖库
sudo apt-get install libboost-python-dev libboost-thread-dev libbluetooth-dev libglib2.0-dev
问题解决后,运行又一次报错
AttributeError: attribute '__doc__' of 'type' objects is not writable
解决方法是指定版本安装pybluez
pip install pybluez==0.22
附上代码
# -*-coding:utf-8-*-
import bluetooth
import time
bd_addr = "00:13:EF:00:B0:02" #蓝牙的地址
port = 1
sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
sock.connect((bd_addr, port))
sock.send('1')
# 1前进,2后退,3左转,4右转
# -*-coding:utf-8-*-
import cv2
import mediapipe as mp
import time
import math
import bluetooth
cap = cv2.VideoCapture(0)
#该函数的参数
#static_image_mode,max_num_hands,min_detection_confidence,min_tracking_confidence
mpHands = mp.solutions.hands
hands = mpHands.Hands()
mpDraw = mp.solutions.drawing_utils
pTime = 0#开始时间初始化
cTime = 0#目前时间初始化
xs = []
ys = []
ks = []
bd_addr = "00:21:07:00:06:5D" # 蓝牙的地址
port = 1
sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
sock.connect((bd_addr, port))
#大小为21的数组初始化
for i in range(0, 21):
xs.append(0)
ys.append(0)
for i in range(0, 4):
ks.append(0)
def blue(num):
sock.send(str(num))
sock.send('\r\n')
#计算两点间的距离
def point_distance(x1, y1, x2, y2 ):
dis = abs(math.sqrt((x2 - x1)*(x2 - x1)+(y2 - y1)*(y2 - y1)))
return dis
def K_count():
k=5
i=0
while(k<20):
aveX1 = (xs[k] + xs[k+1] + xs[k+2] + xs[k+3]) / 4
aveY1 = (ys[k] + ys[k+1] + ys[k+2] + ys[k+3]) / 4
a=xs[k] * ys[k] + xs[k+1] * ys[k+1] + xs[k+2] * ys[k+2] + xs[k+3] * ys[k+3] - 4 * aveX1 * aveY1
b=xs[k] * xs[k] + xs[k+1] * xs[k+1] + xs[k+2] * xs[k+2] + xs[k+3] * xs[k+3] - 4 * aveX1 * aveX1
if(b == 0):#防止分母为0
b=1
k1 = a/b
ks[i] = k1
i += 1
k += 4
avek = sum(ks)/4
ks0 = abs(ks[0] - avek)
ks1 = abs(ks[1] - avek)
ks2 = abs(ks[2] - avek)
ks3 = abs(ks[3] - avek)
dis = point_distance(xs[12], ys[12], xs[0], ys[0])
if (ks0+ks1+ks2+ks3) < 3.5 and dis > 100 and abs(ks[1]) > 3:
print("前进")
blue(2)
elif (ks0+ks1+ks2+ks3) < 3.5 and dis > 100 and -1 < ks[1] < 0:
print("左转")
blue(3)
elif (ks0 + ks1 + ks2 + ks3) < 3.5 and dis > 100 and 0 < ks[1] < 1:
print("右转")
blue(4)
elif dis < 100:
print("停止")
blue(5)
elif(ys[0]<ys[12]):
print("后退")
blue(1)
while True:
count = 0
success, img = cap.read()#BGR存储格式
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)#转为RGB存储
#处理返回的手的标志点以及处理
results = hands.process(imgRGB)
if results.multi_hand_landmarks:#返回none或手的标志点坐标
for handLms in results.multi_hand_landmarks:
#id是索引,lm是x,y坐标
for id, lm in enumerate(handLms.landmark):
h, w, c = img.shape
cx, cy = int(lm.x*w), int(lm.y*h)
# print(id, cx, cy)
xs[id] = cx
ys[id] = cy
cv2.circle(img, (cx, cy), 14, (205, 100, 255), cv2.FILLED)
cv2.putText(img, str(int(id)), (cx, cy), cv2.FONT_HERSHEY_PLAIN, 2,
(200, 20, 50), 5)
K_count()
mpDraw.draw_landmarks(img, handLms, mpHands.HAND_CONNECTIONS)
cTime = time.time()
fps = 1 / (cTime - pTime)
pTime = cTime
cv2.putText(img, str(int(fps)), (10, 70), cv2.FONT_HERSHEY_PLAIN, 3,
(255, 0, 255), 1)
cv2.imshow("Image", img)
k = cv2.waitKey(1)
if k == 27:
blue(5)
cap.release()
cv2.destroyAllWindows()
exit()
到目前为止手势小车的全部分块功能都已实现,博主电赛回来后会继续更新接下来用ROS串连各部分功能