def Read_Video(video_path, apex_num, onset_save_path, apex_save_path, flow_save_path): vc = cv2.VideoCapture(video_path) rval, frame = vc.read() frames = np.expand_dims(frame, axis=0) while rval: rval, frame = vc.read() if rval: frame = np.expand_dims(frame, axis=0) frames = np.append(frames, frame, axis=0) cv2.waitKey(1) else: break vc.release() # return frames detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat') onset_image = frames[0] apex_image = frames[apex_num] onset_image = cv2.cvtColor(onset_image, cv2.COLOR_RGB2BGR) apex_image = cv2.cvtColor(apex_image, cv2.COLOR_RGB2BGR) onset_det = detector(onset_image, 1)[0] apex_det = detector(apex_image, 1)[0] onset_faces = dlib.full_object_detections() apex_faces = dlib.full_object_detections() onset_faces.append(predictor(onset_image, onset_det)) apex_faces.append(predictor(apex_image, apex_det)) onset_crops = dlib.get_face_chips(onset_image, onset_faces, size=320) apex_crops = dlib.get_face_chips(apex_image, apex_faces, size=320) onset_crop = onset_crops[0][:280, 50:270] apex_crop = apex_crops[0][:280, 50:270] onset_cropped = Image.fromarray(cv2.cvtColor(onset_crop, cv2.COLOR_BGR2RGB)) apex_cropped = Image.fromarray(cv2.cvtColor(apex_crop, cv2.COLOR_BGR2RGB)) onset_g = cv2.cvtColor(onset_crops[0], cv2.COLOR_RGB2GRAY) apex_g = cv2.cvtColor(apex_crops[0], cv2.COLOR_RGB2GRAY) pic_size = onset_crops[0].shape hsv = np.zeros(pic_size) hsv[:,:,1] = cv2.cvtColor(apex_crops[0], cv2.COLOR_RGB2HSV)[:,:,1] flow = cv2.calcOpticalFlowFarneback(onset_g, apex_g, flow=None, pyr_scale=0.5, levels=1, winsize=15, iterations=2, poly_n=5, poly_sigma=1.1, flags=0) mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1]) hsv[:,:,0] = ang * (180/ np.pi / 2) hsv[:,:,2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX) hsv = np.asarray(hsv, dtype=np.float32) rgb_flow = cv2.cvtColor(hsv, cv2.COLOR_HSV2RGB) rgb_flow = Image.fromarray(rgb_flow.astype('uint8')) rgb_flow.save(flow_save_path) onset_cropped.save(onset_save_path) apex_cropped.save(apex_save_path)
x, y, w, h = det.left(), det.top(), det.width(), det.height( ) #x, y, 폭, 높이 rect = patches.Rectangle((x, y), w, h, linewidth=2, edgecolor='r', facecolor='none') ax.add_patch(rect) ax.imshow(img_result) plt.show() # In[5]: fig, ax = plt.subplots(1, figsize=(16, 10)) objs = dlib.full_object_detections() #삐뚤어진 얼굴 돌려주는 명령어 for detection in dets: s = sp(img, detection) objs.append(s) for point in s.parts(): circle = patches.Circle((point.x, point.y), radius=3, edgecolor='r', facecolor='r') ax.add_patch(circle) ax.imshow(img_result) # In[6]: faces = dlib.get_face_chips(img, objs, size=256, padding=0.3) fig, axes = plt.subplots(1, len(faces) + 1, figsize=(20, 16))
import cv2 import numpy as np def imprimePontos(imagem, pontosFacias): for p in pontosFacias.parts(): cv2.circle(imagem, (p.x, p.y), 2, (0, 255, 0), 2) detectorFace = dlib.get_frontal_face_detector() detectorPontos = dlib.shape_predictor( "recursos/shape_predictor_5_face_landmarks.dat") imagem = cv2.imread("fotos/treinamento/ronald.0.1.jpg") imagemRgb = cv2.cvtColor(imagem, cv2.COLOR_BGR2RGB) facesDetectadas = detectorFace(imagemRgb, 0) facesPontos = dlib.full_object_detections() for face in facesDetectadas: pontos = detectorPontos(imagemRgb, face) facesPontos.append(pontos) imprimePontos(imagem, pontos) imagens = dlib.get_face_chips(imagemRgb, facesPontos) for img in imagens: imagemBgr = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) cv2.imshow("Imagem original", imagem) cv2.waitKey(0) cv2.imshow("Imagem alinhada", imagemBgr) cv2.waitKey(0) #cv2.imshow("5 pontos", imagem) #cv2.waitKey(0)
else: fig, ax = plt.subplots(1, figsize=(16, 10)) for det in dets: x, y, w, h = det.left(), det.top(), det.width(), det.height() rect = patches.Rectangle((x, y), w, h, linewidth=2, edgecolor='w', facecolor='none') ax.add_patch(rect) ax.imshow(img_result) plt.show() fig, ax = plt.subplots(1, figsize=(16, 10)) objs = dlib.full_object_detections() # 얼굴을 정면으로 만들어줌 for detection in dets: s = sp(img, detection) objs.append(s) for point in s.parts(): circle = patches.Circle((point.x, point.y), radius=3, edgecolor='r', facecolor='r') ax.add_patch(circle) ax.imshow(img_result) plt.show() faces = dlib.get_face_chips(img, objs, size=256, padding=0.3) #이미지 불러오고, 얼굴 정면으로 fig, axes = plt.subplots(1, len(faces) + 1, figsize=(20, 16))
def Recognize_face(model, wk): path = 'image_database/' + wk count = 0 update_database.create_new(wk) #Traverse dirs to get names for (sudirs, dirs, files) in os.walk(path): for subdir in dirs: if (subdir != '\0'): ls = str(subdir) a, b = ls.split(':') print('**************(' + str(a) + ',' + str(b) + ')***************') Rollnos[count] = (str(b)) names[count] = (str(a)) count += 1 print names print Rollnos #print The names to test proper Traverse for i in range(len(names)): IDs[i] = i print("Id of %s is :%s" % (names[i], i)) #Select model and load xml file if (model == 'LBPH'): recognizer = cv2.face.LBPHFaceRecognizer_create(1) elif (model == 'Fisher'): recognizer = cv2.face.FisherFaceRecognizer_create() elif (model == 'Eigen'): recognizer = cv2.face.EigenFaceRecognizer_create(10) #print('Trained_models_xml/'+wk+'/'+wk+model+'data.xml') recognizer.read('Trained_models_xml/' + wk + '/' + wk + model + 'data.xml') print('Trained_models_xml/' + wk + '/' + wk + model + 'data.xml') print(model == 'LBPH') #Haar classifier haar_face = 'Haar/haarcascade_frontalface_default.xml' haar_eye = 'Haar/haarcascade_eye_tree_eyeglasses.xml' detector = dlib.get_frontal_face_detector() face_haar_cascade = cv2.CascadeClassifier(haar_face) eye_haar_cascade = cv2.CascadeClassifier(haar_eye) shape_file = 'Haar/shape_predictor_5_face_landmarks.dat' predictor = dlib.shape_predictor(shape_file) #Start recognizing print 'press \'q\' to quit reconition process' webcam = cv2.VideoCapture(0) end = time.time() + 60 * 0.083333 global minschedule minschedule = time.time() + 60 * 1 print minschedule count = 1 arr = [] #url='http://192.168.43.1:8080/photoaf.jpg' while True: (rval, im) = webcam.read() #print(arr) #img_resp=requests.get(url) #img_arr=np.array(bytearray(img_resp.content),dtype=np.uint8) #im=cv2.imdecode(img_arr,1) im = cv2.flip(im, 1, 0) gray_im = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) #if u want to compare bw preprcessed and processed img enable those 2 #cv2.imshow('preprocessed',gray) gray = image_processing.image_process_hist(gray_im) #cv2.imshow('processed',gray) faces = detector(gray, 1) for (i, rect) in enumerate(faces): col_face = im[rect.top():rect.bottom(), rect.left():rect.right()] gr = gray[rect.top():rect.bottom(), rect.left():rect.right()] #cv2.imshow('hist',gray) #cv2.waitKey(150) #algn_face=image_processing.image_process_align(col_face) faces_alg = dlib.full_object_detections() faces_alg.append(predictor(im, faces[i])) images_alg = dlib.get_face_chips(im, faces_alg, size=100) if (len(images_alg) < 1): algn_face = col_face algn_face = cv2.cvtColor(algn_face, cv2.COLOR_BGR2Gray) print 'yes' else: for img_alg in images_alg: algn_face = cv2.cvtColor(img_alg, cv2.COLOR_RGB2BGR) algn_face = cv2.cvtColor(algn_face, cv2.COLOR_RGB2GRAY) #algn_face=image_processing.image_process_hist(algn_face) gray_face = image_processing.image_process_hist(algn_face) #cv2.imshow('algngray',gray_face) #cv2.waitKey(100) #gray_face=gray[rect.top():rect.bottom(), rect.left():rect.right()] #cv2.imshow('gray',gray_face) #eyes=eye_haar_cascade.detectMultiScale(gray_face) #for(ex,ey,ew,eh) in eyes: count += 1 cv2.rectangle(im, (rect.left(), rect.top()), (rect.right(), rect.bottom()), (0, 255, 0), 2) #gray_face=cv2.resize(gray_face,(90,90)) cv2.imshow('goingto', gray_face) cv2.waitKey(50) ID, conf = recognizer.predict(gray_face) arr.append(ID) if (time.time() >= end): updb(arr, wk) end = time.time() + 60 * 0.083333 arr = [] print(str(Rollnos[ID]) + ':' + str(conf)) if (wk == 'Student'): ID_OUT = names[ID] else: ID_OUT = Rollnos[ID] cv2.rectangle(im, (rect.left(), rect.top()), (rect.right(), rect.top() + 25), (0, 255, 0), cv2.FILLED) cv2.putText(im, str(ID_OUT) + ":" + str(round(conf, 2)), (rect.left() + 6, rect.top() + 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255)) #count+=1 cv2.namedWindow("window", cv2.WINDOW_NORMAL) cv2.resizeWindow('image', 1200, 1200) cv2.imshow("window", im) if cv2.waitKey(1) & 0xFF == ord('q'): break print count cv2.destroyAllWindows()
# Load the image using Dlib img = dlib.load_rgb_image(face_file_path) # Ask the detector to find the bounding boxes of each face. The 1 in the # second argument indicates that we should upsample the image 1 time. This # will make everything bigger and allow us to detect more faces. dets = detector(img, 1) num_faces = len(dets) if num_faces == 0: print("Sorry, there were no faces found in '{}'".format(face_file_path)) exit() # Find the 5 face landmarks we need to do the alignment. faces = dlib.full_object_detections() for detection in dets: faces.append(sp(img, detection)) window = dlib.image_window() # Get the aligned face images # Optionally: # images = dlib.get_face_chips(img, faces, size=160, padding=0.25) images = dlib.get_face_chips(img, faces, size=320) for image in images: window.set_image(image) dlib.hit_enter_to_continue() # It is also possible to get a single chip image = dlib.get_face_chip(img, faces[0])
import cv2 import numpy as np def imprimePoints(image, facialPoints): for p in facialPoints.parts(): cv2.circle(image, (p.x, p.y), 2, (0, 255, 0), 2) detectorFace = dlib.get_frontal_face_detector() detectorPoints = dlib.shape_predictor( "assets/shape_predictor_5_face_landmarks.dat") image = cv2.imread("photos/training/ronald.0.1.jpg") imageRGB = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) facesDetected = detectorFace(imageRGB, 0) pointsFaces = dlib.full_object_detections() for face in facesDetected: points = detectorPoints(imageRGB, face) pointsFaces.append(points) imprimePoints(image, points) images = dlib.get_face_chips(imageRGB, pointsFaces) for img in images: imageBGR = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) cv2.imshow("Imagem original: ", image) cv2.waitKey(0) cv2.imshow("Imagem alinhada: ", imageBGR) cv2.waitKey(0) #cv2.imshow("5 Pontos: ", image) #cv2.waitKey(0)
def aligned_face_cropper(indexedFileNames, crpFileDir, imgRes, scope): # 'indexedFileNames'는 총 분량으로부터 할당받아 온 원본 파일들의 이름과, 그 할당분의 순번(index), 'crpFileDir'은 얼굴 부분만 자른 것을 저장할 경로. # 'imgRes'는 이미지를 저장할 해상도(다수 선택 가능)에 대한 Parameter, 'scope'는 이미지 내에서 인물 부분이 차지하는 범위를 설정하는 Parameter 값이다. from tqdm import tqdm import matplotlib.pyplot as plt import dlib import time cnt = 0 # 출력돼 나온 총 파일의 수를 계수할 변수를 0으로 초기화하며 선언한다. coreNum, fileNames = indexedFileNames # 'indexedFileNames'은 멀티프로세싱을 위해 총 작업량으로부터 분할받은 부분과 그 index 정보로, # index 부분을 'coreNum'으로, 할당받은 자료 부분을 'fileNames'로 각각 Un-packaging. # 'coreNum'이라고 해서 실제 CPU 내에서 해당 Core의 물리적인 할당 번호를 의미하는 것은 아니다!! # 중복의 방지와 진행률 표시 시의 구분을 위한 단순한 코드 상의 넘버링일 뿐이다. timeFlag = time.strftime('%m%d-%H%M%S', time.localtime( time.time())) # 모듈 작동 당시의 시각을 기억해 둠.(파일명의 중복 방지에 활용하기 위함) shapePrd = dlib.shape_predictor( 'D:/HSH/Model/shape_predictor_68_face_landmarks.dat' ) # 학습된 형태 예측기의 모델 파일을 불러옴.(!!!!! Github 용량 제한으로 포함돼있지 않음 !!!!!) faceDtcr = dlib.get_frontal_face_detector( ) # 얼굴 정면을 탐지하는 dlib의 Method를 변수화. faceDirs = [] # 출력한 파일을 다른 함수 등에 활용할 수 있도록, 작업을 통해 출력된 파일의 이름들을 저장할 배열 선언. for fileName in tqdm( fileNames, desc="@Core {} - Aligning & Cropping".format(coreNum)): # 특정 폴더 내 사진들의 이름 리스트인 'fileNames'의 내용에 따라 이미지를 불러온 후, 컨테이너에 적재. if plt.imread(fileName).shape[0] < 600: continue # 읽어 올 이미지의 크기가 정사각형 한 차원 기준 600픽셀 미만이라면, 작업을 진행하지 않고 다음 파일로 넘어가도록 처리하였다. imgLoaded = dlib.load_rgb_image(fileName) # 파일을 RGB Color 이미지로 불러옴. dtcdFaces = faceDtcr(imgLoaded, 1) # 불러온 이미지에서 얼굴(들)을 탐지해 변수에 저장. objects = dlib.full_object_detections() # 탐지해 낸 랜드마크 포인트들을 담을 변수를 선언. for dtcdFace in dtcdFaces: # 찾은 얼굴 부분 안에서 모델을 이용해 랜드마크 포인트들을 탐지. if dtcdFace.right() - dtcdFace.left() < 200: continue # 탐지해 낸 얼굴 부분의 한 차원 기준 크기가 200픽셀 미만이면, 작업을 진행하지 않고 다음 파일로 넘어간다. shape = shapePrd(imgLoaded, dtcdFace) objects.append(shape) # 찾아낸 랜드마크 포인트들의 정보를 앞서 선언해 둔 변수에 적재. for res in imgRes: # 이미지를 저장할 해상도를 복수 선택할 수 있게 해 뒀으므로, 그 선택지를 입력한 수 만큼 반복해 작동한다. saveCnt = 0 # 저장한 파일을 계수할 변수 선언. # 'cropFileDir'은 Crop해 온 파일들이 저장될 포괄적 폴더의 경로, 'res'는 그 폴더 안에 생성될 또 다른 폴더의 이름이자 저장될 해상도이다. dir = '{}{}/'.format(crpFileDir, str(res)) try: # 탐지한 포인트 정보와 주어진 Parameter 값들을 가지고 얼굴을 바르게 정렬한 후 잘라낸 다음, 적재한다. # 개별 파일에 대한 작업 도중 오류가 발생할 시(얼굴을 탐지하지 못했다거나 등의 경우), # 그 오류로 인해 전체 과정이 중단돼버리는 대신, 오류를 무시하고 다음 파일로 그냥 넘어갈 수 있도록 Exception 처리를 하였다. # 즉, 특정 몇몇 파일로부터 얼굴을 찾지 못했더라도 이를 그냥 무시하고, 다른 파일에서라도 얼굴을 찾아냈다면 그 것들은 온전히 가져올 수 있도록 한 것이다. face = dlib.get_face_chips(imgLoaded, objects, size=res, padding=scope) # 앞서 추출해 낸 얼굴을 Parameter값(해상도, 인물 부분의 표현 범위)에 따라 .png 파일로 출력. # 동일 인물의 중복 출력을 방지하기 위해 한 사진 당 얼굴 하나씩만을 출력하도록 했다. fileDir = '{}{}_Aligned_{}_{}_{}_{}.png'.format( dir, int(scope), timeFlag, coreNum, res, cnt) # 파일명을 형식에 따라 설정. dlib.save_image(face[0], fileDir) # 'face'배열의 가장 첫 내용만을 지정한 파일명으로 저장. faceDirs.append( fileDir) # 나중 과정에 활용할 수 있도록, 잘라내 저장한 얼굴들에 대한 파일명을 배열에 추가. saveCnt += 1 # 저장한 파일의 수를 1 증가시킨다. except Exception as e: break cnt += saveCnt # 'saveCnt'는 for Loop의 내부변수이기 때문에, 매 Iteration 시마다 초기화될 것이므로, # 회 당 산출된 값을 매 Iteration이 종료될 때마다 전역변수에 더해놓아 줘야 그 수가 의미를 상실하지 않는다. return faceDirs, cnt # 출력한 파일들의 이름들 목록과 그 총 수를 호출측에 반환.
print('cannot find faces!') else: fig, ax = plt.subplots(1, figsize=(16,10)) for det in dets: x, y, w, h = det.left(), det.top(), det.width(), det.height() rect = patches.Rectangle((x, y), w, h, linewidth=2, edgecolor='r', facecolor='none') ax.add_patch(rect) ax.imshow(img_result) plt.show() # In[31]: fig, ax = plt.subplots(1, figsize=(16,10)) objs = dlib.full_object_detections() # 얼굴을 바르게 회전 시켜줌 for detection in dets: s = sp(img, detection) objs.append(s) for point in s.parts(): circle = patches.Circle((point.x, point.y), radius=3, edgecolor='r', facecolor='r') ax.add_patch(circle) ax.imshow(img_result) plt.show() # In[32]: faces = dlib.get_face_chips(img, objs, size=256, padding=0.3) fig, axes = plt.subplots(1, len(faces)+1, figsize=(20,16))
def process_image(filepath, img, image_type, frame_num=None, exif_data=None): image_height, image_width, _ = img.shape resizepath, resized_height, resized_width = None, image_height, image_width if args.resize: img = resize_image(img, args.resize) if args.save_resized: filename = os.path.basename(filepath) resizepath = os.path.join(args.save_resized, filename) basepath, ext = os.path.splitext(resizepath) if ext == '' or frame_num is not None: resizepath = basepath if frame_num is not None: resizepath += "_%04d" % frame_num resizepath += '.jpg' cv2.imwrite(resizepath, img) resized_height, resized_width, _ = img.shape gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces = detector(gray, args.upscale) image_id = db.insert_image([ image_type, filepath, image_width, image_height, resizepath, resized_width, resized_height, frame_num, exif_data, len(faces) ]) poses = dlib.full_object_detections() rects = [] for rect in faces: if args.detector == 'cnn': rect = rect.rect pose = predictor(gray, rect) poses.append(pose) rects.append(rect) # do batched computation of face descriptors img_rgb = img[..., ::-1] # BGR to RGB descriptors = facerec.compute_face_descriptor(img_rgb, poses, args.jitter) for i, (rect, pose, descriptor) in enumerate(zip(rects, poses, descriptors)): face_left = float(rect.left()) / resized_width face_top = float(rect.top()) / resized_height face_right = float(rect.right()) / resized_width face_bottom = float(rect.bottom()) / resized_height face_width = face_right - face_left face_height = face_bottom - face_top landmarks = [[float(p.x) / resized_width, float(p.y) / resized_height] for p in pose.parts()] descriptor = list(descriptor) face_id = db.insert_face([ image_id, i, face_left, face_top, face_right, face_bottom, face_width, face_height, json.dumps(landmarks), json.dumps(descriptor) ]) if args.save_faces: facepath = os.path.join(args.save_faces, "face_%02d.jpg" % face_id) cv2.imwrite( facepath, img[rect.top():rect.bottom(), rect.left():rect.right()]) db.commit() return len(faces)
def main(): parser = argparse.ArgumentParser() parser.add_argument('--path', help='Path of the video you want to test on.', default=0) args = parser.parse_args() CLASSIFIER_PATH = '../data/Friends/clasifier.pkl' VIDEO_PATH = args.path FACE_MODE_PATH = './models/20180402-114759.pb' predictor_path = 'shape_predictor_68_face_landmarks.dat' detector = dlib.get_frontal_face_detector() sp = dlib.shape_predictor(predictor_path) with open(CLASSIFIER_PATH, 'rb') as file: model, class_names = pickle.load(file) with tf.Graph().as_default(): sess = tf.Session() with sess.as_default(): load_model(FACE_MODE_PATH) #get input and output tensors images_placeholder = tf.get_default_graph().get_tensor_by_name( "input:0") embeddings = tf.get_default_graph().get_tensor_by_name( "embeddings:0") phase_train_placeholder = tf.get_default_graph( ).get_tensor_by_name("phase_train:0") embedding_size = embeddings.get_shape()[1] people_detected = set() person_detected = collections.Counter() #READ VIDEO cap = cv2.VideoCapture(VIDEO_PATH) while (cap.isOpened()): ret, frame = cap.read() face_found = detector(frame, 1) faces = dlib.full_object_detections() for detection in face_found: faces.append(sp(frame, detection)) for k, detection in enumerate(face_found): # get toa do cua face (x, y, w, h) = rect_to_bb(detection) #lay phan face da can chinh lai aligned_face = dlib.get_face_chip(frame, faces[k], size=160) aligned_face = prewhiten( aligned_face ) # normalizes the range of the pixel values of input frames. cv2.imshow("prewhite", aligned_face) scaled_reshape = aligned_face.reshape(-1, 160, 160, 3) feed_dict = { images_placeholder: scaled_reshape, phase_train_placeholder: False } emb_array = sess.run(embeddings, feed_dict=feed_dict) predictions = model.predict_proba(emb_array) best_class_indices = np.argmax(predictions, axis=1) best_class_probabilities = predictions[ np.arange(len(best_class_indices)), best_class_indices] best_name = class_names[best_class_indices[0]] print("Name: {}, Probability: {}".format( best_name, best_class_probabilities)) cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) cv2.putText( frame, "{} | {}".format( best_name, str(round(best_class_probabilities[0], 3))), (x - 10, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) person_detected[best_name] += 1 cv2.imshow("face", frame) if cv2.waitKey(0): break cap.release() cv2.destroyAllWindows() print("person detected {}".format(len(person_detected)))
def detect_faces(color_image_list, gray_image_list, dlib_models): """ Detects faces using Dlib's CNN model. Args: color_image_list (list): list of images. Each item is a frame read by the cv2 package. gray_image_list (list): list of images in grayscale. This list should contain the same images as the color_image_list, but in grayscale. This list is used by the CNN model. dlib_models (dict): a dictionary containing dlib cnn, shape_predictor, and recognition models. Returns: face_images (np.array): an array of images of detected faces. n_faces_list (list): a list of ints showing the number of detected faces in each frame. flat_face_rects (list): a list of dlib.rectangle objects containing rectangle info of each detected face. face_descriptors (list): list of face_descriptor. A face_descriptor is a lists of 128 dim vector that describes the face. Example: Given the following inputs color_image_list = [Img1 (has three faces), Img2 (has two faces)] gray_image_list = [Img1_grayscale (has three faces), Img2_grayscale (has two faces)] This function produces the following face_images = np.array([face1_img1, face2_img1, face3_img1, face4_img2, face5_img2]) n_faces_list = [3, 2] flat_face_rects = [r1, r2, r3, r4, r5] Note that the face images are cropped from the original image in the arguments. So, in our example, face1_img1 has a smaller size of Img1, and face2_img1 might have a compeletely different size but again smaller than Img1. """ mmod_rects = dlib_models['cnn'](gray_image_list, upsample_num_times=UPSAMPLE_COUNT) flat_face_rects = [] flat_image_list_indices = [] n_faces_list = [] all_shapes_list = [] # mmod_rects is a list of list of rectangles for i, image_detection_rects in enumerate(mmod_rects): rects = dlib.rectangles() # save rects into an array to use later rects.extend([d.rect for d in image_detection_rects]) flat_face_rects.extend(rects) flat_image_list_indices.extend([i]*len(image_detection_rects)) n_faces_list.append(len(image_detection_rects)) # find shapes in the image -- this is used for face recognition faces = dlib.full_object_detections() for r in rects: shape = dlib_models['shape_predictor'](color_image_list[i], r) faces.append(shape) all_shapes_list.append(faces) # in the above example # flat_face_rects = [r1, r2, r3, r4, r5] # flat_image_list_indices = [0, 0, 0, 1, 1] # n_faces_list = [3, 2] # all_shapes_list = [dlib.full_object_detections, dlib.full_object_detections] # align detected rectangles to get faces for the next step fa = FaceAligner(dlib_models['shape_predictor']) face_images = [] for i, rect in enumerate(flat_face_rects): image_index = flat_image_list_indices[i] aligned_image = fa.align(color_image_list[image_index], gray_image_list[image_index], rect) aligned_image = imutils.resize(aligned_image, width=160, height=160) face_images.append(aligned_image) # in the above example # face_images = [img1, img2, img3, img4, img5] # face encodings face_descriptors = dlib_models['recognition_model'].compute_face_descriptor(color_image_list, all_shapes_list) # face_descriptor is a lists of 128 dim vector that describes the face. # if two face descriptor vectors have a Euclidean distance between them less than 0.6 # then they are from the same person # in the above example # face_descriptors = [[[0..127],[0..127],[0..127]],[[0..127],[0..127]]] return np.array(face_images), n_faces_list, flat_face_rects, face_descriptors
def align(self, imgDim, rgbImg, bb=None, landmarks=None, landmarkIndices=INNER_EYES_AND_BOTTOM_LIP, skipMulti=False): r"""align(imgDim, rgbImg, bb=None, landmarks=None, landmarkIndices=INNER_EYES_AND_BOTTOM_LIP) Transform and align a face in an image. :param imgDim: The edge length in pixels of the square the image is resized to. :type imgDim: int :param rgbImg: RGB image to process. Shape: (height, width, 3) :type rgbImg: numpy.ndarray :param bb: Bounding box around the face to align. \ Defaults to the largest face. :type bb: dlib.rectangle :param landmarks: Detected landmark locations. \ Landmarks found on `bb` if not provided. :type landmarks: list of (x,y) tuples :param landmarkIndices: The indices to transform to. :type landmarkIndices: list of ints :param skipMulti: Skip image if more than one face detected. :type skipMulti: bool :return: The aligned RGB image. Shape: (imgDim, imgDim, 3) :rtype: numpy.ndarray """ assert imgDim is not None assert rgbImg is not None assert landmarkIndices is not None start = time.time() if bb is None: bb = self.getLargestFaceBoundingBox(rgbImg, skipMulti) print("self.getLargestFaceBoundingBox took {} seconds.".format( time.time() - start)) start = time.time() if bb is None: return # if landmarks is None: # landmarks = self.findLandmarks(rgbImg, bb) # print("self.findLandmarks took {} seconds.".format(time.time() - start)) # start = time.time() # npLandmarks = np.float32(landmarks) # npLandmarkIndices = np.array(landmarkIndices) # H = cv2.getAffineTransform(npLandmarks[npLandmarkIndices], # imgDim * MINMAX_TEMPLATE[npLandmarkIndices]) # print("cv2.getAffineTransform took {} seconds.".format(time.time() - start)) # start = time.time() # thumbnail = cv2.warpAffine(rgbImg, H, (imgDim, imgDim)) # print("cv2.warpAffine took {} seconds.".format(time.time() - start)) # start = time.time() faces = dlib.full_object_detections() faces.append(self.predictor(rgbImg, bb)) thumbnail = dlib.get_face_chip(rgbImg, faces[0], size=imgDim) return thumbnail
print('cannot find faces!') else: fig, ax = plt.subplots(1, figsize=(16, 10)) #한줄 for det in dets: #dets 에는 얼굴의 영역들이 들어있고 왼쪽 맨 위를 원점의 x,y 좌표와 영역의 폭과 높이에 대한 정보를 가지고 있음 x, y, w, h = det.left(), det.top(), det.width(), det.height() rect = patches.Rectangle( (x, y), w, h, linewidth=2, edgecolor='r', facecolor='none') #facecolor 에 색을 넣으면 영역 안을 색으로 채워줌 ax.add_patch(rect) #사각형을 띄워줌 ax.imshow(img_result) plt.show() # In[5]: fig, ax = plt.subplots(1, figsize=(16, 10)) #fig 도화지, ax 서브플랏들 objs = dlib.full_object_detections() #얼굴이 돌려졌을 때 똑바로 돌려주는 메서드 for detection in dets: s = sp(img, detection) #sp : 모양을 예측해줌 원본이미지 와 얼굴 영역 정보 부여 (사각형 X) objs.append(s) for point in s.parts(): circle = patches.Circle((point.x, point.y), radius=3, edgecolor='r', facecolor='r') ax.add_patch(circle) ax.imshow(img_result) # In[6]: faces = dlib.get_face_chips(img, objs, size=256, padding=0.3) #얼굴 영역만 이미지를 잘라줌 fig, axes = plt.subplots(1, len(faces) + 1,
def process_queue(): global images_queue global grays_queue global data_queue global num_images global num_faces global num_files faces_queue = detector(grays_queue, args.upscale, batch_size=len(grays_queue)) for faces, img, gray, data in zip(faces_queue, images_queue, grays_queue, data_queue): image_id = db.insert_image(data + [len(faces)]) poses = dlib.full_object_detections() rects = [] for face in faces: pose = predictor(gray, face.rect) poses.append(pose) rects.append(face.rect) # do batched computation of face descriptors img_rgb = img[..., ::-1] # BGR to RGB descriptors = facerec.compute_face_descriptor(img_rgb, poses, args.jitter) resized_width = data[5] resized_height = data[6] for i, (rect, pose, descriptor) in enumerate(zip(rects, poses, descriptors)): face_left = float(rect.left()) / resized_width face_top = float(rect.top()) / resized_height face_right = float(rect.right()) / resized_width face_bottom = float(rect.bottom()) / resized_height face_width = face_right - face_left face_height = face_bottom - face_top landmarks = [[ float(p.x) / resized_width, float(p.y) / resized_height ] for p in pose.parts()] descriptor = list(descriptor) face_id = db.insert_face([ image_id, i, face_left, face_top, face_right, face_bottom, face_width, face_height, json.dumps(landmarks), json.dumps(descriptor) ]) if args.save_faces: facepath = os.path.join(args.save_faces, "face_%02d.jpg" % face_id) cv2.imwrite( facepath, img[rect.top():rect.bottom(), rect.left():rect.right()]) num_images += 1 num_faces += len(faces) db.commit() images_queue.clear() grays_queue.clear() data_queue.clear() elapsed = time.time() - start_time print( "\rFiles: %d, Images: %d, Faces: %d, Elapsed: %.2fs, Images/s: %.1fs" % (num_files, num_images, num_faces, elapsed, num_images / elapsed), end='')
def to_fod(landmarks): detects = dlib.full_object_detections() detects.extend(landmarks) return detects
bgr_img = cv2.imread(face_file_path) if bgr_img is None: print("img no exist") exit() #opencv的颜色空间是BGR,需要转为RGB才能用在dlib中 rgb_img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2RGB) #检测图片中的人脸 dets = detector(rgb_img, 1) num_faces = len(dets) if num_faces == 0: print("no face") exit() #识别人脸特征点,并保存下来 faces = dlib.full_object_detections() for det in dets: faces.append(sp(rgb_img, det)) #人脸对齐 images = dlib.get_face_chips(rgb_img, faces, size=320) #显示计数,按照这个计数创建窗口 image_cnt = 0 #显示对齐结果 for image in images: image_cnt += 1 cv_rgb_image = np.array(image).astype(np.uint8) #先转化为numpy数组 cv_bgr_image = cv2.cvtColor(cv_rgb_image, cv2.COLOR_RGB2BGR) cv2.imshow('%s' % (image_cnt), cv_bgr_image) cv2.waitKey(0) cv2.destroyAllWindows()
def main(): parser = argparse.ArgumentParser() parser.add_argument('-i', '--filename-input', type=str, default=os.path.join('./data/obj/liu_mesh.obj')) parser.add_argument('-o', '--output-dir', type=str, default=os.path.join('./data/obj/vd092_mesh.obj')) args = parser.parse_args() # other settings camera_distance = 3.0 elevation = -5 azimuth = 0 # load from Wavefront .obj file mesh = sr.Mesh.from_obj(args.filename_input, load_texture=True, texture_res=5, texture_type='vertex') # create renderer with SoftRas renderer = sr.SoftRenderer(camera_mode='look_at') net = Net.ResNet([3, 4, 23, 3]).to(device) im = torch.rand(1, 3, 200, 200).to(device) out = net(im).to(device) BFM_coeff = out if not os.path.isfile('./BFM/BFM_model_front.mat'): transferBFM09() Face_model = BFM() BFM_net = compute_bfm( torch.tensor(Face_model.idBase, dtype=torch.float16), torch.tensor(Face_model.exBase, dtype=torch.float16), torch.tensor(Face_model.meanshape, dtype=torch.float16), torch.tensor(Face_model.texBase, dtype=torch.float16), torch.tensor(Face_model.meantex, dtype=torch.float16), torch.tensor(Face_model.tri, dtype=torch.int32)) id_coeff = BFM_coeff[:, 0:80] ex_coeff = BFM_coeff[:, 80:144] tex_coeff = BFM_coeff[:, 144:224] print(id_coeff) vertices, textures, tri = BFM_net(id_coeff, ex_coeff, tex_coeff) # draw object from different view mesh.reset_() elevation = BFM_coeff[:, 226] # elevation = torch.sum(elevation) azimuth = -90 renderer.transform.set_eyes_from_angles(camera_distance, 0, 180) # images = renderer.render_mesh(mesh) print(vertices) print(mesh.faces) faces = torch.tensor(Face_model.tri, dtype=torch.int32).to(device) - 1 faces = faces.unsqueeze(0) print(faces) images = renderer.forward(mesh.vertices, mesh.faces, mesh.textures, texture_type='vertex') print(images) image = images.detach().cpu().numpy()[0, 0:3].transpose((1, 2, 0)) image = (image * 255).astype(np.uint8) plt.figure(0) plt.imshow(image) plt.show() img_name1 = 'D:/files/project/data/human_faces/CACD2000/CACD2000/17_Jennifer_Lawrence_0013.jpg' img_name2 = 'D:/files/project/data/human_faces/CACD2000/CACD2000/17_Lily_Cole_0008.jpg' predictor_model = './model/shape_predictor_68_face_landmarks.dat' detector = dlib.get_frontal_face_detector() # dlib人脸检测器 predictor = dlib.shape_predictor(predictor_model) img1 = cv2.imread(img_name1) image2 = cv2.imread(img_name2) image1 = cv2.cvtColor(img1, cv2.COLOR_BGR2RGB) # image2 = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB) points1 = np.zeros((68, 2)) points2 = np.zeros((68, 2)) rects, scores, idx = detector.run(image, 2, 0) faces = dlib.full_object_detections() for rect in rects: faces.append(predictor(image, rect)) i = 0 for landmark in faces: for idx, point in enumerate(landmark.parts()): points1[i, 0] = point.x points1[i, 1] = point.y i = i + 1 h, w, c = image.shape show_image(image, points1) rects, scores, idx = detector.run(image2, 2, 0) faces = dlib.full_object_detections() for rect in rects: faces.append(predictor(image2, rect)) i = 0 for landmark in faces: for idx, point in enumerate(landmark.parts()): points2[i, 0] = point.x points2[i, 1] = point.y i = i + 1 h, w, c = image2.shape show_image(image2, points2) tr = trans.estimate_transform('affine', src=points1, dst=points2) M = tr.params[0:2, :] cv_img = cv2.warpAffine(image1, M, (image.shape[1], image.shape[0])) show_image(image2, points2) param = np.linalg.inv(tr.params) theta = normalize_transforms(param[0:2, :], w, h) to_tensor = torchvision.transforms.ToTensor() tensor_img = to_tensor(image).unsqueeze(0) theta = torch.Tensor(theta).unsqueeze(0) grid = F.affine_grid(theta, tensor_img.size()) tensor_img = F.grid_sample(tensor_img, grid) tensor_img = tensor_img.squeeze(0) warp_img = convert_image_np(tensor_img) show_image(warp_img, points2) vertices = vertices[0].detach().cpu().numpy() faces = faces[0].detach().cpu().numpy() + 1 textures = textures[0].detach().cpu().numpy() load.save_obj('./123.obj', vertices, faces, textures)
def get_test_face_chips(): rgb_img, shape = get_test_image_and_shape() shapes = dlib.full_object_detections() shapes.append(shape) return dlib.get_face_chips(rgb_img, shapes)