Example #1
0
 def __init__(self, filename='config.csv', video=0):
     self.mask = None
     self.detected = False
     self.right = True
     self.forward = False
     self.rest = False
     self.frame = np.zeros([600, 600, 3], dtype=np.uint8).fill(255)
     self.distance = 0
     self.vs = cv2.VideoCapture(video)
     self.mask_detector = MaskDetector(filename)
Example #2
0
class DlibProcessor(BaseProcessor):
    def __init__(self):
        super().__init__()
        self.recognizer = Recognizer()
        self.detector = MaskDetector()

    def process_detected_faces(self, image, locations):
        self.process_faces(image, locations)

    def get_face_locations(self, image):
        return face_recognition.face_locations(image)

    def process_faces(self, image, locations):
        mask_per_person = self.detector.detect_and_predict_mask(image, locations)
        face_encodings_on_frame = face_recognition.face_encodings(image, locations)
        face_landmarks_list = face_recognition.face_landmarks(image, locations)

        number_of_faces = len(face_landmarks_list)
        number_of_face_encoded = len(face_encodings_on_frame)
        print("I found {} face(s) and encoded {} faces in this photograph.".format(number_of_faces,
                                                                                   number_of_face_encoded))
        if len(mask_per_person) != len(locations):
            return

        for index in range(0, len(locations)):

            location = locations[index]
            in_mask = self.detector.is_mask(mask_per_person[index])
            print("In mask", in_mask)

            if in_mask:
                color = (0, 255, 0)
            else:
                color = (0, 0, 255)

            if self.config.drawLandmarks:
                face_landmarks = face_landmarks_list[index]
                self.draw_landmarks(face_landmarks, image)

            if self.config.enableFaceRecognition:
                print("location dlib ", location)
                face_encoding = face_encodings_on_frame[index]
                person_name = self.recognizer.recognize_person(face_encoding)
                print("Recognized : ", person_name)
            else:
                person_name = None

            image = self.draw_face_rectangle(image, location, self.get_rectangle_title(in_mask, person_name), color)
Example #3
0
class CustomModelProcessor(BaseProcessor):
    def __init__(self):
        super().__init__()
        self.recognizer = Recognizer()
        self.detector = MaskDetector()
        self.face_detector = FaceDetector()
        self.dlibProcessor = DlibProcessor()

    def get_face_locations(self, image):
        return self.face_detector.detect_faces(image)

    def process_detected_faces(self, image, faces):
        if faces is None or len(faces) == 0:
            return

        detected_faces, locations = [], []
        for x, y in faces:
            detected_faces.append(x)
            locations.append(y)

        mask_per_person = self.detector.predict_mask(detected_faces)
        if len(mask_per_person) < len(faces):
            return
        all_without_mask = self.is_all_without_masks(mask_per_person)
        if all_without_mask:
            self.dlibProcessor.process_single_image(image)
        else:
            self.process_with_masks(faces, image, locations, mask_per_person)

    def process_with_masks(self, faces, image, locations, mask_per_person):
        for index in range(0, len(faces)):
            location = locations[index]
            (mask, withoutMask) = mask_per_person[index]

            # determine the class label and color we'll use to draw
            # the bounding box and text
            in_mask = mask > withoutMask
            label = "Mask" if in_mask else "No Mask"
            if label == "Mask":
                color = (0, 255, 0)
            else:
                color = (0, 0, 255)

            label = "{}: {:.2f}%".format(label, max(mask, withoutMask) * 100)
            image = self.draw_face_rectangle(image, location, label, color)

    def is_all_without_masks(self, mask_per_persons):
        if mask_per_persons is None or len(mask_per_persons) == 0:
            return True

        for index in range(0, len(mask_per_persons)):
            (mask, without_mask) = mask_per_persons[index]
            if mask > without_mask:
                return False
        return True
Example #4
0
 def __init__(self):
     super().__init__()
     self.recognizer = Recognizer()
     self.detector = MaskDetector()
Example #5
0
 def __init__(self):
     super().__init__()
     self.recognizer = Recognizer()
     self.detector = MaskDetector()
     self.face_detector = FaceDetector()
     self.dlibProcessor = DlibProcessor()
Example #6
0
class BallDetector(object):
    def __init__(self, filename='config.csv', video=0):
        self.mask = None
        self.detected = False
        self.right = True
        self.forward = False
        self.rest = False
        self.frame = np.zeros([600, 600, 3], dtype=np.uint8).fill(255)
        self.distance = 0
        self.vs = cv2.VideoCapture(video)
        self.mask_detector = MaskDetector(filename)

    def getFrame(self):
        _, img = self.vs.read()
        self.frame = img
        self.frame = imutils.resize(self.frame, width=600)
        self.view = self.frame.copy()
        return self.frame

    def detectBall(self):
        self.mask = self.mask_detector.getMask(self.getFrame())
        cnts = cv2.findContours(self.mask.copy(), cv2.RETR_EXTERNAL,
                                cv2.CHAIN_APPROX_SIMPLE)
        cnts = imutils.grab_contours(cnts)
        radius = 0
        self.rest = False
        self.detected = False
        if len(cnts) > 0:
            self.detected = True
            c = max(cnts, key=cv2.contourArea)
            ((x, y), radius) = cv2.minEnclosingCircle(c)
            cv2.circle(self.view, (int(x), int(y)), int(radius), (0, 255, 255),
                       2)

            distance = 0
            if (radius > MIN_RADIUS) and (radius < MAX_RADIUS):
                # Values range
                distance = map_values(radius, MIN_RADIUS, MAX_RADIUS,
                                      MIN_DISTANCE, MAX_DISTANCE)
                distance = abs(distance - 100)
                print(distance)

                self.rest = True
                self.forward = False

                if distance > 20:
                    self.rest = False
                    if (x >= 0) and (x < 100):
                        self.right = False
                    elif (x >= 100) and (x < 500):
                        self.forward = True
                    else:
                        self.right = True

    def getValue(self):
        self.detectBall()
        cv2.imshow("Frame", self.view)
        cv2.waitKey(1)
        return self.detected, self.forward, self.right, self.rest

    def close(self):
        self.vs.release()
Example #7
0
import cv2
from detector import MaskDetector
from detector import ColorDetertor

detect_mask = MaskDetector(filename='config.csv')
detect_color = ColorDetertor(filename='config.csv')

image = cv2.imread('../images/sample.jpg')

mask = detect_mask.getMask(image)
color = detect_color.detect(image)
print('Color Status: {}'.format(color))
cv2.imshow('Mask', mask)
cv2.waitKey(0)