img = cv2.imread(path, 1) # OpenCV loads images with color channels # in BGR order. So we need to reverse them return img[...,::-1] # Initialize the OpenFace face alignment utility alignment = AlignDlib('models/landmarks.dat') # Load an image of Jacques Chirac jc_orig = load_image(metadata[6].image_path()) # Detect face and return bounding box bb = alignment.getLargestFaceBoundingBox(jc_orig) # Transform image using specified face landmark indices and crop image to 96x96 jc_aligned = alignment.align(96, jc_orig, bb, landmarkIndices=AlignDlib.OUTER_EYES_AND_NOSE) # Show original image # plt.subplot(131) # plt.imshow(jc_orig) # # Show original image with bounding box # plt.subplot(132) # plt.imshow(jc_orig) # plt.gca().add_patch(patches.Rectangle((bb.left(), bb.top()), bb.width(), bb.height(), fill=False, color='red')) # # Show aligned image # plt.subplot(133) # plt.imshow(jc_aligned);
bb = alignment.getAllFaceBoundingBoxes(img) N = len(bb) for j in range(N): if N>=3: abhi_flag=1 aman_flag=1 anomaly.append(i) aman_emo_flag=0 abhi_emo_flag=0 break else: img1 = imgg[bb[j].top():bb[j].bottom(), bb[j].left():bb[j].right()] # img1 = cv2.cvtColor(img1, cv2.COLOR_RGB2BGR) print("face ",j) # cv2.imwrite('E:\\projectImplementation\\projectFiles\\frames\\face' + str(j+1) +'\\frame'+ str(i)+'.jpg', img1) im_aligned = alignment.align(96, img, bb[j], landmarkIndices=AlignDlib.OUTER_EYES_AND_NOSE) # im_aligned = cv2.cvtColor(im_aligned, cv2.COLOR_RGB2BGR) name=find_id(im_aligned) if name=="Abhishek": abhi_flag=1 abhi_emo_flag= find_emo(img1) if abhi_emo_flag==0: es1=eye_ext(img1,trainedModel,eye_cascade_left) if es1=='closed': abhi_flag=0 print(name) elif name=="amany": aman_flag=1 y_flag=1 print(name)
# 人脸检测、对齐和提取 import matplotlib.pyplot as plt import matplotlib.patches as patches from align import AlignDlib # 初始化 OpenFace 人脸对齐工具,使用 Dlib 提供的 68 个关键点 alignment = AlignDlib('landmarks.dat') # 加载一张训练图像 img = load_image(metadata[0].image_path()) # 检测人脸并返回边框 bb = alignment.getLargestFaceBoundingBox(img) # 使用指定的人脸关键点转换图像并截取 96x96 的人脸图像 aligned_img = alignment.align(96, img, bb, landmarkIndices=AlignDlib.OUTER_EYES_AND_NOSE) # 绘制原图 # plt.figure(1) # plt.subplot(131) # plt.imshow(img) # plt.xticks([]) # plt.yticks([]) # # 绘制带人脸边框的原图 # plt.subplot(132) # plt.imshow(img) # plt.gca().add_patch(patches.Rectangle((bb.left(), bb.top()), bb.width(), bb.height(), fill=False, color='red')) # plt.xticks([]) # plt.yticks([]) # # 绘制对齐后截取的 96x96 人脸图像 # plt.subplot(133)
class WebcamTest(): def __init__(self): # Initialize the dlib face alignment utility self.alignment = AlignDlib('shape_predictor_68_face_landmarks.dat') self.detector = dlib.get_frontal_face_detector() # Load the model self.model = load_model('weights_final.hdf5', custom_objects={ 'triplet_loss': self.triplet_loss, 'tf': tf }) # Get the web camera feed self.cap = cv2.VideoCapture(0) # Defining variables self.threshold = 0.7 self.base_images = [] self.distances = [] self.set_new_person = False self.saving = False self.pressed = 0 self.next_base_image = 0 self.names = [] self.saved_images = [] self.counter = 0 # Defining the path for image saving self.path = os.getcwd() + '\persons' # Delete previous directories if os.path.isdir(self.path): shutil.rmtree(self.path, onerror=self.onerror) # Create the 'persons' directory if not os.path.isdir(self.path): os.mkdir(self.path) def onerror(self, func, path, exc_info): """ Error handler for `shutil.rmtree`. If the error is due to an access error (read only file) it attempts to add write permission and then retries. If the error is for another reason it re-raises the error. Usage : `shutil.rmtree(path, onerror=onerror)` """ import stat if not os.access(path, os.W_OK): # Is the error an access error ? os.chmod(path, stat.S_IWUSR) func(path) else: raise # take a bounding predicted by dlib and convert it # to the format (x, y, w, h) as we would normally do # with OpenCV def rect_to_bb(self, rect): x = rect.left() y = rect.top() w = rect.right() - x h = rect.bottom() - y # return a tuple of (x, y, w, h) return (x, y, w, h) # The model calculates the distance between the two images def prediction(self, network, pic_1, pic_2): # increase img dimensions img1 = np.expand_dims(pic_1, axis=0) img1 = np.expand_dims(img1, axis=3) img2 = np.expand_dims(pic_2, axis=0) img2 = np.expand_dims(img2, axis=3) # calculate the network's prediction on img1 and img2 preds = network.predict([img1, img2, img1])[0] pred1 = preds[:128] pred2 = preds[128:256] # calculate the distance between the two images dist = np.sum(np.square(pred1 - pred2)) # print("distance between pictures: {:2.4f}".format(dist)) return dist def triplet_loss(self, y_true, y_pred, alpha=0.2): """ Implementation of the triplet loss function Arguments: y_true -- true labels, required when you define a loss in Keras, you don't need it in this function. y_pred -- python list containing three objects: anchor -- the encodings for the anchor data positive -- the encodings for the positive data (similar to anchor) negative -- the encodings for the negative data (different from anchor) Returns: loss -- real number, value of the loss """ anchor = y_pred[:, :128] positive = y_pred[:, 128:256] negative = y_pred[:, 256:] # distance between the anchor and the positive pos_dist = K.sum(K.square(anchor - positive), axis=1) # distance between the anchor and the negative neg_dist = K.sum(K.square(anchor - negative), axis=1) # compute loss basic_loss = pos_dist - neg_dist + alpha loss = K.maximum(basic_loss, 0.0) return loss # Aligns the given image def image_processing(self, image): # Detect face and return bounding box rect = self.alignment.getLargestFaceBoundingBox(image) img_aligned = self.alignment.align( 64, image, rect, landmarkIndices=AlignDlib.INNER_EYES_AND_BOTTOM_LIP) return img_aligned # Saves the given image with the given name def save_pictures(self, image, path, name, extension): img_item = path + '\\' + name + "." + extension cv2.imwrite(img_item, image) # The main part of the class def cycle(self): # Capture frame-by-frame ret, frame = self.cap.read() # Turn the image into a greyscale gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Detect the faces rects = self.detector(gray, 1) for rect in rects: (x, y, w, h) = self.rect_to_bb(rect) roi_gray = gray[y:y + h, x:x + w] roi_color = frame[y:y + h, x:x + w] # Align the image aligned_image = self.alignment.align( 64, gray, rect, landmarkIndices=AlignDlib.INNER_EYES_AND_BOTTOM_LIP) # If the alignment was succesful if aligned_image is not None: # If there is a new recording save the recorded images (the saving part is done in the 'webacm_app.py') if self.set_new_person: self.counter += 1 if self.counter % 10 == 0: if len(self.saved_images) < 10: self.saved_images.append(aligned_image) else: self.counter = 0 # Create a directory for the new person and save the person's images if self.saving: self.path_person = self.path + '\\' + self.names[-1] os.mkdir(self.path_person) for (i, picture) in enumerate(self.base_images[-1]): self.save_pictures(picture, self.path_person, self.names[-1] + '_' + str(i), 'jpg') self.saving = False # Calculate the average distance from the save distance for i in range(len(self.base_images)): average_distance = 0 for base_image in self.base_images[i]: average_distance += self.prediction( self.model, aligned_image / 255.0, base_image / 255.0) self.distances[i] = average_distance / len( self.base_images[i]) # If the picture alignment was unsucessful, then the distance belonging to the picture is four else: for i in range(len(self.distances)): self.distances[i] = 4 if self.set_new_person: color = (0, 255, 0) else: color = (255, 0, 0) # BGR stroke = 2 end_cord_x = x + w end_cord_y = y + h cv2.rectangle(frame, (x, y), (end_cord_x, end_cord_y), color, stroke) min_index = 0 # Calculate the minimum distances from all person for (i, distance) in enumerate(self.distances): if distance < self.distances[min_index]: min_index = i # Display the distance values on the feed if len(self.distances ) > 0 and self.distances[min_index] < self.threshold: font = cv2.FONT_HERSHEY_SIMPLEX name = self.names[min_index] + "{:2.2f}".format( self.distances[min_index]) color = (255, 255, 255) stroke = 1 cv2.putText(frame, name, (x, y), font, 1, color, stroke, cv2.LINE_AA) else: font = cv2.FONT_HERSHEY_SIMPLEX color = (255, 255, 255) stroke = 1 if len(self.distances) > 0: name = "{:2.2f}".format(self.distances[min_index]) cv2.putText(frame, name, (x, y), font, 1, color, stroke, cv2.LINE_AA) return frame
cap = cv2.VideoCapture(0) while cap.isOpened(): ret, img = cap.read() boxes = get_bb(img) print(boxes) if len(boxes) != 0: for box in boxes: bb = dlib.rectangle(left=int(box[0]), top=int(box[1]), right=int(box[2]), bottom=int(box[3])) img_aligned = alignment.align( 96, img, bb, landmarkIndices=AlignDlib.INNER_EYES_AND_BOTTOM_LIP) encoded = encoding(img_aligned) val = predict(encoded) cv2.rectangle(img, (_bb.left(), _bb.top()), (_bb.left() + _bb.width(), _bb.top() + _bb.height()), (0, 255, 0), 3) font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText(img, val[0], (_bb.left(), _bb.top()), font, 1, (255, 255, 255)) cv2.imshow('img', img) k = cv2.waitKey(1) & 0xff if k == 'q': break cap.release()
def findd(): import cv2 import matplotlib.pyplot as plt import matplotlib.patches as patches import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' import PIL.Image as Image import numpy as np from model import create_model from keras import backend as K import tensorflow as tf from align import AlignDlib from keras import backend as K tf.keras.backend.clear_session() K.clear_session() alignment = AlignDlib('models/landmarks.dat') # img= load_image('E:\\projectImplementation\\projectFiles\\imageee.png') img = cv2.imread('E:\\projectImplementation\\projectFiles\\imageee.png', 1) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) bb = alignment.getLargestFaceBoundingBox(img) im_aligned = alignment.align(96, img, bb, landmarkIndices=AlignDlib.OUTER_EYES_AND_NOSE) anjani = np.load('anjani.npy') aman = np.load('aman.npy') rapaka = np.load('rapaka.npy') akc = np.load('akc.npy') abhi = np.load('abhishek.npy') hemanth = np.load('hemanth.npy') subham = np.load('subham.npy') vivek = np.load('vivek.npy') nn4_small2_pretrained = create_model() nn4_small2_pretrained.load_weights('nn4.small2.v1.h5') im_aligned = (im_aligned / 255.).astype(np.float32) im_aligned = np.expand_dims(im_aligned, axis=0) embedded = nn4_small2_pretrained.predict(im_aligned)[0] # print(embedded) # tf.keras.backend.clear_session() # K.clear_session() distances = [] distances.append(np.linalg.norm(embedded - anjani)) distances.append(np.linalg.norm(embedded - abhi)) distances.append(np.linalg.norm(embedded - subham)) distances.append(np.linalg.norm(embedded - rapaka)) distances.append(np.linalg.norm(embedded - hemanth)) distances.append(np.linalg.norm(embedded - aman)) distances.append(np.linalg.norm(embedded - akc)) fin = min(distances) if (fin == distances[0]): return "Anjani" if (fin == distances[1]): return "Abhishek" if (fin == distances[2]): return "Subham" if (fin == distances[3]): return "Rapaka" if (fin == distances[4]): return "Hemanth" if (fin == distances[5]): return "Aman" if (fin == distances[6]): return "Akshay"
# Capture frame-by-frame ret, frame = cap.read() flipped=cv2.flip(frame,1) bb = alignment.getLargestFaceBoundingBox(flipped) y1=bb.top() x1=bb.left() y2=bb.top()+bb.height() x2=bb.left()+bb.width() x3=bb.left() y3=y2+50 aligned = alignment.align(96, flipped, bb, landmarkIndices=AlignDlib.OUTER_EYES_AND_NOSE) img = (aligned / 255.).astype(np.float32) embedded= nn4_small2_pretrained.predict(np.expand_dims(img, axis=0))[0] example_prediction = svc.predict([embedded]) example_identity = encoder.inverse_transform(example_prediction)[0] cv2.putText(flipped,example_identity,(x3,y3),font,1,(0,0,255),1,cv2.LINE_AA) cv2.imshow('frame',cv2.rectangle(flipped, (x1,y1 ), (x2, y2), (0,0,255), 2)) if cv2.waitKey(1) & 0xFF == ord('q'): break except Exception as e: continue # When everything done, release the capture
bb = get_bb(img) print(bb) font = cv2.FONT_HERSHEY_SIMPLEX for a in bb: t = a.top() l = a.left() r = a.right() b = a.bottom() # _img = img _img = img[t:b, l:r] # print(_img) cv2.imshow('img', _img) _img = alignment.align(96, _img, a, landmarkIndices=AlignDlib.INNER_EYES_AND_BOTTOM_LIP) # cv2.imshow('img',_img) _img = (_img / 255.).astype(np.float32) cv2.waitKey(0) cv2.destroyAllWindows() # cv2.waitKey(0) # cv2.destroyAllWindows() embedded = nn4_small2_pretrained.predict(np.expand_dims(_img, axis=0)) print embedded val = knn.predict(embedded) print val cv2.rectangle(img, (l, t), (r, b), (0, 255, 0), 3) cv2.putText(img, val[0], (l, t), font, 1, (255, 255, 255)) cv2.imshow('img', img)
print("Evaluating %s ."%(hdf5_path)) # Blur limit blur_limit = 100 blur = 0 # Iterates through the pictures in a dataset. for i in range(data_num): # Reading the image and the corresponding label. image = hdf5_read.root.images[i] label = hdf5_read.root.labels[i] print("Evaluation: %.3f %%"%(float(i)/float(data_num-1)*100),end="\r", flush=True) # Detect face and return bounding box rect = alignment.getLargestFaceBoundingBox(image) # Transform image using specified face landmark indices and crop image to 64x64 img_aligned = alignment.align(64, image, rect, landmarkIndices=AlignDlib.INNER_EYES_AND_BOTTOM_LIP) # If the aligning was successful if img_aligned is not None: # Check if the picture has to much black (empty) pixels, which would indicate a bad image, because # a face at the edge of a picture could be recognized and aligned even if it's missing part of the face black = 0 # Counting black (empty pixels) for l in range(img_aligned.shape[0]): for k in range(img_aligned.shape[1]): if img_aligned[l,k] == 0: black+=1 # Checking the threshold for black (empty) pixels if black<100: # Calculating blur value. blur = cv2.Laplacian(img_aligned, cv2.CV_64F).var() # Comparing the blur value to the threshold.
class FaceDetection: def __init__(self, anchors, name): nn4_small2_pretrained = create_model() nn4_small2_pretrained.load_weights('weights/nn4.small2.v1.h5') self.nn4_small2_pretrained = nn4_small2_pretrained self.alignment = AlignDlib('models/landmarks.dat') anchors = [(img / 255.).astype(np.float32) for img in anchors] anchors_embeddings = [ self.nn4_small2_pretrained.predict(np.expand_dims(anchor, axis=0))[0] for anchor in anchors ] self.anchors_embeddings = anchors_embeddings self.name = name def import_image_from_path(self, i): # img = self.load_image(path) imgs = self.align_images(i) imgs = [(img / 255.).astype(np.float32) for img in imgs] return imgs def align_images(self, org_img): bb = self.alignment.getAllFaceBoundingBoxes(org_img) aligned_images = [ self.alignment.align(96, org_img, bb, landmarkIndices=AlignDlib.OUTER_EYES_AND_NOSE) for bb in bb ] return aligned_images def distance(self, emb1, emb2): return np.sum(np.square(emb1 - emb2)) def cv_predict_mat(self, frame, verbose=False): faces = self.import_image_from_path(frame) if (len(faces) == 0 or isinstance(faces, int)): if (verbose): print(chalk.white('No faces')) return [], [[0]] if (verbose): print(chalk.white(str(len(faces)) + ' faces detected')) faces_embeddings = [ self.nn4_small2_pretrained.predict(np.expand_dims(face, axis=0))[0] for face in faces ] r = [] for idx, f in enumerate(faces_embeddings): k = [] for jdx, a in enumerate(self.anchors_embeddings): d = self.distance(f, a) if (verbose): print( chalk.yellow( d, 'face#' + str(idx) + ' with anchor#' + str(jdx))) k.append(d) r.append(k) return (faces, r) def cv_predict(self, frame, verbose=False): faces, m = self.cv_predict_mat(frame, verbose) avg = [] for idx, i in enumerate(m): k = np.mean(i) avg.append(k) if (k < 0.56): print(chalk.green(self.name + " deteced")) k = 1 mx = 0 mn = 0 if (len(m) > 0 and len(m[0]) > 0): mx = np.max(m) mn = np.min(m) return faces, (mx, mn, avg)
def align_image(img): alignment = AlignDlib('models/landmarks.dat') return alignment.align(96, img, alignment.getLargestFaceBoundingBox(img), landmarkIndices=AlignDlib.OUTER_EYES_AND_NOSE)
def alignMain(args): helper.mkdirP(args.outputDir) imgs = list(data.iterImgs(args.inputDir)) # Shuffle so multiple versions can be run at once. random.shuffle(imgs) landmarkMap = { 'outerEyesAndNose': AlignDlib.OUTER_EYES_AND_NOSE, 'innerEyesAndBottomLip': AlignDlib.INNER_EYES_AND_BOTTOM_LIP } if args.landmarks not in landmarkMap: raise Exception("Landmarks unrecognized: {}".format(args.landmarks)) landmarkIndices = landmarkMap[args.landmarks] align = AlignDlib(fileDir) nFallbacks = 0 for imgObject in imgs: print("=== {} ===".format(imgObject.path)) outDir = os.path.join(args.outputDir, imgObject.cls) helper.mkdirP(outDir) outputPrefix = os.path.join(outDir, imgObject.name) imgName = outputPrefix + ".png" if os.path.isfile(imgName): if args.verbose: print(" + Already found, skipping.") else: rgb = imgObject.getRGB() if rgb is None: if args.verbose: print(" + Unable to load.") outRgb = None else: try: outRgb = align.align(args.size, rgb, landmarkIndices=landmarkIndices, skipMulti=args.skipMulti) if outRgb is None and args.verbose: print(" + Unable to align.") except: outRgb = None print(" + Unable to align.") if args.fallbackLfw and outRgb is None: nFallbacks += 1 deepFunneled = "{}/{}.jpg".format(os.path.join(args.fallbackLfw, imgObject.cls), imgObject.name) shutil.copy(deepFunneled, "{}/{}.jpg".format(os.path.join(args.outputDir, imgObject.cls), imgObject.name)) if outRgb is not None: if args.verbose: print(" + Writing aligned file to disk.") outBgr = cv2.cvtColor(outRgb, cv2.COLOR_RGB2BGR) cv2.imwrite(imgName, outBgr) if args.fallbackLfw: print('nFallbacks:', nFallbacks)
def main(predict_eg): nn4_small2 = create_model() # Input for anchor, positive and negative images input_a = Input(shape=(96, 96, 3)) input_p = Input(shape=(96, 96, 3)) input_n = Input(shape=(96, 96, 3)) # Output for anchor, positive and negative embedding vectors emb_a = nn4_small2(input_a) emb_p = nn4_small2(input_p) emb_n = nn4_small2(input_n) # Layer that computes the triplet loss from anchor, positive and negative embedding vectors triplet_loss_layer = TripletLossLayer.TripletLossLayer( alpha=0.2, name='triplet_loss_layer')([emb_a, emb_p, emb_n]) # Model that can be trained with anchor, positive negative images nn4_small2_train = Model([input_a, input_p, input_n], triplet_loss_layer) # triplet_generator() creates a generator that continuously returns # ([a_batch, p_batch, n_batch], None) tuples where a_batch, p_batch # and n_batch are batches of anchor, positive and negative RGB images # each having a shape of (batch_size, 96, 96, 3). generatorImage = triplet_generator() nn4_small2_train.compile(loss=None, optimizer='adam') nn4_small2_train.fit_generator(generatorImage, epochs=10, steps_per_epoch=100) # nn4_small2_train.fit_generator(generatorImage, epochs=2, steps_per_epoch=100) # Please note that the current implementation of the generator only generates # random image data. The main goal of this code snippet is to demonstrate # the general setup for model training. In the following, we will anyway # use a pre-trained model so we don't need a generator here that operates # on real training data. I'll maybe provide a fully functional generator # later. nn4_small2_pretrained = create_model() nn4_small2_pretrained.load_weights('weights/nn4.small2.v1.h5') metadata = load_metadata('images') # Initialize the OpenFace face alignment utility alignment = AlignDlib('models/landmarks.dat') # Load any random image jc_orig = load_image(metadata[77].image_path()) # Detect face and return bounding box bb = alignment.getLargestFaceBoundingBox(jc_orig) # Transform image using specified face landmark indices and crop image to 96x96 jc_aligned = alignment.align(96, jc_orig, bb, landmarkIndices=AlignDlib.OUTER_EYES_AND_NOSE) # Show original image plt.subplot(131) plt.imshow(jc_orig) plt.title("Orig Image") # plt.show() # Show original image with bounding box plt.subplot(132) plt.imshow(jc_orig) plt.title("with BB") # plt.show() plt.gca().add_patch( patches.Rectangle((bb.left(), bb.top()), bb.width(), bb.height(), fill=False, color='red')) # Show aligned image plt.subplot(133) plt.imshow(jc_aligned) plt.title("Aligned Image") plt.show() def align_image(img): return alignment.align(96, img, alignment.getLargestFaceBoundingBox(img), landmarkIndices=AlignDlib.OUTER_EYES_AND_NOSE) embedded = np.zeros((metadata.shape[0], 128)) for i, m in enumerate(metadata): img = load_image(m.image_path()) img = align_image(img) # scale RGB values to interval [0,1] img = (img / 255.).astype(np.float32) # obtain embedding vector for image embedded[i] = nn4_small2_pretrained.predict(np.expand_dims(img, axis=0))[0] def show_pair(idx1, idx2): plt.figure(figsize=(8, 3)) plt.suptitle( f'Distance = {distance(embedded[idx1], embedded[idx2]):.2f}') plt.subplot(121) plt.imshow(load_image(metadata[idx1].image_path())) # plt.show() plt.subplot(122) plt.imshow(load_image(metadata[idx2].image_path())) plt.show() show_pair(77, 78) show_pair(77, 50) distances = [] # squared L2 distance between pairs identical = [] # 1 if same identity, 0 otherwise num = len(metadata) for i in range(num - 1): for j in range(i + 1, num): distances.append(distance(embedded[i], embedded[j])) identical.append(1 if metadata[i].name == metadata[j].name else 0) distances = np.array(distances) identical = np.array(identical) thresholds = np.arange(0.3, 1.0, 0.01) #F1 Score f1_scores = [f1_score(identical, distances < t) for t in thresholds] acc_scores = [accuracy_score(identical, distances < t) for t in thresholds] opt_idx = np.argmax(f1_scores) # Threshold at maximal F1 score opt_tau = thresholds[opt_idx] # Accuracy at maximal F1 score opt_acc = accuracy_score(identical, distances < opt_tau) # Plot F1 score and accuracy as function of distance threshold plt.plot(thresholds, f1_scores, label='F1 score') plt.plot(thresholds, acc_scores, label='Accuracy') plt.axvline(x=opt_tau, linestyle='--', lw=1, c='lightgrey', label='Threshold') plt.title(f'Accuracy at threshold {opt_tau:.2f} = {opt_acc:.3f}') plt.xlabel('Distance threshold') plt.legend() plt.show() dist_pos = distances[identical == 1] dist_neg = distances[identical == 0] plt.figure(figsize=(12, 4)) plt.subplot(121) plt.hist(dist_pos) plt.axvline(x=opt_tau, linestyle='--', lw=1, c='lightgrey', label='Threshold') plt.title('Distances (+ve pairs)') plt.legend() plt.subplot(122) plt.hist(dist_neg) plt.axvline(x=opt_tau, linestyle='--', lw=1, c='lightgrey', label='Threshold') plt.title('Distances (-ve pairs)') plt.legend() plt.show() targets = np.array([m.name for m in metadata]) encoder = LabelEncoder() encoder.fit(targets) # Numerical encoding of identities y = encoder.transform(targets) train_idx = np.arange(metadata.shape[0]) % 2 != 0 test_idx = np.arange(metadata.shape[0]) % 2 == 0 # 50 train examples of 10 identities (5 examples each) X_train = embedded[train_idx] # 50 test examples of 10 identities (5 examples each) X_test = embedded[test_idx] y_train = y[train_idx] y_test = y[test_idx] knn = KNeighborsClassifier(n_neighbors=1, metric='euclidean') svc = LinearSVC() knn.fit(X_train, y_train) svc.fit(X_train, y_train) acc_knn = accuracy_score(y_test, knn.predict(X_test)) acc_svc = accuracy_score(y_test, svc.predict(X_test)) print(f'KNN accuracy = {acc_knn}, SVM accuracy = {acc_svc}') warnings.filterwarnings('ignore') example_idx = predict_eg print("example_idx", example_idx) example_image = load_image(metadata[test_idx][example_idx].image_path()) example_prediction = svc.predict([embedded[test_idx][example_idx]]) example_identity = encoder.inverse_transform(example_prediction)[0] plt.imshow(example_image) plt.title(f'Recognized as {example_identity}') plt.show() X_embedded = TSNE(n_components=2).fit_transform(embedded) for i, t in enumerate(set(targets)): idx = targets == t plt.scatter(X_embedded[idx, 0], X_embedded[idx, 1], label=t) plt.legend(bbox_to_anchor=(1, 1)) plt.show()
# print encodes # for a in range(len(bb)): # val = knn.predict(encodes[a]) # labels.append(val) # # print val for a in bb: t = a.top() l = a.left() r = a.right() b = a.bottom() # _img = img _img = img[t:b,l:r] _img = alignment.align(96,_img) cv2.imshow('img',_img) _img = (_img / 255.).astype(np.float32) cv2.waitKey(0) cv2.destroyAllWindows() embedded = nn4_small2_pretrained.predict(np.expand_dims(_img, axis=0)) print embedded val = knn.predict(embedded) print val cv2.rectangle(img,(l,t),(r,b),(0,255,0),3) cv2.putText(img,val[0],(l,t), font, 1,(255,255,255)) cv2.imshow('img',img) cv2.imwrite('res1.jpg',img) cv2.waitKey(0) cv2.destroyAllWindows()
class Trainer(): def __init__(self): # self.image_dir = sys.argv[1] # self.image_dir = "/home/webwerks/projects/project-face-recognition/23-07/face_detection/uploads" self.image_dir = "/home/webwerks/projects/project-face-recognition/23-07/face_detection/src/images" self.model = self.create_model() self.alignment = AlignDlib('models/landmarks.dat') def create_model(self): nn4_small2_pretrained = create_model() nn4_small2_pretrained.load_weights('weights/nn4.small2.v1.h5') return nn4_small2_pretrained def load_metadata(self, path): print("loading metadata ...") metadata = [] for i in sorted(os.listdir(path)): for f in sorted(os.listdir(os.path.join(path, i))): # Check file extension. Allow only jpg/jpeg' files. ext = os.path.splitext(f)[1].lower() if ext == '.jpg' or ext == '.jpeg' or ext == '.png': metadata.append(IdentityMetadata(path, i, f)) return np.array(metadata) def load_image(self, path): img = cv2.imread(path, 1) return img def align_image(self, img): return self.alignment.align( 96, img, self.alignment.getLargestFaceBoundingBox(img), landmarkIndices=AlignDlib.OUTER_EYES_AND_NOSE) def train(self): metadata = self.load_metadata(self.image_dir) np.save('train-files/metadata.npy', metadata) print("creating embeddings ...") embedded = {} for i, m in enumerate(metadata): img = self.load_image(m.image_path()) img = self.align_image(img) if img is not None: # scale RGB values to interval [0,1] img = (img / 255.).astype(np.float32) # obtain embedding vector for image embedded[m.image_path()] = self.model.predict( np.expand_dims(img, axis=0))[0] pickle.dump(embedded, open("train-files/embeddings.pkl", "wb")) def train_classifier(self): print("training classifier ...") embedded = pickle.load(open("train-files/embeddings.pkl", "rb")) metadata = np.load('train-files/metadata.npy') targets = np.array([m.name for m in metadata]) encoder = LabelEncoder() encoder.fit(targets) pickle.dump(encoder, open("train-files/encoder.pkl", "wb")) # Numerical encoding of identities y = encoder.transform(targets) train_idx = np.arange(metadata.shape[0]) % 2 != 0 test_idx = np.arange(metadata.shape[0]) % 2 == 0 X_train = embedded[train_idx] X_test = embedded[test_idx] y_train = y[train_idx] y_test = y[test_idx] svc = LinearSVC() svc.fit(X_train, y_train) joblib.dump(svc, 'train-files/svc.pkl') acc_svc = accuracy_score(y_test, svc.predict(X_test)) print(f'SVM accuracy = {acc_svc}')
#image = cv2.imread(image) img_cvt = cv2.cvtColor(image,cv2.COLOR_BGR2RGB) img_normal = img_cvt/255 emb_image = nn4_small2_pretrained.predict(np.expand_dims(img_normal,axis=0)) dist = [] for i in range(len(embedded)): dist.append(distance(embedded[i],emb_image)) min_dist = np.min(np.array(dist)) if min_dist > threshold: text = "Stranger" else : arg_min = np.argmin(np.array(dist)) text = label[arg_min] return text ap = argparse.ArgumentParser() ap.add_argument("-i","--image",required=True,help = "Path to the image") args = vars(ap.parse_args()) image = cv2.imread(args["image"]) rects = detec(image,1) for rect in rects : x,y,w,h = rect.left(),rect.top(),rect.right(),rect.bottom() roi = image[y:h,x:w] bb = alignment.getLargestFaceBoundingBox(image) image_alignment = alignment.align(96, image, bb, landmarkIndices=AlignDlib.OUTER_EYES_AND_NOSE) text = predict_face(image_alignment,0.5) cv2.putText(image,text,(x,y-10),cv2.FONT_ITALIC,1,(0,255,0),2) cv2.rectangle(image,(x,y),(w,h),(0,255,0),2) cv2.imshow("image",image) cv2.waitKey() cv2.destroyAllWindows()