diff --git a/__pycache__/identify.cpython-38.pyc b/__pycache__/identify.cpython-38.pyc index 8e3f59d..9d08e41 100644 Binary files a/__pycache__/identify.cpython-38.pyc and b/__pycache__/identify.cpython-38.pyc differ diff --git a/identify.py b/identify.py index 10ac7ad..e95de2d 100644 --- a/identify.py +++ b/identify.py @@ -14,6 +14,8 @@ import util class Identify: def __init__(self, v): self.v = v + self.left_hand_flag = False + self.right_hand_flag = False self.result = 0 self.position_x = 0 self.position_y = 0 @@ -22,16 +24,19 @@ class Identify: self.image_width = 0 self.rgb_image = [] self.identify_results = [] - self.hand_points = [] + self.left_hand_points = [] + self.right_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.step = 0 self.last_wrist_point = (0, 0) self.now_time = 0 self.lase_time = 0 + self.flag = 0 self.mp_drawing = mp.solutions.drawing_utils self.mp_hands = mp.solutions.hands self.hands = self.mp_hands.Hands( @@ -42,32 +47,44 @@ class Identify: def begin(self): capture = cv2.VideoCapture(0) + last_time = 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 + self.left_hand_flag = False + self.right_hand_flag = False for i in range(5): self.is_finger_straight[i] = False - self.hand_points.clear() + self.left_hand_points.clear() + self.right_hand_points.clear() self.get_hand_points() self.judge_finger_straight() flag = self.judge_control() - print("**************") - for i in range(5): - print(self.is_finger_straight[i]) + if flag: + self.flag = flag + now_time = time.time() + if now_time - last_time < 1: + continue + last_time = now_time + # 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 - control_flag = flag - self.v.value = flag - print("final_control_flag = " + str(control_flag)) + # cv2.namedWindow("Video") + # cv2.imshow("Video", self.image) + # if cv2.waitKey(1) & 0xFF == ord('q'): + # break + # control_flag = flag + self.v.value = self.flag + # print("self.v.value = " + str(self.flag)) + print("final_control_flag = " + str(self.flag)) + self.flag = 0 capture.release() cv2.destroyAllWindows() @@ -79,23 +96,38 @@ class Identify: 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) + # print(self.identify_results.multi_handedness[i].classification[0].label) + if self.identify_results.multi_handedness[i].classification[0].label == "Left": + 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.left_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) + self.left_hand_flag = True + + else: + 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.right_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) + self.right_hand_flag = True def hand_angle(self): ''' 获取对应手相关向量的二维角度,根据角度确定手势 ''' angle_list = [] - hand_ = self.hand_points + if self.left_hand_flag: + hand_ = self.left_hand_points + else: + hand_ = self.right_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]))), @@ -203,61 +235,89 @@ class Identify: # 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_step_one(self, is_left): + if is_left: + if self.judge_five() and self.left_hand_points[8][0] < self.left_hand_points[0][0] and \ + self.left_hand_points[12][0] < self.left_hand_points[0][0] and \ + self.left_hand_points[16][0] < self.left_hand_points[0][0] and self.left_hand_points[20][0] < \ + self.left_hand_points[0][0]: + return True + else: + if self.judge_five() and self.right_hand_points[8][0] > self.right_hand_points[0][0] and \ + self.right_hand_points[12][0] > self.right_hand_points[0][0] and \ + self.right_hand_points[16][0] > self.right_hand_points[0][0] and self.right_hand_points[20][0] > \ + self.right_hand_points[0][0]: + return True + return False + + def judge_step_two(self, is_left): + if is_left: + if self.judge_five() and self.left_hand_points[8][0] > self.left_hand_points[0][0] and \ + self.left_hand_points[12][0] > self.left_hand_points[0][0] and \ + self.left_hand_points[16][0] > self.left_hand_points[0][0] and self.left_hand_points[20][0] > \ + self.left_hand_points[0][0]: + return True + else: + if self.judge_five() and self.right_hand_points[8][0] < self.right_hand_points[0][0] and \ + self.right_hand_points[12][0] < self.right_hand_points[0][0] and \ + self.right_hand_points[16][0] < self.right_hand_points[0][0] and self.right_hand_points[20][0] < \ + self.right_hand_points[0][0]: + return True + return False 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 + if not self.right_hand_flag: 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) + if self.step == 0: + self.lase_time = time.time() + if self.step == 0 and self.judge_step_one(False): + self.step = 1 + elif self.step == 1 and self.judge_step_two(False): + self.step = 3 + elif self.step == 2 and self.judge_zero(): + self.step = 3 + elif self.step == 3: + self.step = 0 + now_time = time.time() + if now_time - self.lase_time < 3: + self.lase_time = now_time return True - self.page_up_count += 1 - self.last_wrist_point = self.hand_points[0] + else: + self.lase_time = now_time + return False 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 + if not self.left_hand_flag: 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) + if self.step == 0: + self.lase_time = time.time() + if self.step == 0 and self.judge_step_one(True): + self.step = 1 + elif self.step == 1 and self.judge_step_two(True): + self.step = 3 + elif self.step == 2 and self.judge_zero(): + self.step = 3 + elif self.step == 3: + self.step = 0 + now_time = time.time() + if now_time - self.lase_time < 3: + self.lase_time = now_time return True - self.page_down_count += 1 - self.last_wrist_point = self.hand_points[0] + else: + self.lase_time = now_time + return False 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") + elif self.judge_page_up(): return 2 - elif self.judge_three(): - # print("ppt_down") + elif self.judge_page_down(): return 3 elif self.judge_zero(): - # print("ppt_end") return 4 else: print("other") diff --git a/main.py b/main.py index 2677abc..f1935cc 100644 --- a/main.py +++ b/main.py @@ -95,11 +95,15 @@ def control_draw(): def control_thread(v, flag): last_time = 0.0 + step = 0 while 1: + # print("step = " + str(step)) # print("control_thread") now_time = time.time() - time.sleep(0.1) + # time.sleep(0.1) # print(end - start) + + control_flag = v.value if now_time - last_time < 1: continue last_time = now_time @@ -108,16 +112,22 @@ def control_thread(v, flag): # elif control_flag == 6: # time.sleep(0.05) # start = time.time() - print("control_flag = " + str(v.value)) - 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 == 4: - # control_ppt_end() + print("control_flag = " + str(control_flag)) + if step == 0 and control_flag == 1: + control_ppt_begin() + # print("control_flag == 1") + step = 1 + elif step == 1: + if control_flag == 2: + control_page_up() + # print("control_flag == 2") + if control_flag == 3: + control_page_down() + # print("control_flag == 3") + if control_flag == 4: + control_ppt_end() + # print("control_flag == 4") + step = 0 # if control_flag == 5: # control_open_pencil() # if control_flag == 6: