Skip to content

Instantly share code, notes, and snippets.

@lpicanco
Created May 3, 2019 03:06
Show Gist options
  • Save lpicanco/0e6695e8e037f4eef5b93053a67f36ad to your computer and use it in GitHub Desktop.
Save lpicanco/0e6695e8e037f4eef5b93053a67f36ad to your computer and use it in GitHub Desktop.
export image to json - keras
import cv2 as cv
import argparse
import numpy as np
import json
def predict(image_file):
faces = process_image(image_file)
doc = json.dumps(faces.tolist())
print(doc)
def process_image(image_file):
face_cascade = cv.CascadeClassifier("haarcascade_frontalface_default.xml")
image_gray = cv.cvtColor(cv.imread(image_file), cv.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(image_gray, 1.1, 6)
processed_faces = []
for (x, y, w, h) in faces:
cropped = image_gray[y : y+h, x : x+w]
resized = cv.resize(cropped,(48,48))
scaled = resized.reshape(48,48,1) / 255
processed_faces.append(scaled)
return np.array(processed_faces)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("image", help="image file to predict")
args = parser.parse_args()
np.set_printoptions(suppress=True)
predict(args.image)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment