【发布时间】:2019-05-24 03:30:45
【问题描述】:
我正在尝试使用 OpenCV 制作带有 python 和 Arduino 的面部跟踪相机。我在序列号出现此错误时遇到问题:
'avrdude: ser_open(): 无法打开设备“\.\COM5”:访问被拒绝。'
我不确定如何防止这种情况发生。如果python程序已经打开它就不会运行,如果我打开Arduino然后python它会运行但不会工作。
import cv2
import serial
ser = serial.Serial('COM5',baudrate = 52000)
def detectface(camera):
cap = cv2.VideoCapture(camera)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 480)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 640)
faceDetect = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
print(cap.isOpened())
while(cap.isOpened()):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
face = faceDetect.detectMultiScale(gray, 1.3, 5)
for(x, y, w, h) in face:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 0, 255), 2)
center = [(x+x+w)/2, (y+y+h)/2]
center[0] = int(center[0])
center[1] = int(center[1])
if(center[0]<235):
ser.write(b'x')
elif(center[0]>245):
ser.write(b'w')
if(center[1]>335):
ser.write(b'h')
elif(center[1]<305):
ser.write(b'y')
cv2.circle(frame,(center[0],center[1]),25,(0,0,2),3,8,0)
cv2.imshow('face', frame)
if(cv2.waitKey(1) & 0xFF == ord('q')):
break
cap.release()
cv2.destroyAllWindows()
detectface(0)
#include <Servo.h>
char tiltChannel=0,panChannel=1;
char serialChar=0;
int center1;
int center2;
char pyInput;
Servo servoTilt, servoPan;
void setup() {
Serial.begin(52000);
servoTilt.attach(9);
servoPan.attach(10);
servoTilt.write(90);
servoPan.write(90);
}
void loop() {
int currentRotationX = 90;
int currentRotationY = 90;
if(Serial.available()>0){
pyInput = Serial.read();
if(pyInput == 'x'){
servoTilt.write(currentRotationX++);
currentRotationX=currentRotationX++;
}
else if(pyInput == 'w'){
servoTilt.write(currentRotationX--);
currentRotationX=currentRotationX--;
}
if(pyInput=='y'){
servoTilt.write(currentRotationY++);
currentRotationY=currentRotationY++;
}
else if(pyInput == 'h'){
servoTilt.write(currentRotationY--);
currentRotationY=currentRotationY--;
}
}
}
【问题讨论】:
-
您在哪一步收到该错误?请记住,在 Python 代码运行时,您无法对 Arduino 进行编程或打开其串行监视器。