加入多进程
This commit is contained in:
parent
c1809992a5
commit
b6be6d3bde
3801
DRAW_SVM.xml
3801
DRAW_SVM.xml
File diff suppressed because it is too large
Load Diff
5052
OPEN_PENCIL_SVM.xml
5052
OPEN_PENCIL_SVM.xml
File diff suppressed because it is too large
Load Diff
6407
PAGE_DOWN_SVM.xml
6407
PAGE_DOWN_SVM.xml
File diff suppressed because it is too large
Load Diff
4260
PAGE_UP_SVM.xml
4260
PAGE_UP_SVM.xml
File diff suppressed because it is too large
Load Diff
7465
PPT_BEGIN_SVM.xml
7465
PPT_BEGIN_SVM.xml
File diff suppressed because it is too large
Load Diff
8836
PPT_END_SVM.xml
8836
PPT_END_SVM.xml
File diff suppressed because it is too large
Load Diff
Binary file not shown.
Before Width: | Height: | Size: 142 KiB |
Binary file not shown.
Binary file not shown.
382
identify.py
382
identify.py
|
@ -1,144 +1,288 @@
|
|||
import time
|
||||
|
||||
from mem_top import mem_top
|
||||
import cv2
|
||||
import mediapipe as mp
|
||||
import math
|
||||
import util
|
||||
|
||||
|
||||
def binary_picture(image):
|
||||
# 灰度图像
|
||||
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
||||
# 二值图像
|
||||
ret, binary = cv2.threshold(gray, 50, 255, cv2.THRESH_BINARY)
|
||||
return binary
|
||||
# page_up_count = 0
|
||||
# page_down_count = 0
|
||||
# last_wrist_point = (0, 0)
|
||||
|
||||
|
||||
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_
|
||||
class Identify:
|
||||
def __init__(self, v):
|
||||
self.result = 0
|
||||
self.position_x = 0
|
||||
self.position_y = 0
|
||||
self.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)
|
||||
|
||||
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.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
|
||||
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.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
|
||||
# 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))
|
||||
control_flag = flag
|
||||
self.v.value = flag
|
||||
print("final_control_flag = " + str(control_flag))
|
||||
capture.release()
|
||||
cv2.destroyAllWindows()
|
||||
|
||||
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 binary_picture(self, image):
|
||||
# # 灰度图像
|
||||
# gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
||||
# # 二值图像
|
||||
# ret, binary = cv2.threshold(gray, 50, 255, cv2.THRESH_BINARY)
|
||||
# return binary
|
||||
|
||||
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 is_curve(angle):
|
||||
return angle < 40
|
||||
# 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()
|
||||
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 get_distance(point1, point2):
|
||||
return math.sqrt((point1[0] - point2[0]) ** 2 + (point1[1] - point2[1]) ** 2)
|
||||
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 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_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(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_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_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_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_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_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_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
|
||||
def judge_control(self):
|
||||
if self.is_identify:
|
||||
if self.judge_one():
|
||||
# print("open_ppt")
|
||||
return 1
|
||||
elif self.judge_page_up():
|
||||
# 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("other")
|
||||
else:
|
||||
print("no_hand_points")
|
||||
return 0
|
||||
print("no_hand_points")
|
||||
return 0
|
||||
|
||||
#
|
||||
# identify = Identify()
|
||||
# identify.begin()
|
||||
|
|
167
main.py
167
main.py
|
@ -1,7 +1,9 @@
|
|||
import multiprocessing
|
||||
import tkinter
|
||||
|
||||
import cv2
|
||||
|
||||
import identify
|
||||
from identify import *
|
||||
from tkinter.filedialog import *
|
||||
import threading
|
||||
|
@ -32,56 +34,6 @@ x = 0
|
|||
y = 0
|
||||
|
||||
|
||||
def identify_thread():
|
||||
capture = cv2.VideoCapture(0)
|
||||
j = 1
|
||||
count = 0
|
||||
last_control_flag = 0
|
||||
global control_flag, x, y
|
||||
while 1:
|
||||
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 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 open_ppt(file_path):
|
||||
ppt = Dispatch('PowerPoint.Application')
|
||||
ppt.Visible = 1 # 后台运行
|
||||
ppt.DisplayAlerts = 0 # 不显示,不警告
|
||||
pptSel = ppt.Presentations.Open(file_path)
|
||||
|
||||
|
||||
def show():
|
||||
win32api.keybd_event(116, 0, 0, 0) # 代表按下f键
|
||||
time.sleep(0.02)
|
||||
|
@ -128,49 +80,130 @@ def control_open_pencil():
|
|||
|
||||
def control_draw():
|
||||
global x, y
|
||||
|
||||
# m = pymouse.PyMouse()
|
||||
# m.move(x, y)
|
||||
# win32api.keybd_event(40, 0, 0, 0) # 代表按下f键
|
||||
# time.sleep(0.02)
|
||||
# win32api.keybd_event(40, 0, win32con.KEYEVENTF_KEYUP, 0) # 释放f键
|
||||
|
||||
# class Control:
|
||||
# def __init__(self, control_flag):
|
||||
# self.control_flag = control_flag
|
||||
#
|
||||
|
||||
def control_thread():
|
||||
global control_flag
|
||||
|
||||
def control_thread(v, flag):
|
||||
last_time = 0.0
|
||||
while 1:
|
||||
time.sleep(2)
|
||||
# print("control_thread")
|
||||
now_time = time.time()
|
||||
time.sleep(0.1)
|
||||
# print(end - start)
|
||||
if now_time - last_time < 1:
|
||||
continue
|
||||
last_time = now_time
|
||||
# if control_flag in range(1, 2, 3, 4, 5):
|
||||
# time.sleep(2)
|
||||
# elif control_flag == 6:
|
||||
# time.sleep(0.05)
|
||||
print("control_flag = " + str(control_flag))
|
||||
if control_flag == 1:
|
||||
control_ppt_begin()
|
||||
# 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()
|
||||
# if control_flag == 4:
|
||||
# control_ppt_end()
|
||||
# if control_flag == 5:
|
||||
# control_open_pencil()
|
||||
# if control_flag == 6:
|
||||
# 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()
|
||||
|
||||
|
||||
def open_file():
|
||||
global control_flag
|
||||
file_path = askopenfilename(title=u'选择文件', initialdir=(os.path.expanduser('H:/')))
|
||||
open_ppt(file_path)
|
||||
v = multiprocessing.Value('i', 0)
|
||||
p1 = multiprocessing.Process(target=identify_thread, args=(v, 0))
|
||||
p2 = multiprocessing.Process(target=control_thread, args=(v, 0))
|
||||
p1.start()
|
||||
p2.start()
|
||||
# identify_t = threading.Thread(target=identify_thread)
|
||||
# # print("control_flag1 = " + str(control_flag))
|
||||
# control_t = threading.Thread(target=control_thread)
|
||||
# # print("control_flag2 = " + str(control_flag))
|
||||
# identify_t.setDaemon(True)
|
||||
# control_t.setDaemon(True)
|
||||
# identify_t.start()
|
||||
# control_t.start()
|
||||
|
||||
identify_t = threading.Thread(target=identify_thread)
|
||||
# print("control_flag1 = " + str(control_flag))
|
||||
control_t = threading.Thread(target=control_thread)
|
||||
# print("control_flag2 = " + str(control_flag))
|
||||
identify_t.setDaemon(True)
|
||||
control_t.setDaemon(True)
|
||||
identify_t.start()
|
||||
control_t.start()
|
||||
|
||||
def open_ppt(file_path):
|
||||
ppt = Dispatch('PowerPoint.Application')
|
||||
ppt.Visible = 1 # 后台运行
|
||||
ppt.DisplayAlerts = 0 # 不显示,不警告
|
||||
# pptSel = ppt.Presentations.Open(file_path)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
BIN
open_ppt.jpg
BIN
open_ppt.jpg
Binary file not shown.
Before Width: | Height: | Size: 143 KiB |
BIN
paga_down.jpg
BIN
paga_down.jpg
Binary file not shown.
Before Width: | Height: | Size: 140 KiB |
BIN
page_up.jpg
BIN
page_up.jpg
Binary file not shown.
Before Width: | Height: | Size: 138 KiB |
123
svm_test.py
123
svm_test.py
|
@ -1,123 +0,0 @@
|
|||
import cv2
|
||||
import numpy
|
||||
import time
|
||||
import mediapipe as mp
|
||||
import math
|
||||
|
||||
k = 0
|
||||
|
||||
|
||||
def binary_picture(image):
|
||||
# 灰度图像
|
||||
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
||||
# 二值图像
|
||||
ret, binary = cv2.threshold(gray, 50, 255, cv2.THRESH_BINARY)
|
||||
return binary
|
||||
|
||||
|
||||
def resize_picture(image, scale):
|
||||
width = int(image.shape[1] * scale)
|
||||
height = int(image.shape[0] * scale)
|
||||
image = cv2.resize(image, (width, height), cv2.INTER_AREA)
|
||||
return image
|
||||
|
||||
|
||||
def draw_min_rotated_rect(image, contours):
|
||||
rects = []
|
||||
for i in range(len(contours)):
|
||||
rect = cv2.minAreaRect(contours[i])
|
||||
if rect[1][0] * rect[1][1] < 500:
|
||||
continue
|
||||
box = cv2.boxPoints(rect)
|
||||
box = numpy.int0(box)
|
||||
rects.append(rect)
|
||||
cv2.drawContours(image, [box], 0, (255, 0, 0), 1)
|
||||
return image, rects
|
||||
|
||||
|
||||
def draw_min_rect(image, image_binary, contours, j):
|
||||
rects = []
|
||||
for i in range(len(contours)):
|
||||
x, y, w, h = cv2.boundingRect(contours[i])
|
||||
if w * h < 500:
|
||||
continue
|
||||
rect = []
|
||||
if x != 0 and y != 0 and w != image.shape[1] and h != image.shape[0]:
|
||||
# 左上角坐标和右下角坐标
|
||||
# 如果执行里面的这个画框,就是分别来画的,
|
||||
# cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 1)
|
||||
rect.append(x)
|
||||
rect.append(y)
|
||||
rect.append(x + w)
|
||||
rect.append(y + h)
|
||||
rects.append(rect)
|
||||
rect_image = image_binary[y:y + h, x:x + w]
|
||||
rect_image = cv2.resize(rect_image, (28, 28), cv2.INTER_AREA)
|
||||
# cv2.imwrite(save_path + str(i) + '.jpg',hv_flip)
|
||||
# cv2.imwrite('sample\\draw\\false\\' + str(j) + '.jpg', rect_image)
|
||||
j = j + 1
|
||||
return image, rects, j
|
||||
|
||||
|
||||
def judge_control(rects, image_binary):
|
||||
global k
|
||||
for i in range(len(rects)):
|
||||
k = k + 1
|
||||
one_rect = rects[i]
|
||||
rect_image = image_binary[one_rect[1]:one_rect[3], one_rect[0]:one_rect[2]]
|
||||
rect_image = cv2.resize(rect_image, (28, 28), cv2.INTER_AREA)
|
||||
save_image = rect_image
|
||||
rect_image = rect_image.astype(numpy.float32)
|
||||
rect_image = rect_image.reshape(-1, )
|
||||
rect_image = rect_image.reshape(1, -1)
|
||||
cv2.normalize(rect_image, rect_image)
|
||||
page_down_svm = cv2.ml.SVM_load('PAGE_DOWN_SVM.xml')
|
||||
page_up_svm = cv2.ml.SVM_load('PAGE_UP_SVM.xml')
|
||||
ppt_begin_svm = cv2.ml.SVM_load('PPT_BEGIN_SVM.xml')
|
||||
ppt_end_svm = cv2.ml.SVM_load('PPT_END_SVM.xml')
|
||||
open_pencil_svm = cv2.ml.SVM_load('OPEN_PENCIL_SVM.xml')
|
||||
draw_svm = cv2.ml.SVM_load('DRAW_SVM.xml')
|
||||
page_down_image_predict = page_down_svm.predict(rect_image)
|
||||
page_up_image_predict = page_up_svm.predict(rect_image)
|
||||
ppt_begin_image_predict = ppt_begin_svm.predict(rect_image)
|
||||
ppt_end_image_predict = ppt_end_svm.predict(rect_image)
|
||||
open_pencil_image_predict = open_pencil_svm.predict(rect_image)
|
||||
draw_image_predict = draw_svm.predict(rect_image)
|
||||
# if page_up_image_predict[1][0]:
|
||||
# cv2.imwrite('sample\\page_up\\true\\' + str(k) + '.jpg', save_image)
|
||||
# return 1
|
||||
# # else:
|
||||
# # cv2.imwrite('sample\\page_up\\false\\' + str(k) + '.jpg', save_image)
|
||||
# if page_down_image_predict[1][0]:
|
||||
# cv2.imwrite('sample\\page_down\\true\\' + str(k) + '.jpg', save_image)
|
||||
# return 2
|
||||
# # else:
|
||||
# # cv2.imwrite('sample\\page_down\\false\\' + str(k) + '.jpg', save_image)
|
||||
# if ppt_begin_image_predict[1][0]:
|
||||
# cv2.imwrite('sample\\ppt_begin\\true\\' + str(k) + '.jpg', save_image)
|
||||
# return 3
|
||||
# # else:
|
||||
# # cv2.imwrite('sample\\ppt_begin\\false\\' + str(k) + '.jpg', save_image)
|
||||
# if ppt_end_image_predict[1][0]:
|
||||
# cv2.imwrite('sample\\ppt_end\\true\\' + str(k) + '.jpg', save_image)
|
||||
# return 4
|
||||
# if open_pencil_image_predict[1][0]:
|
||||
# cv2.imwrite('sample\\open_pencil\\true\\' + str(k) + '.jpg', save_image)
|
||||
# return 5
|
||||
# if draw_image_predict[1][0]:
|
||||
# cv2.imwrite('sample\\draw\\true\\' + str(k) + '.jpg', save_image)
|
||||
# return 6, [one_rect[0], one_rect[1]]
|
||||
if page_up_image_predict[1][0]:
|
||||
return 1
|
||||
if page_down_image_predict[1][0]:
|
||||
return 2
|
||||
if ppt_begin_image_predict[1][0]:
|
||||
return 3
|
||||
if ppt_end_image_predict[1][0]:
|
||||
return 4
|
||||
if open_pencil_image_predict[1][0]:
|
||||
return 5
|
||||
# if draw_image_predict[1][0]:
|
||||
# return 6
|
||||
|
||||
return 0
|
|
@ -0,0 +1,28 @@
|
|||
import cv2
|
||||
import mediapipe as mp
|
||||
import math
|
||||
|
||||
|
||||
class Util:
|
||||
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 is_straight(angle):
|
||||
return angle < 40
|
||||
|
||||
def get_distance(point1, point2):
|
||||
return math.sqrt((point1[0] - point2[0]) ** 2 + (point1[1] - point2[1]) ** 2)
|
Loading…
Reference in New Issue