Exemplo n.º 1
0
def inception_index_ssd_detection_by_id(video_id):
    start = TEvent()
    start.video_id = video_id
    start.started = True
    start.operation = inception_index_ssd_detection_by_id.name
    start.save()
    start_time = time.time()
    dv = Video.objects.get(id=video_id)
    video = entity.WVideo(dv, settings.MEDIA_ROOT)
    detections = Detection.objects.all().filter(video=dv,
                                                object_name__startswith='SSD_',
                                                w__gte=50,
                                                h__gte=50)
    logging.info("Indexing {} SSD detections".format(detections.count()))
    visual_index = inception_index_ssd_detection_by_id.visual_indexer[
        'inception']
    index_name, index_results, feat_fname, entries_fname = video.index_detections(
        detections, 'SSD', visual_index)
    i = IndexEntries()
    i.video = dv
    i.count = len(index_results)
    i.contains_detections = True
    i.detection_name = 'SSD'
    i.algorithm = index_name
    i.entries_file_name = entries_fname.split('/')[-1]
    i.features_file_name = feat_fname.split('/')[-1]
    i.save()
    process_video_next(video_id, start.operation)
    start.completed = True
    start.seconds = time.time() - start_time
    start.save()
Exemplo n.º 2
0
def inception_index_regions_by_id(task_id):
    start = TEvent.objects.get(pk=task_id)
    start.task_id = inception_index_regions_by_id.request.id
    start.started = True
    start.operation = inception_index_regions_by_id.name
    video_id = start.video_id
    dv = Video.objects.get(id=video_id)
    arguments = json.loads(start.arguments_json)
    start.save()
    start_time = time.time()
    video = entity.WVideo(dv, settings.MEDIA_ROOT)
    arguments['video_id'] = dv.pk
    detections = Region.objects.all().filter(**arguments)
    logging.info("Indexing {} Regions".format(detections.count()))
    visual_index = inception_index_regions_by_id.visual_indexer['inception']
    detection_name = 'Regions_subset_by_{}'.format(start.pk)
    index_name, index_results, feat_fname, entries_fname = video.index_regions(
        detections, detection_name, visual_index)
    i = IndexEntries()
    i.video = dv
    i.count = len(index_results)
    i.contains_detections = True
    i.detection_name = detection_name
    i.algorithm = index_name
    i.entries_file_name = entries_fname.split('/')[-1]
    i.features_file_name = feat_fname.split('/')[-1]
    i.source = start
    i.save()
    start.completed = True
    start.seconds = time.time() - start_time
    start.save()
    process_next(task_id)
Exemplo n.º 3
0
def alexnet_index_by_id(task_id):
    start = TEvent.objects.get(pk=task_id)
    start.task_id = alexnet_index_by_id.request.id
    start.started = True
    start.operation = alexnet_index_by_id.name
    start.save()
    start_time = time.time()
    video_id = start.video_id
    dv = Video.objects.get(id=video_id)
    video = entity.WVideo(dv, settings.MEDIA_ROOT)
    frames = Frame.objects.all().filter(video=dv)
    visual_index = alexnet_index_by_id.visual_indexer['alexnet']
    index_name, index_results, feat_fname, entries_fname = video.index_frames(
        frames, visual_index)
    i = IndexEntries()
    i.video = dv
    i.count = len(index_results)
    i.contains_frames = True
    i.detection_name = 'Frame'
    i.algorithm = index_name
    i.entries_file_name = entries_fname.split('/')[-1]
    i.features_file_name = feat_fname.split('/')[-1]
    i.source = start
    i.save()
    process_next(task_id)
    start.completed = True
    start.seconds = time.time() - start_time
    start.save()
Exemplo n.º 4
0
def pyscenedetect(video_id, rescaled_width=0):
    """
    Pyscenedetect often causes unexplainable double free errors on some machine when executing cap.release()
    This ensures that the task recovers partial frame data i.e. every nth frame even if the command running inside a subprocess fails
    :param video_id:
    :param rescaled_width:
    :return:
    """
    import django
    from PIL import Image
    sys.path.append(os.path.dirname(__file__))
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "dva.settings")
    django.setup()
    from dvaapp.models import Video
    from django.conf import settings
    from dvalib import pyscenecustom, entity
    dv = Video.objects.get(id=video_id)
    v = entity.WVideo(dvideo=dv, media_dir=settings.MEDIA_ROOT)
    rescaled_width = int(rescaled_width)
    if rescaled_width == 0:
        rescale = False
    else:
        rescale = True
    manager = pyscenecustom.manager.SceneManager(
        save_image_prefix="{}/{}/frames/".format(settings.MEDIA_ROOT,
                                                 video_id),
        rescaled_width=int(rescaled_width),
        rescale=rescale)
    pyscenecustom.detect_scenes_file(v.local_path, manager)
