|
@@ -1,14 +1,14 @@
|
|
-import cv2
|
|
|
|
|
|
+import typing as T
|
|
import uuid
|
|
import uuid
|
|
|
|
+
|
|
|
|
+import cv2
|
|
import numpy as np
|
|
import numpy as np
|
|
-import typing as T
|
|
|
|
|
|
|
|
from flask import abort
|
|
from flask import abort
|
|
from flask import make_response
|
|
from flask import make_response
|
|
from flask import request
|
|
from flask import request
|
|
from flask.views import View
|
|
from flask.views import View
|
|
|
|
|
|
-from pycs import db
|
|
|
|
from pycs.database.File import File
|
|
from pycs.database.File import File
|
|
from pycs.database.Result import Result
|
|
from pycs.database.Result import Result
|
|
from pycs.frontend.notifications.NotificationManager import NotificationManager
|
|
from pycs.frontend.notifications.NotificationManager import NotificationManager
|
|
@@ -34,7 +34,7 @@ class EstimateBoundingBox(View):
|
|
if 'x' not in request_data or 'y' not in request_data:
|
|
if 'x' not in request_data or 'y' not in request_data:
|
|
abort(400, "coordinates for the estimation are missing")
|
|
abort(400, "coordinates for the estimation are missing")
|
|
|
|
|
|
- x,y = map(request_data.get, "xy")
|
|
|
|
|
|
+ x, y = map(request_data.get, "xy")
|
|
|
|
|
|
# get project
|
|
# get project
|
|
project = file.project
|
|
project = file.project
|
|
@@ -56,13 +56,15 @@ class EstimateBoundingBox(View):
|
|
|
|
|
|
|
|
|
|
def estimate(file_id: int, x: float, y: float) -> Result:
|
|
def estimate(file_id: int, x: float, y: float) -> Result:
|
|
|
|
+ """ estimation function """
|
|
|
|
+
|
|
file = File.query.get(file_id)
|
|
file = File.query.get(file_id)
|
|
|
|
|
|
- im = cv2.imread(file.absolute_path, cv2.IMREAD_GRAYSCALE)
|
|
|
|
|
|
+ image = cv2.imread(file.absolute_path, cv2.IMREAD_GRAYSCALE)
|
|
|
|
|
|
- h, w = im.shape
|
|
|
|
|
|
+ h, w = image.shape
|
|
pos = int(x * w), int(y * h)
|
|
pos = int(x * w), int(y * h)
|
|
- x0, y0, x1, y1 = detect(im, pos,
|
|
|
|
|
|
+ x0, y0, x1, y1 = detect(image, pos,
|
|
window_size=1000,
|
|
window_size=1000,
|
|
pixel_delta=50,
|
|
pixel_delta=50,
|
|
enlarge=1e-2,
|
|
enlarge=1e-2,
|
|
@@ -77,18 +79,19 @@ def estimate(file_id: int, x: float, y: float) -> Result:
|
|
|
|
|
|
return file.create_result('pipeline', 'bounding-box', label=None, data=data)
|
|
return file.create_result('pipeline', 'bounding-box', label=None, data=data)
|
|
|
|
|
|
-def detect(im: np.ndarray,
|
|
|
|
|
|
+def detect(image: np.ndarray,
|
|
pos: T.Tuple[int, int],
|
|
pos: T.Tuple[int, int],
|
|
window_size: int = 1000,
|
|
window_size: int = 1000,
|
|
pixel_delta: int = 0,
|
|
pixel_delta: int = 0,
|
|
enlarge: float = -1) -> T.Tuple[int, int, int, int]:
|
|
enlarge: float = -1) -> T.Tuple[int, int, int, int]:
|
|
- # im = blur(im, 3)
|
|
|
|
|
|
+ """ detection function """
|
|
|
|
+ # image = blur(image, 3)
|
|
x, y = pos
|
|
x, y = pos
|
|
- pixel = im[y, x]
|
|
|
|
|
|
+ pixel = image[y, x]
|
|
|
|
|
|
min_pix, max_pix = pixel - pixel_delta, pixel + pixel_delta
|
|
min_pix, max_pix = pixel - pixel_delta, pixel + pixel_delta
|
|
|
|
|
|
- mask = np.logical_and(min_pix < im, im < max_pix).astype(np.float32)
|
|
|
|
|
|
+ mask = np.logical_and(min_pix < image, image < max_pix).astype(np.float32)
|
|
# mask = open_close(mask)
|
|
# mask = open_close(mask)
|
|
# mask = blur(mask)
|
|
# mask = blur(mask)
|
|
|
|
|
|
@@ -98,18 +101,19 @@ def detect(im: np.ndarray,
|
|
|
|
|
|
sum_x, sum_y = window.sum(axis=0), window.sum(axis=1)
|
|
sum_x, sum_y = window.sum(axis=0), window.sum(axis=1)
|
|
|
|
|
|
- enlarge = int(enlarge * max(im.shape))
|
|
|
|
|
|
+ enlarge = int(enlarge * max(image.shape))
|
|
(x0, x1), (y0, y1) = get_borders(sum_x, enlarge), get_borders(sum_y, enlarge)
|
|
(x0, x1), (y0, y1) = get_borders(sum_x, enlarge), get_borders(sum_y, enlarge)
|
|
|
|
|
|
x0 = max(x + x0 - pad, 0)
|
|
x0 = max(x + x0 - pad, 0)
|
|
y0 = max(y + y0 - pad, 0)
|
|
y0 = max(y + y0 - pad, 0)
|
|
|
|
|
|
- x1 = min(x + x1 - pad, im.shape[1])
|
|
|
|
- y1 = min(y + y1 - pad, im.shape[0])
|
|
|
|
|
|
+ x1 = min(x + x1 - pad, image.shape[1])
|
|
|
|
+ y1 = min(y + y1 - pad, image.shape[0])
|
|
|
|
|
|
return x0, y0, x1, y1
|
|
return x0, y0, x1, y1
|
|
|
|
|
|
def get_borders(arr, enlarge: int, eps=5e-1):
|
|
def get_borders(arr, enlarge: int, eps=5e-1):
|
|
|
|
+ """ returns borders based on coordinate extrema """
|
|
mid = len(arr) // 2
|
|
mid = len(arr) // 2
|
|
|
|
|
|
arr0, arr1 = arr[:mid], arr[mid:]
|
|
arr0, arr1 = arr[:mid], arr[mid:]
|
|
@@ -130,18 +134,3 @@ def get_borders(arr, enlarge: int, eps=5e-1):
|
|
upper = min(upper + enlarge, len(arr)-1)
|
|
upper = min(upper + enlarge, len(arr)-1)
|
|
|
|
|
|
return int(lower), int(upper)
|
|
return int(lower), int(upper)
|
|
-
|
|
|
|
-
|
|
|
|
-"""
|
|
|
|
-def blur(im, sigma=5):
|
|
|
|
- from skimage import filters
|
|
|
|
- return filters.gaussian(im, sigma=sigma, preserve_range=True)
|
|
|
|
-
|
|
|
|
-def open_close(im, kernel_size=3):
|
|
|
|
-
|
|
|
|
- kernel = np.ones((kernel_size, kernel_size), dtype=np.uint8)
|
|
|
|
-
|
|
|
|
- im = cv2.morphologyEx(im, cv2.MORPH_OPEN, kernel)
|
|
|
|
- im = cv2.morphologyEx(im, cv2.MORPH_CLOSE, kernel)
|
|
|
|
- return im
|
|
|
|
-"""
|
|
|