Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
import base64, cv2, sys, os
from datetime import datetime
from elasticsearch import Elasticsearch
# Elastic
es = Elasticsearch()
# Get user supplied values
imagePath = sys.argv[1]
cascPath = "haarcascade_frontalface_default.xml"
# Create the haar cascade
faceCascade = cv2.CascadeClassifier(cascPath)
# Read the image
image = cv2.imread(imagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Detect faces in the image
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30),
flags = cv2.cv.CV_HAAR_SCALE_IMAGE
)
print("Found {0} faces!".format(len(faces)))
# Gets all of the faces in the picture
count=0;
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
cropMe = image[y:y+h, x:x+w]
cv2.imwrite('temp_' + sys.argv[1], cropMe)
with open('temp_' + sys.argv[1], "r+b") as image_file:
doc = {
'face': base64.b64encode(image_file.read()),
'validated': -1, # -1, 0, 1
'timestamp': datetime.now(),
}
# Index all faces as base64
res = es.index(index="faces", body=doc, doc_type='tweet')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment