def loadNet(self): global Pnet, Rnet, Onet Pnet = create_Kao_Pnet(r'12net.h5') Rnet = create_Kao_Rnet(r'24net.h5') Onet = create_Kao_Onet(r'48net.h5') # will not work. caffe and TF incompatible img = cv2.imread('data/0001.png') scale_img = cv2.resize(img, (100, 100)) input = scale_img.reshape(1, *scale_img.shape) Pnet.predict(input) img = cv2.imread('data/0001.png') scale_img = cv2.resize(img, (24, 24)) input = scale_img.reshape(1, *scale_img.shape) Rnet.predict(input) img = cv2.imread('data/0001.png') scale_img = cv2.resize(img, (48, 48)) input = scale_img.reshape(1, *scale_img.shape) Onet.predict(input) return Pnet, Rnet, Onet
import sys import tools_matrix as tools import cv2 import numpy as np import matplotlib.pyplot as plt import time from MTCNN import create_Kao_Onet, create_Kao_Rnet, create_Kao_Pnet Pnet = create_Kao_Pnet(r'12net.h5') Rnet = create_Kao_Rnet(r'24net.h5') Onet = create_Kao_Onet(r'48net.h5') # will not work. caffe and TF incompatible def detectFace(img, threshold): caffe_img = (img.copy() - 127.5) / 127.5 origin_h, origin_w, ch = caffe_img.shape scales = tools.calculateScales(img) out = [] t0 = time.time() # del scales[:4] for scale in scales: hs = int(origin_h * scale) ws = int(origin_w * scale) scale_img = cv2.resize(caffe_img, (ws, hs)) input = scale_img.reshape(1, *scale_img.shape) ouput = Pnet.predict( input ) # .transpose(0,2,1,3) should add, but seems after process is wrong then. out.append(ouput)
#Usage: Extracted the faces of all the images in the DIR_PATH and save it to tmp/ #Ref :https://github.com/xiangrufan/keras-mtcnn import tools import os import cv2 import numpy as np import time from MTCNN import create_Kao_Onet, create_Kao_Rnet, create_Kao_Pnet #The threshold can be modified for custom usage #Example: For the original high-accuracy usage :threshold = [0.6,0.6,0.7] threshold = [0.4, 0.4, 0.5] DIR_PATH = 'path_to_imagedir/image_dir/' Pnet = create_Kao_Pnet('./weight/12net.h5') Rnet = create_Kao_Rnet('./weight/24net.h5') Onet = create_Kao_Onet( './weight/48net.h5') # will not work. caffe and TF incompatible def detectFace(img, threshold): caffe_img = (img.copy() - 127.5) / 127.5 origin_h, origin_w, ch = caffe_img.shape scales = tools.calculateScales(img) out = [] t0 = time.time() # del scales[:4] for scale in scales: