from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
from tensorflow.keras.models import load_model
import numpy as np
import cv2

class MaskDetection:
    def __init__(self, image, facenet, model):
        self.image = image
        self.facenet = facenet
        self.model = model
        print("Import Success")

    def execute(self):
        img = image
        h, w = img.shape[:2]

        blob = cv2.dnn.blobFromImage(img, scalefactor=1., size=(300, 300), mean=(104., 177., 123.))
        facenet.setInput(blob)
        dets = facenet.forward()

        faces = []

        for i in range(dets.shape[2]):
            confidence = dets[0, 0, i, 2]
            if confidence < 0.5:
                continue

            x1 = int(dets[0, 0, i, 3] * w)
            y1 = int(dets[0, 0, i, 4] * h)
            x2 = int(dets[0, 0, i, 5] * w)
            y2 = int(dets[0, 0, i, 6] * h)
    
            face = img[y1:y2, x1:x2]
            faces.append(face)

        for i, face in enumerate(faces):
            face_input = cv2.resize(face, dsize=(224, 224))
            face_input = cv2.cvtColor(face_input, cv2.COLOR_BGR2RGB)
            face_input = preprocess_input(face_input)
            face_input = np.expand_dims(face_input, axis=0)
    
            mask, nomask = model.predict(face_input).squeeze()

            if mask > nomask:
                color = (0, 255, 0)
                label = 'Mask %d%%' % (mask * 100)
            else:
                color = (0, 0, 255)
                label = 'No Mask %d%%' % (nomask * 100)
            cv2.rectangle(img, pt1=(x1, y1), pt2=(x2, y2), thickness=2, color=color, lineType=cv2.LINE_AA)
            cv2.putText(img, text=label, org=(x1, y1 - 10), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=0.8,
                    color=color, thickness=2, lineType=cv2.LINE_AA)
        #return cv2.imwrite('result.bmp',img)
        #return img
        return 
ca25ea414c0ba8b3b29a5badd66fa5675f869fcc
<mask token>


class MaskDetection:
    <mask token>
    <mask token>

<mask token>


class MaskDetection:

    def __init__(self, image, facenet, model):
        self.image = image
        self.facenet = facenet
        self.model = model
        print('Import Success')
    <mask token>

<mask token>


class MaskDetection:

    def __init__(self, image, facenet, model):
        self.image = image
        self.facenet = facenet
        self.model = model
        print('Import Success')

    def execute(self):
        img = image
        h, w = img.shape[:2]
        blob = cv2.dnn.blobFromImage(img, scalefactor=1.0, size=(300, 300),
            mean=(104.0, 177.0, 123.0))
        facenet.setInput(blob)
        dets = facenet.forward()
        faces = []
        for i in range(dets.shape[2]):
            confidence = dets[0, 0, i, 2]
            if confidence < 0.5:
                continue
            x1 = int(dets[0, 0, i, 3] * w)
            y1 = int(dets[0, 0, i, 4] * h)
            x2 = int(dets[0, 0, i, 5] * w)
            y2 = int(dets[0, 0, i, 6] * h)
            face = img[y1:y2, x1:x2]
            faces.append(face)
        for i, face in enumerate(faces):
            face_input = cv2.resize(face, dsize=(224, 224))
            face_input = cv2.cvtColor(face_input, cv2.COLOR_BGR2RGB)
            face_input = preprocess_input(face_input)
            face_input = np.expand_dims(face_input, axis=0)
            mask, nomask = model.predict(face_input).squeeze()
            if mask > nomask:
                color = 0, 255, 0
                label = 'Mask %d%%' % (mask * 100)
            else:
                color = 0, 0, 255
                label = 'No Mask %d%%' % (nomask * 100)
            cv2.rectangle(img, pt1=(x1, y1), pt2=(x2, y2), thickness=2,
                color=color, lineType=cv2.LINE_AA)
            cv2.putText(img, text=label, org=(x1, y1 - 10), fontFace=cv2.
                FONT_HERSHEY_SIMPLEX, fontScale=0.8, color=color, thickness
                =2, lineType=cv2.LINE_AA)
        return

