Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
순건 노
hand-window
Commits
d4fb40d3
Commit
d4fb40d3
authored
3 years ago
by
Nor-s
Browse files
Options
Download
Email Patches
Plain Diff
Add hand tracking python src
parent
c7561dde
main
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
pysrc/hand.py
+76
-0
pysrc/hand.py
with
76 additions
and
0 deletions
+76
-0
pysrc/hand.py
0 → 100644
View file @
d4fb40d3
import
cv2
import
mediapipe
as
mp
# shared memory
import
mmap
import
struct
# 랜드마크를 그리기 위한 변수
mp_drawing
=
mp
.
solutions
.
drawing_utils
# 손을 처리하기 위한 변수
mp_drawing_styles
=
mp
.
solutions
.
drawing_styles
# 랜드마크 표시 스타일
mp_hands
=
mp
.
solutions
.
hands
# For webcam input:
cap
=
cv2
.
VideoCapture
(
0
)
width
=
cap
.
get
(
cv2
.
CAP_PROP_FRAME_WIDTH
)
height
=
cap
.
get
(
cv2
.
CAP_PROP_FRAME_HEIGHT
)
print
(
'original size: %d, %d'
%
(
width
,
height
))
cap
.
set
(
cv2
.
CAP_PROP_FRAME_WIDTH
,
width
/
3
)
cap
.
set
(
cv2
.
CAP_PROP_FRAME_HEIGHT
,
height
/
3
)
width
=
cap
.
get
(
cv2
.
CAP_PROP_FRAME_WIDTH
)
height
=
cap
.
get
(
cv2
.
CAP_PROP_FRAME_HEIGHT
)
print
(
'changed size: %d, %d'
%
(
width
,
height
))
with
mp_hands
.
Hands
(
model_complexity
=
0
,
max_num_hands
=
1
,
min_detection_confidence
=
0.5
,
# 손가락 감지 최소 신뢰성(1.0에 가까울수록 신뢰성이 높은 것만 인식)
min_tracking_confidence
=
0.5
# 손가락 추적 최소 신뢰성(1.0에 가까울수록 추적율이 높은 것만 인식)
)
as
hands
:
while
cap
.
isOpened
():
success
,
image
=
cap
.
read
()
if
not
success
:
print
(
"Ignoring empty camera frame."
)
# If loading a video, use 'break' instead of 'continue'.
continue
# To improve performance, optionally mark the image as not writeable to
# pass by reference.
image
.
flags
.
writeable
=
False
# 사진을 좌/우 반전 시킨후 BGR에서 RGB로 변환
# image = cv2.cvtColor(cv2.flip(image, 1), cv2.COLOR_BGR2RGB)
image
=
cv2
.
cvtColor
(
image
,
cv2
.
COLOR_BGR2RGB
)
# 손 인식 결과
results
=
hands
.
process
(
image
)
# Draw the hand annotations on the image.
image
.
flags
.
writeable
=
True
# 이미지를 다시 RGB에서 BGR로 변경
image
=
cv2
.
cvtColor
(
image
,
cv2
.
COLOR_RGB2BGR
)
if
results
.
multi_hand_landmarks
:
for
hand_landmarks
in
results
.
multi_hand_landmarks
:
# ipc 공유메모리를 위한 변수
shm
=
mmap
.
mmap
(
0
,
512
,
"Local
\\
MPipe"
)
if
shm
:
for
idx
in
range
(
0
,
21
):
shm
.
write
(
struct
.
pack
(
'f'
,
hand_landmarks
.
landmark
[
idx
].
x
))
shm
.
write
(
struct
.
pack
(
'f'
,
hand_landmarks
.
landmark
[
idx
].
y
))
shm
.
write
(
struct
.
pack
(
'f'
,
hand_landmarks
.
landmark
[
idx
].
z
))
mp_drawing
.
draw_landmarks
(
image
,
hand_landmarks
,
mp_hands
.
HAND_CONNECTIONS
,
mp_drawing_styles
.
get_default_hand_landmarks_style
(),
mp_drawing_styles
.
get_default_hand_connections_style
())
# Flip the image horizontally for a selfie-view display.
cv2
.
imshow
(
'MediaPipe Hands'
,
cv2
.
flip
(
image
,
1
))
if
cv2
.
waitKey
(
5
)
&
0xFF
==
27
:
break
cap
.
release
()
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Projects
Groups
Snippets
Help