diff --git a/.gitignore b/.gitignore index 894a44c..ed2b11b 100644 --- a/.gitignore +++ b/.gitignore @@ -102,3 +102,9 @@ venv.bak/ # mypy .mypy_cache/ + +# my +data +.idea +media + diff --git a/api/__init__.py b/api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/api/admin.py b/api/admin.py new file mode 100644 index 0000000..34d5293 --- /dev/null +++ b/api/admin.py @@ -0,0 +1,15 @@ +from django.contrib import admin + +from .models import Comic, Video + + +class ComicAdmin(admin.ModelAdmin): + pass + + +class VideoAdmin(admin.ModelAdmin): + pass + + +admin.site.register(Comic, ComicAdmin) +admin.site.register(Video, VideoAdmin) diff --git a/api/apps.py b/api/apps.py new file mode 100644 index 0000000..d87006d --- /dev/null +++ b/api/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class ApiConfig(AppConfig): + name = 'api' diff --git a/api/exceptions.py b/api/exceptions.py new file mode 100644 index 0000000..c99a79f --- /dev/null +++ b/api/exceptions.py @@ -0,0 +1,12 @@ +from django.conf import settings +from rest_framework.exceptions import APIException + + +class FileExtensionError(APIException): + status_code = 400 + default_detail = 'Invalid file extension' + + +class TooLargeFile(APIException): + status_code = 400 + default_detail = f'File cannot be larger than {settings.MAX_FILE_SIZE/1000000} MB' diff --git a/api/migrations/0001_initial.py b/api/migrations/0001_initial.py new file mode 100644 index 0000000..2f318f3 --- /dev/null +++ b/api/migrations/0001_initial.py @@ -0,0 +1,35 @@ +# Generated by Django 2.0.7 on 2018-07-31 18:43 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Comic', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('file', models.FileField(upload_to='comic')), + ], + ), + migrations.CreateModel( + name='Video', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('file', models.FileField(upload_to='raw_videos')), + ('timestamp', models.DateTimeField(auto_now_add=True)), + ], + ), + migrations.AddField( + model_name='comic', + name='video', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='comic', to='api.Video'), + ), + ] diff --git a/api/migrations/__init__.py b/api/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/api/models.py b/api/models.py new file mode 100644 index 0000000..2a538fa --- /dev/null +++ b/api/models.py @@ -0,0 +1,28 @@ +import os +import uuid + +import cv2 +from django.core.files import File +from django.db import models + + +class Video(models.Model): + file = models.FileField(blank=False, null=False, upload_to='raw_videos') + timestamp = models.DateTimeField(auto_now_add=True) + + +class Comic(models.Model): + file = models.FileField(blank=False, null=False, upload_to='comic') + video = models.ForeignKey(Video, on_delete=models.CASCADE, related_name='comic') + + @classmethod + def create_from_nparray(cls, nparray_file, video): + tmp_name = uuid.uuid4() + if not os.path.exists('tmp/'): + os.makedirs('tmp/') + cv2.imwrite(f'tmp/{tmp_name}.png', nparray_file) + with open(f'tmp/{tmp_name}.png', mode='rb') as tmp_file: + comic_image = File(tmp_file, name=f'{tmp_name}.png') + comic = Comic.objects.create(file=comic_image, video=video) + os.remove(f'tmp/{tmp_name}.png') + return comic diff --git a/api/serializers.py b/api/serializers.py new file mode 100644 index 0000000..31f912b --- /dev/null +++ b/api/serializers.py @@ -0,0 +1,19 @@ +from django.conf import settings +from rest_framework import serializers + +from .exceptions import FileExtensionError, TooLargeFile +from .models import Video + + +class VideoSerializer(serializers.ModelSerializer): + class Meta: + model = Video + fields = ('file', 'timestamp') + + def validate(self, attrs): + file = attrs.get('file') + if file.name.split('.')[-1] not in settings.PERMITTED_VIDEO_EXTENSIONS: + raise FileExtensionError + if file.size > settings.MAX_FILE_SIZE: + raise TooLargeFile + return attrs diff --git a/api/tests.py b/api/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/api/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/api/urls.py b/api/urls.py new file mode 100644 index 0000000..c3bb986 --- /dev/null +++ b/api/urls.py @@ -0,0 +1,8 @@ +from django.conf.urls import url + +from .views import Comixify + + +urlpatterns = [ + url(r'^$', Comixify.as_view(), name='annotate'), +] diff --git a/api/views.py b/api/views.py new file mode 100644 index 0000000..e524f7f --- /dev/null +++ b/api/views.py @@ -0,0 +1,35 @@ +from rest_framework.parsers import FormParser, MultiPartParser +from rest_framework.response import Response +from rest_framework.views import APIView + +from comic_layout.comic_layout import LayoutGenerator +from keyframes.keyframes import KeyFramesExtractor +from style_transfer.style_transfer import StyleTransfer +from .models import Video, Comic +from .serializers import VideoSerializer + + +class Comixify(APIView): + parser_classes = (FormParser, MultiPartParser) + + def post(self, request): + """ + Receives video, and returns comic image + """ + + serializer = VideoSerializer(data=request.data) + serializer.is_valid(raise_exception=True) + + video_file = serializer.validated_data['file'] + + keyframes = KeyFramesExtractor.get_keyframes(video=video_file) + stylized_keyframes = StyleTransfer.get_stylized_frames(frames=keyframes) + comic_image = LayoutGenerator.get_layout(frames=stylized_keyframes) + + video = Video.objects.create(file=video_file) + comic = Comic.create_from_nparray(comic_image, video) + response = { + 'status_message': 'ok', + 'comic': comic.file.url, + } + return Response(response) diff --git a/backend.env b/backend.env new file mode 100644 index 0000000..4296764 --- /dev/null +++ b/backend.env @@ -0,0 +1,2 @@ +# python +PYTHONUNBUFFERED=1 diff --git a/comic_layout/__init__.py b/comic_layout/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/comic_layout/admin.py b/comic_layout/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/comic_layout/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/comic_layout/apps.py b/comic_layout/apps.py new file mode 100644 index 0000000..efc38a0 --- /dev/null +++ b/comic_layout/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class ComicLayoutConfig(AppConfig): + name = 'comic_layout' diff --git a/comic_layout/comic_layout.py b/comic_layout/comic_layout.py new file mode 100644 index 0000000..c34e5af --- /dev/null +++ b/comic_layout/comic_layout.py @@ -0,0 +1,7 @@ +from PIL import Image +import cv2 + +class LayoutGenerator(): + @staticmethod + def get_layout(frames): + return cv2.imread('tmp/test.jpg') diff --git a/comic_layout/migrations/__init__.py b/comic_layout/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/comic_layout/tests.py b/comic_layout/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/comic_layout/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/db.env b/db.env new file mode 100644 index 0000000..32facee --- /dev/null +++ b/db.env @@ -0,0 +1,4 @@ +# database +POSTGRES_USER=postgres +POSTGRES_PASSWORD=postgres +POSTGRES_DB=db diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..43245e3 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,50 @@ +version: '3' + +services: + db: + image: postgres:10 + env_file: + - db.env + networks: + - db_network + volumes: + - db_volume:/var/lib/postgresql/data + + web: + build: . + volumes: + - .:/comixify + - static_volume:/comixify/static + - media_volume:/comixify/media + networks: + - nginx_network + - db_network + depends_on: + - db + env_file: + - backend.env + tty: true + + nginx: + image: nginx:1.13 + ports: + - 8080:8080 + volumes: + - ./nginx.conf:/etc/nginx/conf.d/default.conf + - static_volume:/comixify/static + - media_volume:/comixify/media + depends_on: + - web + networks: + - nginx_network + +networks: + nginx_network: + driver: bridge + db_network: + driver: bridge + +volumes: + db_volume: + static_volume: + media_volume: \ No newline at end of file diff --git a/dockerfile b/dockerfile new file mode 100644 index 0000000..7a1d775 --- /dev/null +++ b/dockerfile @@ -0,0 +1,12 @@ +FROM python:3.6 + +RUN mkdir /comixify +WORKDIR /comixify +COPY . /comixify +RUN pip install --upgrade pip && pip install -r requirements.txt + +# Port to expose +EXPOSE 8080 + +ENTRYPOINT ["sh", "entrypoint.sh"] +CMD ['start'] \ No newline at end of file diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..3d5f215 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,17 @@ +#!/bin/sh + +start (){ + gunicorn --bind :8080 settings.wsgi:application +} + +migrate (){ + python3 manage.py migrate --noinput +} + +collectstatic (){ + python3 manage.py collectstatic --clear --no-input +} + +migrate +collectstatic +start diff --git a/keyframes/__init__.py b/keyframes/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/keyframes/admin.py b/keyframes/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/keyframes/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/keyframes/apps.py b/keyframes/apps.py new file mode 100644 index 0000000..54f72e6 --- /dev/null +++ b/keyframes/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class KeyframesConfig(AppConfig): + name = 'keyframes' diff --git a/keyframes/keyframes.py b/keyframes/keyframes.py new file mode 100644 index 0000000..4e8d46a --- /dev/null +++ b/keyframes/keyframes.py @@ -0,0 +1,4 @@ +class KeyFramesExtractor(): + @staticmethod + def get_keyframes(video): + return [] diff --git a/keyframes/migrations/__init__.py b/keyframes/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/keyframes/tests.py b/keyframes/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/keyframes/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/manage.py b/manage.py new file mode 100755 index 0000000..6689145 --- /dev/null +++ b/manage.py @@ -0,0 +1,15 @@ +#!/usr/bin/env python +import os +import sys + +if __name__ == "__main__": + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings.settings") + try: + from django.core.management import execute_from_command_line + except ImportError as exc: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) from exc + execute_from_command_line(sys.argv) diff --git a/nginx.conf b/nginx.conf new file mode 100644 index 0000000..8fcfb57 --- /dev/null +++ b/nginx.conf @@ -0,0 +1,25 @@ +upstream hello_server { + server web:8080; +} + +server { + listen 8080; + server_name localhost; + access_log /var/log/nginx/app.log; + error_log /var/log/nginx/error.log; + client_max_body_size 50M; + location / { + # everything is passed to Gunicorn + proxy_pass http://hello_server; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header Host $host; + proxy_redirect off; + } + location /static/ { + alias /comixify/static/; + } + + location /media/ { + alias /comixify/media/; + } +} \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..6a99bc6 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,9 @@ +Django==2.0.7 +django-rest-framework==0.1.0 +djangorestframework==3.8.2 +gunicorn==19.9.0 +numpy==1.15.0 +opencv-python==3.4.2.17 +Pillow==5.2.0 +psycopg2==2.7.5 +pytz==2018.5 diff --git a/settings/__init__.py b/settings/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/settings/settings.py b/settings/settings.py new file mode 100644 index 0000000..7601f6d --- /dev/null +++ b/settings/settings.py @@ -0,0 +1,137 @@ +""" +Django settings for comixify project. + +Generated by 'django-admin startproject' using Django 2.0.7. + +For more information on this file, see +https://docs.djangoproject.com/en/2.0/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/2.0/ref/settings/ +""" + +import os + +# Build paths inside the project like this: os.path.join(BASE_DIR, ...) +BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'secret_123' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = [] + + +# Application definition + +INSTALLED_APPS = [ + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + 'rest_framework', + 'api', + 'style_transfer', + 'comic_layout', +] + +MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', +] + +ROOT_URLCONF = 'settings.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +WSGI_APPLICATION = 'settings.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/2.0/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.postgresql_psycopg2', + 'NAME': 'postgres', + 'USER': 'postgres', + 'HOST': 'db', + 'PORT': '5432', + 'PASSWORD': 'postgres', + 'CONN_MAX_AGE': 60, + } +} + + +# Password validation +# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/2.0/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_L10N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/2.0/howto/static-files/ + +STATIC_URL = '/static/' +STATIC_ROOT = os.path.join(BASE_DIR, 'static') + +MEDIA_URL = '/media/' +MEDIA_ROOT = os.path.join(BASE_DIR, 'media') + + +PERMITTED_VIDEO_EXTENSIONS = ['mp4', 'avi'] +MAX_FILE_SIZE = 50000000 diff --git a/settings/urls.py b/settings/urls.py new file mode 100644 index 0000000..8f4db0f --- /dev/null +++ b/settings/urls.py @@ -0,0 +1,29 @@ +"""comixify URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/2.0/topics/http/urls/ +Examples: +Function views + 1. Add an import: from my_app import views + 2. Add a URL to urlpatterns: path('', views.home, name='home') +Class-based views + 1. Add an import: from other_app.views import Home + 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') +Including another URLconf + 1. Import the include() function: from django.urls import include, path + 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) +""" +from django.conf import settings +from django.conf.urls import include +from django.conf.urls.static import static +from django.contrib import admin +from django.contrib.staticfiles.urls import staticfiles_urlpatterns +from django.urls import path + +urlpatterns = [ + path('admin/', admin.site.urls), + path(r'comixify/', include('api.urls')), +] + +urlpatterns += staticfiles_urlpatterns() +urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) diff --git a/settings/wsgi.py b/settings/wsgi.py new file mode 100644 index 0000000..cd9ee22 --- /dev/null +++ b/settings/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for comixify project. + +It exposes the WSGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/2.0/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings.settings") + +application = get_wsgi_application() diff --git a/style_transfer/__init__.py b/style_transfer/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/style_transfer/admin.py b/style_transfer/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/style_transfer/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/style_transfer/apps.py b/style_transfer/apps.py new file mode 100644 index 0000000..6203570 --- /dev/null +++ b/style_transfer/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class StyleTransferConfig(AppConfig): + name = 'style_transfer' diff --git a/style_transfer/migrations/__init__.py b/style_transfer/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/style_transfer/style_transfer.py b/style_transfer/style_transfer.py new file mode 100644 index 0000000..0f34a89 --- /dev/null +++ b/style_transfer/style_transfer.py @@ -0,0 +1,4 @@ +class StyleTransfer(): + @staticmethod + def get_stylized_frames(frames): + return frames diff --git a/style_transfer/tests.py b/style_transfer/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/style_transfer/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/tmp/test.jpg b/tmp/test.jpg new file mode 100644 index 0000000..68cc421 Binary files /dev/null and b/tmp/test.jpg differ diff --git a/tmp/test.mp4 b/tmp/test.mp4 new file mode 100644 index 0000000..c5ead95 Binary files /dev/null and b/tmp/test.mp4 differ diff --git a/tmp/test2.mp4 b/tmp/test2.mp4 new file mode 100644 index 0000000..c4e66d0 Binary files /dev/null and b/tmp/test2.mp4 differ