def main(): symbol = lightened_moon_feature(num_classes=40, use_fuse=True) devs = mx.cpu() if args.gpus is None else [mx.gpu(int(i)) for i in args.gpus.split(',')] _, arg_params, aux_params = mx.model.load_checkpoint(args.model_load_prefix, args.model_load_epoch) detector = dlib.get_frontal_face_detector() face_cascade = cv2.CascadeClassifier(args.opencv) # read img and drat face rect img = cv2.imread(args.img) faces = detector(img, 1) gray = np.zeros(img.shape[0:2]) if len(faces) == 0: gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) opencv_faces = face_cascade.detectMultiScale(gray, 1.3, 5) for (x,y,w,h) in opencv_faces: faces.append(dlib.rectangle(int(x), int(y), int(x+w), int(y+h))) max_face = faces[0] if len(faces) > 0: max_face = max(faces, key=lambda rect: rect.width() * rect.height()) for f in faces: if f == max_face: cv2.rectangle(img, (f.left(), f.top()), (f.right(), f.bottom()), (0,0,255), 2) else: cv2.rectangle(img, (f.left(), f.top()), (f.right(), f.bottom()), (255,0,0), 2) cv2.imwrite(args.img.replace('jpg', 'png'), img) # crop face area gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) pad = [0.25, 0.25, 0.25, 0.25] if args.pad is None else args.pad left = int(max(0, max_face.left() - max_face.width()*float(pad[0]))) top = int(max(0, max_face.top() - max_face.height()*float(pad[1]))) right = int(min(gray.shape[1], max_face.right() + max_face.width()*float(pad[2]))) bottom = int(min(gray.shape[0], max_face.bottom()+max_face.height()*float(pad[3]))) gray = gray[left:right, top:bottom] gray = cv2.resize(gray, (args.size, args.size))/255.0 img = np.expand_dims(np.expand_dims(gray, axis=0), axis=0) # get pred arg_params['data'] = mx.nd.array(img, devs) exector = symbol.bind(devs, arg_params ,args_grad=None, grad_req="null", aux_states=aux_params) exector.forward(is_train=False) exector.outputs[0].wait_to_read() output = exector.outputs[0].asnumpy() text = ["5_o_Clock_Shadow","Arched_Eyebrows","Attractive","Bags_Under_Eyes","Bald", "Bangs","Big_Lips","Big_Nose", "Black_Hair","Blond_Hair","Blurry","Brown_Hair","Bushy_Eyebrows","Chubby","Double_Chin","Eyeglasses","Goatee", "Gray_Hair", "Heavy_Makeup","High_Cheekbones","Male","Mouth_Slightly_Open","Mustache","Narrow_Eyes","No_Beard", "Oval_Face","Pale_Skin","Pointy_Nose","Receding_Hairline","Rosy_Cheeks","Sideburns","Smiling","Straight_Hair", "Wavy_Hair","Wearing_Earrings","Wearing_Hat","Wearing_Lipstick","Wearing_Necklace","Wearing_Necktie","Young"] pred = np.ones(40) print("attribution is:") for i in range(40): print text[i].rjust(20)+" : \t", if output[0][i] < 0: pred[i] = -1 print "No" else: pred[i] = 1 print "Yes"
def main(): _, model_args, model_auxs = mx.model.load_checkpoint( args.model_prefix, args.epoch) symbol = lightened_moon_feature() cnt = correct_cnt = 0 with open(args.test_list, 'r') as f: label = np.ones(40) pred = np.ones(40) for line in f.readlines(): # get img and label line_list = line.strip('\n').split('\t') img_name = line_list[-1] logging.info("processing {}, the {}th image".format( img_name, int(cnt))) for label_idx in range(1, 41): label[label_idx - 1] = int(line_list[label_idx]) img = cv2.imread(os.path.join(args.root, img_name), 0) / 255.0 img = cv2.resize(img, (args.size, args.size)) img = np.expand_dims(np.expand_dims(img, axis=0), axis=0) # get pred model_args['data'] = mx.nd.array(img, ctx) exector = symbol.bind(ctx, model_args, args_grad=None, grad_req="null", aux_states=model_auxs) exector.forward(is_train=False) exector.outputs[0].wait_to_read() output = exector.outputs[0].asnumpy() for i in range(40): if output[0][i] < 0: pred[i] = -1 else: pred[i] = 1 correct_cnt += (label == pred).sum() cnt += 1.0 logging.info("acc is:{}".format(correct_cnt / (cnt * 40.0)))
def main(): symbol = lightened_moon_feature(num_classes=40, use_fuse=True) devs = mx.cpu() if args.gpus is None else [ mx.gpu(int(i)) for i in args.gpus.split(',') ] _, arg_params, aux_params = mx.model.load_checkpoint( args.model_load_prefix, args.model_load_epoch) detector = dlib.get_frontal_face_detector() face_cascade = cv2.CascadeClassifier(args.opencv) # read img and drat face rect img = cv2.imread(args.img) faces = detector(img, 1) gray = np.zeros(img.shape[0:2]) if len(faces) == 0: gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) opencv_faces = face_cascade.detectMultiScale(gray, 1.3, 5) for (x, y, w, h) in opencv_faces: faces.append(dlib.rectangle(int(x), int(y), int(x + w), int(y + h))) max_face = faces[0] if len(faces) > 0: max_face = max(faces, key=lambda rect: rect.width() * rect.height()) for f in faces: if f == max_face: cv2.rectangle(img, (f.left(), f.top()), (f.right(), f.bottom()), (0, 0, 255), 2) else: cv2.rectangle(img, (f.left(), f.top()), (f.right(), f.bottom()), (255, 0, 0), 2) cv2.imwrite(args.img.replace('jpg', 'png'), img) # crop face area gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) pad = [0.25, 0.25, 0.25, 0.25] if args.pad is None else args.pad left = int(max(0, max_face.left() - max_face.width() * float(pad[0]))) top = int(max(0, max_face.top() - max_face.height() * float(pad[1]))) right = int( min(gray.shape[1], max_face.right() + max_face.width() * float(pad[2]))) bottom = int( min(gray.shape[0], max_face.bottom() + max_face.height() * float(pad[3]))) gray = gray[left:right, top:bottom] gray = cv2.resize(gray, (args.size, args.size)) / 255.0 img = np.expand_dims(np.expand_dims(gray, axis=0), axis=0) # get pred arg_params['data'] = mx.nd.array(img, devs) exector = symbol.bind(devs, arg_params, args_grad=None, grad_req="null", aux_states=aux_params) exector.forward(is_train=False) exector.outputs[0].wait_to_read() output = exector.outputs[0].asnumpy() text = [ "5_o_Clock_Shadow", "Arched_Eyebrows", "Attractive", "Bags_Under_Eyes", "Bald", "Bangs", "Big_Lips", "Big_Nose", "Black_Hair", "Blond_Hair", "Blurry", "Brown_Hair", "Bushy_Eyebrows", "Chubby", "Double_Chin", "Eyeglasses", "Goatee", "Gray_Hair", "Heavy_Makeup", "High_Cheekbones", "Male", "Mouth_Slightly_Open", "Mustache", "Narrow_Eyes", "No_Beard", "Oval_Face", "Pale_Skin", "Pointy_Nose", "Receding_Hairline", "Rosy_Cheeks", "Sideburns", "Smiling", "Straight_Hair", "Wavy_Hair", "Wearing_Earrings", "Wearing_Hat", "Wearing_Lipstick", "Wearing_Necklace", "Wearing_Necktie", "Young" ] pred = np.ones(40) print("attribution is:") for i in range(40): print text[i].rjust(20) + " : \t", if output[0][i] < 0: pred[i] = -1 print "No" else: pred[i] = 1 print "Yes"
def main(args): #MOON feature extractor; not sure how to make this a modular component symbol = lightened_moon_feature(num_classes=40, use_fuse=True) #the detector passed in from the command line; requires files in facenet/data/ detector = align_dlib.AlignDlib( os.path.expanduser(args.dlib_face_predictor)) landmarkIndices = align_dlib.AlignDlib.OUTER_EYES_AND_NOSE if args.landmarkIndices is not None: landmarkIndices = args.landmarkIndices video = cv2.VideoCapture(args.input_video) devs = mx.cpu() #begin to iterate over the frames and process them ret, frame = video.read() #a list of dictionaries containing face_output for each frame total_output = [] #maps encoding matrix to id number known_faces_dict = dict() known_faces_encoding = [] id_count = 0 while ret is True: face_boxes = detector.getAllFaceBoundingBoxes(frame) id_attr, known_faces_dict, known_faces_encoding, id_count = processFrame( args, frame, known_faces_dict, known_faces_encoding, id_count, symbol, detector, landmarkIndices, devs, face_boxes) total_output.append(id_attr) ret, frame = video.read() #==========CONVERT TO JSON FILE=============== print('done processing; converting to json') #print(total_output) #ith element represents the ith frame frame_num = 0 json_output = '{\r\n"video":\r\n{\r\n"frames":\r\n[\r\n' for frame_info in total_output: #begin the num-faces entry json_output += '{\r\n"num": ' + str(frame_num) + ',\r\n' if len(frame_info.keys()) == 0: #if this still isnt valid, try doing "faces": 0 and closing the field # remove last occurrence of comma k = json_output.rfind(',') json_output = json_output[:k] + json_output[k + 1:] json_output += '},\r\n' #close the num-faces entry; no faces field frame_num += 1 continue json_output += '"faces":\r\n[\r\n' # process the face information in frame_info in a loop for face in frame_info.keys(): #get actual content, which is a list #content shouldnt ever be empty, because there exists a key #TODO may be a bug bc of this assumption content = frame_info[face] pid = content[0] #check if content is length > 1 #there may be an individual with 0 yes-attributes if len(content) == 3: #attributes will contain the topleft,bottomright coordinates, #followed by the attributes themselves attributes = content[1:len(content) - 1] attributes.extend(['Negatives']) d = {pid: attributes} #looks like 0:[] json_output += json.dumps(d) + ',\r\n' continue #attributes will contain the topleft, bottomright coordinates #followed by the attributes themselves attributes = content[1:len(content) - 1] d = {pid: attributes} #now we have the proper split json_output += json.dumps(d) + ',\r\n' #outside of loop # remove last occurrence of comma k = json_output.rfind(',') json_output = json_output[:k] + json_output[k + 1:] json_output += ']\r\n' #close the faces array json_output += '},\r\n' #close the num-faces entry frame_num += 1 # remove last occurrence of comma k = json_output.rfind(',') json_output = json_output[:k] + json_output[k + 1:] json_output += '\r\n]\r\n}\r\n}' d = json.loads(json_output) json_output = json.dumps(d, indent=4, separators=(',', ': ')) #write out to file print('done converting to json; writing to file') f = open('output.json', 'wb') f.write(json_output) f.close() print('done!')