一文带你读懂Python计算机视觉中的OpenCV手势识别方法( 二 )

识别视频输入方法
完成手部模型的获取与识别,现在我们就要将内容传入到计算机当中,使其能进行手部的识别以及手势的识别 。本处我们将使用OpenCV进行内容的输入流,开启计算机的摄像头获取内容,并使用刚刚我们写的HandTrackingModule模块作为手部的识别模块 。
Main.py
# -*- coding:utf-8 -*-"""CODE >>> SINCE IN CAIXYPROMISE.MOTTO >>> STRIVE FOR EXCELLENT.CONSTANTLY STRIVING FOR SELF-IMPROVEMENT.@ By: CaixyPromise@ Date: 2021-10-17"""import cv2from HandTrackingModule import HandDetectorclass Main:def __init__(self):self.camera = cv2.VideoCapture(0,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) # 获取你手部的方位cv2.imshow("camera", img)if cv2.getWindowProperty('camera', cv2.WND_PROP_VISIBLE) < 1:break# 通过关闭按钮退出程序cv2.waitKey(1)# if cv2.waitKey(1) & 0xFF == ord("q"):#break # 按下q退出现在,当我们运行程序后,程序会运行你的计算机默认摄像头,当你露出你的手时,会传出图像圈住你的手部,并且绘制出你的手部主要关节点 。
其中,你的手部主要关节点已经标好序号,你的手部分为了21个关节点,指尖分别为4 8 12 16 20
具体关节分为:
手势识别方法
通过前面的讲解,我们完成了手部获取与识别、识别内容的输入,那么我们现在就来开始写我们的手势识别方法 。这里,我们用到识别模块里的fingersUp()方法 。
找到我们刚刚写的Main.py文件(识别内容输入方法),当我们找到并绘制出我们的手部位置以后,此时的findPosition()方法会得到你的手部具体方位,其中lmList是关节位置方位(type:list),bbox是边框方位(type:dict),当未识别到内容时两者均为空 。所以,我们只需要写当数组中存在数据(非空),进行手指判断即可,那么我们可以写成
# -*- coding:utf-8 -*-"""CODE >>> SINCE IN CAIXYPROMISE.MOTTO >>> STRIVE FOR EXCELLENT.CONSTANTLY STRIVING FOR SELF-IMPROVEMENT.@ By: CaixyPromise@ Date: 2021-10-17"""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:x1, x2, x3, x4, x5 = self.detector.fingersUp()上面我们fingersUp()方法谈到,fingersUp()方法会传回从大拇指开始数的长度为5的数组,立起的手指标记为1,放下标记为0 。
本次我们的目的是写一个识别我们生活常见的数字手势以及一个赞扬大拇指的手势 。结合我们生活,识别你的手势可以写为
# -*- coding:utf-8 -*-"""CODE >>> SINCE IN CAIXYPROMISE.MOTTO >>> STRIVE FOR EXCELLENT.CONSTANTLY STRIVING FOR SELF-IMPROVEMENT.@ By: CaixyPromise@ Date: 2021-10-17"""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:x1, x2, x3, x4, x5 = self.detector.fingersUp()if (x2 == 1 and x3 == 1) and (x4 == 0 and x5 == 0 and x1 == 0):# TWOelif (x2 == 1 and x3 == 1 and x4 == 1) and (x1 == 0 and x5 == 0):# THREEelif (x2 == 1 and x3 == 1 and x4 == 1 and x5 == 1) and (x1 == 0):# FOURelif x1 == 1 and x2 == 1 and x3 == 1 and x4 == 1 and x5 == 1:# FIVEelif x2 == 1 and (x1 == 0, x3 == 0, x4 == 0, x5 == 0):# ONEelif x1 and (x2 == 0, x3 == 0, x4 == 0, x5 == 0):# NICE_GOOD完成基本的识别以后,我们要把内容表达出来 。这里我们结合bbox返回的手部方框方位,再使用opencv里的putText方法,实现识别结果的输出 。
# -*- coding:utf-8 -*-"""CODE >>> SINCE IN CAIXYPROMISE.MOTTO >>> STRIVE FOR EXCELLENT.CONSTANTLY STRIVING FOR SELF-IMPROVEMENT.@ By: CaixyPromise@ Date: 2021-10-17"""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:breakcv2.waitKey(1)


推荐阅读