Ask Your Question

Revision history [back]

This tutorial shows how to perform gender classification with OpenCV:

It will work the same for any other classification problem. But please note, that this works very good on well-aligned images and sufficient image data (in terms of the number of training samples). You probably need to apply some pre-processing to make it work in real life settings.

Because you said you want to work with Python, you could also have a look on how to it in Python:

Good choice to do it in Python, as it is easy to see some results pretty quick. For the face detection part you can use the CascadeClassifier in OpenCV. A whole program to do face detection from a webcam video (for example) is nothing more than a 10-liner. Since you said you are planning to work on a weak device, you should go for the LBP cascades as they should eat up much less resources. The classes video and common are helper classes from the samples/python2 folder, the cascades are located in the data folder of your OpenCV download:

import cv2
from video import create_capture
from common import clock, draw_str

video_src = 0
cascade_fn = "lbpcascades/lbpcascade_frontalface.xml"
cascade = cv2.CascadeClassifier(cascade_fn)
cam = create_capture(video_src)

while True:
  ret, img = cam.read()
  gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  gray = cv2.equalizeHist(gray)
  rects = cascade.detectMultiScale(img, scaleFactor=1.2, minNeighbors=4, minSize=(80, 80))
  for x, y, width, height in rects:
    cv2.rectangle(img, (x, y), (x+width, y+height), (255,0,0), 2)
  cv2.imshow('facedetect',img)
  if cv2.waitKey(20) == 27:
    break

As for the recognition part. This tutorial shows how to perform gender classification with OpenCV:

It will work the same for any other classification problem. But please note, that this works very good on well-aligned images and sufficient image data (in terms of the number of training samples). You probably need to apply some pre-processing to make it work in real life settings.

Because you said you want to work with Python, you could also have a look on how to it in Python:

Good choice to do it in Python, as it is easy to see some results pretty quick.

For the face detection part you can use the CascadeClassifier in OpenCV. A whole program to do face detection from a webcam video (for example) is nothing more than a 10-liner. Since you said you are planning to work on a weak device, you should go for the LBP cascades as they should eat up much less resources.

The following sample uses the classes video and common , which are helper classes from the samples/python2 folder, the folder. The cascades are located in the data folder of your OpenCV download:download, so you'll probably need to adjust cascade_fn to make it work:

import cv2
from video import create_capture
from common import clock, draw_str

video_src = 0
cascade_fn = "lbpcascades/lbpcascade_frontalface.xml"
cascade = cv2.CascadeClassifier(cascade_fn)
cam = create_capture(video_src)

while True:
  ret, img = cam.read()
  gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  gray = cv2.equalizeHist(gray)
  rects = cascade.detectMultiScale(img, scaleFactor=1.2, minNeighbors=4, minSize=(80, 80))
  for x, y, width, height in rects:
    cv2.rectangle(img, (x, y), (x+width, y+height), (255,0,0), 2)
  cv2.imshow('facedetect',img)
  if cv2.waitKey(20) == 27:
    break

As for the recognition part. This tutorial shows how to perform gender classification with OpenCV:

It will work the same for any other classification problem. But please note, that this works very good on well-aligned images and sufficient image data (in terms of the number of training samples). You probably need to apply some pre-processing to make it work in real life settings.

Because you said you want to work with Python, you could also have a look on at how to it in Python:

Good choice to do it in Python, as it is easy to see some results pretty quick.

For the face detection part you can use the CascadeClassifier in OpenCV. A whole program to do face detection from a webcam video (for example) is nothing more than a 10-liner. Since you said you are planning to work on a weak device, you should go for the LBP cascades as they should eat up much less resources.

The following sample uses the classes video (samples/python2/video.py) and common, (samples/python2/common.py), which are helper classes from the samples/python2 folder. The cascades are located in the data folder of your OpenCV download, so you'll probably need to adjust cascade_fn to make it work:

import cv2
from video import create_capture
from common import clock, draw_str

video_src = 0
cascade_fn = "lbpcascades/lbpcascade_frontalface.xml"
cascade = cv2.CascadeClassifier(cascade_fn)
cam = create_capture(video_src)

while True:
  ret, img = cam.read()
  gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  gray = cv2.equalizeHist(gray)
  rects = cascade.detectMultiScale(img, scaleFactor=1.2, minNeighbors=4, minSize=(80, 80))
  for x, y, width, height in rects:
    cv2.rectangle(img, (x, y), (x+width, y+height), (255,0,0), 2)
  cv2.imshow('facedetect',img)
  if cv2.waitKey(20) == 27:
    break

As for the recognition part. This tutorial shows how to perform gender classification with OpenCV:

It will work the same for any other classification problem. But please note, that this works very good on well-aligned images and sufficient image data (in terms of the number of training samples). You probably need to apply some pre-processing to make it work in real life settings.

Because you said you want to work with Python, you could also have a look at how to it in Python:

Good choice to do it in Python, as it is easy to see some results pretty quick.

For the face detection part you can use the CascadeClassifier in OpenCV. A whole program to do face detection from a webcam video (for example) is nothing more than a 10-liner. tiny script. Since you said you are planning to work on a weak device, you should go for the LBP cascades as they should eat up much less resources.

The following sample uses the classes video (samples/python2/video.py) and common (samples/python2/common.py), which are helper classes from the samples/python2 folder. The cascades are located in the data folder of your OpenCV download, so you'll probably need to adjust cascade_fn to make it work:

import cv2
from video import create_capture
from common import clock, draw_str

# You probably need to adjust some of these:
video_src = 0
1
cascade_fn = "lbpcascades/lbpcascade_frontalface.xml"
"haarcascades/haarcascade_frontalface_default.xml"
# Create a new CascadeClassifier from given cascade file:
cascade = cv2.CascadeClassifier(cascade_fn)
cam = create_capture(video_src)

