Add core structure for application

This commit is contained in:
Maciej Pęśko 2018-07-31 21:51:39 +02:00
parent 5605535115
commit f191675939
45 changed files with 530 additions and 0 deletions

6
.gitignore vendored
View file

@ -102,3 +102,9 @@ venv.bak/
# mypy
.mypy_cache/
# my
data
.idea
media

0
api/__init__.py Normal file
View file

15
api/admin.py Normal file
View file

@ -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)

5
api/apps.py Normal file
View file

@ -0,0 +1,5 @@
from django.apps import AppConfig
class ApiConfig(AppConfig):
name = 'api'

12
api/exceptions.py Normal file
View file

@ -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'

View file

@ -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'),
),
]

View file

28
api/models.py Normal file
View file

@ -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

19
api/serializers.py Normal file
View file

@ -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

3
api/tests.py Normal file
View file

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

8
api/urls.py Normal file
View file

@ -0,0 +1,8 @@
from django.conf.urls import url
from .views import Comixify
urlpatterns = [
url(r'^$', Comixify.as_view(), name='annotate'),
]

35
api/views.py Normal file
View file

@ -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)

2
backend.env Normal file
View file

@ -0,0 +1,2 @@
# python
PYTHONUNBUFFERED=1

0
comic_layout/__init__.py Normal file
View file

3
comic_layout/admin.py Normal file
View file

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

5
comic_layout/apps.py Normal file
View file

@ -0,0 +1,5 @@
from django.apps import AppConfig
class ComicLayoutConfig(AppConfig):
name = 'comic_layout'

View file

@ -0,0 +1,7 @@
from PIL import Image
import cv2
class LayoutGenerator():
@staticmethod
def get_layout(frames):
return cv2.imread('tmp/test.jpg')

View file

3
comic_layout/tests.py Normal file
View file

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

4
db.env Normal file
View file

@ -0,0 +1,4 @@
# database
POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres
POSTGRES_DB=db

50
docker-compose.yml Normal file
View file

@ -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:

12
dockerfile Normal file
View file

@ -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']

17
entrypoint.sh Normal file
View file

@ -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

0
keyframes/__init__.py Normal file
View file

3
keyframes/admin.py Normal file
View file

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

5
keyframes/apps.py Normal file
View file

@ -0,0 +1,5 @@
from django.apps import AppConfig
class KeyframesConfig(AppConfig):
name = 'keyframes'

4
keyframes/keyframes.py Normal file
View file

@ -0,0 +1,4 @@
class KeyFramesExtractor():
@staticmethod
def get_keyframes(video):
return []

View file

3
keyframes/tests.py Normal file
View file

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

15
manage.py Executable file
View file

@ -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)

25
nginx.conf Normal file
View file

@ -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/;
}
}

9
requirements.txt Normal file
View file

@ -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

0
settings/__init__.py Normal file
View file

137
settings/settings.py Normal file
View file

@ -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

29
settings/urls.py Normal file
View file

@ -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)

16
settings/wsgi.py Normal file
View file

@ -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()

View file

3
style_transfer/admin.py Normal file
View file

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

5
style_transfer/apps.py Normal file
View file

@ -0,0 +1,5 @@
from django.apps import AppConfig
class StyleTransferConfig(AppConfig):
name = 'style_transfer'

View file

View file

@ -0,0 +1,4 @@
class StyleTransfer():
@staticmethod
def get_stylized_frames(frames):
return frames

3
style_transfer/tests.py Normal file
View file

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

BIN
tmp/test.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 96 KiB

BIN
tmp/test.mp4 Normal file

Binary file not shown.

BIN
tmp/test2.mp4 Normal file

Binary file not shown.