OpenCV (python)에서 얼굴을 감지하면 arduino에 연결된 서보를 이동하고 싶습니다.Python이 Arduino에 신호를 한 번만 전송합니다.
는OpenCV의 코드 :
import numpy as np
import cv2
import serial
import time
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(0)
ser = serial.Serial('COM1', 19200,timeout=5)
time.sleep(6)
print(ser)
while 1:
ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x1, y1, w1, h1) in faces:
cv2.rectangle(img, (x1, y1), (x1 + w1, y1 + h1), (255, 0, 0), 2)
detect_face=x1
print 'face distance: ', detect_face
cv2.imshow('img', img)
k = cv2.waitKey(30) & 0xff
if 0 < detect_face < 100:
ser.write('Y')
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
아두 이노 코드 : 나는 다음과 같은 face_detect
값을 얻을
#include <Servo.h>
int servoPin = 3;
Servo Servo1;
char incomingBit;
void setup() {
Servo1.attach(servoPin);
pinMode(servoPin, OUTPUT);
Serial.begin(19200);
}
void loop() {
if (Serial.available() > 0) {
incomingBit = Serial.read();
Serial.print("I received: ");
Serial.println(incomingBit);
if(incomingBit == 'Y' || incomingBit == 'y') {
Servo1.write(0);
delay(1000);
Servo1.write(90);
delay(1000);
Servo1.write(180);
delay(1000);
exit(0);
}
else {
digitalWrite(servoPin, LOW);
}
}
}
:
Serial<id=0x3203c30, open=True>(port='COM1', baudrate=19200, bytesize=8,
parity='N', stopbits=1, timeout=5, xonxoff=False, rtscts=False,
dsrdtr=False)
face distance: 203
face distance: 192
face distance: 187
face distance: 177
face distance: 163
face distance: 157
face distance: 145
face distance: 130
face distance: 116
face distance: 109
face distance: 104
face distance: 95
face distance: 80
face distance: 80
face distance: 98
face distance: 100
face distance: 98
face distance: 101
face distance: 110
face distance: 108
face distance: 109
face distance: 110
face distance: 110
face distance: 96
face distance: 88
face distance: 81
face_detect
가 100 이하가 처음으로, 파이썬은 신호를 보낸다 서보는 180도 회전합니다. 그러나 그 후에 그것은 단지 거기에 남아 있습니다. face_detect
이 100 회를 넘지 만 서보가 움직이지 않습니다.
나는 루프 문제가 있다고 생각합니다. 어떻게 해결할 수 있을까요?
그리고'exit (0);'이유가 무엇입니까? 또한'digitalWrite (servoPin, LOW);는 무엇을해야합니까? –
각 루프 이후에'exit (0)'을 거기에 넣습니다. 그것은 그것이 적절한 사용인지 몰랐던 것 같습니다. 이제 나는 서보가 계속 움직이고 있다고 설명했다. 하지만 '0
sayem48