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

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

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

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:

确定