2017-05-11 2 views
1

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 회를 넘지 만 서보가 움직이지 않습니다.

나는 루프 문제가 있다고 생각합니다. 어떻게 해결할 수 있을까요?

+0

그리고'exit (0);'이유가 무엇입니까? 또한'digitalWrite (servoPin, LOW);는 무엇을해야합니까? –

+0

각 루프 이후에'exit (0)'을 거기에 넣습니다. 그것은 그것이 적절한 사용인지 몰랐던 것 같습니다. 이제 나는 서보가 계속 움직이고 있다고 설명했다. 하지만 '0 sayem48

답변

1

exit (0)는 직렬 버퍼에 데이터가있을 때 첫 번째 루프 사이클에서 프로그램을 중지합니다. 기본적으로 arduino를 무한 루프에 넣습니다.

코드를 읽은 적이 있습니까?

+0

각 루프 이후에'exit (0)'을 거기에 넣습니다. 그것은 그것이 적절한 사용인지 몰랐던 것 같습니다. 이제 나는 서보가 계속 움직이고 있다고 설명했다. 하지만 매번'0 sayem48

+1

@ sayem48 그것이 당신의 주된 루프이다. 아두 이노를 유휴 상태로두고 시리얼 입력을 처리하지 않습니다. 그 루프가 arduino 소프트웨어를 살아있게합니다. 어떻게 지속적으로 실행할 수 있습니까? 1 초의 지연이 있습니다. 당신은 "incomingBit"을 읽지 않습니다. 그 1 바이트. – Piglet

+0

내가 말한 것은 'exit (0)'이 없으므로 서보가 90도마다 1ms 휴식 시간을두고 180도 움직이고 있다는 것입니다. 필자가 원하는 것은 매번 '0 sayem48