"""
import tensorflow as tf 
modeltest=tf.keras.models.load_model("facial_1 (1)")
print("--model loaded successfully--")
"""
import cv2
import sys
import os

class FaceCropper(object):
    cascades_path = 'haarcascade_frontalface_default.xml'

    def __init__(self):
        self.face_cascade = cv2.CascadeClassifier(self.cascades_path)

    def generate(self, image_path, show_result):
        name=""
        img = cv2.imread(image_path)
        if (img is None):
            print("Can't open image file")
            return 0

        #img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        faces = self.face_cascade.detectMultiScale(img, 1.1, 3, minSize=(100, 100))
        if (faces is None):
            print('Failed to detect face')
            return 0

        if (show_result):
            for (x, y, w, h) in faces:

                cv2.rectangle(img, (x,y), (x+w, y+h), (255,0,0), 2)
                roi_color = img[y:y + h, x:x + w] 
                print("[INFO] Object found. Saving locally.")
                name= str(w) + str(h) + '_faces.jpg'
                cv2.imwrite(str(w) + str(h) + '_faces.jpg', roi_color)
                #cv2.imshow('cropped image',roi_color) 
            #cv2.imshow('marked image',img)
            cv2.waitKey(0)
            cv2.destroyAllWindows()

        facecnt = len(faces)
        print("Detected faces: %d" % facecnt)
        i = 0
        height, width = img.shape[:2]

        for (x, y, w, h) in faces:
            r = max(w, h) / 2
            centerx = x + w / 2
            centery = y + h / 2
            nx = int(centerx - r)
            ny = int(centery - r)
            nr = int(r * 2)

            faceimg = img[ny:ny+nr, nx:nx+nr]
            lastimg = cv2.resize(faceimg, (32, 32))
            i += 1
            cv2.imwrite("image%d.jpg" % i, lastimg)
        return name

#fc=FaceCropper().generate("IMG_20200226_000431.png",True)

