https://github.com/hirokoma/face_detection_of_base64_string
!/usr/bin/python
-*- coding: utf-8 -*-
import sys
import cv2, os
import numpy as np
from PIL import Image
import base64
from StringIO import StringIO
import scipy.misc
# Haar-like feature classifier
cascadePath = "/path/to/haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascadePath)
def readb64(base64_string):
sbuf = StringIO()
sbuf.write(base64.b64decode(base64_string))
pimg = Image.open(sbuf)
return pimg
#cv2.cvtColor(np.array(pimg), cv2.COLOR_RGB2BGR)
def detect():
# Load base64 in grayscale
image_pil = readb64(sys.argv[1].replace("\\", "\\\\")).convert('L')
Stored in an array of # NumPy
image = np.array(image_pil, 'uint8')
# Haar-like Feature classifier detects face (parameters are appropriate)
faces = faceCascade.detectMultiScale(image,1.1,9,0)
scipy.misc.imsave('outfile.jpg', image)
# Display the coordinates of the detected face image
for (x, y, w, h) in faces:
print x,y,w,h
detect()
Recommended Posts