Exemplo n.º 1
0
class App(object):
    def __init__(self, model, camera_id, cascade_filename):
        self.model = model
        self.detector = CascadedDetector(cascade_fn=cascade_filename,
                                         minNeighbors=5,
                                         scaleFactor=1.1)
        # self.camera_id = camera_id

    def run(self):
        while True:
            array, _ = freenect.sync_get_video()
            frame = cv2.cvtColor(array, cv2.COLOR_RGB2BGR)
            # 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)
            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)
                # Get a prediction from the model:
                prediction = self.model.predict(face)[0]
                # Draw the face area in image:
                requested_name = 'Hao'
                if self.model.subject_names[prediction] == requested_name:
                    cv2.rectangle(imgout, (x0, y0), (x1, y1), (0, 255, 0), 2)
                    # Draw the predicted name (folder name...):
                    draw_str(imgout, (x0 - 20, y0 - 20),
                             self.model.subject_names[prediction])
            cv2.imshow('videofacerec', imgout)
            # Show image & exit on escape:
            ch = cv2.waitKey(10)
            if ch == 27:
                break
Exemplo n.º 2
0
 def __init__(self, model, camera_id, cascade_filename):
     self.model = model
     self.detector = CascadedDetector(cascade_fn=cascade_filename,
                                      minNeighbors=5,
                                      scaleFactor=1.1)