英文:
Is working except it isnt opening my camera to get my face cords
问题
import cv2
import mediapipe as mp
import pyautogui as py
cam = cv2.VideoCapture(0)
face_mesh = mp.solutions.face_mesh.FaceMesh(refine_landmarks=True)
while True:
_, frame = cam.read()
frame = cv2.flip(frame, 1)
frameRGB = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # 修复RGB颜色转换问题
output = face_mesh.process(frameRGB)
face_landmark = output.multi_face_landmarks
frame_w, frame_h, _ = frame.shape
if face_landmark:
landmarks = face_landmark[0].landmark
for landmark in enumerate(landmarks[474:478]):
x = int(landmark.x * frame_w)
y = int(landmark.y * frame_h)
cv2.circle(frame, (x, y), 3, (0, 255, 0))
if id == 1:
py.moveTo(x, y)
print(x, y)
cv2.imshow('Lazy mouse', frame)
cv2.waitKey(1)
英文:
import cv2
import mediapipe as mp
import pyautogui as py
cam = cv2.VideoCapture(0)
face_mesh = mp.solutions.face_mesh.FaceMesh(refine_landmarks=True)
while True:
_, frame = cam.read()
frame = cv2.flip(frame, 1)
'''frameRGB = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)'''
output = face_mesh.process(frameRGB)
face_landmark = output.multi_face_landmarks
frame_w, frame_h, _ = frame.shape
'''if face_landmark:
landmarks = face_landmark[0].landmark
for landmark in enumerate(landmarks[474:478]):
x = int(landmark.x * frame_w)
y = int(landmark.y * frame_h)
cv2.circle(frame, (x, y), 3, (0, 255, 0))
if id == 1:
py.moveTo(x, y)'''
print(x, y)
cv2.imshow('Lazy mouse', frame)
cv2.waitKey(1)
it's printing where my head is for x, y, isnt showing me a camera with my face I believe it has to do with the frame or with the RGB I tried debugging but no luck, highlighted where I believe are the problems
答案1
得分: 0
enumerate()
是 Python 内置函数,它返回一个包含元组的序列。
替代以下代码:
for landmark in enumerate(landmarks[474:478]): # 错误的写法
你应该使用以下写法之一:
for landmark in landmarks[474:478]:
或者
for (index, landmark) in enumerate(landmarks[474:478]):
index
是子列表/切片中的索引,因此你会得到索引 0, 1, 2, 3
。
英文:
enumerate()
is a Python built-in function that returns a sequence of tuples.
Instead of
for landmark in enumerate(landmarks[474:478]): # wrong
you should use
for landmark in landmarks[474:478]:
or
for (index, landmark) in enumerate(landmarks[474:478]):
index
is the index into the sublist/slice, so you will get indices 0,1,2,3
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论