37,744
社区成员




用pycharm写的,手势识别的手势数字通过串口发送到单片机上,这里用的虚拟串口来测试,看能不能接收到数据,之前运行成功了,结果后面又开始报错,麻烦大神帮我看看!!!
import cv2
import mediapipe as mp
import time
import serial
class handDetector():
def __init__(self, mode=False, maxHands=2, model_complexity=1, detectionCon=0.8, trackCon=0.8):
self.mode = mode
self.maxHands = maxHands
self.detectionCon = detectionCon
self.trackCon = trackCon
self.model_complexity = model_complexity
#初始化手部识别模型
self.mpHands = mp.solutions.hands
self.hands = self.mpHands.Hands(self.mode, self.maxHands, self.model_complexity, self.detectionCon, self.trackCon)
self.mpDraw = mp.solutions.drawing_utils
self.tipIds = [4, 8, 12, 16, 20]
def findHands(self, img, draw=True):
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
self.results = self.hands.process(imgRGB)
print(self.results.multi_handedness) # 获取检测结果中的左右手标签并打印
if self.results.multi_hand_landmarks:
for handLms in self.results.multi_hand_landmarks:
if draw:
self.mpDraw.draw_landmarks(img, handLms, self.mpHands.HAND_CONNECTIONS)
return img
def findPosition(self, img, draw=True):
self.lmList = []
if self.results.multi_hand_landmarks:
for handLms in self.results.multi_hand_landmarks:
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)
self.lmList.append([id, cx, cy])
if draw:
cv2.circle(img, (cx, cy), 12, (255, 0, 255), cv2.FILLED)
return self.lmList
def fingersUp(self):
fingers = []
# 大拇指
if self.lmList[self.tipIds[0]][1] > self.lmList[self.tipIds[0] - 1][1]:
fingers.append(1)
else:
fingers.append(0)
# 其余手指
for id in range(1, 5):
if self.lmList[self.tipIds[id]][2] < self.lmList[self.tipIds[id] - 2][2]:
fingers.append(1)
else:
fingers.append(0)
# totalFingers = fingers.count(1)
return fingers
wCam, hCam = 640, 480
#############################
cap = cv2.VideoCapture(0) # 若使用笔记本自带摄像头则编号为0 若使用外接摄像头 则更改为1或其他编号
cap.set(3, wCam)
cap.set(4, hCam)
pTime = 0
detector = handDetector()
#定义一个手势数字字典
values = {
0: "0",
1: "1",
2: "2",
3: "3",
4: "4",
5: "5",
}
#串口通信
#串口通信
ser = serial.Serial()
ser.baudrate = 115200
ser.port = "COM2"
ser.open() #打开串口
#手势识别
while True:
success, img = cap.read()
img = detector.findHands(img)
lmList = detector.findPosition(img, draw=False)
pointList = [4, 8, 12, 16, 20]
if len(lmList) != 0:
countList = []
# 大拇指
if lmList[4][1] > lmList[3][1]:
countList.append(1)
else:
countList.append(0)
# 余下四个手指
for i in range(1, 5):
if lmList[pointList[i]][2] < lmList[pointList[i] - 2][2]:
countList.append(1)
else:
countList.append(0)
print(countList)
count = countList.count(1) # 对列表中含有的1计数(计数值为手势数字)
print(count)
HandImage = cv2.imread(f'FingerImg/{count}.jpg')
HandImage = cv2.resize(HandImage, (150, 200))
h, w, c = HandImage.shape
img[0:h, 0:w] = HandImage
cv2.putText(img, str(count), (15, 400), cv2.FONT_HERSHEY_PLAIN, 15, (255, 0, 255), 10)
data = values.get(count)
print(data)
ser.write(str(data).encode("gbk"))
print('python send:\\n', data)
cTime = time.time()
fps = 1 / (cTime - pTime)
pTime = cTime
cv2.putText(img, f'fps: {int(fps)}', (600, 40), cv2.FONT_HERSHEY_PLAIN, 2, (255, 0, 0), 2)
cv2.imshow("Image", img)
cv2.waitKey(1)
ser.close()
这里是错误
Traceback (most recent call last):
File "D:/基于机器视觉的手势控制项目开发/基于机器视觉的手势控制项目开发/xiangmu/HandTrackingModule.py", line 119, in <module>
ser.write(str(data).encode("gbk"))
File "F:\anaconda\lib\site-packages\serial\serialwin32.py", line 306, in write
raise PortNotOpenError()
serial.serialutil.PortNotOpenError: Attempting to use a port that is not open
5
你好,我也是,请问你解决了吗
你好!我也是遇到这个问题Attempting to use a port that is not open 请问你解决了嘛