def predict(self, image_path, threshold=0.85): utils = IU() model = load_model('models/predictor_NN_model.h5') image = cv2.imread(image_path) locs = utils.get_face_locations(image=image, model=self.face_model, scaleup=self.upsample) encodings = list() for (sX, sY, eX, eY) in locs: face = cv2.resize(image[sY:eY, sX:eX], (160, 160)) encoding = utils.face_encodings(face, model=self.encoding_model, jitters=self.jitters) if len(encoding) == 0: continue else: encodings.append(encoding[0]) if len(encodings) == 0: return False data = np.vstack(encodings) predictions = model.predict(data) for prediction in predictions: if prediction[0] > threshold: return True return False
def convolve2d(img, kernel): """ Performs convolution in 2D array. :param img: numpy 2D array: input array :param kernel: array 3x3: convolution kernel :return: numpy 2D array: result of convolution, image has same shape as input """ kernel = np.flip(kernel, 0) kernel = np.flip(kernel, 1) output = np.zeros(img.shape) window_size = 3 padded_input = ImageUtilities.pad_image(img, 1) h, w = img.shape for x in range(0, w): for y in range(0, h): window = padded_input[y:y + window_size, x:x + window_size] window = np.multiply(window, kernel) output[y, x] = window.sum() return output
def median_filter(img, window_size): """ Filters image using median filter. :param img: numpy 2D array: array to filter :param window_size: int: size of filter :return: numpy 2D array: filtered image, same shape as input """ if Config.USE_CV2_FILTERS: return cv2.medianBlur(img, 3) output = np.zeros(img.shape) padded_input = ImageUtilities.pad_image(img, 1) half_win_size = window_size // 2 h, w = img.shape mid = (window_size**2) // 2 for x in range(0, w): for y in range(0, h): window = padded_input[y:y + window_size, x:x + window_size] window = window.flatten() window.sort() val = window[mid] output[y][x] = val return output
def generate_training_set(self): if self.paths is None or len(self.paths) < 10: error = QErrorMessage() error.showMessage('Select atleast 10 images') error.exec_() else: utils = IU() self.status.setText('Generating training data...') encodings = list() for image_path in self.paths: image = cv2.imread(image_path) (sX, sY, eX, eY) = utils.get_face_locations(image=image)[0] face = cv2.resize(image[sY:eY, sX:eX], (200, 200)) face = face[:, :, ::-1] encoding = FR.face_encodings(face, num_jitters=10, known_face_locations=[(sX, sY, eX, eY)]) if len(encoding) == 0: pass else: encodings.append(encoding[0]) with open('models/training_data.clf', 'wb') as file: pickle.dump(encodings, file) self.status.setText('Done!')