作者阿#杰
本篇博客通过旭日x3搭载手势识别算法,实现实时检测,同时测试其运行性能。
针对旭日x3上并没有很好的python ide编译环境的问题,本篇博客通过ssh远程连接的方式,可以在不给旭日x3内存压力的同时, 提供一个更好的代码编写环境, 同时通过ssh的方式给旭日x3配置对应的环境 ,起到方便快捷的作用。
一、准备工作
首先在电脑上安装配有pycharm专业版(专业版可以使用ssh远程登陆,学生使用教育邮箱申请pycharm专业版)。
pycharm是一款用于python编程的集成开发环境(ide)。它可以帮助您编写,测试和调试代码,并具有诸如代码提示,自动完成和错误检测等功能,以帮助您更快地编写高质量的代码。
本次主要使用通过ssh远程连接旭日x3,以便于传输代码到x3派上,实现python的远程操作。
其次,本次工作用到meidapipe功能包,mediapipe 是一款由 google research 开发并开源的多媒体机器学习模型应用框架,可以直接调用其api完成目标检测、人脸检测以及关键点检测等。
meidapipe是一个针对深度学习模型的高效执行库,由pytorch设计而成,提供了高级api,用于加速预测和推理的过程。它为模型执行提供了优化和并行化的功能,并且支持多个gpu和分布式训练。
同时,meidapipe还支持模型压缩和部署,使模型在嵌入式设备和移动设备上的执行更加高效和灵活。
项目完成的效果展示:
二、部署过程
在x3派上安装pip3及导入meidapipe包:
sudo apt install python3-pippip3 install mediapipe -i https://mirrors.cloud.tencent.com/pypi/simple
确定旭日x3中摄像头端口:
首先不插usb摄像头输入
ls /dev/video*
然后插上usb摄像头再输入ls /dev/video*
多出的端口号即为摄像头端口。
创建手部类对象
初始化手部对象
hands是检测手部关键点的函数,其中有4个输入参数量可以选择
1、static_image_mode:默认为false,如果设置为false, 就是把输入看作一个视频流,在检测到手之后对手加了一个目标跟踪(目标检测+跟踪),无需调用另一次检测,直到失去对任何手的跟踪为止。
如果设置为true,则手部检测将在每个输入图像上运行(目标检测),非常适合处理一批静态的,可能不相关的图像。(如果检测的是图片就要设置成true)
2、max_num_hands:可以检测到的手的数量最大值,默认是2
3、min_detection_confidence: 手部检测的最小置信度值,大于这个数值被认为是成功的检测。默认为0.5
4、min_tracking_confidence:目标踪模型的最小置信度值,大于这个数值将被视为已成功跟踪的手部,默认为0.5,如果static_image_mode设置为true,则忽略此操作。
class handdetector: 使用mediapipe库查找手。导出地标像素格式。添加了额外的功能。 如查找方式,许多手指向上或两个手指之间的距离。而且提供找到的手的边界框信息。 def __init__(self, mode=false, maxhands=2, detectioncon=0.5, mintrackcon=0.5): :param mode: 在静态模式下,对每个图像进行检测 :param maxhands: 要检测的最大手数 :param detectioncon: 最小检测置信度 :param mintrackcon: 最小跟踪置信度 self.mode = mode self.maxhands = maxhands self.modelcomplex = false self.detectioncon = detectioncon self.mintrackcon = mintrackcon # 初始化手部识别模型 self.mphands = mp.solutions.hands self.hands = self.mphands.hands(self.mode, self.maxhands, self.modelcomplex, self.detectioncon, self.mintrackcon) self.mpdraw = mp.solutions.drawing_utils # 初始化绘图器 self.tipids = [4, 8, 12, 16, 20] # 指尖列表 self.fingers = [] self.lmlist = []
创建发现手部并在图像中绘制函数,返回绘制后的图像。
def findhands(self, img, draw=true): 从图像(brg)中找到手部。 :param img: 用于查找手的图像。 :param draw: 在图像上绘制输出的标志。 :return: 带或不带图形的图像 imgrgb = cv2.cvtcolor(img, cv2.color_bgr2rgb) # 将传入的图像由bgr模式转标准的opencv模式——rgb模式, self.results = self.hands.process(imgrgb) 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, handno=0, draw=true): 查找单手的地标并将其放入列表中像素格式。还可以返回手部周围的边界框。 :param img: 要查找的主图像 :param handno: 如果检测到多只手,则为手部id :param draw: 在图像上绘制输出的标志。(默认绘制矩形框) :return: 像素格式的手部关节位置列表;手部边界框 xlist = [] ylist = [] bbox = [] bboxinfo = [] self.lmlist = [] if self.results.multi_hand_landmarks: myhand = self.results.multi_hand_landmarks[handno] for id, lm in enumerate(myhand.landmark): h, w, c = img.shape px, py = int(lm.x * w), int(lm.y * h) xlist.append(px) ylist.append(py) self.lmlist.append([px, py]) if draw: cv2.circle(img, (px, py), 5, (255, 0, 255), cv2.filled) xmin, xmax = min(xlist), max(xlist) ymin, ymax = min(ylist), max(ylist) boxw, boxh = xmax - xmin, ymax - ymin bbox = xmin, ymin, boxw, boxh cx, cy = bbox[0] + (bbox[2] // 2), bbox[1] + (bbox[3] // 2) bboxinfo = {id: id, bbox: bbox, center: (cx, cy)} if draw: cv2.rectangle(img, (bbox[0] - 20, bbox[1] - 20), (bbox[0] + bbox[2] + 20, bbox[1] + bbox[3] + 20), (0, 255, 0), 2) return self.lmlist, bboxinfo
判断展开手指数量,同时区分左右手。
def fingersup(self): 查找列表中打开并返回的手指数。会分别考虑左手和右手 :return:竖起手指的列表 if self.results.multi_hand_landmarks: myhandtype = self.handtype() fingers = [] # thumb if myhandtype == right: if self.lmlist[self.tipids[0]][0] > self.lmlist[self.tipids[0] - 1][0]: fingers.append(1) else: fingers.append(0) else: if self.lmlist[self.tipids[0]][0] < self.lmlist[self.tipids[0] - 1][0]: fingers.append(1) else: fingers.append(0) # 4 fingers for id in range(1, 5): if self.lmlist[self.tipids[id]][1] < self.lmlist[self.tipids[id] - 2][1]: fingers.append(1) else: fingers.append(0) return fingers
判断识别到的是左手 or 右手。
def handtype(self): 检查传入的手部是左还是右 :return: right 或 left if self.results.multi_hand_landmarks: if self.lmlist[17][0] < self.lmlist[5][0]: return right else: return left
以上为手部检测类中方法的创建实现,通过调用类的方式来实现相应的功能。
创建main类,用于具体实现功能
摄像头初始化
class main: def __init__(self): self.camera = cv2.videocapture(1, cv2.cap_dshow) self.camera.set(3, 1280) self.camera.set(4, 720)
调用手部类,并将相关信息显示在实时画面中
def gesture_recognition(self): while true: self.detector = handdetector() frame, img = self.camera.read() img = self.detector.findhands(img) lmlist, bbox = self.detector.findposition(img) if lmlist: x_1, y_1 = bbox[bbox][0], bbox[bbox][1] x1, x2, x3, x4, x5 = self.detector.fingersup() if (x2 == 1 and x3 == 1) and (x4 == 0 and x5 == 0 and x1 == 0): cv2.puttext(img, 2_two, (x_1, y_1), cv2.font_hershey_plain, 3, (0, 0, 255), 3) elif (x2 == 1 and x3 == 1 and x4 == 1) and (x1 == 0 and x5 == 0): cv2.puttext(img, 3_three, (x_1, y_1), cv2.font_hershey_plain, 3, (0, 0, 255), 3) elif (x2 == 1 and x3 == 1 and x4 == 1 and x5 == 1) and (x1 == 0): cv2.puttext(img, 4_four, (x_1, y_1), cv2.font_hershey_plain, 3, (0, 0, 255), 3) elif x1 == 1 and x2 == 1 and x3 == 1 and x4 == 1 and x5 == 1: cv2.puttext(img, 5_five, (x_1, y_1), cv2.font_hershey_plain, 3, (0, 0, 255), 3) elif x2 == 1 and (x1 == 0, x3 == 0, x4 == 0, x5 == 0): cv2.puttext(img, 1_one, (x_1, y_1), cv2.font_hershey_plain, 3, (0, 0, 255), 3) elif x1 and (x2 == 0, x3 == 0, x4 == 0, x5 == 0): cv2.puttext(img, good!, (x_1, y_1), cv2.font_hershey_plain, 3, (0, 0, 255), 3) cv2.imshow(camera, img) if cv2.getwindowproperty('camera', cv2.wnd_prop_visible) self.lmlist[self.tipids[0] - 1][0]: fingers.append(1) else: fingers.append(0) else: if self.lmlist[self.tipids[0]][0] < self.lmlist[self.tipids[0] - 1][0]: fingers.append(1) else: fingers.append(0) # 4 fingers for id in range(1, 5): if self.lmlist[self.tipids[id]][1] < self.lmlist[self.tipids[id] - 2][1]: fingers.append(1) else: fingers.append(0) return fingers def handtype(self): 检查传入的手部是左还是右 :return: right 或 left if self.results.multi_hand_landmarks: if self.lmlist[17][0] < self.lmlist[5][0]: return right else: return leftclass main: def __init__(self): self.camera = cv2.videocapture(1, cv2.cap_dshow) self.camera.set(3, 1280) self.camera.set(4, 720) def gesture_recognition(self): while true: self.detector = handdetector() frame, img = self.camera.read() img = self.detector.findhands(img) lmlist, bbox = self.detector.findposition(img) if lmlist: x_1, y_1 = bbox[bbox][0], bbox[bbox][1] x1, x2, x3, x4, x5 = self.detector.fingersup() if (x2 == 1 and x3 == 1) and (x4 == 0 and x5 == 0 and x1 == 0): cv2.puttext(img, 2_two, (x_1, y_1), cv2.font_hershey_plain, 3, (0, 0, 255), 3) elif (x2 == 1 and x3 == 1 and x4 == 1) and (x1 == 0 and x5 == 0): cv2.puttext(img, 3_three, (x_1, y_1), cv2.font_hershey_plain, 3, (0, 0, 255), 3) elif (x2 == 1 and x3 == 1 and x4 == 1 and x5 == 1) and (x1 == 0): cv2.puttext(img, 4_four, (x_1, y_1), cv2.font_hershey_plain, 3, (0, 0, 255), 3) elif x1 == 1 and x2 == 1 and x3 == 1 and x4 == 1 and x5 == 1: cv2.puttext(img, 5_five, (x_1, y_1), cv2.font_hershey_plain, 3, (0, 0, 255), 3) elif x2 == 1 and (x1 == 0, x3 == 0, x4 == 0, x5 == 0): cv2.puttext(img, 1_one, (x_1, y_1), cv2.font_hershey_plain, 3, (0, 0, 255), 3) elif x1 and (x2 == 0, x3 == 0, x4 == 0, x5 == 0): cv2.puttext(img, good!, (x_1, y_1), cv2.font_hershey_plain, 3, (0, 0, 255), 3) cv2.imshow(camera, img) if cv2.getwindowproperty('camera', cv2.wnd_prop_visible) < 1: break cv2.waitkey(1) # if cv2.waitkey(1) & 0xff == ord(q): # breakif __name__ == '__main__': solution = main() solution.gesture_recognition()
晶闸管的基础知识
国家集成电路产业投资基金,助推各地集成电路产业发展
感应马达是什么
变频器制动单元电路原理及检修技术
“不将就旗舰”一加5最新消息:双面玻璃机身+骁龙835+6GB超大内存,这回可能要涨价啦
使用旭日X3派实现手势检测
各种光学仪器成像原理图解大全
详细分析万用表的四大用法和各个档位的含义
电视机偏转线圈四根怎么样接
有一些电路图是经典的,值得我们永远记住
Vitis下Zynq硬件平台的测试
RFID系统是智慧物流仓储产业的首要步伐
你用4K视频电视看里约奥运会了吗?
感恩节,用华为nova 2记录美好的一天
集成电路构成的振荡电路大全
LCD液晶屏检测仪的设计
关注了这些参数,才能将滤波器理论基础转化为硬件实践
三星与IBM共同开发移动边缘计算和端到端专用5G网络的方案
DSP与PC机及单片机通信接口的扩展设计概述
特斯拉动力电池,松下来打造