 
import os
from django.urls import path, include
import face_recognition
import cv2
from imutils.video import VideoStream
import imutils
import numpy as np
from tensorflow.keras.models import load_model
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
from tensorflow.keras.preprocessing.image import img_to_array



# load our serialized face detector model from disk
prototxtPath = r"face_detector\deploy.prototxt"
weightsPath = r"face_detector\res10_300x300_ssd_iter_140000.caffemodel"
faceNet = cv2.dnn.readNet(prototxtPath, weightsPath)

# load the face mask detector model from disk
maskNet = load_model(r"C:\Users\mkjsr\OneDrive\Desktop\Django_mask_attendance\main_base\mask_detector.model")


def detect_faces(frame,email):
    # grab the dimensions of the frame and then construct a blob
    # from it
    (h, w) = frame.shape[:2]
    blob = cv2.dnn.blobFromImage(frame, 1.0, (224, 224),
        (104.0, 177.0, 123.0))

    # pass the blob through the network and obtain the face detections
    faceNet.setInput(blob)
    detections = faceNet.forward()
    print(detections.shape)

    # initialize our list of faces, their corresponding locations,
    # and the list of predictions from our face mask network
    faces = []
    locs = []
    lable = "Not Verified"

    BASE_DIR    = os.path.dirname(os.path.abspath(__file__))
    MEDIA_ROOT  = os.path.join(BASE_DIR,'face_dataset')
    loc=(str(MEDIA_ROOT)+'\\'+str(email)+'.jpg')
    face_1_image = face_recognition.load_image_file(loc)
    small_frame_1 = cv2.resize(face_1_image, (0, 0), fx=0.25, fy=0.25)
    rgb_small_frame_1 = small_frame_1[:, :, ::-1]
    face_1_face_encoding = face_recognition.face_encodings(rgb_small_frame_1)[0]

    # loop over the detections
    for i in range(0, detections.shape[2]):
        # extract the confidence (i.e., probability) associated with
        # the detection
        confidence = detections[0, 0, i, 2]

        # filter out weak detections by ensuring the confidence is
        # greater than the minimum confidence
        if confidence > 0.5:
            # compute the (x, y)-coordinates of the bounding box for
            # the object
            box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
            (startX, startY, endX, endY) = box.astype("int")

            # ensure the bounding boxes fall within the dimensions of
            # the frame
            (startX, startY) = (max(0, startX), max(0, startY))
            (endX, endY) = (min(w - 1, endX), min(h - 1, endY))

            # extract the face ROI, convert it from BGR to RGB channel
            # ordering, resize it to 224x224, and preprocess it
            face = frame[startY:endY, startX:endX]
            face = cv2.cvtColor(face, cv2.COLOR_BGR2RGB)
            face = cv2.resize(face, (224, 224))
            face = img_to_array(face)
            face = preprocess_input(face)

            # add the face and bounding boxes to their respective
            # lists
            faces.append(face)
            locs.append((startX, startY, endX, endY))

    if len(faces) > 0:
		# for faster inference we'll make batch predictions on *all*
		# faces at the same time rather than one-by-one predictions
        # in the above `for` loop
        faces = np.array(faces, dtype="float32")
        rgb_small_frame = frame[:, :, ::-1]
        face_locations  = face_recognition.face_locations(rgb_small_frame)
        face_encodings  = face_recognition.face_encodings(rgb_small_frame, face_locations)
        if len(face_encodings):

            check = face_recognition.compare_faces(face_1_face_encoding, face_encodings)
            if check[0]:
                    lable = 'Verified'
                    print(lable)

            else :
                    lable = 'Not Verified'
                    print(lable)


    return (locs,lable)

# initialize the camera
def facedect(email):

    cam = VideoStream(src=0).start()   # 0 -> index of camera
    lab = 'Not Verified'
    while True:
        img = cam.read()
        small_frame = imutils.resize(img, width=400)
        # rgb_small_frame = small_frame[:, :, ::-1]
        # face_locations = face_recognition.face_locations(rgb_small_frame)
        # face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)
        # check=face_recognition.compare_faces(face_1_face_encoding, face_encodings)

        
        # if check[0]:
        #         label = 'Verified'
        #         print(label)

        # else :
        #         label = 'Verified'
        #         print(label)

                    
        (locs,lable) = detect_faces(small_frame,email)

        # loop over the detected face locations and their corresponding
        # locations
        for box in locs:
            # unpack the bounding box and predictions
            (startX, startY, endX, endY) = box

            # determine the class label and color we'll use to draw
            # the bounding box and text
            # display the label and bounding box rectangle on the output
            # frame
            color = (0, 255, 0) if lable == "Verified" else (0, 0, 255)

            cv2.putText(small_frame, lable, (startX, startY - 10),
                cv2.FONT_HERSHEY_SIMPLEX, 0.45, color, 2)
            cv2.rectangle(small_frame, (startX, startY), (endX, endY), color, 2)

        cv2.imshow("Frame", small_frame)
        key = cv2.waitKey(2) & 0xFF

        # if the `q` key was pressed, break from the loop
        if key == ord("q"):
            lab = lable
            break
    cv2.destroyAllWindows()
    cam.stop()
    return lab


