Opencv facetracking frozen arduino serial python

asked 2015-05-18 05:21:50 -0600

jua123 gravatar image

updated 2018-12-25 10:05:53 -0600

Hello,

He had a question .. I made the code for the web camera with pan tilt follow my face .. but have a problem when the program takes 2 or 3 minutes by detecting faces and sending the coordinates for serial to Arduino using python , open cv it stays frozen until leave the visual field of the webcam and this stops detecting , I 've tried on two computers with windows and ubuntu, its power is high , so I do not think it hardware problem, I also lowered the resolution to:

img = cv2.resize ( img , ( 0,0) , fx = 0.5 , f = 0.5)

So I do not think it pc problem , I think it's something Arduino , the buffer fills or something , I do not quite understand , I wonder if someone did the same to me, and I can solve .

Posting the code if anyone can help me.

Thank you very much !

import numpy as np

import cv2 import sys import serial import time

from array import array

serialConnection = serial.Serial('COM9', 9600)

faceCascadePath = sys.argv[1]

cap = cv2.VideoCapture(0)

Create the haar cascade

face_cascade = cv2.CascadeClassifier(faceCascadePath)

smile_cascade = cv2.CascadeClassifier(smileCascadePath)

print "Press q to exit ..."

while(True): # Capture frame-by-frame ret, img = cap.read()

if not ret:continue

# Resize image, both axes by half
#img = cv2.resize(img, (0,0), fx=0.5, fy=0.5)

# Our operations on the frame come here
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Get faces from gray scale image
faces = face_cascade.detectMultiScale(gray, 1.2, 3)
#faces = face_cascade.detectMultiScale(gray)

for (x,y,w,h) in faces:
    cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)

    #arr = {y:y+h, x:x+w}
    #print arr

#print "X:"  + str ( x )
#print "Y:"  + str ( y )
#print "x+w:"  + str ( x+w )
#print "y+h:"  + str ( y+h )

xx = int(x+(x+h))/2
    yy = int(y+(y+w))/2
print xx
print yy
centro=(xx,yy)
print "El centro de mi rectangulo:", centro
output = "X{0:d}Y{1:d}Z".format(xx, yy)
    print "output = '" + output + "'"
serialConnection.write(output)


#Return to original size
#img = cv2.resize(img, (0,0), fx=2.0, fy=2.0)

# Show image

cv2.imshow('img',img)

# Display the resulting frame
# cv2.imshow('frame',gray)

if cv2.waitKey(100) & 0xFF == ord('q'):
    break

When everything done, release the capture

cap.release() cv2.destroyAllWindows()

Y el codigo de arduino:

#include <Servo.h>

Servo servoVertical; Servo servoHorizontal;

int servoVerticalPin = 10; int servoHorizontalPin = 11;

int x, prevX; int y, prevY;

void setup() { Serial.begin(9600); servoVertical.attach(servoVerticalPin); servoHorizontal.attach(servoHorizontalPin); servoHorizontal.write(130); servoVertical.write(130); }

void loop() { if (Serial.available() > 0) { if (Serial.read() == 'X') {

        x = Serial.parseInt();
        Serial.println(x);
        if (Serial.read() == 'Y') {

            y = Serial.parseInt();
            Serial.println(y);
            moveTurret();

        }
    }

    while (Serial.available() > 0) {
        Serial.read();
    }        
}

} void moveTurret() { if (prevX != x || prevY != y) { prevX = x; prevY = y;

    int servoX = map(x, 600, 0, 70, 180);
    int servoY = map(y, 450, 0, 180, 95);

    servoX = min(servoX ...
(more)
edit retag flag offensive close merge delete

Comments

I have already solved, was that in the Arduino sketch printed the x and y values ​​, and python not collected and guess the buffer is filled or something , but removing it works , so nothing !!

jua123 gravatar imagejua123 ( 2015-05-18 10:27:41 -0600 )edit