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 get_static_detectors(self): if DetectorTask._detectors is None: DetectorTask._detectors = { 'coco_mobilenet': detector.TFDetector( model_path=model_path, class_index_to_string=coco_class_index_to_string), 'face_mtcnn': detector.FaceDetector() } return DetectorTask._detectors
def load_detector(self,cd): if cd.pk not in DetectorTask._detectors: if cd.detector_type == DeepModel.TFD: DetectorTask._detectors[cd.pk] = detector.TFDetector(model_path=cd.get_model_path(), class_index_to_string=cd.class_index_to_string) elif cd.detector_type == DeepModel.YOLO: DetectorTask._detectors[cd.pk] = detector.YOLODetector(cd.get_yolo_args()) elif cd.name == 'face': DetectorTask._detectors[cd.pk] = detector.FaceDetector() elif cd.name == 'textbox': DetectorTask._detectors[cd.pk] = detector.TextBoxDetector(model_path=cd.get_model_path()) else: raise ValueError,"{}".format(cd.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_static_detectors(self): if DetectorTask._detectors is None: detectors_root = "{}/detectors/".format(settings.MEDIA_ROOT) DetectorTask._detectors = { 'coco': detector.TFDetector( model_path=detectors_root + 'coco/coco_mobilenet.pb', class_index_to_string=coco_class_index_to_string), 'face': detector.FaceDetector(), 'textbox': detector.TextBoxDetector() } return DetectorTask._detectors
def load_detector(cls, cd): cd.ensure() if cd.pk not in Detectors._detectors: if cd.detector_type == TrainedModel.TFD: Detectors._detectors[cd.pk] = detector.TFDetector( model_path=cd.get_model_path(), class_index_to_string=cd.arguments['class_index_to_string'] ) elif cd.detector_type == TrainedModel.YOLO: raise NotImplementedError("YOLO model has been removed") elif cd.name == 'face': Detectors._detectors[cd.pk] = detector.FaceDetector() elif cd.name == 'textbox': Detectors._detectors[cd.pk] = detector.TextBoxDetector( model_path=cd.get_model_path()) else: raise ValueError, "{}".format(cd.pk)
def load_detector(cls, cd): cd.ensure() if cd.pk not in Detectors._detectors: if cd.detector_type == TrainedModel.TFD: Detectors._detectors[cd.pk] = detector.TFDetector( model_path=cd.get_model_path(), class_index_to_string=cd.arguments['class_index_to_string'] ) elif cd.detector_type == TrainedModel.YOLO: # class_names = {k: v for k, v in json.loads(self.class_names)} # args = {'root_dir': model_dir, # 'detector_pk': self.pk, # 'class_names':{i: k for k, i in class_names.items()} # } Detectors._detectors[cd.pk] = detector.YOLODetector( cd.get_yolo_args()) elif cd.name == 'face': Detectors._detectors[cd.pk] = detector.FaceDetector() elif cd.name == 'textbox': Detectors._detectors[cd.pk] = detector.TextBoxDetector( model_path=cd.get_model_path()) else: raise ValueError, "{}".format(cd.pk)
def get_static_detectors(self): if DetectorTask._detectors is None: DetectorTask._detectors = {'coco': detector.TFDetector(model_path=model_path,class_index_to_string=coco_class_index_to_string), 'face': detector.FaceDetector(), 'textbox':detector.TextBoxDetector()} return DetectorTask._detectors