import joblib
import json
import numpy as np
import base64
import cv2
from wavelet import w2d
import os

__class_name_to_number = {}
__class_number_to_name = {}
__model = None

def classify_image(image_base64_data, file_path=None):
    images = get_cropped_image_if_2_eyes(file_path, image_base64_data)
    result = []
    for img in images:
        scaled_raw_img = cv2.resize(img, (32,32))
        img_har = w2d(img, 'db1', 5)
        scaled_har_img = cv2.resize(img_har, (32,32))
        combined_img = np.vstack((scaled_raw_img.reshape(32*32*3,1),scaled_har_img.reshape(32*32,1)))
        len_image_array = (32 * 32 * 3) + (32 * 32)
        final = combined_img.reshape(1, len_image_array).astype(float)
        result.append({
            'class': class_number_to_name(__model.predict(final)[0]),
            'class_probability': np.round(__model.predict_proba(final)*100,2).tolist()[0],
            'class_dictionary': __class_name_to_number
        })
        
    return result

def load_saved_artifacts():
    print("Loading artifacts.....")
    global __class_name_to_number
    global __class_number_to_name

    with open("./artifacts/class_dictionary.json",'r') as f:
        __class_name_to_number = json.load(f)
        __class_number_to_name = {v:k for k,v in __class_name_to_number.items()}

    global __model
    if __model is None:
        with open("./artifacts/saved_LogisticRegression_model.pkl","rb") as f:
            __model = joblib.load(f)
    print("Artifacts Loaded!!!!!")

def class_number_to_name (class_num):
    return __class_number_to_name[class_num]

def get_cv2_image_from_base64_string(b64str):
    encoded_data = b64str.split(',')[1]
    nparr = np.frombuffer(base64.b64decode(encoded_data), np.uint8)
    img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
    return img

def get_cropped_image_if_2_eyes(image_path, image_base64_data):
    face_cascade = cv2.CascadeClassifier('./opencv/haarcascades/haarcascade_frontalface_default.xml')
    eye_cascade = cv2.CascadeClassifier('./opencv/haarcascades/haarcascade_eye.xml')

    if image_path: # if image is not proper then we are just removing it
        img = cv2.imread(image_path)
    else:
        img = get_cv2_image_from_base64_string(image_base64_data)

    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #convert to grayscale
    faces = face_cascade.detectMultiScale(gray, 1.3, 5) #detect faces

    final_croppes_faces = []
    for x, y, w, h in faces:
        roi_gray = gray[y:y+h,x:x+w]
        roi_color = img[y:y+h,x:x+w]
        eyes = eye_cascade.detectMultiScale(roi_gray) #detect eyes
        if len(eyes) >= 2: # if both images are visible in a single face
            final_croppes_faces.append(roi_color)
    return final_croppes_faces

#dummy function & data for testing
def get_b64_test_image_for_sundar():
    with open("b64.txt") as f:
        return f.read()

if __name__ == '__main__':
    load_saved_artifacts()
    # print(classify_image(get_b64_test_image_for_sundar(), None))
    # print(classify_image(None, './test_images/elon.jpg'))
    # print(classify_image(None, './test_images/sheldon-cooper.jpg'))
    # print(classify_image(None, './test_images/sundar.jpg'))
    # print(classify_image(None, './test_images/apj.jpg')) # Inconsistent result could be due to https://github.com/scikit-learn/scikit-learn/issues/13211
    # print(classify_image(None, './test_images/sherlock.jpg'))
    print(classify_image(None, './test_images/sundar2.jpg'))


import joblib
import json
import numpy as np
import base64
import cv2
from wavelet import w2d
import os

__class_name_to_number = {}
__class_number_to_name = {}
__model = None

def classify_image(image_base64_data, file_path=None):
    images = get_cropped_image_if_2_eyes(file_path, image_base64_data)
    result = []
    for img in images:
        scaled_raw_img = cv2.resize(img, (32,32))
        img_har = w2d(img, 'db1', 5)
        scaled_har_img = cv2.resize(img_har, (32,32))
        combined_img = np.vstack((scaled_raw_img.reshape(32*32*3,1),scaled_har_img.reshape(32*32,1)))
        len_image_array = (32 * 32 * 3) + (32 * 32)
        final = combined_img.reshape(1, len_image_array).astype(float)
        result.append({
            'class': class_number_to_name(__model.predict(final)[0]),
            'class_probability': np.round(__model.predict_proba(final)*100,2).tolist()[0],
            'class_dictionary': __class_name_to_number
        })
        
    return result

def load_saved_artifacts():
    print("Loading artifacts.....")
    global __class_name_to_number
    global __class_number_to_name

    with open("./artifacts/class_dictionary.json",'r') as f:
        __class_name_to_number = json.load(f)
        __class_number_to_name = {v:k for k,v in __class_name_to_number.items()}

    global __model
    if __model is None:
        with open("./artifacts/saved_LogisticRegression_model.pkl","rb") as f:
            __model = joblib.load(f)
    print("Artifacts Loaded!!!!!")

def class_number_to_name (class_num):
    return __class_number_to_name[class_num]

def get_cv2_image_from_base64_string(b64str):
    encoded_data = b64str.split(',')[1]
    nparr = np.frombuffer(base64.b64decode(encoded_data), np.uint8)
    img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
    return img

def get_cropped_image_if_2_eyes(image_path, image_base64_data):
    face_cascade = cv2.CascadeClassifier('./opencv/haarcascades/haarcascade_frontalface_default.xml')
    eye_cascade = cv2.CascadeClassifier('./opencv/haarcascades/haarcascade_eye.xml')

    if image_path: # if image is not proper then we are just removing it
        img = cv2.imread(image_path)
    else:
        img = get_cv2_image_from_base64_string(image_base64_data)

    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #convert to grayscale
    faces = face_cascade.detectMultiScale(gray, 1.3, 5) #detect faces

    final_croppes_faces = []
    for x, y, w, h in faces:
        roi_gray = gray[y:y+h,x:x+w]
        roi_color = img[y:y+h,x:x+w]
        eyes = eye_cascade.detectMultiScale(roi_gray) #detect eyes
        if len(eyes) >= 2: # if both images are visible in a single face
            final_croppes_faces.append(roi_color)
    return final_croppes_faces

#dummy function & data for testing
def get_b64_test_image_for_sundar():
    with open("b64.txt") as f:
        return f.read()

if __name__ == '__main__':
    load_saved_artifacts()
    # print(classify_image(get_b64_test_image_for_sundar(), None))
    # print(classify_image(None, './test_images/elon.jpg'))
    # print(classify_image(None, './test_images/sheldon-cooper.jpg'))
    # print(classify_image(None, './test_images/sundar.jpg'))
    # print(classify_image(None, './test_images/apj.jpg')) # Inconsistent result could be due to https://github.com/scikit-learn/scikit-learn/issues/13211
    # print(classify_image(None, './test_images/sherlock.jpg'))
    print(classify_image(None, './test_images/sundar2.jpg'))


# if image is not proper then we are just removing it #convert to grayscale #detect faces #detect eyes # if both images are visible in a single face #dummy function & data for testing # print(classify_image(get_b64_test_image_for_sundar(), None)) # print(classify_image(None, './test_images/elon.jpg')) # print(classify_image(None, './test_images/sheldon-cooper.jpg')) # print(classify_image(None, './test_images/sundar.jpg')) # print(classify_image(None, './test_images/apj.jpg')) # Inconsistent result could be due to https://github.com/scikit-learn/scikit-learn/issues/13211 # print(classify_image(None, './test_images/sherlock.jpg'))