comixify/api/models.py

30 lines
1 KiB
Python
Raw Normal View History

2018-07-31 19:51:39 +00:00
import os
import uuid
import cv2
2018-08-20 23:00:22 +00:00
from django.conf import settings
2018-07-31 19:51:39 +00:00
from django.core.files import File
from django.db import models
class Video(models.Model):
2018-08-20 23:00:22 +00:00
file = models.FileField(blank=False, null=False, upload_to="raw_videos")
2018-07-31 19:51:39 +00:00
timestamp = models.DateTimeField(auto_now_add=True)
class Comic(models.Model):
2018-08-20 23:00:22 +00:00
file = models.FileField(blank=False, null=False, upload_to="comic")
video = models.ForeignKey(Video, on_delete=models.CASCADE, related_name="comic")
2018-07-31 19:51:39 +00:00
@classmethod
def create_from_nparray(cls, nparray_file, video):
2018-08-22 09:52:17 +00:00
if nparray_file.max() <= 1:
nparray_file = (nparray_file * 255).astype(int)
2018-07-31 19:51:39 +00:00
tmp_name = uuid.uuid4()
2018-08-20 23:00:22 +00:00
cv2.imwrite(f"{settings.TMP_DIR}{tmp_name}.png", nparray_file)
with open(f"{settings.TMP_DIR}{tmp_name}.png", mode="rb") as tmp_file:
comic_image = File(tmp_file, name=f"{tmp_name}.png")
2018-07-31 19:51:39 +00:00
comic = Comic.objects.create(file=comic_image, video=video)
2018-08-20 23:00:22 +00:00
os.remove(f"{settings.TMP_DIR}{tmp_name}.png")
2018-07-31 19:51:39 +00:00
return comic