feat(v3): simplify the project to contain only required tools
This commit is contained in:
@@ -1,13 +1,15 @@
|
||||
import time
|
||||
|
||||
import cv2
|
||||
import numpy as np
|
||||
from django.conf import settings
|
||||
from django.db.models import Max
|
||||
from django.utils import timezone
|
||||
|
||||
from .algorithms import ProcessingError, average_images, decode_image, histogram, histogram_payload, normalize_to_uint8, process_image, verify_registration
|
||||
from .models import ImageSession, ImageState, ProcessingJob
|
||||
from .registry import apply_registered_operation
|
||||
from .storage import load_image_array, payload_for_image, save_image_array
|
||||
from .storage import delete_relative_file, load_image_array, payload_for_image, save_image_array
|
||||
from .tasks import run_batch_job
|
||||
|
||||
|
||||
@@ -15,7 +17,7 @@ def image_session_create(*, uploaded_file=None, image_base64=None):
|
||||
if uploaded_file and uploaded_file.size > settings.MAX_UPLOAD_MB * 1024 * 1024:
|
||||
raise ProcessingError(f"Upload exceeds {settings.MAX_UPLOAD_MB} MB.")
|
||||
|
||||
image = decode_image(uploaded_file=uploaded_file, base64_image=image_base64)
|
||||
image = compact_workspace_image(decode_image(uploaded_file=uploaded_file, base64_image=image_base64))
|
||||
relative_path = save_image_array(image, "original")
|
||||
hist = histogram(image)
|
||||
session = ImageSession.objects.create(
|
||||
@@ -52,9 +54,23 @@ def image_session_create(*, uploaded_file=None, image_base64=None):
|
||||
return payload
|
||||
|
||||
|
||||
def compact_workspace_image(image):
|
||||
max_dimension = int(getattr(settings, "IMAGE_WORKSPACE_MAX_DIMENSION", 1400))
|
||||
if max_dimension <= 0:
|
||||
return image
|
||||
height, width = image.shape[:2]
|
||||
longest = max(width, height)
|
||||
if longest <= max_dimension:
|
||||
return image
|
||||
scale = max_dimension / float(longest)
|
||||
next_size = (max(1, int(round(width * scale))), max(1, int(round(height * scale))))
|
||||
return cv2.resize(image, next_size, interpolation=cv2.INTER_AREA).astype(np.uint8)
|
||||
|
||||
|
||||
def image_state_create(*, session, parent, image, operation, params, label=None, prefix="state"):
|
||||
relative_path = save_image_array(image, prefix)
|
||||
sequence = session.states.count()
|
||||
max_sequence = session.states.aggregate(value=Max("sequence"))["value"]
|
||||
sequence = 0 if max_sequence is None else max_sequence + 1
|
||||
state = ImageState.objects.create(
|
||||
session=session,
|
||||
parent=parent,
|
||||
@@ -101,6 +117,15 @@ def image_states_payload(*, states):
|
||||
return [image_state_payload(state=state, include_image=True) for state in states]
|
||||
|
||||
|
||||
def image_state_delete(*, state):
|
||||
if state.sequence == 0 or state.operation == "upload":
|
||||
raise ProcessingError("The original S0 upload state cannot be deleted.")
|
||||
image_path = state.image
|
||||
state.children.update(parent=None)
|
||||
state.delete()
|
||||
delete_relative_file(image_path)
|
||||
|
||||
|
||||
def image_state_apply_operation(*, state, operation, params):
|
||||
if state.session.expired:
|
||||
raise ProcessingError("Image session has expired.")
|
||||
|
||||
Reference in New Issue
Block a user