用于跟踪面部并将数据发送到伺服系统的 Python 代码不起作用(缓慢且滞后或伺服系统不拾取数据)

问题描述 投票:0回答:0

我有跟踪你眼睛的 python 代码。

应该将坐标发送到 arduino,它控制跟随你的眼睛的伺服器。

(我想重现这个:https://www.youtube.com/watch?v=Q8zC3-ZQFJI

蟒蛇:

import face_recognition
import cv2
import re
import serial
import time

# send serial data to arduino (communicate python --> arduino)
ser = serial.Serial('COM3', 115200)  # 115200

data = ser.readline().decode().rstrip()
print(data)
# print(arduino.readline())

# Get a reference to webcam #0 (the default one)
video_capture = cv2.VideoCapture(0)

# Initialize some variables
face_locations = []
scaling = 0.5 # must be a float (X.Y)
# the factor to scale image back up by (to display correctly)
scaleUp = int(1 / scaling)


while True:
    # Grab a single frame of video
    ret, frame = video_capture.read()

    # Resize frame of video to 1/4 size for faster face detection processing
    small_frame = cv2.resize(frame, (0, 0), fx=scaling, fy=scaling)

    # Find all the faces and face encodings in the current frame of video
    face_locations = face_recognition.face_locations(small_frame)
    face_landmarks_list = face_recognition.face_landmarks(small_frame)

    # leftEyeMiddleX = 0
    # leftEyeMiddleY = 0

    for face_landmarks in face_landmarks_list:
        # Find left eye location, store in string
        for facial_feature in face_landmarks.keys():
            if facial_feature == "left_eye":
                leftEye = face_landmarks[facial_feature]

                # coordinates for top left of eye, bottom right of eye
                topLeft = str(leftEye[1])
                bottomRight = str(leftEye[4])

                # seperate all digits from the list
                # find 0th and 1st value (X coord & Y coord respectively)
                # convert that string to an int:
                # topLeft coordinates
                topLeftX = int((re.findall(r'\d+', topLeft))[0])  # X
                topLeftY = int((re.findall(r'\d+', topLeft))[1])  # Y
                # bottomRight coordinates
                bottomRightX = int((re.findall(r'\d+', bottomRight))[0])  # X
                bottomRightY = int((re.findall(r'\d+', bottomRight))[1])  # Y

                # find the middle of the two points: (X1 + X2) / 2, (Y1 + Y2) / 2 = (middleX, middleY)
                # convert to int since division by 2 can give a value w/ .5
                leftEyeMiddleX = int((topLeftX + bottomRightX)/2)  # middle x
                leftEyeMiddleY = int((topLeftY + bottomRightY)/2)  # middle y

                angle = leftEyeMiddleX
                ser.write(str(angle).encode())
                time.sleep(1.0)
                #data = ser.readline().decode().rstrip()
                #print(data)

                # send coords over to the arduino as serial data
                # leftEyeMiddleXS = str(leftEyeMiddleX)
                # arduino.write(str(leftEyeMiddleX).encode())
                # time.sleep(1)
                # arduino.write(str.encode(leftEyeMiddleY))
                # convert from int BACK to string and print the coordinates
                print(str(leftEyeMiddleX) + ", " + str(leftEyeMiddleY))

                for top, right, bottom, left in face_locations:
                    # Scale back up face locations since the frame we detected in was scaled to "scale" size
                    top *= scaleUp
                    right *= scaleUp
                    bottom *= scaleUp
                    left *= scaleUp

                    # Extract the region of the image that contains the face
                    face_image = frame[top:bottom, left:right]

                    face_image = cv2.rectangle(
                        frame, (left, top), (right, bottom), (0, 0, 255), 2)

                    # the circles need the coordinates scaled too
                    face_image = cv2.circle(
                        frame, (leftEyeMiddleX * scaleUp, leftEyeMiddleY * scaleUp), 3, (0, 0, 255), -1)
                    face_image = cv2.circle(
                        frame, (leftEyeMiddleX * scaleUp, leftEyeMiddleY * scaleUp), 1, (255, 255, 255), -1)
    # Display the resulting image
    cv2.imshow('shitter eye tracker (q = quit)', frame)

    # Hit 'q' on the keyboard to quit!
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# Release handle to the webcam
video_capture.release()
cv2.destroyAllWindows()

阿杜诺:

// void setup() {
//   Serial.begin(9600);
// }

// void loop() {
//   Serial.println("Hello, world!");
//   delay(1000);
// }

#include <Servo.h>

Servo myservo;

void setup() {
  myservo.attach(9);
  Serial.begin(115200);
  Serial.println("Arduino is online");
}

void loop() {
  if (Serial.available()) {
    int angle = Serial.parseInt();
    Serial.println(angle);
    myservo.write(angle);
  }
  delay(10);
}

什么有效:

  • 数据被发送到arduino

  • 伺服移动***

什么不起作用:

  • *** 如果 time.sleep() 值高,伺服会移动

    • 喜欢1.0(延迟1秒)

      • 这个问题是伺服变得超级迟钝,不能准确地跟随你的眼睛,并且没有反应
python c arduino face-recognition servo
© www.soinside.com 2019 - 2024. All rights reserved.