tensorflow.keras.models.load_model tensorflow.keras.applications.mobilenet_v2.preprocess_input numpy.array tensorflow.keras.preprocessing.image.img_to_array
[(18, 'cv2.dnn.readNet', 'cv2.dnn.readNet', (['prototxtPath', 'weightsPath'], {}), False, 'import cv2\n'), (21, 'tensorflow.keras.models.load_model', 'load_model', (['"""C:\\\\Users\\\\mkjsr\\\\OneDrive\\\\Desktop\\\\Django_mask_attendance\\\\main_base\\\\mask_detector.model"""'], {}), False, 'from tensorflow.keras.models import load_model\n'), (28, 'cv2.dnn.blobFromImage', 'cv2.dnn.blobFromImage', (['frame', '(1.0)', '(224, 224)', '(104.0, 177.0, 123.0)'], {}), False, 'import cv2\n'), (43, 'os.path.join', 'os.path.join', (['BASE_DIR', '"""face_dataset"""'], {}), False, 'import os\n'), (45, 'face_recognition.load_image_file', 'face_recognition.load_image_file', (['loc'], {}), False, 'import face_recognition\n'), (46, 'cv2.resize', 'cv2.resize', (['face_1_image', '(0, 0)'], {'fx': '(0.25)', 'fy': '(0.25)'}), False, 'import cv2\n'), (152, 'cv2.destroyAllWindows', 'cv2.destroyAllWindows', ([], {}), False, 'import cv2\n'), (42, 'os.path.abspath', 'os.path.abspath', (['__file__'], {}), False, 'import os\n'), (48, 'face_recognition.face_encodings', 'face_recognition.face_encodings', (['rgb_small_frame_1'], {}), False, 'import face_recognition\n'), (86, 'numpy.array', 'np.array', (['faces'], {'dtype': '"""float32"""'}), True, 'import numpy as np\n'), (88, 'face_recognition.face_locations', 'face_recognition.face_locations', (['rgb_small_frame'], {}), False, 'import face_recognition\n'), (89, 'face_recognition.face_encodings', 'face_recognition.face_encodings', (['rgb_small_frame', 'face_locations'], {}), False, 'import face_recognition\n'), (111, 'imutils.resize', 'imutils.resize', (['img'], {'width': '(400)'}), False, 'import imutils\n'), (145, 'cv2.imshow', 'cv2.imshow', (['"""Frame"""', 'small_frame'], {}), False, 'import cv2\n'), (72, 'cv2.cvtColor', 'cv2.cvtColor', (['face', 'cv2.COLOR_BGR2RGB'], {}), False, 'import cv2\n'), (73, 'cv2.resize', 'cv2.resize', (['face', '(224, 224)'], {}), False, 'import cv2\n'), (74, 'tensorflow.keras.preprocessing.image.img_to_array', 'img_to_array', (['face'], {}), False, 'from tensorflow.keras.preprocessing.image import img_to_array\n'), (75, 'tensorflow.keras.applications.mobilenet_v2.preprocess_input', 'preprocess_input', (['face'], {}), False, 'from tensorflow.keras.applications.mobilenet_v2 import preprocess_input\n'), (92, 'face_recognition.compare_faces', 'face_recognition.compare_faces', (['face_1_face_encoding', 'face_encodings'], {}), False, 'import face_recognition\n'), (107, 'imutils.video.VideoStream', 'VideoStream', ([], {'src': '(0)'}), False, 'from imutils.video import VideoStream\n'), (141, 'cv2.putText', 'cv2.putText', (['small_frame', 'lable', '(startX, startY - 10)', 'cv2.FONT_HERSHEY_SIMPLEX', '(0.45)', 'color', '(2)'], {}), False, 'import cv2\n'), (143, 'cv2.rectangle', 'cv2.rectangle', (['small_frame', '(startX, startY)', '(endX, endY)', 'color', '(2)'], {}), False, 'import cv2\n'), (146, 'cv2.waitKey', 'cv2.waitKey', (['(2)'], {}), False, 'import cv2\n'), (61, 'numpy.array', 'np.array', (['[w, h, w, h]'], {}), True, 'import numpy as np\n')]