Exemple #1
0
class Recognizer(object):
    def __init__(self, model, camera_id, cascade_filename, run_local, wait=50):
        self.model = model
        self.wait = wait
        self.detector = CascadedDetector(cascade_fn=cascade_filename,
                                         minNeighbors=5,
                                         scaleFactor=1.1)
        if run_local:
            self.cam = create_capture(camera_id)

    def run(self):
        while True:
            ret, frame = self.cam.read()
            # Resize the frame to half the original size for speeding up the detection process:
            # img = cv2.resize(frame, (frame.shape[1] / 2, frame.shape[0] / 2), interpolation=cv2.INTER_CUBIC)
            img = cv2.resize(frame, (320, 240), interpolation=cv2.INTER_CUBIC)
            imgout = img.copy()
            for i, r in enumerate(self.detector.detect(img)):
                x0, y0, x1, y1 = r
                # (1) Get face, (2) Convert to grayscale & (3) resize to image_size:
                face = img[y0:y1, x0:x1]
                face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)
                face = cv2.resize(face,
                                  self.model.image_size,
                                  interpolation=cv2.INTER_CUBIC)
                prediction = self.model.predict(face)
                predicted_label = prediction[0]
                classifier_output = prediction[1]
                # Now let's get the distance from the assuming a 1-Nearest Neighbor.
                # Since it's a 1-Nearest Neighbor only look take the zero-th element:
                distance = classifier_output['distances'][0]
                # Draw the face area in image:
                cv2.rectangle(imgout, (x0, y0), (x1, y1), (0, 0, 255), 2)
                # Draw the predicted name (folder name...):
                draw_str(imgout, (x0 - 20, y0 - 40),
                         "Label " + self.model.subject_names[predicted_label])
                draw_str(imgout, (x0 - 20, y0 - 20),
                         "Feature Distance " + "%1.1f" % distance)
            cv2.imshow('OCVFACEREC < LOCAL', imgout)
            key = cv2.waitKey(self.wait)
            if 'q' == chr(key & 255):
                print ">> 'q' Pressed. Exiting."
                break
class Recognizer(object):
    def __init__(self, model, camera_id, cascade_filename, run_local, wait=50):
        self.model = model
        self.wait = wait
        self.detector = CascadedDetector(cascade_fn=cascade_filename, minNeighbors=5, scaleFactor=1.1)
        if run_local:
            self.cam = create_capture(camera_id)

    def run(self):
        while True:
            ret, frame = self.cam.read()
            # Resize the frame to half the original size for speeding up the detection process:
            # img = cv2.resize(frame, (frame.shape[1] / 2, frame.shape[0] / 2), interpolation=cv2.INTER_CUBIC)
            img = cv2.resize(frame, (320, 240), interpolation=cv2.INTER_CUBIC)
            imgout = img.copy()
            for i, r in enumerate(self.detector.detect(img)):
                x0, y0, x1, y1 = r
                # (1) Get face, (2) Convert to grayscale & (3) resize to image_size:
                face = img[y0:y1, x0:x1]
                face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)
                face = cv2.resize(face, self.model.image_size, interpolation=cv2.INTER_CUBIC)
                prediction = self.model.predict(face)
                predicted_label = prediction[0]
                classifier_output = prediction[1]
                # Now let's get the distance from the assuming a 1-Nearest Neighbor.
                # Since it's a 1-Nearest Neighbor only look take the zero-th element:
                distance = classifier_output['distances'][0]
                # Draw the face area in image:
                cv2.rectangle(imgout, (x0, y0), (x1, y1), (0, 0, 255), 2)
                # Draw the predicted name (folder name...):
                draw_str(imgout, (x0 - 20, y0 - 40), "Label " + self.model.subject_names[predicted_label])
                draw_str(imgout, (x0 - 20, y0 - 20), "Feature Distance " + "%1.1f" % distance)
            cv2.imshow('OCVFACEREC < LOCAL', imgout)
            key = cv2.waitKey(self.wait)
            if 'q' == chr(key & 255):
                print ">> 'q' Pressed. Exiting."
                break