提高了帧率
This commit is contained in:
parent
b6be6d3bde
commit
3c4a88ca20
Binary file not shown.
112
identify.py
112
identify.py
|
@ -13,91 +13,82 @@ import util
|
|||
|
||||
class Identify:
|
||||
def __init__(self, v):
|
||||
self.v = v
|
||||
self.result = 0
|
||||
self.position_x = 0
|
||||
self.position_y = 0
|
||||
self.image = []
|
||||
self.image_height = 0
|
||||
self.image_width = 0
|
||||
self.rgb_image = []
|
||||
self.identify_results = []
|
||||
self.hand_points = []
|
||||
self.angle_list = []
|
||||
self.is_finger_straight = [False, False, False, False, False]
|
||||
self.is_identify = False
|
||||
self.last_control_flag = 0
|
||||
self.v = v
|
||||
self.page_up_count = 0
|
||||
self.page_down_count = 0
|
||||
self.last_wrist_point = (0, 0)
|
||||
self.now_time = 0
|
||||
self.lase_time = 0
|
||||
self.mp_drawing = mp.solutions.drawing_utils
|
||||
self.mp_hands = mp.solutions.hands
|
||||
self.hands = self.mp_hands.Hands(
|
||||
static_image_mode=False,
|
||||
max_num_hands=2,
|
||||
min_detection_confidence=0.75,
|
||||
min_tracking_confidence=0.75)
|
||||
|
||||
def begin(self):
|
||||
capture = cv2.VideoCapture(0)
|
||||
# j = 1
|
||||
# count = 0
|
||||
# last_control_flag = 0
|
||||
# global control_flag, x, y
|
||||
while 1:
|
||||
ret, self.image = capture.read()
|
||||
# mp_drawing = mp.solutions.drawing_utils
|
||||
mp_holistic = mp.solutions.holistic
|
||||
holistic = mp_holistic.Holistic(static_image_mode=True)
|
||||
rgb_image = cv2.cvtColor(self.image, cv2.COLOR_BGR2RGB)
|
||||
self.identify_results = holistic.process(rgb_image)
|
||||
self.deal_with_image()
|
||||
self.now_time = time.time()
|
||||
# fps = 1 / (self.now_time - self.lase_time)
|
||||
# self.lase_time = self.now_time
|
||||
# print("fps = " + str(fps))
|
||||
self.is_identify = False
|
||||
self.is_finger_straight[0] = False
|
||||
self.is_finger_straight[1] = False
|
||||
self.is_finger_straight[2] = False
|
||||
self.is_finger_straight[3] = False
|
||||
self.is_finger_straight[4] = False
|
||||
for i in range(5):
|
||||
self.is_finger_straight[i] = False
|
||||
self.hand_points.clear()
|
||||
if self.identify_results.left_hand_landmarks:
|
||||
for hand_landmarks in self.identify_results.left_hand_landmarks.landmark:
|
||||
h, w, c = self.image.shape
|
||||
cx, cy = int(hand_landmarks.x * w), int(hand_landmarks.y * h)
|
||||
self.hand_points.append((cx, cy))
|
||||
self.is_identify = True
|
||||
# print(self.hand_points)
|
||||
self.get_hand_points()
|
||||
self.judge_finger_straight()
|
||||
flag = self.judge_control()
|
||||
print("**************")
|
||||
for i in range(5):
|
||||
print(self.is_finger_straight[i])
|
||||
# x = position[0]
|
||||
# y = position[1]
|
||||
# cv2.namedWindow("Video")
|
||||
# cv2.imshow("Video", self.image)
|
||||
# if cv2.waitKey(1) & 0xFF == ord('q'):
|
||||
# break
|
||||
# if flag == 2:
|
||||
# control_flag = flag
|
||||
# continue
|
||||
# if flag == 3:
|
||||
# control_flag = flag
|
||||
# continue
|
||||
# if not flag:
|
||||
# control_flag = 0
|
||||
# continue
|
||||
# if flag != self.last_control_flag:
|
||||
# self.last_control_flag = flag
|
||||
# count = 0
|
||||
# print(str(flag) + " != " + str(self.last_control_flag))
|
||||
# continue
|
||||
# else:
|
||||
# if count < 4:
|
||||
# count = count + 1
|
||||
# if count < 3:
|
||||
# self.v.value = 0
|
||||
# control_flag = 0
|
||||
# print("final_control_flag = " + str(control_flag))
|
||||
# continue
|
||||
# print("count = " + str(count))
|
||||
cv2.namedWindow("Video")
|
||||
cv2.imshow("Video", self.image)
|
||||
if cv2.waitKey(1) & 0xFF == ord('q'):
|
||||
break
|
||||
control_flag = flag
|
||||
self.v.value = flag
|
||||
print("final_control_flag = " + str(control_flag))
|
||||
capture.release()
|
||||
cv2.destroyAllWindows()
|
||||
|
||||
# def binary_picture(self, image):
|
||||
# # 灰度图像
|
||||
# gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
||||
# # 二值图像
|
||||
# ret, binary = cv2.threshold(gray, 50, 255, cv2.THRESH_BINARY)
|
||||
# return binary
|
||||
def deal_with_image(self):
|
||||
self.image = cv2.flip(self.image, 1)
|
||||
self.rgb_image = cv2.cvtColor(self.image, cv2.COLOR_BGR2RGB)
|
||||
self.identify_results = self.hands.process(self.rgb_image)
|
||||
|
||||
def get_hand_points(self):
|
||||
if self.identify_results.multi_handedness:
|
||||
for i in range(len(self.identify_results.multi_handedness)):
|
||||
if self.identify_results.multi_handedness[i].classification[0].label != "Left":
|
||||
continue
|
||||
for hand_landmarks in self.identify_results.multi_hand_landmarks[i].landmark:
|
||||
if self.image_height == 0:
|
||||
self.image_height, self.image_width, c = self.image.shape
|
||||
cx, cy = int(hand_landmarks.x * self.image_width), int(hand_landmarks.y * self.image_height)
|
||||
self.hand_points.append((cx, cy))
|
||||
self.is_identify = True
|
||||
self.mp_drawing.draw_landmarks(
|
||||
self.image, self.identify_results.multi_hand_landmarks[i], self.mp_hands.HAND_CONNECTIONS)
|
||||
|
||||
def hand_angle(self):
|
||||
'''
|
||||
|
@ -137,15 +128,6 @@ class Identify:
|
|||
angle_list.append(angle_)
|
||||
self.angle_list = angle_list
|
||||
|
||||
# def deal_with_one_image():
|
||||
# results = holistic.process(image)
|
||||
# hand_points = []
|
||||
# if results.left_hand_landmarks:
|
||||
# for hand_landmarks in results.left_hand_landmarks.landmark:
|
||||
# h, w, c = image.shape
|
||||
# cx, cy = int(hand_landmarks.x * w), int(hand_landmarks.y * h)
|
||||
# hand_points.append((cx, cy))
|
||||
# return hand_points
|
||||
def judge_finger_straight(self):
|
||||
if self.is_identify:
|
||||
self.hand_angle()
|
||||
|
@ -268,7 +250,7 @@ class Identify:
|
|||
if self.judge_one():
|
||||
# print("open_ppt")
|
||||
return 1
|
||||
elif self.judge_page_up():
|
||||
elif self.judge_five():
|
||||
# print("ppt_up")
|
||||
return 2
|
||||
elif self.judge_three():
|
||||
|
|
60
main.py
60
main.py
|
@ -112,10 +112,10 @@ def control_thread(v, flag):
|
|||
control_flag = v.value
|
||||
# if control_flag == 1:
|
||||
# control_ppt_begin()
|
||||
if control_flag == 2:
|
||||
control_page_up()
|
||||
if control_flag == 3:
|
||||
control_page_down()
|
||||
# if control_flag == 2:
|
||||
# control_page_up()
|
||||
# if control_flag == 3:
|
||||
# control_page_down()
|
||||
# if control_flag == 4:
|
||||
# control_ppt_end()
|
||||
# if control_flag == 5:
|
||||
|
@ -124,58 +124,6 @@ def control_thread(v, flag):
|
|||
# control_draw()
|
||||
|
||||
|
||||
# def identify_thread():
|
||||
# # while True: {
|
||||
# # print("identify_thread")
|
||||
# # }
|
||||
# capture = cv2.VideoCapture(0)
|
||||
# j = 1
|
||||
# count = 0
|
||||
# last_control_flag = 0
|
||||
# global control_flag, x, y
|
||||
# while 1:
|
||||
# # print("identify_thread")
|
||||
# ret, image = capture.read()
|
||||
# mp_drawing = mp.solutions.drawing_utils
|
||||
# mp_holistic = mp.solutions.holistic
|
||||
# holistic = mp_holistic.Holistic(static_image_mode=True)
|
||||
# image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
||||
# results = holistic.process(image)
|
||||
# hand_points = []
|
||||
# if results.left_hand_landmarks:
|
||||
# for hand_landmarks in results.left_hand_landmarks.landmark:
|
||||
# h, w, c = image.shape
|
||||
# cx, cy = int(hand_landmarks.x * w), int(hand_landmarks.y * h)
|
||||
# hand_points.append((cx, cy))
|
||||
# flag = judge_control(hand_points)
|
||||
# # x = position[0]
|
||||
# # y = position[1]
|
||||
# # cv2.namedWindow("Video")
|
||||
# # cv2.imshow("Video", image)
|
||||
# # if cv2.waitKey(1) & 0xFF == ord('q'):
|
||||
# # break
|
||||
# if flag == 2:
|
||||
# control_flag = flag
|
||||
# continue
|
||||
# if flag == 3:
|
||||
# control_flag = flag
|
||||
# continue
|
||||
# if not flag:
|
||||
# control_flag = 0
|
||||
# continue
|
||||
# if flag != last_control_flag:
|
||||
# last_control_flag = flag
|
||||
# count = 0
|
||||
# else:
|
||||
# if count < 4:
|
||||
# count = count + 1
|
||||
# if count < 3:
|
||||
# continue
|
||||
# control_flag = flag
|
||||
# print("final_control_flag = " + str(control_flag))
|
||||
# capture.release()
|
||||
# cv2.destroyAllWindows()
|
||||
|
||||
def identify_thread(v, flag):
|
||||
identify = Identify(v)
|
||||
identify.begin()
|
||||
|
|
29
test.py
29
test.py
|
@ -1 +1,28 @@
|
|||
print("test")
|
||||
import cv2
|
||||
import mediapipe as mp
|
||||
|
||||
mp_drawing = mp.solutions.drawing_utils
|
||||
mp_hands = mp.solutions.hands
|
||||
hands = mp_hands.Hands(
|
||||
static_image_mode=False,
|
||||
max_num_hands=2,
|
||||
min_detection_confidence=0.75,
|
||||
min_tracking_confidence=0.75)
|
||||
cap = cv2.VideoCapture(0)
|
||||
while True:
|
||||
ret, frame = cap.read()
|
||||
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
||||
frame = cv2.flip(frame, 1)
|
||||
results = hands.process(frame)
|
||||
|
||||
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
|
||||
if results.multi_handedness:
|
||||
if results.multi_handedness[0].classification[0].label == "Left":
|
||||
for hand_landmarks in results.multi_hand_landmarks:
|
||||
# 关键点可视化
|
||||
mp_drawing.draw_landmarks(
|
||||
frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)
|
||||
cv2.imshow('MediaPipe Hands', frame)
|
||||
if cv2.waitKey(1) & 0xFF == 27:
|
||||
break
|
||||
cap.release()
|
||||
|
|
|
@ -0,0 +1,259 @@
|
|||
import time
|
||||
from mem_top import mem_top
|
||||
import cv2
|
||||
import mediapipe as mp
|
||||
import math
|
||||
import util
|
||||
|
||||
|
||||
# page_up_count = 0
|
||||
# page_down_count = 0
|
||||
# last_wrist_point = (0, 0)
|
||||
|
||||
|
||||
class Identify:
|
||||
def __init__(self):
|
||||
self.result = 0
|
||||
self.position_x = 0
|
||||
self.position_y = 0
|
||||
self.image = []
|
||||
self.image_height = 0
|
||||
self.image_width = 0
|
||||
self.rgb_image = []
|
||||
self.identify_results = []
|
||||
self.hand_points = []
|
||||
self.angle_list = []
|
||||
self.is_finger_straight = [False, False, False, False, False]
|
||||
self.is_identify = False
|
||||
self.last_control_flag = 0
|
||||
self.page_up_count = 0
|
||||
self.page_down_count = 0
|
||||
self.last_wrist_point = (0, 0)
|
||||
self.now_time = 0
|
||||
self.lase_time = 0
|
||||
self.mp_drawing = mp.solutions.drawing_utils
|
||||
self.mp_hands = mp.solutions.hands
|
||||
self.hands = self.mp_hands.Hands(
|
||||
static_image_mode=False,
|
||||
max_num_hands=2,
|
||||
min_detection_confidence=0.75,
|
||||
min_tracking_confidence=0.75)
|
||||
|
||||
def begin(self):
|
||||
capture = cv2.VideoCapture(0)
|
||||
while 1:
|
||||
ret, self.image = capture.read()
|
||||
self.deal_with_image()
|
||||
self.now_time = time.time()
|
||||
fps = 1 / (self.now_time - self.lase_time)
|
||||
self.lase_time = self.now_time
|
||||
print("fps = " + str(fps))
|
||||
self.is_identify = False
|
||||
for i in range(5):
|
||||
self.is_finger_straight[i] = False
|
||||
self.hand_points.clear()
|
||||
self.get_hand_points()
|
||||
self.judge_finger_straight()
|
||||
flag = self.judge_control()
|
||||
# x = position[0]
|
||||
# y = position[1]
|
||||
cv2.namedWindow("Video")
|
||||
cv2.imshow("Video", self.image)
|
||||
if cv2.waitKey(1) & 0xFF == ord('q'):
|
||||
break
|
||||
control_flag = flag
|
||||
print("final_control_flag = " + str(control_flag))
|
||||
capture.release()
|
||||
cv2.destroyAllWindows()
|
||||
|
||||
def deal_with_image(self):
|
||||
self.rgb_image = cv2.cvtColor(self.image, cv2.COLOR_BGR2RGB)
|
||||
self.rgb_image = cv2.flip(self.rgb_image, 1)
|
||||
self.identify_results = self.hands.process(self.rgb_image)
|
||||
|
||||
def get_hand_points(self):
|
||||
if self.identify_results.multi_handedness:
|
||||
for i in range(len(self.identify_results.multi_handedness)):
|
||||
if self.identify_results.multi_handedness[i].classification[0].label != "Left":
|
||||
continue
|
||||
for hand_landmarks in self.identify_results.multi_hand_landmarks[i].landmark:
|
||||
if self.image_height == 0:
|
||||
self.image_height, self.image_width, c = self.image.shape
|
||||
cx, cy = int(hand_landmarks.x * self.image_width), int(hand_landmarks.y * self.image_height)
|
||||
self.hand_points.append((cx, cy))
|
||||
self.is_identify = True
|
||||
|
||||
def hand_angle(self):
|
||||
'''
|
||||
获取对应手相关向量的二维角度,根据角度确定手势
|
||||
'''
|
||||
angle_list = []
|
||||
hand_ = self.hand_points
|
||||
# ---------------------------- thumb 大拇指角度
|
||||
angle_ = util.Util.vector_2d_angle(
|
||||
((int(hand_[2][0]) - int(hand_[3][0])), (int(hand_[2][1]) - int(hand_[3][1]))),
|
||||
((int(hand_[3][0]) - int(hand_[4][0])), (int(hand_[3][1]) - int(hand_[4][1])))
|
||||
)
|
||||
angle_list.append(angle_)
|
||||
# ---------------------------- index 食指角度
|
||||
angle_ = util.Util.vector_2d_angle(
|
||||
((int(hand_[5][0]) - int(hand_[6][0])), (int(hand_[5][1]) - int(hand_[6][1]))),
|
||||
((int(hand_[7][0]) - int(hand_[8][0])), (int(hand_[7][1]) - int(hand_[8][1])))
|
||||
)
|
||||
angle_list.append(angle_)
|
||||
# ---------------------------- middle 中指角度
|
||||
angle_ = util.Util.vector_2d_angle(
|
||||
((int(hand_[9][0]) - int(hand_[10][0])), (int(hand_[9][1]) - int(hand_[10][1]))),
|
||||
((int(hand_[11][0]) - int(hand_[12][0])), (int(hand_[11][1]) - int(hand_[12][1])))
|
||||
)
|
||||
angle_list.append(angle_)
|
||||
# ---------------------------- ring 无名指角度
|
||||
angle_ = util.Util.vector_2d_angle(
|
||||
((int(hand_[13][0]) - int(hand_[14][0])), (int(hand_[13][1]) - int(hand_[14][1]))),
|
||||
((int(hand_[15][0]) - int(hand_[16][0])), (int(hand_[15][1]) - int(hand_[16][1])))
|
||||
)
|
||||
angle_list.append(angle_)
|
||||
# ---------------------------- pink 小拇指角度
|
||||
angle_ = util.Util.vector_2d_angle(
|
||||
((int(hand_[17][0]) - int(hand_[18][0])), (int(hand_[17][1]) - int(hand_[18][1]))),
|
||||
((int(hand_[19][0]) - int(hand_[20][0])), (int(hand_[19][1]) - int(hand_[20][1])))
|
||||
)
|
||||
angle_list.append(angle_)
|
||||
self.angle_list = angle_list
|
||||
|
||||
def judge_finger_straight(self):
|
||||
if self.is_identify:
|
||||
self.hand_angle()
|
||||
for i in range(5):
|
||||
self.is_finger_straight[i] = util.Util.is_straight(self.angle_list[i])
|
||||
|
||||
def judge_zero(self):
|
||||
return not self.is_finger_straight[1] and not self.is_finger_straight[2] and \
|
||||
not self.is_finger_straight[3] and not self.is_finger_straight[4]
|
||||
|
||||
def judge_one(self):
|
||||
return self.is_finger_straight[1] and not self.is_finger_straight[2] and \
|
||||
not self.is_finger_straight[3] and not self.is_finger_straight[4]
|
||||
|
||||
def judge_two(self):
|
||||
return self.is_finger_straight[1] and self.is_finger_straight[2] and \
|
||||
not self.is_finger_straight[3] and not self.is_finger_straight[4]
|
||||
|
||||
def judge_three(self):
|
||||
return self.is_finger_straight[1] and self.is_finger_straight[2] and \
|
||||
self.is_finger_straight[3] and not self.is_finger_straight[4]
|
||||
|
||||
def judge_four(self):
|
||||
return self.is_finger_straight[1] and self.is_finger_straight[2] and \
|
||||
self.is_finger_straight[3] and self.is_finger_straight[4]
|
||||
|
||||
def judge_five(self):
|
||||
return self.is_finger_straight[1] and self.is_finger_straight[2] and \
|
||||
self.is_finger_straight[3] and self.is_finger_straight[4]
|
||||
|
||||
# def judge_five(self):
|
||||
# self.hand_angle()
|
||||
# return util.Util.is_straight(self.angle_list[1]) and util.Util.is_straight(
|
||||
# self.angle_list[2]) and util.Util.is_straight(self.angle_list[3]) and util.Util.is_straight(
|
||||
# self.angle_list[4])
|
||||
|
||||
# def judge_open(self):
|
||||
# self.hand_angle()
|
||||
# # angle_ = vector_2d_angle(
|
||||
# # ((int(points[0][0]) - int(points[5][0])), (int(points[0][1]) - int(points[5][1]))),
|
||||
# # ((int(points[5][0]) - int(points[8][0])), (int(points[5][1]) - int(points[8][1])))
|
||||
# # )
|
||||
# return not util.Util.is_straight(self.angle_list[1]) and util.Util.is_straight(
|
||||
# self.angle_list[2]) and util.Util.is_straight(self.angle_list[3]) and util.Util.is_straight(
|
||||
# self.angle_list[4])
|
||||
#
|
||||
# def judge_up(self):
|
||||
# self.hand_angle()
|
||||
# angle_ = util.Util.vector_2d_angle(
|
||||
# ((int(self.hand_points[0][0]) - int(self.hand_points[5][0])),
|
||||
# (int(self.hand_points[0][1]) - int(self.hand_points[5][1]))),
|
||||
# ((int(self.hand_points[5][0]) - int(self.hand_points[8][0])),
|
||||
# (int(self.hand_points[5][1]) - int(self.hand_points[8][1])))
|
||||
# )
|
||||
# return util.Util.is_straight(
|
||||
# self.angle_list[1] and not util.Util.is_straight(self.angle_list[2]) and not util.Util.is_straight(
|
||||
# self.angle_list[3]) and not util.Util.is_straight(self.angle_list[4])) and angle_ <= 40
|
||||
#
|
||||
# def judge_down(self):
|
||||
# self.hand_angle()
|
||||
# return util.Util.is_straight(self.angle_list[1]) and util.Util.is_straight(
|
||||
# self.angle_list[2]) and not util.Util.is_straight(self.angle_list[3]) and not util.Util.is_straight(
|
||||
# self.angle_list[4])
|
||||
#
|
||||
# def judge_end(self):
|
||||
# self.hand_angle()
|
||||
# return not util.Util.is_straight(self.angle_list[1]) and not util.Util.is_straight(
|
||||
# self.angle_list[2]) and not util.Util.is_straight(self.angle_list[3]) and not util.Util.is_straight(
|
||||
# self.angle_list[4])
|
||||
|
||||
# def judge_one(self):
|
||||
# self.hand_angle()
|
||||
# return util.Util.is_straight(self.angle_list[1]) and not util.Util.is_straight(
|
||||
# self.angle_list[2]) and not util.Util.is_straight(self.angle_list[3]) and not util.Util.is_straight(
|
||||
# self.angle_list[4])
|
||||
|
||||
def judge_page_up(self):
|
||||
if self.page_up_count == 0:
|
||||
if self.judge_five():
|
||||
self.last_wrist_point = self.hand_points[0]
|
||||
self.page_up_count += 1
|
||||
return False
|
||||
if self.judge_five() and self.hand_points[0][0] < self.last_wrist_point[0]:
|
||||
if self.hand_points[0][0] > self.last_wrist_point[0]:
|
||||
self.page_up_count = 0
|
||||
return False
|
||||
if self.page_up_count >= 2:
|
||||
self.page_up_count = 0
|
||||
self.last_wrist_point = (0, 0)
|
||||
return True
|
||||
self.page_up_count += 1
|
||||
self.last_wrist_point = self.hand_points[0]
|
||||
return False
|
||||
|
||||
def judge_page_down(self):
|
||||
# global last_wrist_point
|
||||
# global page_down_count
|
||||
# print("page_down_count = " + str(page_down_count))
|
||||
# print("last_wrist_point = " + str(last_wrist_point))
|
||||
# print("points[0] = " + str(points[0]))
|
||||
if self.page_down_count == 0:
|
||||
if self.judge_five():
|
||||
self.last_wrist_point = self.hand_points[0]
|
||||
self.page_down_count += 1
|
||||
return False
|
||||
if self.judge_five() and self.hand_points[0][0] > self.last_wrist_point[0]:
|
||||
if self.hand_points[0][0] < self.last_wrist_point[0]:
|
||||
self.page_down_count = 0
|
||||
return False
|
||||
if self.page_down_count >= 2:
|
||||
self.page_down_count = 0
|
||||
self.last_wrist_point = (0, 0)
|
||||
return True
|
||||
self.page_down_count += 1
|
||||
self.last_wrist_point = self.hand_points[0]
|
||||
return False
|
||||
|
||||
def judge_control(self):
|
||||
if self.is_identify:
|
||||
if self.judge_one():
|
||||
# print("open_ppt")
|
||||
return 1
|
||||
elif self.judge_five():
|
||||
# print("ppt_up")
|
||||
return 2
|
||||
elif self.judge_three():
|
||||
# print("ppt_down")
|
||||
return 3
|
||||
elif self.judge_zero():
|
||||
# print("ppt_end")
|
||||
return 4
|
||||
else:
|
||||
print("other")
|
||||
else:
|
||||
print("no_hand_points")
|
||||
return 0
|
Loading…
Reference in New Issue