我目前正在尝试将一个相机安装在一个由两个微型伺服跟踪一张脸的倾斜的云台上。我的python代码一直在工作,并且已经成功地识别了一张脸,但当Arduino不断闪烁时,我的伺服系统没有一个在移动,就像它正在接收来自Python代码的输入一样。我不能让伺服系统根据我的python代码移动,但我已经在旁边编写了简单的代码,以确保伺服系统有良好的连接,并且它们自己工作得很好。我不确定出了什么问题。
Python代码
import numpy as np6
import serial
import time
import sys
import cv2
arduino = serial.Serial('COM3', 9600)
time.sleep(2)
print("Connection to arduino...")
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(0)
while 1:
ret, img = cap.read()
cv2.resizeWindow('img', 500,500)
cv2.line(img,(500,250),(0,250),(0,255,0),1)
cv2.line(img,(250,0),(250,500),(0,255,0),1)
cv2.circle(img, (250, 250), 5, (255, 255, 255), -1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3)
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),5)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
arr = {y:y+h, x:x+w}
print (arr)
print ('X :' +str(x))
print ('Y :'+str(y))
print ('x+w :' +str(x+w))
print ('y+h :' +str(y+h))
xx = int(x+(x+h))/2
yy = int(y+(y+w))/2
print (xx)
print (yy)
center = (xx,yy)
print("Center of Rectangle is :", center)
data =(“X {0: f} Y {1: f} Z” .format (xx, yy))
print ("output = '" +data+ "'")
arduino.write(data.encode())
cv2.imshow('img',img)
k = cv2.waitKey(30) & 0xff
if k == 27:
breakArduino代码
#include<Servo.h>
Servo servoVer; //Vertical Servo
Servo servoHor; //Horizontal Servo
int x;
int y;
int prevX;
int prevY;
void setup()
{
Serial.begin(9600);
servoVer.attach(5); //Vertical Servo to Pin 5
servoHor.attach(6); //Horizontal Servo to Pin 6
servoVer.write(90);
servoHor.write(90);
}
void Pos()
{
if(prevX != x || prevY != y)
{
int servoX = map(x, 600, 0, 70, 179);
int servoY = map(y, 450, 0, 179, 95);
servoX = min(servoX, 179);
servoX = max(servoX, 70);
servoY = min(servoY, 179);
servoY = max(servoY, 95);
servoHor.write(servoX);
servoVer.write(servoY);
}
}
void loop()
{
if(Serial.available() > 0)
{
if(Serial.read() == 'X')
{
x = Serial.parseInt();
if(Serial.read() == 'Y')
{
y = Serial.parseInt();
Pos();
}
}
while(Serial.available() > 0)
{
Serial.read();
}
}
}发布于 2020-03-05 10:33:27
一个很大的问题是你使用Serial.read的方式。该函数消耗缓冲区中的字符。你不会把同一本书读两遍。所以假设你发送了一个'Y‘。第一个if语句从串行缓冲区中读出Y并与'X‘进行比较,这是假的,因此它继续前进。然后,它从序列中读取其他内容,如果没有要读取的内容,则可能为-1。但它看不到'Y‘,因为它是在第一个if中读取的。
您需要做的是将序列中的内容读取到一个char变量中,然后在if语句中使用该char变量。
char c = Serial.read();
if(c == 'X')...
... if (c == 'Y')...https://stackoverflow.com/questions/60521426
复制相似问题