|
| 1 | +# OpenCV Capture Camera Video |
| 2 | +Use OpenCV and MediaPipe to detect hands. |
| 3 | +https://google.github.io/mediapipe/solutions/hands |
| 4 | + |
| 5 | +## Requirements |
| 6 | + |
| 7 | +1. It'll open the computer's camera and show the video in a window. |
| 8 | +2. When hands show in the video, it'll draw the hands landmarks. |
| 9 | +3. When you press "q" key it'll quit. |
| 10 | + |
| 11 | +## What will we practice in this project? |
| 12 | + |
| 13 | +- OpenCV: you need to install the `opencv-python` package via `pip install opencv-python` command in this project environment. |
| 14 | +- while loop |
| 15 | +- MediaPipe hands solution |
| 16 | + |
| 17 | +## A reference code |
| 18 | + |
| 19 | +### handUtils.py |
| 20 | +```python |
| 21 | +import cv2 |
| 22 | +import mediapipe as mp |
| 23 | + |
| 24 | +class HandDetector(): |
| 25 | + def __init__(self): |
| 26 | + self.hand_detector = mp.solutions.hands.Hands() |
| 27 | + self.drawer = mp.solutions.drawing_utils |
| 28 | + |
| 29 | + def process(self, img, draw=True): |
| 30 | + img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) |
| 31 | + self.hands_data = self.hand_detector.process(img_rgb) |
| 32 | + if draw: |
| 33 | + if self.hands_data.multi_hand_landmarks: |
| 34 | + for handlms in self.hands_data.multi_hand_landmarks: |
| 35 | + self.drawer.draw_landmarks(img, handlms, mp.solutions.hands.HAND_CONNECTIONS) |
| 36 | + |
| 37 | + def find_position(self, img): |
| 38 | + h, w, c = img.shape |
| 39 | + position = {'Left': {}, 'Right': {}} |
| 40 | + if self.hands_data.multi_hand_landmarks: |
| 41 | + i = 0 |
| 42 | + for point in self.hands_data.multi_handedness: |
| 43 | + score = point.classification[0].score |
| 44 | + if score >= 0.8: |
| 45 | + label = point.classification[0].label |
| 46 | + hand_lms = self.hands_data.multi_hand_landmarks[i].landmark |
| 47 | + for id, lm in enumerate(hand_lms): |
| 48 | + x, y = int(lm.x * w), int(lm.y * h) |
| 49 | + position[label][id] = (x, y) |
| 50 | + i = i + 1 |
| 51 | + return position |
| 52 | + |
| 53 | +``` |
| 54 | +### hand.py |
| 55 | +```python |
| 56 | +import cv2 |
| 57 | +from handUtils import HandDetector |
| 58 | + |
| 59 | +camera = cv2.VideoCapture(1) |
| 60 | +hand_detector = HandDetector() |
| 61 | + |
| 62 | +while True: |
| 63 | + success, img = camera.read() |
| 64 | + if success: |
| 65 | + img = cv2.flip(img, 1) |
| 66 | + hand_detector.process(img, draw=False) |
| 67 | + position = hand_detector.find_position(img) |
| 68 | + left_finger = position['Left'].get(8, None) |
| 69 | + if left_finger: |
| 70 | + cv2.circle(img, (left_finger[0], left_finger[1]),10, (0, 0, 255), cv2.FILLED) |
| 71 | + right_finger = position['Right'].get(8, None) |
| 72 | + if right_finger: |
| 73 | + cv2.circle(img, (right_finger[0], right_finger[1]),10, (0, 255, 0), cv2.FILLED) |
| 74 | + cv2.imshow('Video', img) |
| 75 | + k = cv2.waitKey(1) |
| 76 | + if k == ord('q'): |
| 77 | + break |
| 78 | + |
| 79 | +camera.release() |
| 80 | +cv2.destroyAllWindows() |
| 81 | + |
| 82 | +``` |
| 83 | + |
| 84 | +## Run the demo |
| 85 | +Install the OpenCV & MediaPipe: |
| 86 | +```shell |
| 87 | +pip install opencv-python mediapipe |
| 88 | +``` |
| 89 | +Please save the 2 Python files and run it: |
| 90 | + |
| 91 | +``` |
| 92 | +python hand.py |
| 93 | +``` |
| 94 | + |
| 95 | +---- |
| 96 | + |
| 97 | +# 使用OpenCV和MediaPipe进行手势识别 |
| 98 | + |
| 99 | +## 项目需求 |
| 100 | + |
| 101 | +- 运行程序会打开电脑摄像头,并显示捕捉到的视频 |
| 102 | +- 如果视频中出现了手,则显示手势连线 |
| 103 | +- 按`q`键会退出程序 |
| 104 | + |
| 105 | +## 项目练习 |
| 106 | + |
| 107 | +- 安装OpenCV,需要使用`pip install opencv-python mediapipe`将OpenCV和MediaPipe安装到项目环境中 |
| 108 | +- MediaPipe手势识别:https://google.github.io/mediapipe/solutions/hands |
| 109 | + |
| 110 | +## 项目参考代码 |
| 111 | + |
| 112 | +### handUtils.py |
| 113 | +```python |
| 114 | +import cv2 |
| 115 | +import mediapipe as mp |
| 116 | + |
| 117 | +class HandDetector(): |
| 118 | + def __init__(self): |
| 119 | + self.hand_detector = mp.solutions.hands.Hands() |
| 120 | + self.drawer = mp.solutions.drawing_utils |
| 121 | + |
| 122 | + def process(self, img, draw=True): |
| 123 | + img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) |
| 124 | + self.hands_data = self.hand_detector.process(img_rgb) |
| 125 | + if draw: |
| 126 | + if self.hands_data.multi_hand_landmarks: |
| 127 | + for handlms in self.hands_data.multi_hand_landmarks: |
| 128 | + self.drawer.draw_landmarks(img, handlms, mp.solutions.hands.HAND_CONNECTIONS) |
| 129 | + |
| 130 | + def find_position(self, img): |
| 131 | + h, w, c = img.shape |
| 132 | + position = {'Left': {}, 'Right': {}} |
| 133 | + if self.hands_data.multi_hand_landmarks: |
| 134 | + i = 0 |
| 135 | + for point in self.hands_data.multi_handedness: |
| 136 | + score = point.classification[0].score |
| 137 | + if score >= 0.8: |
| 138 | + label = point.classification[0].label |
| 139 | + hand_lms = self.hands_data.multi_hand_landmarks[i].landmark |
| 140 | + for id, lm in enumerate(hand_lms): |
| 141 | + x, y = int(lm.x * w), int(lm.y * h) |
| 142 | + position[label][id] = (x, y) |
| 143 | + i = i + 1 |
| 144 | + return position |
| 145 | + |
| 146 | +``` |
| 147 | +### hand.py |
| 148 | +```python |
| 149 | +import cv2 |
| 150 | +from handUtils import HandDetector |
| 151 | + |
| 152 | +camera = cv2.VideoCapture(1) |
| 153 | +hand_detector = HandDetector() |
| 154 | + |
| 155 | +while True: |
| 156 | + success, img = camera.read() |
| 157 | + if success: |
| 158 | + img = cv2.flip(img, 1) |
| 159 | + hand_detector.process(img, draw=False) |
| 160 | + position = hand_detector.find_position(img) |
| 161 | + left_finger = position['Left'].get(8, None) |
| 162 | + if left_finger: |
| 163 | + cv2.circle(img, (left_finger[0], left_finger[1]),10, (0, 0, 255), cv2.FILLED) |
| 164 | + right_finger = position['Right'].get(8, None) |
| 165 | + if right_finger: |
| 166 | + cv2.circle(img, (right_finger[0], right_finger[1]),10, (0, 255, 0), cv2.FILLED) |
| 167 | + cv2.imshow('Video', img) |
| 168 | + k = cv2.waitKey(1) |
| 169 | + if k == ord('q'): |
| 170 | + break |
| 171 | + |
| 172 | +camera.release() |
| 173 | +cv2.destroyAllWindows() |
| 174 | + |
| 175 | +``` |
| 176 | + |
| 177 | +## 测试运行 |
| 178 | + |
| 179 | +Install OpenCV and MediaPipe: |
| 180 | +```shell |
| 181 | +pip install opencv-python mediapipe |
| 182 | +``` |
| 183 | +保存上面2个代码并运行: |
| 184 | + |
| 185 | +``` |
| 186 | +python hand.py |
| 187 | +``` |
0 commit comments