graduation-project/identify.py

145 lines
4.7 KiB
Python
Raw Normal View History

2022-01-25 21:31:18 +08:00
import time
import cv2
import mediapipe as mp
import math
def binary_picture(image):
# 灰度图像
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 二值图像
ret, binary = cv2.threshold(gray, 50, 255, cv2.THRESH_BINARY)
return binary
def vector_2d_angle(v1, v2):
'''
求解二维向量的角度
'''
v1_x = v1[0]
v1_y = v1[1]
v2_x = v2[0]
v2_y = v2[1]
try:
angle_ = math.degrees(math.acos(
(v1_x * v2_x + v1_y * v2_y) / (((v1_x ** 2 + v1_y ** 2) ** 0.5) * ((v2_x ** 2 + v2_y ** 2) ** 0.5))))
except:
angle_ = 65535.
if angle_ > 180.:
angle_ = 65535.
return angle_
def hand_angle(hand_):
'''
获取对应手相关向量的二维角度,根据角度确定手势
'''
angle_list = []
# ---------------------------- thumb 大拇指角度
angle_ = 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_ = 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_ = 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_ = 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_ = 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_)
return angle_list
def is_curve(angle):
return angle < 40
def get_distance(point1, point2):
return math.sqrt((point1[0] - point2[0]) ** 2 + (point1[1] - point2[1]) ** 2)
# 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_five(points):
angle_list = hand_angle(points)
return is_curve(angle_list[1]) and is_curve(angle_list[2]) and is_curve(angle_list[3]) and is_curve(angle_list[4])
def judge_up(points):
angle_list = hand_angle(points)
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 (is_curve(angle_list[1]) and not is_curve(angle_list[2]) and not \
is_curve(angle_list[3]) and not is_curve(angle_list[4])) and angle_ <= 40
def judge_down(points):
angle_list = hand_angle(points)
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])))
)
print(is_curve(angle_list[1]))
print(is_curve(angle_list[2]))
print(is_curve(angle_list[3]))
print(is_curve(angle_list[4]))
return is_curve(angle_list[1]) and is_curve(angle_list[2]) and not \
is_curve(angle_list[3]) and not is_curve(angle_list[4])
def judge_end(points):
angle_list = hand_angle(points)
return not is_curve(angle_list[1]) and not is_curve(angle_list[2]) and not \
is_curve(angle_list[3]) and not is_curve(angle_list[4])
def judge_control(hand_points):
if len(hand_points) != 0:
if judge_five(hand_points):
print("open_ppt")
return 1
elif judge_up(hand_points):
print("ppt_up")
return 2
elif judge_down(hand_points):
print("ppt_down")
return 3
elif judge_end(hand_points):
print("ppt_end")
return 4
else:
print("other")
else:
print("no_hand_points")
return 0