Exemplo n.º 5
0
def extract_frames(video_id):
    start = TEvent()
    start.video_id = video_id
    start.started = True
    start.save()
    dv = Video.objects.get(id=video_id)
    v = entity.WVideo(dvideo=dv, media_dir=settings.MEDIA_ROOT)
    time.sleep(3)  # otherwise ffprobe randomly fails
    if not dv.dataset:
        v.get_metadata()
        dv.metadata = v.metadata
        dv.length_in_seconds = v.duration
        dv.height = v.height
        dv.width = v.width
        dv.save()
    frames = v.extract_frames()
    dv.frames = len(frames)
    dv.save()
    for f in frames:
        df = Frame()
        df.time_seconds = f.time_seconds
        df.video = dv
        df.save()
    # perform_detection_video.apply_async(args=[dv.id, ], queue=settings.Q_DETECTOR)
    finished = TEvent()
    finished.completed = True
    finished.video_id = video_id
    finished.save()
    perform_indexing.apply_async(args=[video_id], queue=settings.Q_INDEXER)
    return 0
Exemplo n.º 6
0
def assign_tags(video_id):
    import django
    from PIL import Image
    sys.path.append(os.path.dirname(__file__))
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "dva.settings")
    django.setup()
    from django.conf import settings
    from dvaapp.models import Video, Frame, Region
    from dvalib import entity, annotator
    dv = Video.objects.get(id=video_id)
    frames = Frame.objects.all().filter(video=dv)
    v = entity.WVideo(dvideo=dv, media_dir=settings.MEDIA_ROOT)
    wframes = {
        df.pk: entity.WFrame(video=v,
                             frame_index=df.frame_index,
                             primary_key=df.pk)
        for df in frames
    }
    algorithm = annotator.OpenImagesAnnotator()
    logging.info("starting annotation {}".format(algorithm.name))
    for k, f in wframes.items():
        tags = algorithm.apply(f.local_path())
        a = Region()
        a.region_type = Region.ANNOTATION
        a.frame_id = k
        a.video_id = video_id
        a.object_name = "OpenImagesTag"
        a.metadata_text = " ".join([t for t, v in tags.iteritems() if v > 0.1])
        a.metadata_json = json.dumps(
            {t: 100.0 * v
             for t, v in tags.iteritems() if v > 0.1})
        a.full_frame = True
        a.save()
        print a.metadata_text
Exemplo n.º 7
0
def perform_detection(video_id):
    start = TEvent()
    start.video_id = video_id
    start.started = True
    start.operation = "detection"
    start.save()
    start_time = time.time()
    dv = Video.objects.get(id=video_id)
    frames = Frame.objects.all().filter(video=dv)
    v = entity.WVideo(dvideo=dv, media_dir=settings.MEDIA_ROOT)
    wframes = [entity.WFrame(video=v, frame_index=df.frame_index, primary_key=df.pk) for df in frames]
    darknet_path = os.path.join(settings.BASE_DIR,'darknet/')
    list_path = "{}/{}_list.txt".format(darknet_path,os.getpid())
    output_path = "{}/{}_output.txt".format(darknet_path,os.getpid())
    logging.info(darknet_path)
    path_to_pk = {}
    with open(list_path,'w') as framelist:
        for frame in wframes:
            framelist.write('{}\n'.format(frame.local_path()))
            path_to_pk[frame.local_path()] = frame.primary_key
    #./darknet detector test cfg/combine9k.data cfg/yolo9000.cfg yolo9000.weights data/list.txt
    with open(output_path,'w') as output:
        args = ["./darknet", 'detector', 'test', 'cfg/combine9k.data', 'cfg/yolo9000.cfg', 'yolo9000.weights', list_path]
        logging.info(args)
        returncode = subprocess.call(args,cwd=darknet_path,stdout=output)
    if returncode == 0:
        detections = 0
        for line in file(output_path):
            if line.strip():
                detections += 1
                frame_path,name,confidence,left,right,top,bot = line.strip().split('\t')
                if frame_path not in path_to_pk:
                    raise ValueError,frame_path
                top = int(top)
                left = int(left)
                right = int(right)
                bot = int(bot)
                confidence = float(confidence)
                dd = Detection()
                dd.video = dv
                dd.frame_id = path_to_pk[frame_path]
                dd.object_name = "darknet_yolo9k_{}".format(name.replace(' ','_'))
                dd.confidence = confidence
                dd.x = left
                dd.y = top
                dd.w = right - left
                dd.h = bot - top
                dd.save()
                img = Image.open(frame_path)
                img2 = img.crop((left, top, right,bot))
                img2.save("{}/{}/detections/{}.jpg".format(settings.MEDIA_ROOT,video_id,dd.pk))
        dv.detections = detections
        dv.save()
    start.completed = True
    start.seconds = time.time() - start_time
    start.save()
    return returncode
