Skip to content

Commit

Permalink
Moved function
Browse files Browse the repository at this point in the history
  • Loading branch information
Marishka17 committed Oct 19, 2020
1 parent 347621a commit 188df45
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 28 deletions.
15 changes: 1 addition & 14 deletions cvat/apps/engine/media_extractors.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import numpy as np
from pyunpack import Archive
from PIL import Image, ImageFile
from cvat.apps.engine.utils import rotate_image

# fixes: "OSError:broken data stream" when executing line 72 while loading images downloaded from the web
# see: https://stackoverflow.com/questions/42462431/oserror-broken-data-stream-when-reading-image-file
Expand All @@ -35,20 +36,6 @@ def delete_tmp_dir(tmp_dir):
if tmp_dir:
shutil.rmtree(tmp_dir)

def rotate_image(image, angle):
import cv2 as cv
height, width = image.shape[:2]
image_center = (width/2, height/2)
matrix = cv.getRotationMatrix2D(image_center, angle, 1.)
abs_cos = abs(matrix[0,0])
abs_sin = abs(matrix[0,1])
bound_w = int(height * abs_sin + width * abs_cos)
bound_h = int(height * abs_cos + width * abs_sin)
matrix[0, 2] += bound_w/2 - image_center[0]
matrix[1, 2] += bound_h/2 - image_center[1]
matrix = cv.warpAffine(image, matrix, (bound_w, bound_h))
return matrix

class IMediaReader(ABC):
def __init__(self, source_path, step, start, stop):
self._source_path = sorted(source_path)
Expand Down
15 changes: 1 addition & 14 deletions cvat/apps/engine/prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from collections import OrderedDict
import hashlib
import os
from cvat.apps.engine.utils import rotate_image

class WorkWithVideo:
def __init__(self, **kwargs):
Expand Down Expand Up @@ -258,17 +259,3 @@ def prepare_meta_for_upload(func, *args):
with open(meta_info.meta_path, 'a') as meta_file:
meta_file.write(str(meta_info.get_task_size()))
return smooth_decoding

def rotate_image(image, angle):
import cv2 as cv
height, width = image.shape[:2]
image_center = (width/2, height/2)
matrix = cv.getRotationMatrix2D(image_center, angle, 1.)
abs_cos = abs(matrix[0,0])
abs_sin = abs(matrix[0,1])
bound_w = int(height * abs_sin + width * abs_cos)
bound_h = int(height * abs_cos + width * abs_sin)
matrix[0, 2] += bound_w/2 - image_center[0]
matrix[1, 2] += bound_h/2 - image_center[1]
matrix = cv.warpAffine(image, matrix, (bound_w, bound_h))
return matrix
14 changes: 14 additions & 0 deletions cvat/apps/engine/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# SPDX-License-Identifier: MIT

import ast
import cv2 as cv
from collections import namedtuple
import importlib
import sys
Expand Down Expand Up @@ -74,3 +75,16 @@ def av_scan_paths(*paths):
res = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if res.returncode:
raise ValidationError(res.stdout)

def rotate_image(image, angle):
height, width = image.shape[:2]
image_center = (width/2, height/2)
matrix = cv.getRotationMatrix2D(image_center, angle, 1.)
abs_cos = abs(matrix[0,0])
abs_sin = abs(matrix[0,1])
bound_w = int(height * abs_sin + width * abs_cos)
bound_h = int(height * abs_cos + width * abs_sin)
matrix[0, 2] += bound_w/2 - image_center[0]
matrix[1, 2] += bound_h/2 - image_center[1]
matrix = cv.warpAffine(image, matrix, (bound_w, bound_h))
return matrix

0 comments on commit 188df45

Please sign in to comment.