Created
January 31, 2015 02:33
-
-
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/.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
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.
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
thank you for this great work