Exemplo n.º 8
0
def perform_indexing(video_id):
    dv = Video.objects.get(id=video_id)
    video = entity.WVideo(dv, settings.MEDIA_ROOT)
    frames = Frame.objects.all().filter(video=dv)
    for index_results in video.index_frames(frames):
        i = IndexEntries()
        i.video = dv
        i.count = index_results['count']
        i.algorithm = index_results['index_name']
        i.save()
Exemplo n.º 9
0
def detect(video_id):
    """
    This is a HACK since Tensorflow is absolutely atrocious in allocating and freeing up memory.
    Once a process / session is allocated a memory it cannot be forced to clear it up.
    As a result this code gets called via a subprocess which clears memory when it exits.

    :param video_id:
    :return:
    """
    import django
    from PIL import Image
    sys.path.append(os.path.dirname(__file__))
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "dva.settings")
    django.setup()
    from django.conf import settings
    from dvaapp.models import Video, Detection, Frame
    from dvalib import entity, detector
    dv = Video.objects.get(id=video_id)
    frames = Frame.objects.all().filter(video=dv)
    v = entity.WVideo(dvideo=dv, media_dir=settings.MEDIA_ROOT)
    wframes = {
        df.pk: entity.WFrame(video=v,
                             frame_index=df.frame_index,
                             primary_key=df.pk)
        for df in frames
    }
    detection_count = 0
    detector_list = {
        'ssd': detector.SSDetector(),
    }
    if 'YOLO_ENABLE' in os.environ:
        detector_list['yolo'] = detector.YOLODetector()
    for alogrithm in detector_list.itervalues():
        logging.info("starting detection {}".format(alogrithm.name))
        frame_detections = alogrithm.detect(wframes.values())
        for frame_pk, detections in frame_detections.iteritems():
            for d in detections:
                dd = Detection()
                dd.video = dv
                dd.frame_id = frame_pk
                dd.object_name = d['name']
                dd.confidence = d['confidence']
                dd.x = d['left']
                dd.y = d['top']
                dd.w = d['right'] - d['left']
                dd.h = d['bot'] - d['top']
                dd.save()
                img = Image.open(wframes[frame_pk].local_path())
                img2 = img.crop((d['left'], d['top'], d['right'], d['bot']))
                img2.save("{}/{}/detections/{}.jpg".format(
                    settings.MEDIA_ROOT, video_id, dd.pk))
                detection_count += 1
    dv.refresh_from_db()
    dv.detections = dv.detections + detection_count
    dv.save()
Exemplo n.º 10
0
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 = 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()
Exemplo n.º 11
0
def detect(video_id):
    """
    This is a HACK since Tensorflow is absolutely atrocious in allocating and freeing up memory
    :param video_id:
    :return:
    """
    import django
    from PIL import Image
    sys.path.append(os.path.dirname(__file__))
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "dva.settings")
    django.setup()
    from django.conf import settings
    from dvaapp.models import Video, Detection, Frame
    from dvalib import entity, detector
    dv = Video.objects.get(id=video_id)
    frames = Frame.objects.all().filter(video=dv)
    v = entity.WVideo(dvideo=dv, media_dir=settings.MEDIA_ROOT)
    wframes = {
        df.pk: entity.WFrame(video=v,
                             frame_index=df.frame_index,
                             primary_key=df.pk)
        for df in frames
    }
    detection_count = 0
    for alogrithm in detector.DETECTORS.itervalues():
        logging.info("starting detection {}".format(alogrithm.name))
        frame_detections = alogrithm.detect(wframes.values())
        for frame_pk, detections in frame_detections.iteritems():
            for d in detections:
                dd = Detection()
                dd.video = dv
                dd.frame_id = frame_pk
                dd.object_name = d['name']
                dd.confidence = d['confidence']
                dd.x = d['left']
                dd.y = d['top']
                dd.w = d['right'] - d['left']
                dd.h = d['bot'] - d['top']
                dd.save()
                img = Image.open(wframes[frame_pk].local_path())
                img2 = img.crop((d['left'], d['top'], d['right'], d['bot']))
                img2.save("{}/{}/detections/{}.jpg".format(
                    settings.MEDIA_ROOT, video_id, dd.pk))
                detection_count += 1
    dv.detections = detection_count
    dv.save()