while True:
  ret, img = cam.read()
  # Do a little preprocessing:
  img_copy = cv2.resize(img, (img.shape[1]/2, img.shape[0]/2))
  gray = cv2.cvtColor(img, cv2.cvtColor(img_copy, cv2.COLOR_BGR2GRAY)
  gray = cv2.equalizeHist(gray)
  # Detect the faces (probably research for the options!):
  rects = cascade.detectMultiScale(img, scaleFactor=1.2, minNeighbors=4, minSize=(80, 80))
cascade.detectMultiScale(gray)
  # Make a copy as we don't want to draw on the original image:
  for x, y, width, height in rects:
    cv2.rectangle(img, cv2.rectangle(img_copy, (x, y), (x+width, y+height), (255,0,0), 2)
  cv2.imshow('facedetect',img)
cv2.imshow('facedetect', img_copy)
  if cv2.waitKey(20) == 27:
    break

As for the recognition part. This tutorial shows how to perform gender classification with OpenCV:

It will work the same for any other classification problem. But please note, that this works very good on well-aligned images and sufficient image data (in terms of the number of training samples). You probably need to apply some pre-processing to make it work in real life settings.

Because you said you want to work with Python, you could also have a look at how to it in Python:

Good choice to do it in Python, as it is easy to see some results pretty quick.

For the face detection part you can use the CascadeClassifier in OpenCV. A whole program to do face detection from a webcam video (for example) is nothing more than a tiny script. Since you said you are planning to work on a weak device, you should go for the LBP cascades as they should eat up much less resources.

The following sample uses the classes video (samples/python2/video.py) and common (samples/python2/common.py), which are helper classes from the samples/python2 folder. The cascades are located in the data folder of your OpenCV download, so you'll probably need to adjust cascade_fn to make it work:

import cv2
from video import create_capture
from common import clock, draw_str

# You probably need to adjust some of these:
video_src = 1
0
cascade_fn = "haarcascades/haarcascade_frontalface_default.xml"
# Create a new CascadeClassifier from given cascade file:
cascade = cv2.CascadeClassifier(cascade_fn)
cam = create_capture(video_src)

while True:
  ret, img = cam.read()
  # Do a little preprocessing:
  img_copy = cv2.resize(img, (img.shape[1]/2, img.shape[0]/2))
  gray = cv2.cvtColor(img_copy, cv2.COLOR_BGR2GRAY)
  gray = cv2.equalizeHist(gray)
  # Detect the faces (probably research for the options!):
  rects = cascade.detectMultiScale(gray)
  # Make a copy as we don't want to draw on the original image:
  for x, y, width, height in rects:
    cv2.rectangle(img_copy, (x, y), (x+width, y+height), (255,0,0), 2)
  cv2.imshow('facedetect', img_copy)
  if cv2.waitKey(20) == 27:
    break

As for the recognition part. This tutorial shows how to perform gender classification with OpenCV:

It will work the same for any other classification problem. But please note, that this works very good on well-aligned images and sufficient image data (in terms of the number of training samples). You probably need to apply some pre-processing to make it work in real life settings.

Because you said you want to work with Python, you could also have a look at how to it in Python:

Good choice to do it in Python, as it is easy to see some results pretty quick.

For the face detection part you can use the CascadeClassifier in OpenCV. A whole program to do face detection from a webcam video (for example) is nothing more than a tiny script. Since you said you are planning to work on a weak device, you should go for the LBP cascades as they should eat up much less resources.

The following sample uses the classes video (samples/python2/video.py) and common (samples/python2/common.py), which are helper classes from the samples/python2 folder. The cascades are located in the data folder of your OpenCV download, so you'll probably need to adjust cascade_fn to make it work:

import cv2
from video import create_capture
from common import clock, draw_str

# You probably need to adjust some of these:
video_src = 0
cascade_fn = "haarcascades/haarcascade_frontalface_default.xml"
# Create a new CascadeClassifier from given cascade file:
cascade = cv2.CascadeClassifier(cascade_fn)
cam = create_capture(video_src)

while True:
  ret, img = cam.read()
  # Do a little preprocessing:
  img_copy = cv2.resize(img, (img.shape[1]/2, img.shape[0]/2))
  gray = cv2.cvtColor(img_copy, cv2.COLOR_BGR2GRAY)
  gray = cv2.equalizeHist(gray)
  # Detect the faces (probably research for the options!):
  rects = cascade.detectMultiScale(gray)
  # Make a copy as we don't want to draw on the original image:
  for x, y, width, height in rects:
    cv2.rectangle(img_copy, (x, y), (x+width, y+height), (255,0,0), 2)
  cv2.imshow('facedetect', img_copy)
  if cv2.waitKey(20) == 27:
    break

As for the recognition part. This tutorial shows how to perform gender classification with OpenCV:

It will work the same for any other classification problem. But please note, that this works very good on well-aligned images and sufficient image data (in terms of the number of training samples). You probably need to apply some pre-processing to make it work in real life settings.

Because you said you want to work with Python, you could also have a look at how to it in Python:

So how difficult is it to write a program for your use case? The above probably makes you feel it is a simple task, it is not. Getting computer vision algorithms to work robust in non-constrained scenarios is hard. In case of face detection and face recognition you have to combat things like head pose or illumination to mention only two tough problems. You can spend years of research on any of these problems.

So my advice is: limit yourself to realistic goals. Don't try to aim for face.com recognition rates. And validate your algorithms as early as possible in the project, so you have figures to rely on and work with:

Good choice to do it in Python, as it is easy to see some results pretty quick.

For the face detection part you can use the CascadeClassifier in OpenCV. A whole program to do face detection from a webcam video (for example) is nothing more than a tiny script. Since you said you are planning to work on a weak device, you should go for the LBP cascades as they should eat up much less resources.

The following sample uses the classes video (samples/python2/video.py) and common (samples/python2/common.py), which are helper classes from the samples/python2 folder. The cascades are located in the data folder of your OpenCV download, so you'll probably need to adjust cascade_fn to make it work:

import cv2
from video import create_capture
from common import clock, draw_str

# You probably need to adjust some of these:
video_src = 0
cascade_fn = "haarcascades/haarcascade_frontalface_default.xml"
# Create a new CascadeClassifier from given cascade file:
cascade = cv2.CascadeClassifier(cascade_fn)
cam = create_capture(video_src)

while True:
  ret, img = cam.read()
  # Do a little preprocessing:
  img_copy = cv2.resize(img, (img.shape[1]/2, img.shape[0]/2))
  gray = cv2.cvtColor(img_copy, cv2.COLOR_BGR2GRAY)
  gray = cv2.equalizeHist(gray)
  # Detect the faces (probably research for the options!):
  rects = cascade.detectMultiScale(gray)
  # Make a copy as we don't want to draw on the original image:
  for x, y, width, height in rects:
    cv2.rectangle(img_copy, (x, y), (x+width, y+height), (255,0,0), 2)
  cv2.imshow('facedetect', img_copy)
  if cv2.waitKey(20) == 27:
    break

As for the recognition part. This tutorial shows how to perform gender classification with OpenCV:

It will work the same for any other classification problem. But please note, that this works very good on well-aligned images and sufficient image data (in terms of the number of training samples). You probably need to apply some pre-processing to make it work in real life settings.

Because you said you want to work with Python, you could also have a look at how to it in Python:

So how difficult is it to write a program for your use case? The above probably makes you feel it is a simple task, it task. It is not. Getting computer vision algorithms to work robust in non-constrained scenarios is hard. In case of face detection and face recognition you have to combat things like head pose or illumination to mention only two tough problems. You can spend years of research on any of these problems.

So my advice is: limit yourself to realistic goals. Don't try to aim for face.com recognition rates. And validate your algorithms as early as possible in the project, so you have figures to rely on and work with: