From 107eb0031b019d0820c821a913c023abf9f048e5 Mon Sep 17 00:00:00 2001 From: ThoughtfulDev Date: Fri, 30 Mar 2018 21:03:17 +0200 Subject: [PATCH 1/2] Noise based on Face Recognition --- person_blocker.py | 81 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 80 insertions(+), 1 deletion(-) diff --git a/person_blocker.py b/person_blocker.py index e903633..6f4ef94 100644 --- a/person_blocker.py +++ b/person_blocker.py @@ -9,6 +9,10 @@ from ast import literal_eval as make_tuple import imageio import visualize +import face_recognition +from pathlib import Path +import cv2 + # Creates a color layer and adds Gaussian noise. # For each pixel, the same noise value is added to each channel @@ -44,6 +48,49 @@ def string_to_rgb_triplet(triplet): return triplet +def loadKnown(pathstr): + known_face_encodings = [] + pathlist = Path(pathstr).glob('**/*.jpg') + for p in pathlist: + p_str = str(p) + im = face_recognition.load_image_file(p_str) + encoding = face_recognition.face_encodings(im)[0] + known_face_encodings.append(encoding) + return known_face_encodings + +def faceRecog(orig_img, known_face_encodings): + face_locations = [] + face_encodings = [] + face_names = [] + #scale down to 1/4 to make it quicker + img = cv2.resize(orig_img, (0, 0), fx=0.25, fy=0.25) + img = img[:, :, ::-1] + + face_locations = face_recognition.face_locations(img) + face_encodings = face_recognition.face_encodings(img, face_locations) + for face_encoding in face_encodings: + matches = face_recognition.compare_faces(known_face_encodings, face_encoding) + name = "Unknown" + if True in matches: + name = "Hit" + face_names.append(name) + + res = [] + for (top, right, bottom, left), name in zip(face_locations, face_names): + if name == "Hit": + # Scale back up face locations since the frame we detected in was scaled to 1/4 size + top *= 4 + right *= 4 + bottom *= 4 + left *= 4 + x1 = left + y1 = top + x2 = right + y2 = bottom + res.append([x1, y1, x2, y2]) + return res + + def person_blocker(args): # Required to load model, but otherwise unused @@ -66,6 +113,26 @@ def person_blocker(args): # Create masks for all objects results = model.detect([image], verbose=0) r = results[0] + + # Do Face Recognition and get the Object ID + if args.facerecog: + if not args.known_path: + print('No known path specified') + sys.exit() + known_face_encodings = loadKnown(args.known_path) + detected_face_locations = faceRecog(image, known_face_encodings) + rois = r['rois'] + tmp_objects = [] + for face_loc in detected_face_locations: + for i,ri in enumerate(rois): + x1 = ri[1] + y1 = ri[0] + x2 = ri[3] + y2 = ri[2] + #check if the face boundaries are within the boundary of the rois + if face_loc[0] >= x1 and face_loc[1] <= x2 and face_loc[2] >= x1 and face_loc[2] <= x2: + if face_loc[1] >= y1 and face_loc[1] <= y2 and face_loc[3] >= y1 and face_loc[3] <= y2: + tmp_objects.append(str(i)) if args.labeled: position_ids = ['[{}]'.format(x) @@ -76,7 +143,11 @@ def person_blocker(args): sys.exit() # Filter masks to only the selected objects - objects = np.array(args.objects) + if args.facerecog: + objects = np.array(tmp_objects) + else: + objects = np.array(args.objects) + # Object IDs: if np.all(np.chararray.isnumeric(objects)): @@ -111,6 +182,7 @@ def person_blocker(args): images.append(new_image) imageio.mimsave('person_blocked.gif', images, fps=30., subrectangles=True) + if __name__ == '__main__': @@ -138,6 +210,13 @@ def person_blocker(args): '--names', dest='names', action='store_true', help='prints class names and exits.') + parser.add_argument('-f', + '--facerecog', dest='facerecog', + action='store_true', + help='Does face recognition') + parser.add_argument('-k', + '--known', dest='known_path', + help='Path to known images', required=False) parser.set_defaults(labeled=False, names=False) args = parser.parse_args() From 08ea2682e9f0c35276f73ea24691198cdea4f344 Mon Sep 17 00:00:00 2001 From: ThoughtfulDev Date: Sat, 31 Mar 2018 10:52:26 +0200 Subject: [PATCH 2/2] Fix for when the persons Face is not found --- person_blocker.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/person_blocker.py b/person_blocker.py index 6f4ef94..212bf31 100644 --- a/person_blocker.py +++ b/person_blocker.py @@ -11,8 +11,6 @@ import visualize import face_recognition from pathlib import Path -import cv2 - # Creates a color layer and adds Gaussian noise. # For each pixel, the same noise value is added to each channel @@ -62,9 +60,7 @@ def faceRecog(orig_img, known_face_encodings): face_locations = [] face_encodings = [] face_names = [] - #scale down to 1/4 to make it quicker - img = cv2.resize(orig_img, (0, 0), fx=0.25, fy=0.25) - img = img[:, :, ::-1] + img = orig_img[:, :, ::-1] face_locations = face_recognition.face_locations(img) face_encodings = face_recognition.face_encodings(img, face_locations) @@ -78,11 +74,6 @@ def faceRecog(orig_img, known_face_encodings): res = [] for (top, right, bottom, left), name in zip(face_locations, face_names): if name == "Hit": - # Scale back up face locations since the frame we detected in was scaled to 1/4 size - top *= 4 - right *= 4 - bottom *= 4 - left *= 4 x1 = left y1 = top x2 = right @@ -144,6 +135,10 @@ def person_blocker(args): # Filter masks to only the selected objects if args.facerecog: + #that face was not found in the image so dont do anything + if len(tmp_objects) == 0: + imageio.imwrite('person_blocked.png', image) + return objects = np.array(tmp_objects) else: objects = np.array(args.objects)