def visual_indexer(self): if IndexerTask._visual_indexer is None: IndexerTask._visual_indexer = { 'inception': indexer.InceptionIndexer(), 'facenet': indexer.FacenetIndexer(), 'alexnet': indexer.AlexnetIndexer() } return IndexerTask._visual_indexer
def perform_face_indexing(video_id): from dvaapp.models import Region,Frame,Video,IndexEntries from dvalib import indexer,detector from dvaapp.operations.video_processing import WFrame,WVideo from django.conf import settings from scipy import misc face_indexer = indexer.FacenetIndexer() dv = Video.objects.get(id=video_id) video = WVideo(dv, settings.MEDIA_ROOT) frames = Frame.objects.all().filter(video=dv) wframes = [WFrame(video=video, frame_index=df.frame_index, primary_key=df.pk) for df in frames] input_paths = {f.local_path(): f.primary_key for f in wframes} faces_dir = '{}/{}/detections'.format(settings.MEDIA_ROOT, video_id) indexes_dir = '{}/{}/indexes'.format(settings.MEDIA_ROOT, video_id) face_detector = detector.FaceDetector() aligned_paths = face_detector.detect(wframes) logging.info(len(aligned_paths)) faces = [] faces_to_pk = {} count = 0 for path, v in aligned_paths.iteritems(): for scaled_img, bb in v: d = Region() d.region_type = Region.DETECTION d.video = dv d.confidence = 100.0 d.frame_id = input_paths[path] d.object_name = "mtcnn_face" left, top, right, bottom = bb[0], bb[1], bb[2], bb[3] d.y = top d.x = left d.w = right - left d.h = bottom - top d.save() face_path = '{}/{}.jpg'.format(faces_dir, d.pk) output_filename = os.path.join(faces_dir, face_path) misc.imsave(output_filename, scaled_img) faces.append(face_path) faces_to_pk[face_path] = d.pk count += 1 dv.refresh_from_db() dv.detections = dv.detections + count dv.save() path_count, emb_array, entries, feat_fname, entries_fname = face_indexer.index_faces(faces, faces_to_pk, indexes_dir, video_id) i = IndexEntries() i.video = dv i.count = len(entries) i.contains_frames = False i.contains_detections = True i.detection_name = "Face" i.algorithm = 'facenet' i.entries_file_name = entries_fname.split('/')[-1] i.features_file_name = feat_fname.split('/')[-1] i.save()
def visual_indexer(self): if IndexerTask._visual_indexer is None: # if IndexerTask._session is None: # logging.info("Creating a global shared session") # config = indexer.tf.ConfigProto() # config.gpu_options.per_process_gpu_memory_fraction = 0.2 # IndexerTask._session = indexer.tf.Session() IndexerTask._visual_indexer = { 'inception': indexer.InceptionIndexer(), 'facenet': indexer.FacenetIndexer(), 'vgg': indexer.VGGIndexer() } return IndexerTask._visual_indexer
def get_index(cls,di): di.ensure() if di.pk not in Indexers._visual_indexer: iroot = "{}/models/".format(settings.MEDIA_ROOT) if di.name == 'inception': Indexers._visual_indexer[di.pk] = indexer.InceptionIndexer(iroot + "{}/network.pb".format(di.uuid)) elif di.name == 'facenet': Indexers._visual_indexer[di.pk] = indexer.FacenetIndexer(iroot + "{}/facenet.pb".format(di.uuid)) elif di.algorithm == 'vgg': Indexers._visual_indexer[di.pk] = indexer.VGGIndexer(iroot + "{}/{}".format(di.uuid,di.files[0]['filename'])) else: raise ValueError,"unregistered indexer with id {}".format(di.pk) return Indexers._visual_indexer[di.pk]
def perform_face_indexing(video_id): face_indexer = indexer.FacenetIndexer() dv = Video.objects.get(id=video_id) video = entity.WVideo(dv, settings.MEDIA_ROOT) frames = Frame.objects.all().filter(video=dv) wframes = [ entity.WFrame(video=video, frame_index=df.frame_index, primary_key=df.pk) for df in frames ] input_paths = {f.local_path(): f.primary_key for f in wframes} faces_dir = '{}/{}/detections'.format(settings.MEDIA_ROOT, video_id) indexes_dir = '{}/{}/indexes'.format(settings.MEDIA_ROOT, video_id) face_detector = detector.FaceDetector() aligned_paths = face_detector.detect(wframes) logging.info(len(aligned_paths)) faces = [] faces_to_pk = {} count = 0 for path, v in aligned_paths.iteritems(): for scaled_img, bb in v: d = Detection() d.video = dv d.confidence = 100.0 d.frame_id = input_paths[path] d.object_name = "mtcnn_face" top, left, bottom, right = bb[0], bb[1], bb[2], bb[3] d.y = top d.x = left d.w = right - left d.h = bottom - top d.save() face_path = '{}/{}.jpg'.format(faces_dir, d.pk) output_filename = os.path.join(faces_dir, face_path) misc.imsave(output_filename, scaled_img) faces.append(face_path) faces_to_pk[face_path] = d.pk count += 1 dv.detections = dv.detections + count dv.save() path_count, emb_array, entries = face_indexer.index_faces( faces, faces_to_pk, indexes_dir, video_id) i = IndexEntries() i.video = dv i.count = len(entries) i.contains_frames = False i.contains_detections = True i.detection_name = "Face" i.algorithm = 'facenet' i.save()
def get_index(self, di): if di.pk not in IndexerTask._visual_indexer: iroot = "{}/models/".format(settings.MEDIA_ROOT) if di.name == 'inception': IndexerTask._visual_indexer[di.pk] = indexer.InceptionIndexer( iroot + "{}/network.pb".format(di.pk)) elif di.name == 'facenet': IndexerTask._visual_indexer[di.pk] = indexer.FacenetIndexer( iroot + "{}/facenet.pb".format(di.pk)) elif di.name == 'vgg': IndexerTask._visual_indexer[di.pk] = indexer.VGGIndexer( iroot + "{}/vgg.pb".format(di.pk)) else: raise ValueError, "unregistered indexer with id {}".format( di.pk) return IndexerTask._visual_indexer[di.pk]
def visual_indexer(self): if IndexerTask._visual_indexer is None: indexer_root_dir = "{}/indexers/".format(settings.MEDIA_ROOT) # if IndexerTask._session is None: # logging.info("Creating a global shared session") # config = indexer.tf.ConfigProto() # config.gpu_options.per_process_gpu_memory_fraction = 0.2 # IndexerTask._session = indexer.tf.Session() IndexerTask._visual_indexer = { 'inception': indexer.InceptionIndexer(indexer_root_dir + "inception/network.pb"), 'facenet': indexer.FacenetIndexer(indexer_root_dir + "facenet/facenet.pb"), 'vgg': indexer.VGGIndexer(indexer_root_dir + "vgg/vgg.pb") } return IndexerTask._visual_indexer
def detection_indexer(self): if self._detection_indexer is None: self._detection_indexer = { 'facenet': indexer.FacenetIndexer(), } return self._detection_indexer