Exemplo n.º 12
0
def perform_face_indexing(video_id):
    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)
    aligned_paths = facerecognition.align(input_paths.keys(), faces_dir)
    logging.info(len(aligned_paths))
    faces = []
    faces_to_pk = {}
    count = 0
    for path, v in aligned_paths.iteritems():
        for face_path, 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()
            os.rename(face_path, '{}/{}.jpg'.format(faces_dir, d.pk))
            faces.append('{}/{}.jpg'.format(faces_dir, d.pk))
            faces_to_pk['{}/{}.jpg'.format(faces_dir, d.pk)] = d.pk
            count += 1
    dv.detections = dv.detections + count
    dv.save()
    path_count, emb_array, entries = facerecognition.represent(
        faces, faces_to_pk, indexes_dir)
    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()
Exemplo n.º 13
0
def extract_frames(video_id, rescale=True):
    start = TEvent()
    start.video_id = video_id
    start.started = True
    start.operation = extract_frames.name
    start.save()
    start_time = time.time()
    dv = Video.objects.get(id=video_id)
    v = entity.WVideo(dvideo=dv, media_dir=settings.MEDIA_ROOT)
    time.sleep(3)  # otherwise ffprobe randomly fails
    if not dv.dataset:
        v.get_metadata()
        dv.metadata = v.metadata
        dv.length_in_seconds = v.duration
        dv.height = v.height
        dv.width = v.width
        dv.save()
    if 'RESCALE_DISABLE' in os.environ:
        rescale = False
    frames = v.extract_frames(rescale)
    dv.frames = len(frames)
    dv.save()
    for f in frames:
        df = Frame()
        df.frame_index = f.frame_index
        df.video = dv
        if f.name:
            df.name = f.name[:150]
            df.subdir = f.subdir.replace('/', ' ')
        df.save()
        if f.name:
            for l in f.subdir.split('/'):
                if l != dv.name and l.strip():
                    fl = Annotation()
                    fl.frame = df
                    fl.label = l
                    fl.video = dv
                    fl.full_frame = True
                    fl.source = "directory_name"
                    fl.save()
    process_video_next(video_id, start.operation)
    start.completed = True
    start.seconds = time.time() - start_time
    start.save()
    return 0
Exemplo n.º 14
0
def extract_frames(video_id):
    start = TEvent()
    start.video_id = video_id
    start.started = True
    start.operation = "extract_frames"
    start.save()
    start_time = time.time()
    dv = Video.objects.get(id=video_id)
    v = entity.WVideo(dvideo=dv, media_dir=settings.MEDIA_ROOT)
    time.sleep(3) # otherwise ffprobe randomly fails
    if not dv.dataset:
        v.get_metadata()
        dv.metadata = v.metadata
        dv.length_in_seconds = v.duration
        dv.height = v.height
        dv.width = v.width
        dv.save()
    frames = v.extract_frames()
    dv.frames = len(frames)
    dv.save()
    for f in frames:
        df = Frame()
        df.frame_index = f.frame_index
        df.video = dv
        if f.name:
            df.name = f.name[:150]
            df.subdir = f.subdir.replace('/',' ')
        df.save()
        if f.name:
            for l in f.subdir.split('/'):
                if l != dv.name and l.strip():
                    fl = FrameLabel()
                    fl.frame = df
                    fl.label = l
                    fl.video = dv
                    fl.source = "directory_name"
                    fl.save()
    perform_indexing.apply_async(args=[video_id],queue=settings.Q_INDEXER)
    perform_detection.apply_async(args=[video_id], queue=settings.Q_DETECTOR)
    start.completed = True
    start.seconds = time.time() - start_time
    start.save()
    return 0
Exemplo n.º 15
0
def perform_indexing(video_id):
    start = TEvent()
    start.video_id = video_id
    start.started = True
    start.operation = "indexing"
    start.save()
    start_time = time.time()
    dv = Video.objects.get(id=video_id)
    video = entity.WVideo(dv, settings.MEDIA_ROOT)
    frames = Frame.objects.all().filter(video=dv)
    for index_results in video.index_frames(frames):
        i = IndexEntries()
        i.video = dv
        i.count = index_results['count']
        i.algorithm = index_results['index_name']
        i.save()
    start.completed = True
    start.seconds = time.time() - start_time
    start.save()
Exemplo n.º 16
0
def extract_frames(video_id, rescale=True):
    start = TEvent()
    start.video_id = video_id
    start.started = True
    start.operation = extract_frames.name
    start.save()
    start_time = time.time()
    dv = Video.objects.get(id=video_id)
    v = entity.WVideo(dvideo=dv, media_dir=settings.MEDIA_ROOT)
    time.sleep(3)  # otherwise ffprobe randomly fails
    if not dv.dataset:
        v.get_metadata()
        dv.metadata = v.metadata
        dv.length_in_seconds = v.duration
        dv.height = v.height
        dv.width = v.width
        dv.save()
    if 'RESCALE_DISABLE' in os.environ:
        rescale = False
    frames = v.extract_frames(rescale)
    dv.frames = len(frames)
    dv.save()
    for f in frames:
        df = Frame()
        df.frame_index = f.frame_index
        df.video = dv
        if f.name:
            df.name = f.name[:150]
            df.subdir = f.subdir.replace('/', ' ')
        df.save()
        f.primary_key = df.pk
    set_directory_labels(frames, dv)
    process_video_next(video_id, start.operation)
    start.completed = True
    start.seconds = time.time() - start_time
    start.save()
    if dv.dataset:
        os.remove("{}/{}/video/{}.zip".format(settings.MEDIA_ROOT, dv.pk,
                                              dv.pk))
    return 0
Exemplo n.º 17
0
def perform_indexing(video_id):
    start = TEvent()
    start.video_id = video_id
    start.started = True
    start.operation = perform_indexing.name
    start.save()
    start_time = time.time()
    dv = Video.objects.get(id=video_id)
    video = entity.WVideo(dv, settings.MEDIA_ROOT)
    frames = Frame.objects.all().filter(video=dv)
    for index_name,index_results in video.index_frames(frames,perform_indexing.frame_indexer).iteritems():
        i = IndexEntries()
        i.video = dv
        i.count = len(index_results)
        i.contains_frames = True
        i.detection_name = 'Frame'
        i.algorithm = index_name
        i.save()
    process_video_next(video_id, start.operation)
    start.completed = True
    start.seconds = time.time() - start_time
    start.save()
Exemplo n.º 18
0
def alexnet_index_by_id(video_id):
    start = TEvent()
    start.video_id = video_id
    start.started = True
    start.operation = alexnet_index_by_id.name
    start.save()
    start_time = time.time()
    dv = Video.objects.get(id=video_id)
    video = entity.WVideo(dv, settings.MEDIA_ROOT)
    frames = Frame.objects.all().filter(video=dv)
    visual_index = alexnet_index_by_id.visual_indexer['alexnet']
    index_name, index_results = video.index_frames(frames,visual_index)
    i = IndexEntries()
    i.video = dv
    i.count = len(index_results)
    i.contains_frames = True
    i.detection_name = 'Frame'
    i.algorithm = index_name
    i.save()
    process_video_next(video_id, start.operation)
    start.completed = True
    start.seconds = time.time() - start_time
    start.save()
Exemplo n.º 19
0
def extract_frames(task_id):
    start = TEvent.objects.get(pk=task_id)
    if celery_40_bug_hack(start):
        return 0
    start.task_id = extract_frames.request.id
    start.started = True
    start.operation = extract_frames.name
    args = json.loads(start.arguments_json)
    if args == {}:
        args['perform_scene_detection'] = False
        args['rescale'] = 0
        args['rate'] = 30
        start.arguments_json = json.dumps(args)
    start.save()
    start_time = time.time()
    video_id = start.video_id
    dv = Video.objects.get(id=video_id)
    if dv.youtube_video:
        create_video_folders(dv)
    v = entity.WVideo(dvideo=dv, media_dir=settings.MEDIA_ROOT)
    time.sleep(3)  # otherwise ffprobe randomly fails
    if not dv.dataset:
        v.get_metadata()
        dv.metadata = v.metadata
        dv.length_in_seconds = v.duration
        dv.height = v.height
        dv.width = v.width
        dv.save()
    frames, cuts, segments, key_frames = v.extract_frames(args)
    dv.frames = len(frames)
    index_to_df = {}
    dv.save()
    df_list = []
    for f in frames:
        df = Frame()
        df.frame_index = f.frame_index
        if f.frame_index in key_frames:
            t = float(key_frames[f.frame_index]['t'])
            df.t = t
            df.keyframe = True
        df.video_id = dv.pk
        if f.h:
            df.h = f.h
        if f.w:
            df.w = f.w
        if f.name:
            df.name = f.name[:150]
            df.subdir = f.subdir.replace('/', ' ')
        df_list.append(df)
    df_ids = Frame.objects.bulk_create(df_list)
    for i, k in enumerate(df_ids):
        index_to_df[df_list[i].frame_index] = k.id
    for start_frame_index, end_frame_index in [
        (cuts[cutindex], cuts[cutindex + 1])
            for cutindex, cut in enumerate(sorted(cuts)[:-1])
    ]:
        ds = Scene()
        ds.video = dv
        ds.start_frame_index = start_frame_index
        ds.end_frame_index = end_frame_index
        ds.start_frame_id = index_to_df[start_frame_index]
        ds.end_frame_id = index_to_df[end_frame_index]
        ds.source = start
        ds.save()
    total_frames = 0
    for s in segments:
        segment_id, start_time, end_time, metadata = s
        ds = Segment()
        ds.segment_index = segment_id
        ds.start_time = start_time
        ds.end_time = end_time
        ds.video_id = dv.pk
        ds.metadata = metadata
        metadata_json = json.loads(metadata)
        ds.frame_count = int(metadata_json["streams"][0]["nb_frames"])
        ds.start_frame_index = total_frames
        if ds.start_frame_index in index_to_df:
            ds.start_frame_id = index_to_df[ds.start_frame_index]
        else:
            logging.info("Could not find find keyframe for index {}".format(
                ds.start_frame_index))
        total_frames += ds.frame_count
        ds.end_frame_index = total_frames - 1
        ds.save()
    set_directory_labels(frames, dv)
    process_next(task_id)
    start.completed = True
    start.seconds = time.time() - start_time
    start.save()
    if dv.dataset:
        os.remove("{}/{}/video/{}.zip".format(settings.MEDIA_ROOT, dv.pk,
                                              dv.pk))
    return 0
Exemplo n.º 20
0
def extract_frames(task_id):
    start = TEvent.objects.get(pk=task_id)
    start.task_id = extract_frames.request.id
    start.started = True
    start.operation = extract_frames.name
    args = json.loads(start.arguments_json)
    if args == {}:
        args['perform_scene_detection'] = True
        args['rescale'] = 0
        args['rate'] = 30
        start.arguments_json = json.dumps(args)
    start.save()
    start_time = time.time()
    video_id = start.video_id
    dv = Video.objects.get(id=video_id)
    v = entity.WVideo(dvideo=dv, media_dir=settings.MEDIA_ROOT)
    time.sleep(3)  # otherwise ffprobe randomly fails
    if not dv.dataset:
        v.get_metadata()
        dv.metadata = v.metadata
        dv.length_in_seconds = v.duration
        dv.height = v.height
        dv.width = v.width
        dv.save()
    frames, cuts = v.extract_frames(args)
    dv.frames = len(frames)
    index_to_df = {}
    dv.save()
    for f in frames:
        df = Frame()
        df.frame_index = f.frame_index
        df.video = dv
        if f.h:
            df.h = f.h
        if f.w:
            df.w = f.w
        if f.name:
            df.name = f.name[:150]
            df.subdir = f.subdir.replace('/', ' ')
        df.save()
        index_to_df[df.frame_index] = df
        f.primary_key = df.pk
    for start_frame_index, end_frame_index in [
        (cuts[cutindex], cuts[cutindex + 1])
            for cutindex, cut in enumerate(sorted(cuts)[:-1])
    ]:
        ds = Scene()
        ds.video = dv
        ds.start_frame_index = start_frame_index
        ds.end_frame_index = end_frame_index
        ds.start_frame = index_to_df[start_frame_index]
        ds.end_frame = index_to_df[end_frame_index]
        ds.source = start
        ds.save()
    set_directory_labels(frames, dv)
    process_next(task_id)
    start.completed = True
    start.seconds = time.time() - start_time
    start.save()
    if dv.dataset:
        os.remove("{}/{}/video/{}.zip".format(settings.MEDIA_ROOT, dv.pk,
                                              dv.pk))
    return 0