我正在尝试使用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--;
}
}
}
答案 0 :(得分:0)
首先尝试打开arduino IDE,然后查看是否在某些COM端口中检测到您的主板。如果您看到那里有一个COM端口打开,请将其复制并粘贴到您的代码中。 如果您使用的是蟒蛇,请尝试通过以下方式安装串口:
sort
然后尝试例如:
conda install -c anaconda pyserial
最诚挚的问候。