from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
from tensorflow.keras.models import load_model
import numpy as np
import cv2


class MaskDetection:

    def __init__(self, image, facenet, model):
        self.image = image
        self.facenet = facenet
        self.model = model
        print('Import Success')

    def execute(self):
        img = image
        h, w = img.shape[:2]
        blob = cv2.dnn.blobFromImage(img, scalefactor=1.0, size=(300, 300),
            mean=(104.0, 177.0, 123.0))
        facenet.setInput(blob)
        dets = facenet.forward()
        faces = []
        for i in range(dets.shape[2]):
            confidence = dets[0, 0, i, 2]
            if confidence < 0.5:
                continue
            x1 = int(dets[0, 0, i, 3] * w)
            y1 = int(dets[0, 0, i, 4] * h)
            x2 = int(dets[0, 0, i, 5] * w)
            y2 = int(dets[0, 0, i, 6] * h)
            face = img[y1:y2, x1:x2]
            faces.append(face)
        for i, face in enumerate(faces):
            face_input = cv2.resize(face, dsize=(224, 224))
            face_input = cv2.cvtColor(face_input, cv2.COLOR_BGR2RGB)
            face_input = preprocess_input(face_input)
            face_input = np.expand_dims(face_input, axis=0)
            mask, nomask = model.predict(face_input).squeeze()
            if mask > nomask:
                color = 0, 255, 0
                label = 'Mask %d%%' % (mask * 100)
            else:
                color = 0, 0, 255
                label = 'No Mask %d%%' % (nomask * 100)
            cv2.rectangle(img, pt1=(x1, y1), pt2=(x2, y2), thickness=2,
                color=color, lineType=cv2.LINE_AA)
            cv2.putText(img, text=label, org=(x1, y1 - 10), fontFace=cv2.
                FONT_HERSHEY_SIMPLEX, fontScale=0.8, color=color, thickness
                =2, lineType=cv2.LINE_AA)
        return

from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
from tensorflow.keras.models import load_model
import numpy as np
import cv2

class MaskDetection:
    def __init__(self, image, facenet, model):
        self.image = image
        self.facenet = facenet
        self.model = model
        print("Import Success")

    def execute(self):
        img = image
        h, w = img.shape[:2]

        blob = cv2.dnn.blobFromImage(img, scalefactor=1., size=(300, 300), mean=(104., 177., 123.))
        facenet.setInput(blob)
        dets = facenet.forward()

        faces = []

        for i in range(dets.shape[2]):
            confidence = dets[0, 0, i, 2]
            if confidence < 0.5:
                continue

            x1 = int(dets[0, 0, i, 3] * w)
            y1 = int(dets[0, 0, i, 4] * h)
            x2 = int(dets[0, 0, i, 5] * w)
            y2 = int(dets[0, 0, i, 6] * h)
    
            face = img[y1:y2, x1:x2]
            faces.append(face)

        for i, face in enumerate(faces):
            face_input = cv2.resize(face, dsize=(224, 224))
            face_input = cv2.cvtColor(face_input, cv2.COLOR_BGR2RGB)
            face_input = preprocess_input(face_input)
            face_input = np.expand_dims(face_input, axis=0)
    
            mask, nomask = model.predict(face_input).squeeze()

            if mask > nomask:
                color = (0, 255, 0)
                label = 'Mask %d%%' % (mask * 100)
            else:
                color = (0, 0, 255)
                label = 'No Mask %d%%' % (nomask * 100)
            cv2.rectangle(img, pt1=(x1, y1), pt2=(x2, y2), thickness=2, color=color, lineType=cv2.LINE_AA)
            cv2.putText(img, text=label, org=(x1, y1 - 10), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=0.8,
                    color=color, thickness=2, lineType=cv2.LINE_AA)
        #return cv2.imwrite('result.bmp',img)
        #return img
        return 