我正在尝试制作带有python&的面部跟踪摄像头Arduino使用OpenCV。我在串行遇到此错误时遇到了麻烦:
'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--;
}
}
}
首先尝试打开Arduino IDE,并在其中查看是否在某些COM端口中检测到板。如果您在那里看到有一个COM端口打开,请复制并将其粘贴到您的代码中。如果您使用的是Anaconda尝试以这种方式安装串行:
conda install -c anaconda pyserial
和尝试i.g:
ser = serial.Serial('COM10', 9600)
最好的问候。