Compare commits
8
Commits
0b3c74c463
...
elisha
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
60d020fb1e | ||
|
|
e5e9354a51 | ||
|
|
35b185e3f5 | ||
|
|
096ee5143f | ||
|
|
e74f1b09d7 | ||
|
|
1b9a1b2c3d | ||
|
|
d3d6c647a6 | ||
|
|
b0f04daa78 |
@@ -52,24 +52,28 @@ cd gesture_rec
|
|||||||
git submodule update --init --recursive
|
git submodule update --init --recursive
|
||||||
```
|
```
|
||||||
### 2. Создание виртуального окружения
|
### 2. Создание виртуального окружения
|
||||||
Рекомендуется использовать виртуальное окружение и Python 3.11:
|
Рекомендуется использовать виртуальное окружение и Python 3.10:
|
||||||
```
|
```
|
||||||
python3.11 -m venv venv
|
python3.10 -m venv venv
|
||||||
source venv/bin/activate
|
source venv/bin/activate
|
||||||
```
|
```
|
||||||
|
|
||||||
Если у вас не скачен питон этой версии, сначала выполните:
|
Если у вас не скачен питон этой версии, сначала выполните:
|
||||||
```
|
```
|
||||||
sudo apt install python3.11 python3.11-venv
|
sudo apt install python3.10 python3.10-venv
|
||||||
```
|
```
|
||||||
|
|
||||||
Если у нас не устанавливается питон 3.11, то это потому что он отсутствует в официальных репозиториях по умолчанию, надо добавить репозиторий перед скачиванием:
|
Если у нас не устанавливается питон 3.10, то это потому что он отсутствует в официальных репозиториях по умолчанию, надо добавить репозиторий перед скачиванием:
|
||||||
```
|
```
|
||||||
sudo apt update && sudo apt install -y software-properties-common
|
sudo apt update && sudo apt install -y software-properties-common
|
||||||
sudo add-apt-repository ppa:deadsnakes/ppa
|
sudo add-apt-repository ppa:deadsnakes/ppa
|
||||||
sudo apt update
|
sudo apt update
|
||||||
```
|
```
|
||||||
|
|
||||||
|
To install pip:
|
||||||
|
|
||||||
|
sudo apt install -y python3-pip
|
||||||
|
|
||||||
### 3. Установка зависимостей
|
### 3. Установка зависимостей
|
||||||
|
|
||||||
### 3.1. Способ 1
|
### 3.1. Способ 1
|
||||||
@@ -86,6 +90,7 @@ sudo apt update
|
|||||||
pip install --upgrade pip
|
pip install --upgrade pip
|
||||||
pip install numpy==1.24.3
|
pip install numpy==1.24.3
|
||||||
pip install pandas==2.0.3
|
pip install pandas==2.0.3
|
||||||
|
pip install pyyaml
|
||||||
pip install opencv-python==4.12.0.88
|
pip install opencv-python==4.12.0.88
|
||||||
pip install opencv-python-headless==4.12.0.88
|
pip install opencv-python-headless==4.12.0.88
|
||||||
pip install matplotlib==3.7.5
|
pip install matplotlib==3.7.5
|
||||||
@@ -128,7 +133,7 @@ pip install protobuf==3.20.3
|
|||||||
|
|
||||||
2. Если будет проблема с функцией cv2.imshow() то, переустановите cv2 без заголовков:
|
2. Если будет проблема с функцией cv2.imshow() то, переустановите cv2 без заголовков:
|
||||||
```
|
```
|
||||||
pip uninstall opencv-python pip install opencv-python-headless
|
pip uninstall opencv-python opencv-python-headless
|
||||||
pip install opencv-python==4.12.0.88
|
pip install opencv-python==4.12.0.88
|
||||||
```
|
```
|
||||||
3. Если будет ошибка с PIL, попробуйте обновить библиотеку:
|
3. Если будет ошибка с PIL, попробуйте обновить библиотеку:
|
||||||
|
|||||||
@@ -92,7 +92,7 @@ class ArmController:
|
|||||||
|
|
||||||
disp = shoulder[1] - wrist[1]
|
disp = shoulder[1] - wrist[1]
|
||||||
return disp / torso_height
|
return disp / torso_height
|
||||||
|
'''
|
||||||
def compute_speeds(self, landmarks):
|
def compute_speeds(self, landmarks):
|
||||||
if (landmarks[11][3] < 0.5 or landmarks[12][3] < 0.5 or
|
if (landmarks[11][3] < 0.5 or landmarks[12][3] < 0.5 or
|
||||||
landmarks[15][3] < 0.5 or landmarks[16][3] < 0.5):
|
landmarks[15][3] < 0.5 or landmarks[16][3] < 0.5):
|
||||||
@@ -116,11 +116,95 @@ class ArmController:
|
|||||||
linear = min(lin_rel, 1.0) * self.config['max_speed_linear']
|
linear = min(lin_rel, 1.0) * self.config['max_speed_linear']
|
||||||
|
|
||||||
# Угловая скорость
|
# Угловая скорость
|
||||||
if abs(ang_rel) < self.dead_zone:
|
if abs(ang_rel) < self.dead_zone:norm_lin * self.config['max_speed_linear']
|
||||||
angular = 0.0
|
angular = 0.0
|
||||||
else:
|
else:
|
||||||
ang_rel_clipped = np.clip(ang_rel, -1.0, 1.0)
|
ang_rel_clipped = np.clip(ang_rel, -1.0, 1.0)
|
||||||
angular = ang_rel_clipped * self.config['max_speed_angular']
|
angular = ang_rel_clipped * self.config['max_speed_angular']
|
||||||
|
|
||||||
return linear, angular
|
return linear, angular
|
||||||
|
'''
|
||||||
|
|
||||||
|
def _get_angle(self, landmark1, landmark2):
|
||||||
|
x1 = landmark1[0]
|
||||||
|
y1 = landmark1[1]
|
||||||
|
x2 = landmark2[0]
|
||||||
|
y2 = landmark2[1]
|
||||||
|
angle = np.arctan2(y2 - y1, x2 - x1)
|
||||||
|
return angle
|
||||||
|
|
||||||
|
def compute_speeds(self, landmarks):
|
||||||
|
if (landmarks[11][3] < 0.5 or landmarks[12][3] < 0.5 or
|
||||||
|
landmarks[15][3] < 0.5 or landmarks[16][3] < 0.5):
|
||||||
|
if self.debug:
|
||||||
|
print("Руки не видны")
|
||||||
|
return 0.0, 0.0
|
||||||
|
|
||||||
|
#right_shoulder_y = landmarks[12][1]
|
||||||
|
#right_hand_y = landmarks[16][1]
|
||||||
|
#right_distance = right_shoulder_y - right_hand_y
|
||||||
|
|
||||||
|
angle_elbow_wirst_left = self._get_angle(landmarks[14], landmarks[16])
|
||||||
|
angle_elbow_wirst_right = self._get_angle(landmarks[13], landmarks[15])
|
||||||
|
linear, angular = 0, 0
|
||||||
|
|
||||||
|
if np.deg2rad(-5) < angle_elbow_wirst_right < np.deg2rad(5):
|
||||||
|
linear = 0
|
||||||
|
|
||||||
|
elif np.deg2rad(-5) > angle_elbow_wirst_right:
|
||||||
|
linear = min(1, angle_elbow_wirst_right / -np.deg2rad(90))
|
||||||
|
|
||||||
|
elif np.deg2rad(5) < angle_elbow_wirst_right:
|
||||||
|
linear = min(1, angle_elbow_wirst_right / -np.deg2rad(90))
|
||||||
|
|
||||||
|
#print(f"Test: {linear} {angle_elbow_wirst_left}")
|
||||||
|
|
||||||
|
error = 15
|
||||||
|
|
||||||
|
#print(f"Test: {angle_elbow_wirst_right} {np.rad2deg(angle_elbow_wirst_right)}")
|
||||||
|
|
||||||
|
#if np.deg2rad(180 - error) < angle_elbow_wirst_left < np.deg2rad(180 + error):
|
||||||
|
|
||||||
|
max_angle = np.deg2rad(180 - error)
|
||||||
|
min_angle = np.deg2rad(-180 + error)
|
||||||
|
|
||||||
|
if angle_elbow_wirst_left < 0:
|
||||||
|
|
||||||
|
if angle_elbow_wirst_left < min_angle:# or angle_elbow_wirst_left > max_angle:
|
||||||
|
angular = 0
|
||||||
|
elif angle_elbow_wirst_left > min_angle:
|
||||||
|
#angular = min(1, angle_elbow_wirst_left / min_angle)
|
||||||
|
angular = -(min_angle - angle_elbow_wirst_left) / (min_angle - np.deg2rad(-90))
|
||||||
|
|
||||||
|
else:
|
||||||
|
if angle_elbow_wirst_left > max_angle:
|
||||||
|
angular = 0
|
||||||
|
elif angle_elbow_wirst_left < max_angle:
|
||||||
|
#angular = -min(1, angle_elbow_wirst_left / max_angle)
|
||||||
|
angular = (max_angle - angle_elbow_wirst_left) / (max_angle - np.deg2rad(90))
|
||||||
|
|
||||||
|
error = 35
|
||||||
|
if -np.deg2rad(error) < angle_elbow_wirst_left < np.deg2rad(error):
|
||||||
|
linear = 0
|
||||||
|
angular = 0
|
||||||
|
|
||||||
|
#angular = angular / 2
|
||||||
|
|
||||||
|
|
||||||
|
#left_shoulder_y = landmarks[11][1]
|
||||||
|
#left_hand_y = landmarks[15][1]
|
||||||
|
#left_distance = left_shoulder_y - left_hand_y
|
||||||
|
|
||||||
|
#torso_height = self._get_torso_height(landmarks)
|
||||||
|
#max_distance = torso_height * 0.5
|
||||||
|
#norm_lin = np.clip((right_distance/max_distance), a_min = 0, a_max = 1)
|
||||||
|
|
||||||
|
#torso_height = self._get_torso_height(landmarks)
|
||||||
|
#max_distance = torso_height * 0.5
|
||||||
|
#norm_ang = np.clip((left_distance/max_distance), a_min = -1, a_max = 1)
|
||||||
|
|
||||||
|
#linear = norm_lin * self.config['max_speed_linear']
|
||||||
|
#angular = norm_ang * self.config['max_speed_linear']
|
||||||
|
|
||||||
|
return linear, -angular
|
||||||
|
#return angular, linear
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
from ml_gestures.predict import MLGesturePredictor
|
|
||||||
|
|
||||||
class SpecialGestureDetector:
|
class SpecialGestureDetector:
|
||||||
def __init__(self, mode='geometric', model_path=None, class_names=None):
|
def __init__(self, mode='geometric', model_path=None, class_names=None):
|
||||||
self.mode = mode
|
self.mode = mode
|
||||||
if mode == 'ml':
|
if mode == 'ml':
|
||||||
|
from ml_gestures.predict import MLGesturePredictor
|
||||||
if model_path is None or class_names is None:
|
if model_path is None or class_names is None:
|
||||||
raise ValueError("Для ML нужны model_path и class_names")
|
raise ValueError("Для ML нужны model_path и class_names")
|
||||||
self.ml_predictor = MLGesturePredictor(model_path, class_names)
|
self.ml_predictor = MLGesturePredictor(model_path, class_names)
|
||||||
@@ -54,7 +54,7 @@ class SpecialGestureDetector:
|
|||||||
l_hip = landmarks[idx['left_hip']][:2]
|
l_hip = landmarks[idx['left_hip']][:2]
|
||||||
r_hip = landmarks[idx['right_hip']][:2]
|
r_hip = landmarks[idx['right_hip']][:2]
|
||||||
nose = np.array(landmarks[idx['nose']][:2])
|
nose = np.array(landmarks[idx['nose']][:2])
|
||||||
|
|
||||||
shoulder_center_y = (l_sh[1] + r_sh[1]) / 2
|
shoulder_center_y = (l_sh[1] + r_sh[1]) / 2
|
||||||
hip_center_y = (l_hip[1] + r_hip[1]) / 2
|
hip_center_y = (l_hip[1] + r_hip[1]) / 2
|
||||||
torso_height = hip_center_y - shoulder_center_y
|
torso_height = hip_center_y - shoulder_center_y
|
||||||
@@ -74,11 +74,30 @@ class SpecialGestureDetector:
|
|||||||
v2 = wrist - elbow
|
v2 = wrist - elbow
|
||||||
return angle_between_vectors(v1, v2)
|
return angle_between_vectors(v1, v2)
|
||||||
|
|
||||||
|
def segments_intersect(p1, p2, p3, p4):
|
||||||
|
def cross(o, a, b):
|
||||||
|
return (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o [1])* (b[0] - o[0])
|
||||||
|
d1 = cross(p3, p4, p1)
|
||||||
|
d2 = cross(p3, p4, p2)
|
||||||
|
d3 = cross(p1, p2, p3)
|
||||||
|
d4 = cross(p1, p2, p4)
|
||||||
|
return (d1 * d2 < 0) and (d3 * d4 < 0)
|
||||||
|
|
||||||
|
def line_intersection(p1, p2, p3, p4):
|
||||||
|
d1 = p2 - p1
|
||||||
|
d2 = p4 -p3
|
||||||
|
denom = d1[0] * d2[1] - d1[1] * d2[0]
|
||||||
|
if abs(denom) < 1e-6:
|
||||||
|
return None
|
||||||
|
t = ((p3[0] - p1[0]) * d2[1] - (p3[1] - p1[1]) * d2[0]) / denom
|
||||||
|
return p1 + t * d1
|
||||||
|
|
||||||
# ---- Вычисляем углы ----
|
# ---- Вычисляем углы ----
|
||||||
l_angle = elbow_angle(l_sh, l_el, l_wr) # угол в левом локте
|
l_angle = elbow_angle(l_sh, l_el, l_wr) # угол в левом локте
|
||||||
r_angle = elbow_angle(r_sh, r_el, r_wr) # угол в правом локте
|
r_angle = elbow_angle(r_sh, r_el, r_wr) # угол в правом локте
|
||||||
|
|
||||||
# ---- КРЕСТ ----
|
# ---- КРЕСТ ----
|
||||||
|
'''
|
||||||
# 1. Оба локтя сильно согнуты (< 100°)
|
# 1. Оба локтя сильно согнуты (< 100°)
|
||||||
elbows_bent = (l_angle < 100 and r_angle < 100)
|
elbows_bent = (l_angle < 100 and r_angle < 100)
|
||||||
# 2. Левое запястье правее правого (перекрест)
|
# 2. Левое запястье правее правого (перекрест)
|
||||||
@@ -90,6 +109,15 @@ class SpecialGestureDetector:
|
|||||||
)
|
)
|
||||||
|
|
||||||
cross = elbows_bent and wrists_crossed and wrists_at_chest
|
cross = elbows_bent and wrists_crossed and wrists_at_chest
|
||||||
|
if cross:
|
||||||
|
return 'cross'
|
||||||
|
'''
|
||||||
|
forearms_cross = segments_intersect(l_el, l_wr, r_el, r_wr)
|
||||||
|
intersection = line_intersection(l_el, l_wr, r_el, r_wr)
|
||||||
|
intersection_on_chest = False
|
||||||
|
if intersection is not None:
|
||||||
|
intersection_on_chest = (shoulder_center_y - 0.2 * torso_height < intersection[1] < hip_center_y + 0.2 * torso_height)
|
||||||
|
cross = forearms_cross and intersection_on_chest
|
||||||
if cross:
|
if cross:
|
||||||
return 'cross'
|
return 'cross'
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ pip install --upgrade pip
|
|||||||
|
|
||||||
pip install numpy==1.24.3
|
pip install numpy==1.24.3
|
||||||
pip install pandas==2.0.3
|
pip install pandas==2.0.3
|
||||||
|
pip install pyyaml==6.0.3
|
||||||
pip install opencv-python==4.12.0.88
|
pip install opencv-python==4.12.0.88
|
||||||
pip install opencv-python-headless==4.12.0.88
|
pip install opencv-python-headless==4.12.0.88
|
||||||
pip install matplotlib==3.7.5
|
pip install matplotlib==3.7.5
|
||||||
|
|||||||
Reference in New Issue
Block a user