def process_image(img_src): img = cv2.imread(img_src, 1) faces = face_detector.detect_faces(img) gimg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) images = [] for x, y, w, h in faces: images.append(gimg[y : y + h, x : x + w]) print 'No. of faces detected :', len(images) if len(images) == 0: return for face in images: eyes = eye_cascade.detectMultiScale(face) eye_images = [] pupils = [] if len(eyes) > 0: print 'Eyes detected :', len(eyes) # print 'Distance between x coords', eyes[1][0] - eyes[0][0] # print 'Canvas width', face.shape diff = (eyes[1][0] + eyes[1][2]/2) - (eyes[0][0] + eyes[0][2]/2) print abs(diff) / face.shape[1] for (ex, ey, ew, eh) in eyes: cv2.rectangle(face, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2) eye = face[ey : ey + eh, ex : ex + ew] eye = cv2.GaussianBlur(eye, (5, 5), 0) eye_images.append(eye) circles = cv2.HoughCircles(eye, cv2.HOUGH_GRADIENT, 1, 20, param1 = 200, param2 = 30, minRadius = 0, maxRadius = 0) # circles = cv2.HoughCircles(eye, cv2.HOUGH_GRADIENT, 1.2, 20) # print ex, ey, ew, eh if circles is None: continue circles = np.uint16(np.around(circles)) for i in circles[0,:]: pupils.append((i[0] + ex, i[1] + ey, i[2])) # draw the outer circle cv2.circle(face, (i[0] + ex, i[1] + ey), i[2], (0, 255, 0), 2) # draw the center of the circle cv2.circle(face, (i[0] + ex, i[1] + ey), 2, (0, 0, 255), 3) mouth = mouth_cascade.detectMultiScale(face) print mouth
# -*- coding:utf-8 -*- import cv2 import sys from sentiment_classifier import SentimentClassifier from face_detector import detect_faces, show_detection_result if __name__ == '__main__': model = SentimentClassifier() #model.train() #model.save() model.load() img_path = input('\n Enter img path:') face_imgs = detect_faces(img_path) if len(face_imgs) != 1: print('Detect no faces.') sys.exit() result = model.predict(img_path) for item in result: print('{0} probability:{1}'.format(item['label'], item['prob']))
embeddings = output.data.numpy() return embeddings def compare_embeddings(embedding, embeddings): embedding_norm = np.linalg.norm(embedding) divider = (np.linalg.norm(embeddings, axis=1) * embedding_norm + 1e-5)[:, None] cosdistances = np.dot(embeddings / divider, embedding) return cosdistances net = getattr(net_sphere, 'sphere20a')() net.load_state_dict(torch.load('sphere20a.pth')) net.eval() net.feature = True if __name__ == "__main__": import os emebeddings = [] for filename in os.listdir("testing_images"): image = cv2.imread("testing_images/" + filename) faces = detect_faces(image) emebeddings.append(get_embeddings(image, faces)) print(emebeddings)
import cv2 import numpy as np from matplotlib import pyplot as plt import os import time import face_detector import descriptor import matcher # Chose an image with multiple faces (At least 2) img_src = 'images/images.jpg' img = cv2.imread(img_src) faces = face_detector.detect_faces(img) images = [] for x, y, w, h in faces: images.append(img[y : y + h, x : x + w]) print len(images) img1 = images[1] img2 = images[3] # img2 = cv2.imread('images/sample.jpg') # kp1, des1, kp2, des2 = descriptor.surf(img1, img2) # kp1, des1, kp2, des2 = descriptor.sift(img1, img2) kp1, des1, kp2, des2 = descriptor.brief(img1, img2) print len(kp1), len(kp2)
def main(): debug_render.init() predictor_confidence = {} last_frame = None tracker = MultiTracker(remove_threshold=100, add_threshold=100, d=3) time_at_last_prediction = time.process_time() for frame in camera.get_frames(source=1):#source='../testing/motinas_multi_face_turning.avi'): grey_frame = camera.greyscale(frame) face_points = face_detector.detect_faces(grey_frame) #print(face_points) predictions = tracker.observe(face_points) # mark the latest predictions that were # assigned observations as being confident for pi in tracker.assigned: predictor = tracker.lookup_filter(pi) #print('assigned', predictor.id) predictor_confidence[predictor] = MAX_CONFIDENCE # mark rubbish predictions as being less confident # and keep track of ones we want to remove marked_for_removal = set() for pi in tracker.rubbish: predictor = tracker.lookup_filter(pi) if predictor in predictor_confidence: # remove 1 confidence points predictor_confidence[predictor] -= 1 # accumulate the points that were missing observations missing_faces = [] print('missing', tracker.missing) for pi in tracker.missing: # find the last observation made by this predictor predictor = tracker.lookup_filter(pi) if predictor not in predictor_confidence: #print('added', predictor.id) predictor_confidence[predictor] = MAX_CONFIDENCE # remove 1 confidence point predictor_confidence[predictor] -= 1 #print(predictor_confidence[predictor], predictor.id) if predictor is not None: last_observation = predictor.last_estimate[:3] # predictor.last_observation # determine how confident this predictor is confidence_vect = predictor.confidence() variance = (confidence_vect[0] + confidence_vect[1] / 2.0) if last_observation is not None and variance < 0.5: missing_faces.append(last_observation) # add new filters for ones that weren't assigned for observation in tracker.unassigned: tracker.add_filter(observation) flow = None inferred_faces = [] if last_frame is not None: # try to infer from the previous frame where missing faces might have moved to inferred_faces = template_matching.template_match_features(last_frame, grey_frame, missing_faces) # add observations for each inferred face predictions = tracker.observe(inferred_faces) flow = pose_inference.calc_flow(last_frame, grey_frame) # remove filters that we have lost faith in tracker.remove_filters([predictor for predictor, confidence in predictor_confidence.items() if confidence <= 0]) last_frame = grey_frame # make periodic predictions if time.process_time() - time_at_last_prediction > PREDICTION_INTERVAL: tracker.predict() time_at_last_prediction = time.process_time() if flow is not None: debug_render.draw_flow(frame, flow) if len(predictions) > 0: pred = predictions[0][1] head = (pred[0], pred[1], pred[2]) #pose_inference.infer_pose(flow, head) #debug_render.draw_pose(frame, {'head': head}, pose_inference.get_dimensions) debug_render.draw_features(frame, { 'face_points': face_points, 'guessed_face_points': inferred_faces, 'predictions': predictions }) output = [ "{0}\t{1:.2f}\t{2:.2f}\t{3:.2f}\t{4:.2f}\t{5:.2f}\t{6:.2f}\n".format(uuid, vect[0], vect[1], vect[2], confidence[0], confidence[1], confidence[2]) for uuid, vect, confidence in predictions ] transport.send_data(''.join(output))
def main(config): MAX_MATCHES = config['max_matches'] USE_SCALING_ALTERNATION = config['use_scaling_alternation'] show_debug = not config['headless'] faces = [] face_data = [] last_frame = None cameras = camera.set_up_cameras(config['cameras']) if USE_SCALING_ALTERNATION: # generate which face sizes to look for in each frame face_size_ranges = [] face_drop = 50 max_size = 250 min_size = 25 face_scale = (max_size/float(max_size-face_drop/2)) while max_size > min_size: face_size_ranges.append([(max_size, max_size), (max_size-face_drop, max_size-face_drop)]) max_size -= face_drop else: face_size_ranges = [[(100, 100), (10, 10)]] face_scale = 1.2 frame_index = 0 cam_args = {} if show_debug: debug_render.init() while True: key = cv2.waitKey(1) & 0xFF if key == ord('q'): # wait for quit key to be pressed break frame = camera.get_blended_frame(cameras) grey_frame = camera.greyscale(frame) if show_debug: debug_frame = copy.copy(grey_frame) debug_frame = cv2.cvtColor(debug_frame, cv2.COLOR_GRAY2BGR) max_face_size, min_face_size = face_size_ranges[frame_index] new_faces = face_detector.detect_faces(grey_frame, scale_factor=face_scale, max_size=max_face_size, min_size=min_face_size) frame_h, frame_w = grey_frame.shape[:2] if len(new_faces) > 0: # Calculate corresponding features in adjacent frames corresponding_faces = correspondence.correspond(face_data, faces, new_faces) missing_faces_data = corresponding_faces['missing_features'] faces = corresponding_faces['features'] face_data = corresponding_faces['feature_data'] else: missing_faces_data = face_data faces = [] face_data = [] # Reset properties for detected faces for face_data_point in face_data: if not face_data_point.get('has_moved', False): has_moved = False if 'last_detected_as' in face_data_point: curr_x, curr_y, curr_s = face_data_point['feature'] last_x, last_y, last_s = face_data_point['last_detected_as'] if ((last_x - curr_x)**2 + (last_y - curr_y)**2) > 10: has_moved = True # see if this face has ever moved is_moving = False if 'v' in face_data_point: vx, vy = face_data_point['v'] if (vx ** 2 + vy ** 2) > 3: is_moving = True face_data_point['has_moved'] = (has_moved and is_moving) face_data_point.update({ 'mode': 'detected', 'last_detected_as': face_data_point['feature'], 'matches_made': 0, 'alive_for': face_data_point.get('alive_for', 0) + 1 }) # Set properties for inferred faces for missing_face_data_point in missing_faces_data: missing_face_data_point['mode'] = 'inferred' if 'last_detected_as' not in missing_face_data_point: missing_face_data_point['last_detected_as'] = missing_face_data_point['feature'] if 'matches_made' in missing_face_data_point: missing_face_data_point['matches_made'] += 1 else: missing_face_data_point['matches_made'] = 1 # Filter out old inferred faces that are really old missing_faces_data = [data for data in missing_faces_data if data['matches_made'] <= MAX_MATCHES] missing_faces = [data['feature'] for data in missing_faces_data] # Infer where missing faces have moved using template matching inferred_faces = [] inferred_face_data = [] flow = None if last_frame is not None and len(missing_faces) > 0: # there's some faces missing since the last frame, let's find where they went inferred_features = template_matching.template_match_features(last_frame, grey_frame, missing_faces, missing_faces_data) # we've inferred where some of them went, let's update our face data if len(inferred_features) > 0: inferred_faces, inferred_face_data = zip(*inferred_features) faces += inferred_faces face_data += inferred_face_data if show_debug: # render pretty face boxes onto the colored frame debug_render.faces(debug_frame, face_data) # detect movement actions from the optic flow and face positions action_regions = action_detector.get_action_regions(face_data) if last_frame is not None: # calculate the optic flow of the frame, at a low sample rate flow = action_detector.calc_flow(last_frame, grey_frame) if flow is not None and show_debug: # render a pretty flow onto the colored frame debug_render.draw_flow(debug_frame, flow) # calculate face velocities for face in face_data: face['v'] = action_detector.get_face_velocity(frame, flow, face) if show_debug: debug_render.draw_action_regions(debug_frame, action_regions) action_detector.detect_actions(grey_frame, flow, action_regions) packed_features = [pack_feature(feature, (frame_w, frame_h)) for feature in face_data] # keep track of the last frame (for flow and template matching) last_frame = grey_frame # send the features over the network transport.send_features(packed_features) if show_debug: debug_render.draw_actions(debug_frame, action_regions) # draw the modified color frame on the screen debug_render.draw_frame(debug_frame) frame_index = (frame_index + 1) % len(face_size_ranges) camera.release_cams(cameras)
def run(self): import numpy as np import datetime now = datetime.datetime.now() def recognize_face(embedding, embeddings, labels, labels1, labels2, labels3, labels4, threshold=0.5): distances = np.linalg.norm(embeddings - embedding, axis=1) argmin = np.argmin(distances) minDistance = distances[argmin] if minDistance > threshold: label = "" else: label = labels[argmin] return (label, minDistance) if __name__ == "__main__": import cv2 import argparse from face_embeddings import extract_face_embeddings from face_detector import detect_faces import cloudpickle as cPickle import dlib import urllib.request import os global off off = False a = os.getcwd() url = enter1.get() ap = argparse.ArgumentParser() ap.add_argument("-i", "--image", default="4.jpg") ap.add_argument("-e", "--embeddings", default="face_embeddings.npy") ap.add_argument("-l", "--labels", default="labels.cpickle") ap.add_argument("-l1", "--labels1", default="labels1.cpickle") ap.add_argument("-l2", "--labels2", default="labels2.cpickle") ap.add_argument("-l3", "--labels3", default="labels3.cpickle") ap.add_argument("-l4", "--labels4", default="labels4.cpickle") args = vars(ap.parse_args()) embeddings = np.load(args["embeddings"]) labels = cPickle.load(open(args["labels"], "rb")) labels1 = cPickle.load(open(args["labels1"], "rb")) labels2 = cPickle.load(open(args["labels2"], "rb")) labels3 = cPickle.load(open(args["labels3"], "rb")) labels4 = cPickle.load(open(args["labels4"], "rb")) shape_predictor = dlib.shape_predictor( "models/" "shape_predictor_5_face_landmarks.dat") face_recognizer = dlib.face_recognition_model_v1( "models/" "dlib_face_recognition_resnet_model_v1.dat") while (not off): imgResp = urllib.request.urlopen(url) imgNp = np.array(bytearray(imgResp.read()), dtype=np.uint8) image = cv2.imdecode(imgNp, -1) image_original = image.copy() image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) faces = detect_faces(image) for face in faces: embedding = extract_face_embeddings( image, face, shape_predictor, face_recognizer) label = recognize_face(embedding, embeddings, labels, labels1, labels2, labels3, labels4) label1 = recognize_face( embedding, embeddings, labels1, labels, labels2, labels3, labels4) label2 = recognize_face( embedding, embeddings, labels2, labels, labels1, labels3, labels4) label3 = recognize_face( embedding, embeddings, labels3, labels, labels1, labels2, labels4) label4 = recognize_face( embedding, embeddings, labels4, labels, labels1, labels2, labels3) (x1, y1, x2, y2) = face.left(), face.top( ), face.right(), face.bottom() cv2.rectangle(image_original, (x1, y1), (x2, y2), (255, 120, 120), 2) cv2.putText(image_original, label4[0], (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 255, 0), 2) file1 = open(a + "\\firebase1.txt", "w") file2 = open(a + "\\firebase2.txt", "w") file3 = open(a + "\\firebase3.txt", "w") file4 = open(a + "\\firebase4.txt", "w") file5 = open(a + "\\firebase5.txt", "w") file6 = open(a + "\\firebase6.txt", "w") file1.write(str(label1[0])) file2.write(label[0]) file3.write(label4[0]) file4.write(label2[0]) file5.write(label3[0]) file6.write( now.strftime("%Y-%m-%d " + "%H:%M:%S")) file1.close() file2.close() file3.close() file4.close() file5.close() file6.close() cv2.imshow("Image", image_original) cv2.waitKey(1) if off == True: break cv2.destroyAllWindows()
frame_counter = 0 # Detections are saved in format {name : [[start_time, end_time]]} detections = {} working_memory = {} thresh = float("inf") while (ret): # Capture frame-by-frame ret, frame = video.read() frame_counter += 1 if ret: if frame_counter % 25 == 0: matches = [] faces = detect_faces(frame) embeddings_from_video = get_embeddings(frame, faces) for video_embd in embeddings_from_video: similiarity = compare_embeddings(video_embd, actor_embeddings) idx = np.argmax(similiarity) if similiarity[idx] < thresh: matches.append(actors[idx]) for match in matches: if match not in working_memory.keys(): working_memory[match] = frame_counter / fps matched_earlier = list(working_memory.keys()) for matched in matched_earlier: if matched not in matches: