def __init__(self, margin_ratio=1.3, resize_factor=1.0): super().__init__(margin_ratio, resize_factor) # load mtcnn model self._mtcnnModel = MTCNN()
def main(): #title st.title('Face Tracking Application') #sidebar title st.sidebar.title('Face Tracking Application') #markdown st.markdown( 'In this application we will track the face of a person by their Face ID' ) st.sidebar.markdown( 'In this application we will track the face of a person by their Face ID' ) #min confidence #onfidence = st.sidebar.slider('Min Confidence',min_value=0.0, max_value=1.0, value = 0.4) min_face_size = st.sidebar.slider('Min Face Size you want to detect', min_value=1, max_value=200, value=40) util.required_height = st.sidebar.slider('Height of the Face', min_value=0, max_value=200, value=10) #define the input file input_file = st.text_input('Input File Path', value='test.mp4') filename = open(input_file, 'rb') video_bytes = filename.read() st.sidebar.text('Input Video') st.sidebar.video(video_bytes) face_detector = MTCNN( min_face_size=min_face_size) #Initializing MTCNN detector object face_tracker = Sort(max_age=50) stframe = st.empty() vid = cv2.VideoCapture(input_file) video_frame_cnt = int(vid.get(7)) video_width = int(vid.get(3)) video_height = int(vid.get(4)) video_fps = int(vid.get(5)) record_video = True out = cv2.VideoWriter('output.mp4', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), video_fps, (video_width, video_height)) output = st.checkbox('Save The Video', value=False) while True: ret, frame = vid.read() original_frame = frame.copy() frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) result = face_detector.detect_faces(frame) box = [] for i in range(len(result)): box_ = result[i]["box"] print("Face Detectes: Box=", box_) box.append([ box_[0], box_[1], box_[0] + box_[2], box_[1] + box_[3], result[i]["confidence"] ]) dets = np.array(box) track_bbx, pts = face_tracker.update(dets) _MODEL_SIZE = [original_frame.shape[1], original_frame.shape[0]] original_frame = track_img(original_frame, track_bbx, _MODEL_SIZE, pts) #if record_video: #out.write(original_frame) #cv2.imshow('Frame',original_frame) stframe.image(original_frame, use_column_width=True, channels='BGR') key = cv2.waitKey(1) & 0xFF out.write(original_frame) if output: filename1 = open('output.mp4', 'rb') video_bites1 = filename1.read() st.text("Output Video:") st.video(video_bites1) break
def main(): try: os.mkdir('./img') except OSError: pass K.set_learning_phase(0) # make sure its testing mode # face_cascade = cv2.CascadeClassifier('lbpcascade_frontalface_improved.xml') detector = MTCNN() # load model and weights img_size = 64 stage_num = [3, 3, 3] lambda_local = 1 lambda_d = 1 img_idx = 0 detected = '' # make this not local variable time_detection = 0 time_network = 0 time_plot = 0 skip_frame = 5 # every 5 frame do 1 detection and network forward propagation ad = 0.6 # Parameters num_capsule = 3 dim_capsule = 16 routings = 2 stage_num = [3, 3, 3] lambda_d = 1 num_classes = 3 image_size = 64 num_primcaps = 7*3 m_dim = 5 S_set = [num_capsule, dim_capsule, routings, num_primcaps, m_dim] model1 = FSA_net_Capsule(image_size, num_classes, stage_num, lambda_d, S_set)() model2 = FSA_net_Var_Capsule( image_size, num_classes, stage_num, lambda_d, S_set)() num_primcaps = 8*8*3 S_set = [num_capsule, dim_capsule, routings, num_primcaps, m_dim] model3 = FSA_net_noS_Capsule( image_size, num_classes, stage_num, lambda_d, S_set)() print('Loading models ...') weight_file1 = '../pre-trained/300W_LP_models/fsanet_capsule_3_16_2_21_5/fsanet_capsule_3_16_2_21_5.h5' model1.load_weights(weight_file1) print('Finished loading model 1.') weight_file2 = '../pre-trained/300W_LP_models/fsanet_var_capsule_3_16_2_21_5/fsanet_var_capsule_3_16_2_21_5.h5' model2.load_weights(weight_file2) print('Finished loading model 2.') weight_file3 = '../pre-trained/300W_LP_models/fsanet_noS_capsule_3_16_2_192_5/fsanet_noS_capsule_3_16_2_192_5.h5' model3.load_weights(weight_file3) print('Finished loading model 3.') inputs = Input(shape=(64, 64, 3)) x1 = model1(inputs) # 1x1 x2 = model2(inputs) # var x3 = model3(inputs) # w/o avg_model = Average()([x1, x2, x3]) model = Model(inputs=inputs, outputs=avg_model) # capture video cap = cv2.VideoCapture(0) cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1024*1) cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 768*1) print('Start detecting pose ...') detected_pre = [] while True: # get video frame ret, input_img = cap.read() img_idx = img_idx + 1 img_h, img_w, _ = np.shape(input_img) if img_idx == 1 or img_idx % skip_frame == 0: time_detection = 0 time_network = 0 time_plot = 0 # 使用LBP检测器检测人脸 gray_img = cv2.cvtColor(input_img, cv2.COLOR_BGR2GRAY) # detected = face_cascade.detectMultiScale(gray_img, 1.1) detected = detector.detect_faces(input_img) if len(detected_pre) > 0 and len(detected) == 0: detected = detected_pre faces = np.empty((len(detected), img_size, img_size, 3)) input_img = draw_results_mtcnn( detected, input_img, faces, ad, img_size, img_w, img_h, model, time_detection, time_network, time_plot) cv2.imwrite('img/'+str(img_idx)+'.png', input_img) else: input_img = draw_results_mtcnn( detected, input_img, faces, ad, img_size, img_w, img_h, model, time_detection, time_network, time_plot) if len(detected) > len(detected_pre) or img_idx % (skip_frame*3) == 0: detected_pre = detected key = cv2.waitKey(1)
import cv2 import tensorflow as tf import numpy as np from mtcnn.mtcnn import MTCNN import pickle face_detector = MTCNN(min_face_size=50) facenet = tf.keras.models.load_model('.\\models\\facenet.h5') [model, names] = pickle.load(open('.\\models\\SVC_classifier.pkl', 'rb')) video = cv2.VideoCapture(0) while True: ret, frame = video.read() frame = cv2.flip(frame, 1) faces = face_detector.detect_faces(frame) if len(faces) >= 1: for face in faces: [x, y, w, h] = face['box'] x, y, w, h = abs(x), abs(y), abs(w), abs(h) face_box = tf.expand_dims(cv2.resize( frame[y:y + h, x:x + w] / 255.0, (160, 160)), axis=0) face_box = (face_box - np.mean(face_box)) / np.std(face_box) faceEmbed = facenet.predict(face_box) prediction = model.predict(faceEmbed) name = names[prediction[0]] if max(model.predict_proba(faceEmbed)[0]) > 0.8: cv2.putText(frame, name, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
import numpy as np import matplotlib.pyplot as plt import cv2 import os from PIL import Image from mtcnn.mtcnn import MTCNN train_dir = 'data/train' valid_dir = 'data/val' face_detector = MTCNN() # for i in os.listdir(train_dir): # print(i) # my_img = 'data/train/madonna/httpiamediaimdbcomimagesMMVBMTANDQNTAxNDVeQTJeQWpwZBbWUMDIMjQOTYVUXCRALjpg.jpg' # img = img.convert("RGB") def extract_faces(filename): img_path = filename img = Image.open(img_path) img = img.convert("RGB") pixels = np.asarray(img) results = face_detector.detect_faces(pixels) x1, y1, width, height = results[0]['box'] x1 = abs(x1) y1 = abs(y1) x2, y2 = x1 + width, y1 + height face = pixels[y1:y2, x1:x2]
def FaceRec(path): img = pyplot.imread(path) detector = MTCNN() faces = detector.detect_faces(img) output = resize_faces(path, faces) return output
def detect_face(self): # mysql connection 얻어오기 mydb, mycursor = get_cursor() def draw_boundary(img, classifier, scaleFactor, minNeighbors, color, text, clf): features = None features = classifier.detect_faces(img) gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) coords = [] for feature in features: x, y, w, h = feature['box'] cv2.rectangle(img, (x, y), (x + w, y + h), color, 2) id, pred = clf.predict(gray_image[y:y + h, x:x + w]) confidence = int(100 * (1 - pred / 300)) mydb, mycursor = get_cursor() mycursor.execute( "SELECT user_name from last_member where id=" + str(id)) s = mycursor.fetchone() s = '' + ''.join(s) c_s = s + '님 출석체크를 완료했습니다.' if confidence > 80: cv2.putText(img, s + str(confidence) + "%", (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 1, cv2.LINE_AA) if confidence >= 85: mycursor.execute( "UPDATE last_member SET readcount = readcount + 1 WHERE user_name= '" + s + "'") messagebox.showinfo('Result', c_s) messagebox.showinfo('Result', '종료를 원하시면 Enter를 눌러 주세요.') else: cv2.putText(img, "Unknown", (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 1, cv2.LINE_AA) coords = [x, y, w, h] return coords def recognize(img, clf, faceCascade): coords = draw_boundary( img, faceCascade, 1.1, # scaleFactor 10, # minNeighbors (255, 255, 255), "Face", clf) return img faceCascade = MTCNN() clf = cv2.face.LBPHFaceRecognizer_create() clf.read("capstone-20-1-face-detection/classifier.xml") video_capture = cv2.VideoCapture(0, cv2.CAP_MSMF) while True: ret, img = video_capture.read() img = recognize(img, clf, faceCascade) cv2.imshow("face detection", img) if cv2.waitKey(1) == 13 or 0xFF == ord('q'): break video_capture.release() cv2.destroyAllWindows() mydb.commit() mydb.close() self.bottom_title.focus()
from .Get_Embeddings import get_embedding import pickle import cv2 import numpy as np from numpy import asarray from numpy import savez_compressed from numpy import load from numpy import expand_dims from sklearn.metrics import accuracy_score from sklearn.preprocessing import LabelEncoder from sklearn.preprocessing import Normalizer from PIL import Image import mtcnn from mtcnn.mtcnn import MTCNN detector = MTCNN() # Creating instance from the class MTCNN def multiple_faces(filename, required_size=(160, 160)): data = load(EMBEDDINGS_PATH + '/Embeddings-dataset.npz') trainy = data['arr_1'] out_encoder = LabelEncoder() out_encoder.fit(trainy) trainy = out_encoder.transform(trainy) # LOAD THE MODEL print("TESTING ON AN IMAGE") print("LOADING THE MODEL...") svm_model = pickle.load(open(SVM_MODEL_PATH + '/svm_model.sav', 'rb')) print("DONE LOADING THE MODEL!") print("LOADING THE IMAGE...")
def get_file_faces(filename=None, bytes=None): result = dict() if filename is None and bytes is None: result['error'] = 'You need to either set the filename or the bytes' return result if filename is not None and not os.path.isfile(filename): result['error'] = 'File does not exist' return result try: if filename is not None: img = cv2.imread(filename) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) else: # https://stackoverflow.com/questions/17170752/python-opencv-load-image-from-byte-string img_array = np.fromstring(bytes, np.uint8) img = cv2.imdecode(img_array, cv2.IMREAD_COLOR) height, width, channels = img.shape detector = MTCNN() faces = detector.detect_faces(img) data = [] for face in faces: item = face print(item) item['box'] = [ item['box'][0] / width, item['box'][1] / height, item['box'][2] / width, item['box'][3] / height, ] item['keypoints'] = { 'left_eye': [ item['keypoints']['left_eye'][0] / width, item['keypoints']['left_eye'][1] / height, ], 'right_eye': [ item['keypoints']['right_eye'][0] / width, item['keypoints']['right_eye'][1] / height, ], 'nose': [ item['keypoints']['nose'][0] / width, item['keypoints']['nose'][1] / height, ], 'mouth_left': [ item['keypoints']['mouth_left'][0] / width, item['keypoints']['mouth_left'][1] / height, ], 'mouth_right': [ item['keypoints']['mouth_right'][0] / width, item['keypoints']['mouth_right'][1] / height, ], } data.append(item) result['data'] = data except: result['error'] = 'Something went wrong' return result
def __init__(self, path, optimize, minfacesize): from mtcnn.mtcnn import MTCNN # lazy loading self._optimize = optimize self._minfacesize = minfacesize self._detector = MTCNN(min_face_size = minfacesize)
def main_test(): detector = MTCNN(steps_threshold=(.05, .7, .7)) create_folder(FACE_DIR1) for file in glob.glob( "D:\Summer Intern 2019\FACENET/testing/train\P17EC001\*.jpg"): (dirname, filename) = os.path.split(file) print(filename) img = cv2.imread(dirname + '/' + filename, 1) img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces = detector.detect_faces(img) if (len(faces) == 1): print(detector.detect_faces(img)) bounding_box = faces[0]['box'] face_img = img[bounding_box[1]:bounding_box[1] + bounding_box[3], bounding_box[0]:bounding_box[0] + bounding_box[2]] #face_aligned = face_aligner.align(img, img_gray, bounding_box) path = FACE_DIR1 + "/" + (filename) face_img = cv2.resize(face_img, (200, 200)) cv2.imwrite(path, face_img) elif (len(faces) > 1): for i in range(len(faces)): bounding_box = faces[i]['box'] face_img = img[bounding_box[1]:bounding_box[1] + bounding_box[3], bounding_box[0]:bounding_box[0] + bounding_box[2]] #face_aligned = face_aligner.align(img, img_gray, bounding_box) path = "FACE_DIR1" + "/" + str(i) + (filename) face_img = cv2.resize(face_img, (200, 200)) cv2.imwrite(path, face_img) for file1 in glob.glob( "D:\Summer Intern 2019\FACENET/testing/train\P17EC001\*.png"): (dirname1, filename1) = os.path.split(file1) print(filename1) img1 = cv2.imread(dirname1 + '/' + filename1, 1) img_gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY) faces1 = detector.detect_faces(img) if (len(faces1) == 1): bounding_box = faces1[0]['box'] face_img1 = img1[bounding_box[1]:bounding_box[1] + bounding_box[3], bounding_box[0]:bounding_box[0] + bounding_box[2]] #face_aligned1 = face_aligner.align(img1, img_gray1, bounding_box) path1 = FACE_DIR1 + "/" + filename1 face_img1 = cv2.resize(face_img1, (200, 200)) cv2.imwrite(path1, face_img1) elif (len(faces1) > 1): for i in range(len(faces1)): bounding_box = faces1[i]['box'] face_img1 = img1[bounding_box[1]:bounding_box[1] + bounding_box[3], bounding_box[0]:bounding_box[0] + bounding_box[2]] #face_aligned1 = face_aligner.align(img1, img_gray1, bounding_box) path1 = FACE_DIR1 + "/" + str(i) + filename1 face_img1 = cv2.resize(face_img1, (200, 200)) cv2.imwrite(path1, face_img1) #main_test()
import numpy as np import cv2, pickle #import pkg_resources from os import listdir, path from mtcnn.mtcnn import MTCNN from keras.preprocessing import image from keras_vggface.vggface import VGGFace from scipy.spatial.distance import cosine from keras_vggface.utils import preprocess_input #weights_file=pkg_resources.resource_stream('mtcnn', 'data/mtcnn_weights.npy') #steps_threshold=[0.6, 0.7, 0.7] detector = MTCNN(min_face_size=20, scale_factor=0.709) network = VGGFace(model='resnet50', include_top=False, input_shape=(224, 224, 3), pooling='avg') class RTAS(): def __init__(self): self.path='train/' self.size=(224, 224) self.unknown='?' self.mindist=0.4 self.rectcolor=(0, 0, 255) self.textcolor=(255, 255, 255) self.circlecolor=(255, 255, 255) self.cricleradius=2 self.textthickness=2 self.rectthickness=5 self.circlethickness=-1 self.metadata={} self.traindata()
#!/usr/bin/env python # coding: utf-8 # ## Comparison Face detection methods (MTCNN, dlib HOG and opencv HAARCASCADE) # In[1]: from matplotlib import pyplot as plt from mtcnn.mtcnn import MTCNN import dlib import cv2 # In[2]: # MTCNN detector_mtcnn = MTCNN() def face_detection_mtcnn(image): faces = detector_mtcnn.detect_faces(image) # print("Number of faces detected:",len(faces)) for face in faces: left, top, width, height = face['box'] right, bottom = left + width, top + height cv2.rectangle(image, (left, top), (right, bottom), (255, 0, 0), 2) # plt.imshow(image) # plt.show() return image
def _load_mtcnn_detector(self): self.face_detector = MTCNN()
x1, y1, width, height = results[0]['box'] # print(x1, y1, width,height) x2, y2 = x1 + width, y1 + height # # extract the face face = pixels[y1:y2, x1:x2] # resize pixels to the model size image = Image.fromarray(face) image = image.resize(required_size) face_array = asarray(image) return face_array MTCNN_DETECTOR = MTCNN() def getImage(imageName): return misc.imread(imageName, mode='RGB') def getFaces(img): return MTCNN_DETECTOR.detect_faces(img) def getOneFace(img, faceBox): x1, y1, width, height = faceBox if x1 < 0: x1 = 0
def __init__(self): print("Loading face detector") self.detector = MTCNN() print("Loaded face detector")
def generate_dataset(self): if (self.t1.get() == "" or self.t1.get() == "아이디를 입력해주세요." or self.t2.get() == "" or self.t3.get() == "" or self.t4.get() == "" or self.t5.get() == ""): messagebox.showinfo('입력오류', '아이디, 비밀번호, 이름, 학번, 전공 5 가지 모두를 입력해주세요.') self.t1.focus() else: mydb = pymysql.connect(host="localhost", port=3306, user="******", passwd="root", db="dcu_member", charset='utf8') mycursor = mydb.cursor() mycursor.execute("SELECT * FROM last_member") myresult = mycursor.fetchall() id = 1 for x in myresult: id += 1 sql = """ INSERT INTO last_member(id, user_Id, user_passwd, user_name, student_id, student_major) VALUES(%s, %s, %s, %s, %s, %s) """ val = (id, self.t1.get(), self.t2.get(), self.t3.get(), self.t4.get(), self.t5.get()) mycursor.execute(sql, val) mydb.commit() mydb.close() face_classifier = MTCNN() def face_cropped(img): cropped_face = None faces = face_classifier.detect_faces(img) if faces is (): return None for face in faces: x, y, w, h = face['box'] cropped_face = img[y:y + h, x:x + w] return cropped_face cap = cv2.VideoCapture(0, cv2.CAP_MSMF) img_id = 0 while True: ret, frame = cap.read() if face_cropped(frame) is not None: img_id += 1 face = cv2.resize(face_cropped(frame), (200, 200)) face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY) file_name_path = "data/user." + str(id) + "." + str( img_id) + ".jpg" cv2.imwrite(file_name_path, face) cv2.putText(face, str(img_id), (50, 50), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0), 2) cv2.imshow("Cropped face", face) if cv2.waitKey(1) == 13 or int(img_id) == 20: break cap.release() cv2.destroyAllWindows() messagebox.showinfo('Reuslt', '데이터 수집을 성공했습니다.') people1 = Two()
def setUpClass(): global mtcnn mtcnn = MTCNN()
# create the shape rect = Rectangle((x, y), width, height, fill=False, color='red') # draw the box ax.add_patch(rect) break # show the plot pyplot.show() filename0 = args[0] filename1 = args[1] # load image from file pixels0 = pyplot.imread(filename0) # create the detector, using default weights detector0 = MTCNN() # detect faces in the image faces0 = detector0.detect_faces(pixels0) # display faces on the original image draw_image_with_boxes0(filename0, faces0) #cv2.imwrite('bounded.jpg',) # load image from file pixels1 = pyplot.imread(filename1) # create the detector, using default weights detector1 = MTCNN() # detect faces in the image faces1 = detector1.detect_faces(pixels1) # display faces on the original image draw_image_with_boxes1(filename1, faces1)
def main(): weight_file = "../pre-trained/wiki/ssrnet_3_3_3_64_1.0_1.0/ssrnet_3_3_3_64_1.0_1.0.h5" # for face detection # detector = dlib.get_frontal_face_detector() detector = MTCNN() try: os.mkdir('./img') except OSError: pass # load model and weights img_size = 64 stage_num = [3, 3, 3] lambda_local = 1 lambda_d = 1 model = SSR_net(img_size, stage_num, lambda_local, lambda_d)() model.load_weights(weight_file) clip = VideoFileClip(sys.argv[1]) # can be gif or movie #python version pyFlag = '' if len(sys.argv) < 3: pyFlag = '2' #default to use moviepy to show, this can work on python2.7 and python3.5 elif len(sys.argv) == 3: pyFlag = sys.argv[2] #python version else: print('Wrong input!') sys.exit() img_idx = 0 detected = '' #make this not local variable time_detection = 0 time_network = 0 time_plot = 0 ad = 0.4 skip_frame = 5 # every 5 frame do 1 detection and network forward propagation for img in clip.iter_frames(): img_idx = img_idx + 1 input_img = img #using python2.7 with moivepy to show th image without channel flip if pyFlag == '3': input_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img_h, img_w, _ = np.shape(input_img) input_img = cv2.resize(input_img, (1024, int(1024 * img_h / img_w))) img_h, img_w, _ = np.shape(input_img) if img_idx == 1 or img_idx % skip_frame == 0: # detect faces using dlib detector detected = detector.detect_faces(input_img) faces = np.empty((len(detected), img_size, img_size, 3)) start_time = timeit.default_timer() for i, d in enumerate(detected): print(i) print(d['confidence']) if d['confidence'] > 0.95: x1, y1, w, h = d['box'] x2 = x1 + w y2 = y1 + h xw1 = max(int(x1 - ad * w), 0) yw1 = max(int(y1 - ad * h), 0) xw2 = min(int(x2 + ad * w), img_w - 1) yw2 = min(int(y2 + ad * h), img_h - 1) cv2.rectangle(input_img, (x1, y1), (x2, y2), (255, 0, 0), 2) # cv2.rectangle(img, (xw1, yw1), (xw2, yw2), (255, 0, 0), 2) faces[i, :, :, :] = cv2.resize( input_img[yw1:yw2 + 1, xw1:xw2 + 1, :], (img_size, img_size)) elapsed_time = timeit.default_timer() - start_time time_detection = time_detection + elapsed_time start_time = timeit.default_timer() if len(detected) > 0: # predict ages and genders of the detected faces results = model.predict(faces) predicted_ages = results # draw results for i, d in enumerate(detected): if d['confidence'] > 0.95: x1, y1, w, h = d['box'] label = "{}".format(int(predicted_ages[i])) draw_label(input_img, (x1, y1), label) elapsed_time = timeit.default_timer() - start_time time_network = time_network + elapsed_time start_time = timeit.default_timer() if pyFlag == '2': img_clip = ImageClip(input_img) img_clip.show() cv2.imwrite('img/' + str(img_idx) + '.png', cv2.cvtColor(input_img, cv2.COLOR_RGB2BGR)) elif pyFlag == '3': cv2.imshow("result", input_img) cv2.imwrite('img/' + str(img_idx) + '.png', cv2.cvtColor(input_img, cv2.COLOR_RGB2BGR)) elapsed_time = timeit.default_timer() - start_time time_plot = time_plot + elapsed_time else: for i, d in enumerate(detected): if d['confidence'] > 0.95: x1, y1, w, h = d['box'] x2 = x1 + w y2 = y1 + h xw1 = max(int(x1 - ad * w), 0) yw1 = max(int(y1 - ad * h), 0) xw2 = min(int(x2 + ad * w), img_w - 1) yw2 = min(int(y2 + ad * h), img_h - 1) cv2.rectangle(input_img, (x1, y1), (x2, y2), (255, 0, 0), 2) # cv2.rectangle(img, (xw1, yw1), (xw2, yw2), (255, 0, 0), 2) faces[i, :, :, :] = cv2.resize( input_img[yw1:yw2 + 1, xw1:xw2 + 1, :], (img_size, img_size)) # draw results for i, d in enumerate(detected): if d['confidence'] > 0.95: x1, y1, w, h = d['box'] label = "{}".format(int(predicted_ages[i])) draw_label(input_img, (x1, y1), label) start_time = timeit.default_timer() if pyFlag == '2': img_clip = ImageClip(input_img) img_clip.show() elif pyFlag == '3': cv2.imshow("result", input_img) elapsed_time = timeit.default_timer() - start_time time_plot = time_plot + elapsed_time #Show the time cost (fps) print('avefps_time_detection:', img_idx / time_detection) print('avefps_time_network:', img_idx / time_network) print('avefps_time_plot:', img_idx / time_plot) print('===============================') if pyFlag == '3': key = cv2.waitKey(30) if key == 27: break
def __init__(self): self.detector = MTCNN()
def main(**args): # initialize loader dblfw = lfw.DBLFW(args['lfw']) print(dblfw) files, boxes, landmarks = dblfw.read_test_annotations() loader = ioutils.ImageLoaderWithPath(files, prefix=dblfw.dbasedir) # initialize original pypi mtcnn detector detector = MTCNN() residuals = [] for (image, path), target_boxes, target_landmarks in zip(loader, boxes, landmarks): faces = detector.detect_faces(image) if len(faces) == 0: print('\nface is not detected for the image', path) continue detected_landmarks = [np.zeros([5, 2])] detected_landmarks[0][0] = faces[0]['keypoints']['left_eye'] detected_landmarks[0][1] = faces[0]['keypoints']['right_eye'] detected_landmarks[0][2] = faces[0]['keypoints']['nose'] detected_landmarks[0][3] = faces[0]['keypoints']['mouth_left'] detected_landmarks[0][4] = faces[0]['keypoints']['mouth_right'] # compute inter-ocular distance inter_ocular = np.linalg.norm(target_landmarks[0] - target_landmarks[1]) # compute distances between detected and target landmarks distances = np.linalg.norm(target_landmarks - detected_landmarks[0], axis=1) residuals.append(distances.mean() / inter_ocular) # show rectangles if args['show']: for face in faces: # the bounding box is formatted as [x, y, width, height] bbox = face['box'] bbox[2] += bbox[0] bbox[3] += bbox[1] position = (int(bbox[0]), int(bbox[1])) cv2.rectangle(image, position, (int(bbox[2]), int(bbox[3])), color=(0, 0, 255)) text = str(np.round(face['confidence'], 2)) cv2.putText(image, text, position, cv2.FONT_HERSHEY_TRIPLEX, 1, (255, 0, 255)) # show detected landmarks for landmark in detected_landmarks: for x, y in landmark: cv2.circle(image, (np.float32(x), np.float32(y)), 3, (0, 0, 255)) # show target landmarks for x, y in target_landmarks: cv2.circle(image, (np.float32(x), np.float32(y)), 3, (255, 0, 0)) cv2.imshow(str(path), image) cv2.waitKey(0) cv2.destroyAllWindows() print('errors landmark detection for LFW database') print(' accuracy', len(residuals) / loader.size) print('median error', np.median(residuals)) print(' mean error', np.mean(residuals))
def __init__(self): self.model = MTCNN()
def main(): font = cv2.FONT_HERSHEY_SIMPLEX font_scale = 1 thickness = 3 # load json and create model json_file = open('ssrnet_3_3_3_64_1.0_1.0.json', 'r') loaded_model_json = json_file.read() json_file.close() loaded_model = model_from_json(loaded_model_json) # load weights into new model loaded_model.load_weights("ssrnet_3_3_3_64_1.0_1.0.h5") print("Loaded model from disk") img_size = 64 # Load face Detector detector = MTCNN() imagePath = sys.argv[1] # Read the image image = cv2.imread(imagePath) # image = cv2.resize(image, (1280, 720)) ad = 0.4 input_img = image detected = detector.detect_faces(input_img) img_h, img_w, _ = np.shape(input_img) input_img = cv2.resize(input_img, (1024, int(1024 * img_h / img_w))) img_h, img_w, _ = np.shape(input_img) faces = np.empty((len(detected), img_size, img_size, 3)) for i, d in enumerate(detected): if d['confidence'] > 0.95: x1, y1, w, h = d['box'] x2 = x1 + w y2 = y1 + h xw1 = max(int(x1 - ad * w), 0) yw1 = max(int(y1 - ad * h), 0) xw2 = min(int(x2 + ad * w), img_w - 1) yw2 = min(int(y2 + ad * h), img_h - 1) cv2.rectangle(image, (x1, y1), (x2, y2), (255, 0, 0), 2) try: faces[i, :, :, :] = cv2.resize(image[yw1:yw2, xw1:xw2, :], (img_size, img_size)) except cv2.error: faces[i, :, :, :] = cv2.resize( image[yw1:yw2 - 10, xw1:xw2 - 10, :], (img_size, img_size)) if len(detected) > 0: # predict ages and genders of the detected faces results = loaded_model.predict(faces) predicted_ages = results print(predicted_ages) print('Detect {} faces!'.format(len(detected))) # draw results for i, d in enumerate(detected): if d['confidence'] > 0.95: x1, y1, w, h = d['box'] label = "{}".format(int(predicted_ages[i][0])) size = cv2.getTextSize(label, font, font_scale, thickness)[0] x, y = (x1, y1) cv2.rectangle(image, (x, y - size[1]), (x + size[0], y), (255, 0, 0), cv2.FILLED) cv2.putText(image, label, (x1, y1), font, font_scale, (255, 255, 255), thickness) cv2.imshow('Result', image) cv2.waitKey(0) cv2.destroyAllWindows()
rectangle = Rectangle((x, y), width, height, fill=False, color='yellow') # draw the boxes ax.add_patch(rectangle) # Step 8 for key, value in result['keypoints'].items(): # dot creator dot = Circle(value, radius=4, color='red') ax.add_patch(dot) # Display plot plt.show() ## Step 2: set up the model, box sizes and keypoints # setup the model variable that calls for a weights file model = MTCNN(weights_file='mtcnn_weights.npy') ## Step 3: Load image filename = 'group2013.jpg' pixel_array = plt.imread(filename) # Read an image into an array # establish detector with default weights detector = model # find facial features in image faces = detector.detect_faces(pixel_array) for face in faces: print(face) # print out the coordinates (optional) ## Step 6: Add the call to the Draw Function draw_rectangles(filename, faces) # place the rectanlges on image
import numpy as np from mtcnn.mtcnn import MTCNN import cv2 as cv cap = cv.VideoCapture(0) model = MTCNN() while(True): # Capture frame-by-frame ret, frame = cap.read() # Our operations on the frame come here # gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY) faces = model.detect_faces(frame) roi_frame = None for face in faces: x, y, w, h = face['box'] cv.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) roi_frame = frame[y:y + h, x:x + w] # Display the resulting frame if roi_frame is not None : cv.imshow('window', frame) rc,png = cv.imencode('.png',roi_frame) msg = png.tobytes() # local_mqttclient.publish("pictures", payload = msg,qos = 0, retain = False) if cv.waitKey(1) & 0xFF == ord('q'): break
def __init__(self, extra_pad=(0, 0)): super().__init__(extra_pad) self.detector_model = MTCNN() self.detector_name = "MTCNN"
def __init__(self): self.model = MTCNN() self.embedder = FaceNet()
import numpy as np import cv2 import os import glob from mtcnn.mtcnn import MTCNN detector = MTCNN() import openpyxl #To create an workbook wb = openpyxl.Workbook() sheet = wb.get_active_sheet() sheet.title = 'intensity values' sheet.cell(row=1, column=1).value = 'Image_number' #Name of first column sheet.cell(row=1, column=2).value = 'Intensity_face' #Name of second column sheet.cell(row=1, column=3).value = 'Intensity_left_cheek' #Name of third column sheet.cell(row=1, column=4).value = 'Intensity_right_cheek' #Name of fourth column sheet.cell(row=1, column=5).value = 'Intensity_forehead' #Name of fifth column sheet.cell(row=1, column=6).value = 'Intensity_nose' #Name of sixth column def output(result, thermal_image): d = dict() #Dictonary to return the means of ROI's bounding_box = result[0]['box'] keypoints = result[0]['keypoints'] #print(bounding_box) # Points along with box xb = bounding_box[0] yb = bounding_box[1] - 30 wb = bounding_box[2] hb = bounding_box[3]
def __init__(self, mtcnn=False): if mtcnn == True: from mtcnn.mtcnn import MTCNN self.mtcnn = MTCNN()