伺服电机无法正常使用倾斜仪进行面部跟踪

时间:2019-07-02 12:31:31

标签: python opencv iot servo

我已经使用伺服马达来倾斜以跟踪脸部 但问题是伺服系统没有直接连接到覆盆子 相反,我使用16通道adafruit pca9685来控制伺服器 教授树莓派面部跟踪的所有教程均未使用此类渠道,因此我试图找出自己的实现方法

for frame in camera.capture_continuous(rawCapture, format = 'bgr', use_video_port = True):
        image = frame.array

        resized = cv2.resize(image, (320, 240))
        gray = cv2.cvtColor(resized,cv2.COLOR_BGR2GRAY)

        faces = face_cascade.detectMultiScale(gray, 1.1, 5)
        if len(faces) > 0:
            for (x, y, w, h) in faces:
                # Track(pan, tilt, Point(x + w/2.0, y+ h/2.0))
                break
        faces_resized = [(int(scale[0]*x), int(scale[1]*y), int(scale[0]*w), int(scale[1]*h)) for (x, y, w, h) in faces]
        for (x,y,w,h) in faces_resized:
            cv2.rectangle(image,(x,y),(x+w,y+h),(255,255,0),2)

对于这一部分,代码是绝对清晰的,可以很好地检测到人脸,但是我想使用它来跟踪和更改伺服电机的位置

我只需要实现它的代码 我的adafruit板代码结构

            pwm.set_pwm(0, 0,cam_pan)
0 for index of channels 
0 min angle

cam_pan应该移动的角度

考虑到这些参数,我只想控制我的伺服/摆角的移动以相应地移动

我试图通过此方法控制电机

x = x + (w/2)
        y = y + (h/2)

        # Correct relative to center of image
        turn_x  = float(x - (FRAME_W/2))
        turn_y  = float(y - (FRAME_H/2))

        # Convert to percentage offset
        turn_x  /= float(FRAME_W/2)
        turn_y  /= float(FRAME_H/2)

        # Scale offset to degrees
    turn_x   = 2 # VFOV
    turn_y   = 2 # HFOV
    if x > turn_x:
        cam_pan  += -turn_x
        pwm.set_pwm(0, 0,cam_pan)
    elif x < turn_x:

        cam_pan  += +turn_x
        pwm.set_pwm(0, 0,cam_pan)

但伺服电机只是开始有点抽搐,这毫无用处

from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
# from pisoc import *
import Adafruit_PCA9685
pwm = Adafruit_PCA9685.PCA9685()
FRAME_W = 180
FRAME_H = 100
cam_pan = 90
cam_tilt = 60

if __name__ == "__main__":

    camera = PiCamera()
    camera.resolution = (640, 480)
    camera.framerate = 32
    rawCapture = PiRGBArray(camera, size = camera.resolution)

    face_cascade = cv2.CascadeClassifier('/usr/local/share/OpenCV/lbpcascades/lbpcascade_frontalface.xml')

    scale = (camera.resolution[0]/320.0, camera.resolution[1]/240.0)

    time.sleep(0.1)

    for frame in camera.capture_continuous(rawCapture, format = 'bgr', use_video_port = True):
        image = frame.array

        resized = cv2.resize(image, (320, 240))
        gray = cv2.cvtColor(resized,cv2.COLOR_BGR2GRAY)

        faces = face_cascade.detectMultiScale(gray, 1.1, 5)
        if len(faces) > 0:
            for (x, y, w, h) in faces:
                # Track(pan, tilt, Point(x + w/2.0, y+ h/2.0))
                break
        faces_resized = [(int(scale[0]*x), int(scale[1]*y), int(scale[0]*w), int(scale[1]*h)) for (x, y, w, h) in faces]
        for (x,y,w,h) in faces_resized:
            cv2.rectangle(image,(x,y),(x+w,y+h),(255,255,0),2)
            # Track first face

            # Get the center of the face
            x = x + (w/2)
            y = y + (h/2)

            # Correct relative to center of image
            turn_x  = float(x - (FRAME_W/2))
            turn_y  = float(y - (FRAME_H/2))

            # Convert to percentage offset
            turn_x  /= float(FRAME_W/2)
            turn_y  /= float(FRAME_H/2)

            # Scale offset to degrees
        turn_x   = 2 # VFOV
        turn_y   = 2 # HFOV
        if x > turn_x:
            cam_pan  += -turn_x
            pwm.set_pwm(0, 0,cam_pan)
        elif x < turn_x:

            cam_pan  += +turn_x
            pwm.set_pwm(0, 0,cam_pan)
        # cam_tilt += turn_y


        # Clamp Pan/Tilt to 0 to 180 degrees
        # cam_pan = max(0,min(180,cam_pan))
        # cam_tilt = max(0,min(180,cam_tilt))

        # Update the servos
        # pwm.set_pwm(0, 0,cam_pan)

            # print(cam_pan-90, cam_tilt-90)
            #
            # # Clamp Pan/Tilt to 0 to 180 degrees
            # cam_pan = max(0,min(180,cam_pan))
            # # cam_tilt = max(0,min(180,cam_tilt))
            #
            # # Update the servos
            # pwm.set_pwm(0, 0,cam_pan)
            #
            # # pan(int(cam_pan-90))
            # # tilt(int(cam_tilt-90))

            break


        cv2.imshow("Result", image)
        key = cv2.waitKey(1) & 0xFF

        rawCapture.truncate(0)

        if key == ord('q') or key == 27:
            break

这是完整的代码

我不知道问题到底是什么 我什至不确定问题是否出在代码部分 但是我只想根据脸部改变

0 个答案:

没有答案