Skip to content

Instantly share code, notes, and snippets.

@46bit
Created January 31, 2015 02:33
Show Gist options
  • Save 46bit/d49f6fd44b9e690a6ac5 to your computer and use it in GitHub Desktop.
Save 46bit/d49f6fd44b9e690a6ac5 to your computer and use it in GitHub Desktop.
Face detection using OpenCV. Refactored from https://realpython.com/blog/python/face-recognition-with-python/.
import sys, cv2
# Refactored https://realpython.com/blog/python/face-recognition-with-python/
def cascade_detect(cascade, image):
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
return cascade.detectMultiScale(
gray_image,
scaleFactor = 1.15,
minNeighbors = 5,
minSize = (30, 30),
flags = cv2.cv.CV_HAAR_SCALE_IMAGE
)
def detections_draw(image, detections):
for (x, y, w, h) in detections:
print "({0}, {1}, {2}, {3})".format(x, y, w, h)
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
def main(argv = None):
if argv is None:
argv = sys.argv
cascade_path = sys.argv[1]
image_path = sys.argv[2]
result_path = sys.argv[3] if len(sys.argv) > 3 else None
cascade = cv2.CascadeClassifier(cascade_path)
image = cv2.imread(image_path)
if image is None:
print "ERROR: Image did not load."
return 2
detections = cascade_detect(cascade, image)
detections_draw(image, detections)
print "Found {0} objects!".format(len(detections))
if result_path is None:
cv2.imshow("Objects found", image)
cv2.waitKey(0)
else:
cv2.imwrite(result_path, image)
if __name__ == "__main__":
sys.exit(main())
@buddiex
Copy link

buddiex commented Dec 14, 2015

Thanks for this...
Am doing something like this but for finger prints. i have a database of images that i want to filter out images that are not finger prints. Is there a haar cascade file like haarcascade_frontalface_default.xml for finger prints detection?

@Dhara4510
Copy link

Thanks for this.... Currently I am doing for face recognition. Can anyone please help me for that ?

@Antz411
Copy link

Antz411 commented Nov 10, 2016

I get a couple errors with this, am I missing something (sorry bit of a nube):
sys.exit(main()) error which is not giving me the error code, though seems to be something to do with the 'cascade_path' & 'cascade', though I am passing "haarcascade_frontalface_default.xml" directly in the project folder?
Previous example project that this was re-factored from works fine?
Any suggestion would be great, thanks.

@anojkulas
Copy link

can you help me to run this python file
how can in run this file

@tann21
Copy link

tann21 commented Apr 3, 2019

I am getting error in this code , I run the code like (python fac-de.py abc.jpg haarcascade_frontalface_default.xml)
The error bellow:

python fac-de.py abc.jpg haarcascade_frontalface_default.xml
File "", line 1
python fac-de.py abc.jpg haarcascade_frontalface_default.xml
^
SyntaxError: invalid syntax

@balde9745
Copy link

thank you for this great work

@m2005dh
Copy link

m2005dh commented Nov 6, 2019

I am getting error in this code , I run the code like (python fac-de.py abc.jpg haarcascade_frontalface_default.xml)
The error bellow:

python fac-de.py abc.jpg haarcascade_frontalface_default.xml
File "", line 1
python fac-de.py abc.jpg haarcascade_frontalface_default.xml
^
SyntaxError: invalid syntax

You need to have those files (image,xml and execute file) in same folder, after that you can see the result.

@m2005dh
Copy link

m2005dh commented Nov 9, 2019

hi
thanks for your codes, how I should define the identification codes here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment