Pan Tilt机制

时间:2014-11-04 01:51:46

标签: python opencv arduino

我是初学者,我在opencv和python方面有几个月的经验。我正在尝试项目。我正在使用python,opencv和ubuntu。我发现这个代码(下面),我想调用它的平移倾斜机制并使用arduino和servos ..我如何定义伺服位置?代码可以任何人指导我完成这个..我已经参考了许多例子,但我没有在这段代码中实现它..

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import cv2, math
import numpy as np
import serial
ser = serial.Serial('/dev/ttyACM0', 9600)
class ColourTracker:
      def __init__(self):
       cv2.namedWindow("ColourTrackerWindow", cv2.CV_WINDOW_AUTOSIZE)
       self.capture = cv2.VideoCapture(0)
       self.scale_down = 4
      def run(self):
          while True:
            f, orig_img = self.capture.read()
            orig_img = cv2.flip(orig_img, 1)
            img = cv2.GaussianBlur(orig_img, (5,5), 0)
        laplacian = cv2.Laplacian(orig_img,cv2.CV_64F)
            sobelx = cv2.Sobel(orig_img,cv2.CV_64F,1,0,ksize=5)
                sobely = cv2.Sobel(orig_img,cv2.CV_64F,0,1,ksize=5)
            img = cv2.cvtColor(orig_img, cv2.COLOR_BGR2HSV)
            img = cv2.resize(img, (len(orig_img[0]) / self.scale_down, len(orig_img) / self.scale_down))
            red_lower = np.array([0, 150, 0],np.uint8)
            red_upper = np.array([5, 255, 255],np.uint8)
            red_binary = cv2.inRange(img, red_lower, red_upper)
            dilation = np.ones((15, 15), "uint8")
            red_binary = cv2.dilate(red_binary, dilation)
        edge = cv2.Canny(red_binary,200,300,apertureSize = 3)
            contours, hierarchy = cv2.findContours(red_binary, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
            max_area = 0
            largest_contour = None
            for idx, contour in enumerate(contours):
                area = cv2.contourArea(contour)
                if area > max_area:
                   max_area = area
                   largest_contour = contour
                if not largest_contour == None:
                    moment = cv2.moments(largest_contour)
                    if moment["m00"] > 1000 / self.scale_down:
                            rect = cv2.minAreaRect(largest_contour)
                            rect = ((rect[0][0] * self.scale_down, rect[0][1] * self.scale_down), (rect[1][0] * self.scale_down, rect[1][1] * self.scale_down), rect[2])
                            box = cv2.cv.BoxPoints(rect)
                            box = np.int0(box)
                            cv2.drawContours(orig_img,[box], 0, (0, 0, 255), 2)
                            cv2.imshow("ColourTrackerWindow", orig_img)
                            k = cv2.waitKey(10)
                        if cv2.waitKey(10) == 27:
                                cv2.destroyWindow("ColourTrackerWindow")
                                self.capture.release()
                                break
if __name__ == "__main__":
   colour_tracker = ColourTracker()
   colour_tracker.run()

提前谢谢!!

Arduino代码:

#include <Servo.h>

int p_fltXYRadius[0];

Servo servo;
Servo servo1;
int servoPosition = 90;
int servoPosition1=90 ;

int incomingByte = 0;   // for incoming serial data

void setup()
{
  Serial.begin(9600); // // opens serial port, sets data rate to 9600 bps

  servo.attach(9); // attaches the servo on pin 9 to the servo object
  servo1.attach(10);// attaches the servo1 on pin 10 to the servo object
  servo.write(servoPosition); // set the servo at the mid position
  servo.write(servoPosition1);// set the servo1 at the mid position

}

void loop()
{
  if (Serial.available() > 0) {
    // read the incoming byte:
    incomingByte = Serial.read();

    switch(incomingByte)
    {
      // Rotate camera left
    case 'l':
      servoPosition+=1;
      delay(20);
      if (servoPosition > 180)
      {
        servoPosition = 180;
      }

      break;

      // Rotate camera right
    case 'r': 
      servoPosition-=1;
      delay(20);
      if (servoPosition < 0)
      {
        servoPosition = 0;
      }

      break;

      // Center camera
    case 'c':
      servoPosition = 90;
      delay(20);
      break;

      // Camera in upward direction
    case 'u':
      servoPosition1+=5;
      delay(100);
      if (servoPosition1 > 160)
      {
        servoPosition1 = 160;
      }

      break;

      // Camera in downward direction
    case 'd':
      servoPosition1-=5;

      if (servoPosition1 < 140)
      {
        servoPosition1 = 140;
      }

      break;  

      // Camera would move upward if it finds an object moving up
    case 'f':
      servoPosition1+=1;
      delay(100);
      if (servoPosition1 > 180)
      {
        servoPosition1 = 180;
      }

      break;

      // Camera would move downward if it finds an object moving down
    case 'e':
      servoPosition1-=1;
      delay(100);
      if (servoPosition1 < 0)
      {
        servoPosition1 = 0;
      }

      break;
    }
    servo.write(servoPosition);
    servo1.write(servoPosition1);

  }
}

1 个答案:

答案 0 :(得分:0)

一切似乎都很好。 Arduino正等着你的命令。

在你的python代码中,你应该发送命令到这样的arduino:

ser.write('l')

使用适当的命令更改l