Created
July 19, 2018 20:16
-
-
Save msis/61029278b2da51649a5f7c7780935aae to your computer and use it in GitHub Desktop.
import video sliced directories to beaverdam
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.core.management.base import BaseCommand, CommandError | |
from annotator.models import Video | |
import os | |
import json | |
import re | |
import os | |
import shutil | |
# referring to https://stackoverflow.com/questions/5967500/how-to-correctly-sort-a-string-with-a-number-inside for "human sorting" | |
def atoi(text): | |
return int(text) if text.isdigit() else text | |
def natural_keys(text): | |
return [ atoi(c) for c in re.split('(\d+)', text) ] | |
class Command(BaseCommand): | |
help = "Imports a directory of images as a new view object" | |
def add_arguments(self, parser): | |
parser.add_argument('directory') | |
parser.add_argument('host_url') | |
parser.add_argument('--recursive', action="store_true") | |
def handle(self, *args, **options): | |
if (options['directory'] and options['host_url']): | |
directory = options['directory'] | |
if not options['recursive']: | |
self.create_entry_from_dir(options['directory'], | |
options['host_url']) | |
else: | |
for subdir in os.listdir(options['directory']): | |
host_url = ''.join([options['host_url'], subdir]) | |
self.create_entry_from_dir(os.path.join(directory, subdir), | |
host_url) | |
self.stdout.write("You got it!") | |
def create_entry_from_dir(self, directory, host_url): | |
f_list = [] | |
for f in os.listdir(directory): | |
f_path = os.path.join(directory, f) | |
if (os.path.isfile(f_path)): | |
f_list.append(f) | |
f_list.sort(key = natural_keys) | |
image_list_json = json.dumps(f_list) | |
source = os.path.basename(directory) | |
v = Video(source = source, image_list = image_list_json, host = host_url) | |
v.save() | |
#self.stdout.write(f"Video {v.id} was added.") | |
self.stdout.write("Video {} was added.".format(v.id)) | |
self.stdout.write("Video.source = {}".format(v.source)) | |
self.stdout.write("Video.host = {}".format(v.host)) | |
self.stdout.write("Video.image_list = {}".format(v.image_list)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment