1

I want to move a servo connected to arduino when I detect face in OpenCV (python).

OpenCV code:

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()

Arduino Code:

#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); 
            }
  }

}   

I get the following face_detect value :

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

The first time face_detect goes below 100, python sends a signal and servo does a 180 degree turn. But after that it just remains there. Although face_detect goes below 100 several times, servo does not move.

I think i am having a loop problem. How to solve that?

2
  • And exit(0); is there why? Also what is digitalWrite(servoPin, LOW); supposed to do? Commented May 11, 2017 at 18:27
  • I put exit(0) there to stop it after each loop. It seems I didn't know it's proper use. Now that I commented it out the servo is moving continuously. But I wanted to move it once every time 0<detect_face<100. What am i doing wrong? Commented May 11, 2017 at 22:17

1 Answer 1

1

exit(0) stops your program in the first loop cycle when you have a data in the serial buffer. It basically puts your arduino into an infinite loop that does nothing.

Have you even read your code befor posting that question?

Sign up to request clarification or add additional context in comments.

4 Comments

I put exit(0) there to stop it after each loop. It seems I didn't know it's proper use. Now that I commented it out the servo is moving continuously. But I wanted to move it once everytime 0<detect_face<100. What am i doing wrong? I am new to this, sorry for these naive problems
@sayem48 that's your main loop. if you leave it your arduino will idle and will not process any serial inputs. the loop is what's keeping your arduino software alive. how can it run continuously? you have 1s delays. you don't read an "incomingBit" by the way. its a byte.
What I meant is, now that there is no exit(0), servo is moving 180 degree with 1ms break after every 90 degree, continuously. What I want is, everytime 0<detect_face<100 servo should complete the loop once then stop, ready for next 0<detect_face<100. But now after the first 0<detect_face<100, servo is moving continuously. How can I solve this? @Piglet
I think I can solve it by using switch. Am I right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.