在pycharm下面安装pybluez库
本人:win11+python3.8+pybluez2
第一步,直接在pycharm终端运行
pip install pybluez
一般都会直接报错
第二步,下载安装win11的SDK文件,Windows SDK - Windows 应用开发 | Microsoft Developer
第三步,到pyblue官网,下载对应的版本
官方地址:GitHub - pybluez/pybluez: Bluetooth Python extension module
直接下载zip文件包,然后解压在自己的python解释器环境中,然后在pycharm终端打开进入到这个文件夹目录,执行python setup.py install
但是会遇到报错
error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/
这篇文章很好的解决了这个问题:Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools"的解决办法 - 知乎
然后运行
env -list
就安装成功了
除此之外,我发现我安装之后,确实有了pybluez,但是无法找到库,我又安装了pybluez2【支持python3.x版本】,才可以
首先找到你蓝牙的地址,以及你的端口号
#!/usr/bin/env python
# --*--coding=utf-8--*--
# pip install pybluez
import time
from bluetooth import *
#列表,用于存放已搜索过的蓝牙名称
alreadyFound = []
#搜索蓝牙
def findDevs():
foundDevs = discover_devices(lookup_names=True)
# 循环遍历,如果在列表中存在的就不打印
for (addr,name) in foundDevs:
if addr not in alreadyFound:
print("[*]蓝牙设备:" + str(name))
print("[+]蓝牙MAC:" + str(addr))
# 新增的设备mac地址定到列表中,用于循环搜索时过滤已打印的设备
alreadyFound.append(addr)
# 循环执行,每5秒执行一次
while True:
findDevs()
time.sleep(1)
找到你的蓝牙地址和端口号,借助sock.connect连接在一起。
import bluetooth
import time
bd_addr = "00:20:04:00:DB:02" #蓝牙的地址
port = 13
sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
sock.connect((bd_addr, port))
sock.send('1')
完整代码:
sock.send :是将信息发送到已连接的蓝牙设备上面,所以会直接将信息发送到蓝牙的串口上
# -*-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 = "你的蓝牙地址" # 蓝牙的地址
port = 13 # 端口号
sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
sock.connect((bd_addr, port))
# 发送信息到串口
def send_Serial(num):
sock.send(str(num))
sock.send('\r\n')
# 大小为21的数组初始化
for i in range(0,21):
xs.append(0)
ys.append(0)
for i in range(0,4):
ks.append(0)
# 计算两点间的距离
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
t = (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(abs (t - 0) < 0.000001):
t += 0.000001
if aveX1 != 0 and aveY1 != 0:
k1 = (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) /t
else:
k1 = 0
ks[i] = k1
i += 1
k += 4
avek = sum(ks)/4
#print(avek)
ks0 = abs(ks[0] - avek)
ks1 = abs(ks[1] - avek)
ks2 = abs(ks[2] - avek)
ks3 = abs(ks[3] - avek)
#ks0 = (ks[0] - avek)
#ks1 = (ks[1] - avek)
#ks2 = (ks[2] - avek)
#ks3 = (ks[3] - avek)
print(ks[1])
dis = point_distance(xs[4], ys[4], xs[8], ys[8])
if (ks0+ks1+ks2+ks3) < 3.5 and dis > 100 and abs(ks[1]) > 3:
print("前进")
send_Serial(1)
elif (ks0+ks1+ks2+ks3) < 3.5 and dis > 100 and -1 < ks[1] < 0:
print("左转")
send_Serial(2)
elif (ks0 + ks1 + ks2 + ks3) < 3.5 and dis > 100 and 0 < ks[1] < 1:
print("右转")
send_Serial(3)
elif dis < 100:
print("停止")
send_Serial(4)
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), 5, (0, 255, 105), cv2.FILLED)
cv2.putText(img, str(int(id)), (cx, cy), cv2.FONT_HERSHEY_PLAIN, 2,
(200, 20, 50), 3)
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:
cap.release()
cv2.destroyAllWindows()
exit()