增加:MP版本兼容
(被动包含调整复杂系数为1)
This commit is contained in:
parent
8feabea649
commit
4a85384e6e
351
datatest.py
351
datatest.py
@ -1,174 +1,177 @@
|
|||||||
import cv2
|
import cv2
|
||||||
import mediapipe as mp
|
import mediapipe as mp
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
|
|
||||||
class HandDetector:
|
class HandDetector:
|
||||||
"""
|
"""
|
||||||
使用mediapipe库查找手。导出地标像素格式。添加了额外的功能。
|
使用mediapipe库查找手。导出地标像素格式。添加了额外的功能。
|
||||||
如查找方式,许多手指向上或两个手指之间的距离。而且提供找到的手的边界框信息。
|
如查找方式,许多手指向上或两个手指之间的距离。而且提供找到的手的边界框信息。
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, mode=False, max_hands=2, detection_con=0.5, min_track_con=0.5):
|
def __init__(self, mode=False, max_hands=2, detection_con=0.5, min_track_con=0.5):
|
||||||
"""
|
"""
|
||||||
:param mode: 在静态模式下,对每个图像进行检测
|
:param mode: 在静态模式下,对每个图像进行检测
|
||||||
:param max_hands: 要检测的最大手数
|
:param max_hands: 要检测的最大手数
|
||||||
:param detection_con: 最小检测置信度
|
:param detection_con: 最小检测置信度
|
||||||
:param min_track_con: 最小跟踪置信度
|
:param min_track_con: 最小跟踪置信度
|
||||||
"""
|
"""
|
||||||
self.results = None
|
self.results = None
|
||||||
self.mode = mode
|
self.mode = mode
|
||||||
self.max_hands = max_hands
|
self.max_hands = max_hands
|
||||||
self.modelComplex = False
|
self.modelComplex = 1
|
||||||
self.detection_con = detection_con
|
self.detection_con = detection_con
|
||||||
self.min_track_con = min_track_con
|
self.min_track_con = min_track_con
|
||||||
|
|
||||||
# 初始化手部的识别模型
|
# 初始化手部的识别模型
|
||||||
self.mpHands = mp.solutions.hands
|
self.mpHands = mp.solutions.hands
|
||||||
self.hands = self.mpHands.Hands(self.mode, self.max_hands, self.detection_con, self.min_track_con)
|
self.hands = self.mpHands.Hands(static_image_mode=self.mode,
|
||||||
self.mpDraw = mp.solutions.drawing_utils # 初始化绘图器
|
max_num_hands=self.max_hands,
|
||||||
self.tipIds = [4, 8, 12, 16, 20] # 指尖列表
|
min_detection_confidence=self.detection_con,
|
||||||
self.fingers = []
|
min_tracking_confidence=self.min_track_con)
|
||||||
self.lmList = []
|
self.mpDraw = mp.solutions.drawing_utils # 初始化绘图器
|
||||||
|
self.tipIds = [4, 8, 12, 16, 20] # 指尖列表
|
||||||
def find_hands(self, img, draw=True):
|
self.fingers = []
|
||||||
"""
|
self.lmList = []
|
||||||
从图像(BRG)中找到手部。
|
|
||||||
:param img: 用于查找手的图像。
|
def find_hands(self, img, draw=True):
|
||||||
:param draw: 在图像上绘制输出的标志。
|
"""
|
||||||
:return: 带或不带图形的图像
|
从图像(BRG)中找到手部。
|
||||||
"""
|
:param img: 用于查找手的图像。
|
||||||
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # 将传入的图像由BGR模式转标准的Opencv模式——RGB模式,
|
:param draw: 在图像上绘制输出的标志。
|
||||||
self.results = self.hands.process(img_rgb)
|
:return: 带或不带图形的图像
|
||||||
|
"""
|
||||||
if self.results.multi_hand_landmarks:
|
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # 将传入的图像由BGR模式转标准的Opencv模式——RGB模式,
|
||||||
for handLms in self.results.multi_hand_landmarks:
|
self.results = self.hands.process(img_rgb)
|
||||||
if draw:
|
|
||||||
self.mpDraw.draw_landmarks(img, handLms,
|
if self.results.multi_hand_landmarks:
|
||||||
self.mpHands.HAND_CONNECTIONS)
|
for handLms in self.results.multi_hand_landmarks:
|
||||||
return img
|
if draw:
|
||||||
|
self.mpDraw.draw_landmarks(img, handLms,
|
||||||
def find_position(self, img, hand_no=0, draw=True):
|
self.mpHands.HAND_CONNECTIONS)
|
||||||
"""
|
return img
|
||||||
查找单手的地标并将其放入列表中像素格式。还可以返回手部的周围的边界框。
|
|
||||||
:param img: 要查找的主图像
|
def find_position(self, img, hand_no=0, draw=True):
|
||||||
:param hand_no: 如果检测到多只手,则为手部id
|
"""
|
||||||
:param draw: 在图像上绘制输出的标志。(默认绘制矩形框)
|
查找单手的地标并将其放入列表中像素格式。还可以返回手部的周围的边界框。
|
||||||
:return: 像素格式的手部关节位置列表;手部边界框
|
:param img: 要查找的主图像
|
||||||
"""
|
:param hand_no: 如果检测到多只手,则为手部id
|
||||||
|
:param draw: 在图像上绘制输出的标志。(默认绘制矩形框)
|
||||||
x_list = []
|
:return: 像素格式的手部关节位置列表;手部边界框
|
||||||
y_list = []
|
"""
|
||||||
onedata = np.zeros([21,3])
|
|
||||||
zerodata = np.zeros([21,3])
|
x_list = []
|
||||||
h, w, c = img.shape
|
y_list = []
|
||||||
self.lmList = []
|
onedata = np.zeros([21,3])
|
||||||
|
zerodata = np.zeros([21,3])
|
||||||
if self.results.multi_hand_landmarks:
|
h, w, c = img.shape
|
||||||
my_hand = self.results.multi_hand_landmarks[hand_no]
|
self.lmList = []
|
||||||
for i, lm in enumerate(my_hand.landmark):
|
|
||||||
onedata[i] = np.array([lm.x,lm.y,lm.z]) #将三维坐标添加到单次截屏的数据中
|
if self.results.multi_hand_landmarks:
|
||||||
|
my_hand = self.results.multi_hand_landmarks[hand_no]
|
||||||
px, py= int(lm.x * w), int(lm.y * h)
|
for i, lm in enumerate(my_hand.landmark):
|
||||||
x_list.append(px)
|
onedata[i] = np.array([lm.x,lm.y,lm.z]) #将三维坐标添加到单次截屏的数据中
|
||||||
y_list.append(py)
|
|
||||||
self.lmList.append([px, py])
|
px, py= int(lm.x * w), int(lm.y * h)
|
||||||
if draw:
|
x_list.append(px)
|
||||||
cv2.circle(img, (px, py), 5, (255, 0, 255), cv2.FILLED)
|
y_list.append(py)
|
||||||
|
self.lmList.append([px, py])
|
||||||
return onedata, (h, w)
|
if draw:
|
||||||
|
cv2.circle(img, (px, py), 5, (255, 0, 255), cv2.FILLED)
|
||||||
def fingers_up(self):
|
|
||||||
"""
|
return onedata, (h, w)
|
||||||
查找列表中打开并返回的手指数。会分别考虑左手和右手
|
|
||||||
:return: 竖起手指的列表
|
def fingers_up(self):
|
||||||
"""
|
"""
|
||||||
fingers = []
|
查找列表中打开并返回的手指数。会分别考虑左手和右手
|
||||||
if self.results.multi_hand_landmarks:
|
:return: 竖起手指的列表
|
||||||
my_hand_type = self.hand_type()
|
"""
|
||||||
# Thumb
|
fingers = []
|
||||||
if my_hand_type == "Right":
|
if self.results.multi_hand_landmarks:
|
||||||
if self.lmList[self.tipIds[0]][0] > self.lmList[self.tipIds[0] - 1][0]:
|
my_hand_type = self.hand_type()
|
||||||
fingers.append(1)
|
# Thumb
|
||||||
else:
|
if my_hand_type == "Right":
|
||||||
fingers.append(0)
|
if self.lmList[self.tipIds[0]][0] > self.lmList[self.tipIds[0] - 1][0]:
|
||||||
else:
|
fingers.append(1)
|
||||||
if self.lmList[self.tipIds[0]][0] < self.lmList[self.tipIds[0] - 1][0]:
|
else:
|
||||||
fingers.append(1)
|
fingers.append(0)
|
||||||
else:
|
else:
|
||||||
fingers.append(0)
|
if self.lmList[self.tipIds[0]][0] < self.lmList[self.tipIds[0] - 1][0]:
|
||||||
# 4 Fingers
|
fingers.append(1)
|
||||||
for i in range(1, 5):
|
else:
|
||||||
if self.lmList[self.tipIds[i]][1] < self.lmList[self.tipIds[i] - 2][1]:
|
fingers.append(0)
|
||||||
fingers.append(1)
|
# 4 Fingers
|
||||||
else:
|
for i in range(1, 5):
|
||||||
fingers.append(0)
|
if self.lmList[self.tipIds[i]][1] < self.lmList[self.tipIds[i] - 2][1]:
|
||||||
return fingers
|
fingers.append(1)
|
||||||
|
else:
|
||||||
def hand_type(self):
|
fingers.append(0)
|
||||||
"""
|
return fingers
|
||||||
检查传入的手部是左还是右
|
|
||||||
:return: "Right" 或 "Left"
|
def hand_type(self):
|
||||||
"""
|
"""
|
||||||
if self.results.multi_hand_landmarks:
|
检查传入的手部是左还是右
|
||||||
if self.lmList[17][0] < self.lmList[5][0]:
|
:return: "Right" 或 "Left"
|
||||||
return 1
|
"""
|
||||||
else:
|
if self.results.multi_hand_landmarks:
|
||||||
return 0
|
if self.lmList[17][0] < self.lmList[5][0]:
|
||||||
|
return 1
|
||||||
|
else:
|
||||||
class Main:
|
return 0
|
||||||
def __init__(self, label, N = 100):
|
|
||||||
self.detector = None
|
|
||||||
self.camera = cv2.VideoCapture(0, cv2.CAP_DSHOW)
|
class Main:
|
||||||
self.camera.set(3, 1280)
|
def __init__(self, label, N = 100):
|
||||||
self.camera.set(4, 720)
|
self.detector = None
|
||||||
self.N = N
|
self.camera = cv2.VideoCapture(0, cv2.CAP_DSHOW)
|
||||||
#初始化数据包
|
self.camera.set(3, 1280)
|
||||||
self.label = label
|
self.camera.set(4, 720)
|
||||||
self.data = np.zeros([N,21,3])
|
self.N = N
|
||||||
self.shape = np.zeros([N,2], dtype = np.int16)
|
#初始化数据包
|
||||||
self.handtype = np.zeros(N, dtype = np.int8)
|
self.label = label
|
||||||
|
self.data = np.zeros([N,21,3])
|
||||||
def gesture_recognition(self):
|
self.shape = np.zeros([N,2], dtype = np.int16)
|
||||||
self.detector = HandDetector()
|
self.handtype = np.zeros(N, dtype = np.int8)
|
||||||
#初始化数据
|
|
||||||
|
def gesture_recognition(self):
|
||||||
zerodata = np.zeros([21,3])
|
self.detector = HandDetector()
|
||||||
rezult = np.zeros([21,3])
|
#初始化数据
|
||||||
count = 0
|
|
||||||
|
zerodata = np.zeros([21,3])
|
||||||
while True:
|
rezult = np.zeros([21,3])
|
||||||
frame, img = self.camera.read()
|
count = 0
|
||||||
img = self.detector.find_hands(img)
|
|
||||||
|
while True:
|
||||||
rezult,shape = self.detector.find_position(img)
|
frame, img = self.camera.read()
|
||||||
if rezult.all() != zerodata.all(): #假设矩阵不为0,即捕捉到手部时
|
img = self.detector.find_hands(img)
|
||||||
self.data[count] = rezult
|
|
||||||
self.handtype[count] = self.detector.hand_type()
|
rezult,shape = self.detector.find_position(img)
|
||||||
self.shape[count] = np.array(shape)
|
if rezult.all() != zerodata.all(): #假设矩阵不为0,即捕捉到手部时
|
||||||
count += 1
|
self.data[count] = rezult
|
||||||
|
self.handtype[count] = self.detector.hand_type()
|
||||||
cv2.imshow("camera", img)
|
self.shape[count] = np.array(shape)
|
||||||
key = cv2.waitKey(1)
|
count += 1
|
||||||
if cv2.getWindowProperty('camera', cv2.WND_PROP_VISIBLE) < 1:
|
|
||||||
break
|
cv2.imshow("camera", img)
|
||||||
elif key == 27:
|
key = cv2.waitKey(1)
|
||||||
break
|
if cv2.getWindowProperty('camera', cv2.WND_PROP_VISIBLE) < 1:
|
||||||
elif count == self.N - 1:
|
break
|
||||||
break
|
elif key == 27:
|
||||||
|
break
|
||||||
np.savez('firstdata', label = self.label, data = self.data,
|
elif count == self.N - 1:
|
||||||
handtype = self.handtype, shape = self.shape)
|
break
|
||||||
|
|
||||||
|
np.savez('firstdata', label = self.label, data = self.data,
|
||||||
if __name__ == '__main__':
|
handtype = self.handtype, shape = self.shape)
|
||||||
Solution = Main(label = "five")
|
|
||||||
Solution.gesture_recognition()
|
|
||||||
npzfile = np.load('firstdata.npz')
|
if __name__ == '__main__':
|
||||||
|
Solution = Main(label = "five")
|
||||||
#print(npzfile['data'][0])
|
Solution.gesture_recognition()
|
||||||
#print(" ")
|
npzfile = np.load('firstdata.npz')
|
||||||
#print(npzfile['handtype'])
|
|
||||||
#print(npzfile['label'])
|
#print(npzfile['data'][0])
|
||||||
#print(npzfile['shape'])
|
#print(" ")
|
||||||
|
#print(npzfile['handtype'])
|
||||||
|
#print(npzfile['label'])
|
||||||
|
#print(npzfile['shape'])
|
||||||
|
11
demo.py
11
demo.py
@ -27,14 +27,16 @@ class HandDetector:
|
|||||||
self.results = None
|
self.results = None
|
||||||
self.mode = mode
|
self.mode = mode
|
||||||
self.max_hands = max_hands
|
self.max_hands = max_hands
|
||||||
self.modelComplex = False
|
self.modelComplex = 1
|
||||||
self.detection_con = detection_con
|
self.detection_con = detection_con
|
||||||
self.min_track_con = min_track_con
|
self.min_track_con = min_track_con
|
||||||
|
|
||||||
# 初始化手部的识别模型
|
# 初始化手部的识别模型
|
||||||
self.mpHands = mp.solutions.hands
|
self.mpHands = mp.solutions.hands
|
||||||
self.hands = self.mpHands.Hands(self.mode, self.max_hands, self.modelComplex,
|
self.hands = self.mpHands.Hands(static_image_mode=self.mode,
|
||||||
self.detection_con, self.min_track_con)
|
max_num_hands=self.max_hands,
|
||||||
|
min_detection_confidence=self.detection_con,
|
||||||
|
min_tracking_confidence=self.min_track_con)
|
||||||
self.mpDraw = mp.solutions.drawing_utils # 初始化绘图器
|
self.mpDraw = mp.solutions.drawing_utils # 初始化绘图器
|
||||||
self.tipIds = [4, 8, 12, 16, 20] # 指尖列表
|
self.tipIds = [4, 8, 12, 16, 20] # 指尖列表
|
||||||
self.fingers = []
|
self.fingers = []
|
||||||
@ -154,6 +156,9 @@ class Main:
|
|||||||
if (x2 == 1 and x3 == 1) and (x4 == 0 and x5 == 0 and x1 == 0):
|
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,
|
cv2.putText(img, "2_TWO", (x_1, y_1), cv2.FONT_HERSHEY_PLAIN, 3,
|
||||||
(0, 0, 255), 3)
|
(0, 0, 255), 3)
|
||||||
|
elif x3 and x1 == 0 and x2 == 0 and (x4 == 0, x5 == 0):
|
||||||
|
cv2.putText(img, "FUCK YOU!!", (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):
|
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,
|
cv2.putText(img, "3_THREE", (x_1, y_1), cv2.FONT_HERSHEY_PLAIN, 3,
|
||||||
(0, 0, 255), 3)
|
(0, 0, 255), 3)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user