I want to draw a circle on my index finger. I need to process the coordinates of index finger. The landmark is 8, as stated on their website. I can read the x, and y of landmark 8 by
cap = cv2.VideoCapture(0)
with mp_hands.Hands(
model_complexity=0,
min_detection_confidence=0.5,
min_tracking_confidence=0.5) 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
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
results = hands.process(image)
# Draw the hand annotations on the image.
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
if results.multi_hand_landmarks:
x = results.multi_hand_world_landmarks[0].landmark[8].x
y = results.multi_hand_world_landmarks[0].landmark[8].y
These x, and y are normalized values. So after googling, I found a solution that says to add image width and height to x, and y. But doing so doesn't solve my problem. I still see negative values and if I draw, it goes beyond the image scene. Here is the .py file and also the snipped below;
cap = cv2.VideoCapture(0)
with mp_hands.Hands(
model_complexity=0,
min_detection_confidence=0.5,
min_tracking_confidence=0.5) 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
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
results = hands.process(image)
# Draw the hand annotations on the image.
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
if results.multi_hand_landmarks:
x = results.multi_hand_world_landmarks[0].landmark[8].x
y = results.multi_hand_world_landmarks[0].landmark[8].y
z = results.multi_hand_world_landmarks[0].landmark[8].z
shape = image.shape
relative_x = int(x * shape[1])
relative_y = int(y * shape[0])
# Center coordinates
# center_coordinates = (results.multi_hand_world_landmarks[0].landmark[4].x, results.multi_hand_world_landmarks[0].landmark[4].y)
print(relative_x, relative_y, x, y, z)
# Radius of circle
radius = 20
# Blue color in BGR
color = (255, 0, 0)
# Line thickness of 2 px
thickness = 2
# Using cv2.circle() method
# Draw a circle with blue line borders of thickness of 2 px
image = cv2.circle(image, (relative_x, relative_y), radius, color, thickness)