Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions 08.OpenCV-Mediapipe-DEMO/app-hands.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import mediapipe as mp
import time

cap = cv2.VideoCapture(1)
cap = cv2.VideoCapture(0)
mpHands = mp.solutions.hands

hands = mpHands.Hands(min_detection_confidence=0.5, min_tracking_confidence=0.5)
Expand Down Expand Up @@ -42,5 +42,8 @@

cv2.imshow('img', img)

if cv2.waitKey(1) == ord('q'):
break
if cv2.waitKey(1) == ord('q') or cv2.waitKey(1) == 27:
break

cap.release()
cv2.destroyAllWindows()
9 changes: 6 additions & 3 deletions 08.OpenCV-Mediapipe-DEMO/app-holistic.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import cv2
import mediapipe as mp
import numpy as np

mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_holistic = mp.solutions.holistic

# For static images:
IMAGE_FILES = []
BG_COLOR = (192, 192, 192) # gray
with mp_holistic.Holistic(
static_image_mode=True,
model_complexity=2,
Expand Down Expand Up @@ -53,7 +55,7 @@
results.pose_world_landmarks, mp_holistic.POSE_CONNECTIONS)

# For webcam input:
cap = cv2.VideoCapture(1)
cap = cv2.VideoCapture(0)
with mp_holistic.Holistic(
min_detection_confidence=0.5,
min_tracking_confidence=0.5) as holistic:
Expand Down Expand Up @@ -88,6 +90,7 @@
.get_default_pose_landmarks_style())
# Flip the image horizontally for a selfie-view display.
cv2.imshow('MediaPipe Holistic', cv2.flip(image, 1))
if cv2.waitKey(5) & 0xFF == 27:
if cv2.waitKey(5) & 0xFF == 27 or cv2.waitKey(5) == ord('q'):
break
cap.release()
cap.release()
cv2.destroyAllWindows()
9 changes: 6 additions & 3 deletions 08.OpenCV-Mediapipe-DEMO/app-pose.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import cv2
import mediapipe as mp
import numpy as np

mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_pose = mp.solutions.pose
Expand Down Expand Up @@ -46,7 +48,7 @@
results.pose_world_landmarks, mp_pose.POSE_CONNECTIONS)

# For webcam input:
cap = cv2.VideoCapture(1)
cap = cv2.VideoCapture(0)
with mp_pose.Pose(
min_detection_confidence=0.5,
min_tracking_confidence=0.5) as pose:
Expand All @@ -73,6 +75,7 @@
landmark_drawing_spec=mp_drawing_styles.get_default_pose_landmarks_style())
# Flip the image horizontally for a selfie-view display.
cv2.imshow('MediaPipe Pose', cv2.flip(image, 1))
if cv2.waitKey(5) & 0xFF == 27:
if cv2.waitKey(5) & 0xFF == 27 or cv2.waitKey(5) == ord('q'):
break
cap.release()
cap.release()
cv2.destroyAllWindows()
3 changes: 2 additions & 1 deletion 08.OpenCV-Mediapipe-DEMO/handle-hand-demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,10 @@ def detect():
center_y = int(keypoint_pos[node][1])
frame = mosaic(frame, [center_x - 15 , center_y - 10], [center_x + 30, center_y + 50])
cv2.imshow('MediaPipe Hands', frame)
if cv2.waitKey(1) & 0xFF == 27:
if cv2.waitKey(1) & 0xFF == 27 or cv2.waitKey(1) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()

if __name__ == '__main__':
detect()
Expand Down
14 changes: 10 additions & 4 deletions 08.OpenCV-Mediapipe-DEMO/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@


## OpenCV + MediaPipe
- 建議使用pipenv虛擬環境,本案例採python 3.8,相依外部套件可透過`pipenv sync`將`Pipfile.lock`內鎖定的相依套件進行安裝
- 建議使用pipenv虛擬環境,本案例採python 3.10以上,相依外部套件可透過`pipenv install`安裝所需套件

```
pipenv --python 3.10
pipenv sync
pipenv install
```

- pipenv相關命令:
Expand All @@ -20,14 +20,20 @@


- 執行程式:
`pipenv shell` 進入虛擬環境後,選擇自己想執行的程式開啟,開啟後按`esc`或`q`離開。
`pipenv shell` 進入虛擬環境後,選擇自己想執行的程式開啟,開啟後按`ESC`或`q`離開。

```
```bash
python app-hands.py #手部辨識
python app-holistic.py #肢體辨識
python app-pose.py #姿態辨識
python handle-hand-demo.py #手勢辨識(愛心/中指)
```

- 注意事項:
- 程式預設使用編號0的攝影機(通常是主要攝影機)
- 如果有多個攝影機,可以在程式碼中修改`cv2.VideoCapture(0)`的數字
- 執行時需要允許程式存取攝影機權限



## 參考:
Expand Down
Loading