Ask Your Question

Finding coordinates of face in Python[Updated]

asked 2014-11-05 10:25:20 -0500

luketheduke gravatar image

updated 2014-11-06 12:15:39 -0500

berak gravatar image

I am trying to send the coordinates of a face found with opencv over serial. I got the serial part downpath but I can't figure out how to find the coordinates of the face. I would like to if possible use the example as the base... Any ideas? Thanks ahead of time! Here is the code:

#!/usr/bin/env python

import numpy as np
import cv2

# local modules
from video import create_capture
from common import clock, draw_str

help_message = '''
USAGE: [--cascade <cascade_fn>] [--nested-cascade <cascade_fn>] [<video_source>]

def detect(img, cascade):
    rects = cascade.detectMultiScale(img, scaleFactor=1.3, minNeighbors=4, minSize=(30, 30), flags = cv2.CASCADE_SCALE_IMAGE)
    if len(rects) == 0:
        return []
    rects[:,2:] += rects[:,:2]
    return rects

def draw_rects(img, rects, color):
    for x1, y1, x2, y2 in rects:
        cv2.rectangle(img, (x1, y1), (x2, y2), color, 2)

if __name__ == '__main__':
    import sys, getopt
    print help_message

    args, video_src = getopt.getopt(sys.argv[1:], '', ['cascade=', 'nested-cascade='])
        video_src = video_src[0]
        video_src = 0
    args = dict(args)
    cascade_fn = args.get('--cascade', "../../data/haarcascades/haarcascade_frontalface_alt.xml")
    nested_fn  = args.get('--nested-cascade', "../../data/haarcascades/haarcascade_eye.xml")

    cascade = cv2.CascadeClassifier(cascade_fn)
    nested = cv2.CascadeClassifier(nested_fn)

    cam = create_capture(video_src, fallback='synth:bg=../cpp/lena.jpg:noise=0.05')

    while True:
        ret, img =
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        gray = cv2.equalizeHist(gray)

        t = clock()
        rects = detect(gray, cascade)
        vis = img.copy()
        draw_rects(vis, rects, (0, 255, 0))
        for x1, y1, x2, y2 in rects:
            roi = gray[y1:y2, x1:x2]
            vis_roi = vis[y1:y2, x1:x2]
            subrects = detect(roi.copy(), nested)
            draw_rects(vis_roi, subrects, (255, 0, 0))
        dt = clock() - t

        draw_str(vis, (20, 20), 'time: %.1f ms' % (dt*1000))
        cv2.imshow('facedetect', vis)

        if 0xFF & cv2.waitKey(5) == 27:

Sorry about the formatting... Thanks for all the help so far!

edit retag flag offensive close merge delete



Could you provide the link or the code?

FooBar gravatar imageFooBar ( 2014-11-05 10:30:40 -0500 )edit

hi, luke, the formatting is actually easy(once you know it..) : just mark the code part with the mouse, and press the 10101 button.

berak gravatar imageberak ( 2014-11-06 12:17:03 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2015-02-26 11:43:29 -0500

luketheduke gravatar image

I figured it out. I can just isolate the variables x1 and y1 in the for loop towards the bottom and I get my location... Thanks for all the help guys!

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2014-11-05 10:25:20 -0500

Seen: 1,151 times

Last updated: Feb 26 '15