除了它不打开我的摄像头来获取我的面部坐标之外,它正在运行。

huangapple go评论91阅读模式
英文:

Is working except it isnt opening my camera to get my face cords

问题

  1. import cv2
  2. import mediapipe as mp
  3. import pyautogui as py
  4. cam = cv2.VideoCapture(0)
  5. face_mesh = mp.solutions.face_mesh.FaceMesh(refine_landmarks=True)
  6. while True:
  7. _, frame = cam.read()
  8. frame = cv2.flip(frame, 1)
  9. frameRGB = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # 修复RGB颜色转换问题
  10. output = face_mesh.process(frameRGB)
  11. face_landmark = output.multi_face_landmarks
  12. frame_w, frame_h, _ = frame.shape
  13. if face_landmark:
  14. landmarks = face_landmark[0].landmark
  15. for landmark in enumerate(landmarks[474:478]):
  16. x = int(landmark.x * frame_w)
  17. y = int(landmark.y * frame_h)
  18. cv2.circle(frame, (x, y), 3, (0, 255, 0))
  19. if id == 1:
  20. py.moveTo(x, y)
  21. print(x, y)
  22. cv2.imshow('Lazy mouse', frame)
  23. cv2.waitKey(1)
英文:
  1. import cv2
  2. import mediapipe as mp
  3. import pyautogui as py
  4. cam = cv2.VideoCapture(0)
  5. face_mesh = mp.solutions.face_mesh.FaceMesh(refine_landmarks=True)
  6. while True:
  7. _, frame = cam.read()
  8. frame = cv2.flip(frame, 1)
  9. '''frameRGB = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)'''
  10. output = face_mesh.process(frameRGB)
  11. face_landmark = output.multi_face_landmarks
  12. frame_w, frame_h, _ = frame.shape
  13. '''if face_landmark:
  14. landmarks = face_landmark[0].landmark
  15. for landmark in enumerate(landmarks[474:478]):
  16. x = int(landmark.x * frame_w)
  17. y = int(landmark.y * frame_h)
  18. cv2.circle(frame, (x, y), 3, (0, 255, 0))
  19. if id == 1:
  20. py.moveTo(x, y)'''
  21. print(x, y)
  22. cv2.imshow('Lazy mouse', frame)
  23. 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 内置函数,它返回一个包含元组的序列。

替代以下代码:

  1. for landmark in enumerate(landmarks[474:478]): # 错误的写法

你应该使用以下写法之一:

  1. for landmark in landmarks[474:478]:

或者

  1. 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

  1. for landmark in enumerate(landmarks[474:478]): # wrong

you should use

  1. for landmark in landmarks[474:478]:

or

  1. 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

huangapple
  • 本文由 发表于 2023年2月6日 14:16:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75357917.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定