Skip to content

Commit

Permalink
尝试优化
Browse files Browse the repository at this point in the history
  • Loading branch information
ligb888 committed Oct 11, 2023
1 parent 5d39317 commit 938a58e
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ show = 1
# 截取摄像头局部范围,左上角坐标、右下角坐标(摄像头范围大时需要截取,crop2为"0, 0"时不截取),截取范围长宽比为16:9或16:10,好映射屏幕
crop1 = 0, 0
crop2 = 0, 0
# 是否触发执行,0否,1是,为0时不会执行具体操作,用于调试
trigger = 1
```

# 打包
Expand Down
2 changes: 2 additions & 0 deletions config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ smooth = 4
show = 1
crop1 = 0, 0
crop2 = 0, 0
trigger = 1

;[common]
;index = 1
Expand All @@ -23,3 +24,4 @@ crop2 = 0, 0
;show = 1
;crop1 = 0, 720
;crop2 = 640, 1080
;trigger = 1
14 changes: 14 additions & 0 deletions handProcess.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ def distance(self, lm1, lm2):
z = lm1.z - lm2.z
return math.sqrt(x ** 2 + y ** 2 + z ** 2)

def distance2(self, lm1, lm2):
x = lm1[1] - lm2[1]
y = lm1[2] - lm2[2]
return math.sqrt(x ** 2 + y ** 2)

def lm_distance(self):
self.landmark_distance_list = []
lm0 = self.landmark_world_list[0]
Expand Down Expand Up @@ -221,6 +226,15 @@ def checkFingersUp(self):
else:
upList.append(0)

# for i in range(1, 5):
# dis1 = self.distance2(self.landmark_list[fingerTipIndexs[i]], self.landmark_list[fingerTipIndexs[i] - 3])
# dis2 = self.distance2(self.landmark_list[fingerTipIndexs[i] - 2], self.landmark_list[fingerTipIndexs[i] - 3])
# diff = dis1 - dis2
# if diff > 0 and diff / dis1 > 0.35 and dis2 / dis1 > 0.35:
# upList.append(1)
# else:
# upList.append(0)

return upList

# 分析手指的位置,得到手势动作
Expand Down
3 changes: 2 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
crop1 = (int(crop1_arr[0]), int(crop1_arr[1]))
crop2_arr = config['common']['crop2'].split(",")
crop2 = (int(crop2_arr[0]), int(crop2_arr[1]))
trigger = int(config['common']['trigger'])
if not math.isclose(cap_width / cap_height, 1.68, abs_tol=0.1):
messagebox.showerror("错误", "摄像头分辨率长宽比必须为16:9或16:10")
exit()
Expand All @@ -50,7 +51,7 @@
pt1, pt2 = (100, 100), (1180, 620)

logging.info("读取配置完成")
control = VirtualMouse(index, rtsp, hand, show)
control = VirtualMouse(index, rtsp, hand, show, trigger)
control.recognize(cap_width, cap_height, cap_fps, cap_flip, crop1, crop2, w, h, pt1, pt2, smooth)
except:
logging.info("读取配置出错:" + traceback.format_exc())
Expand Down
5 changes: 3 additions & 2 deletions virtualMouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@

# 识别控制类
class VirtualMouse:
def __init__(self, index=0, rtsp="", hand="Right", show=False):
def __init__(self, index=0, rtsp="", hand="Right", show=False, trigger=0):
# image实例,以便另一个类调用
self.image = None
self.index = index
self.rtsp = rtsp
self.hand = hand
self.show = show
self.trigger = trigger

# 主函数
def recognize(self, cap_width, cap_height, cap_fps, cap_flip, crop1, crop2, w, h, pt1, pt2, smooth):
Expand Down Expand Up @@ -101,7 +102,7 @@ def recognize(self, cap_width, cap_height, cap_fps, cap_flip, crop1, crop2, w, h
# 通过手势识别得到手势动作,将其画在图像上显示
action_zh = handprocess.action_labels[action]

if key_point:
if key_point and self.trigger == 1:
x3 = np.interp(key_point[0], (pt1[0], pt2[0]), (0, wScr))
y3 = np.interp(key_point[1], (pt1[1], pt2[1]), (0, hScr))

Expand Down

0 comments on commit 938a58e

Please sign in to comment.