Skip to content

Commit

Permalink
Disable smartcrop zoom
Browse files Browse the repository at this point in the history
  • Loading branch information
jrrodri committed Nov 24, 2024
1 parent faef7f7 commit ddd78d1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 14 deletions.
2 changes: 1 addition & 1 deletion abraia/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from dotenv import load_dotenv
load_dotenv()

__version__ = '0.20.0'
__version__ = '0.20.1'

from . import config
from .client import Abraia, APIError
Expand Down
22 changes: 9 additions & 13 deletions abraia/editing/smartcrop.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,8 @@ def sliding_window(img, step, size, rect):
def max_pooling(img):
w = img.shape[1] >> 1 << 1
h = img.shape[0] >> 1 << 1
dstack = np.dstack([img[0:h:2, 0:w:2],
img[0:h:2, 1:w:2],
img[1:h:2, 0:w:2],
img[1:h:2, 1:w:2]])
dstack = np.dstack([img[0:h:2, 0:w:2], img[0:h:2, 1:w:2],
img[1:h:2, 0:w:2], img[1:h:2, 1:w:2]])
return np.max(dstack, axis=2)


Expand Down Expand Up @@ -237,10 +235,12 @@ def attention_windows(salmap, ratio, recti, recto, step=2):
areas.extend(rects[idxs])
if sw < wi or sh < hi:
break
break # disable crop zoom
return np.array(areas)


def best_crop_area(salmap, energy, ratio, recti, recto):
def best_crop_area(salmap, faces, energy, ratio):
recti, recto = crop_region(salmap, faces)
salmap = max_pooling(salmap)
energy = max_pooling(energy)
energy = energy.astype(np.float32) * salmap
Expand All @@ -250,10 +250,8 @@ def best_crop_area(salmap, energy, ratio, recti, recto):
recti = rectangle_scale(recti, (0.5, 0.5))
recti = rectangle_intersection(recto, recti)
rects = attention_windows(salmap, ratio, recti, recto)
preservation = np.array([
content_preservation(salmap, rect) for rect in rects])
simplicities = np.array([
boundary_simplicity(energy, rect) for rect in rects])
preservation = np.array([content_preservation(salmap, rect) for rect in rects])
simplicities = np.array([boundary_simplicity(energy, rect) for rect in rects])
content = preservation > 0.8 * np.max(preservation)
if not np.all(content):
simplicities = simplicities[:np.argmin(content)]
Expand Down Expand Up @@ -284,13 +282,11 @@ def detect(self, img, size):
faces = self.faces.detect(rimg)
salmap = self.saliency.predict(rimg, faces=faces)
ar = max(size[0] / size[1], 0.027)
recti, recto = crop_region(salmap)
# recti, recto = crop_region(salmap, faces)
rect = best_crop_area(salmap, energy, ar, recti, recto)
rect = best_crop_area(salmap, faces, energy, ar)
return rectangle_scale(rect, (w / rw, h / rh))
return [0, 0, w, h]

def transform(self, img, size):
rect = self.smart_crop(img, size)
rect = self.detect(img, size)
img = image_roi(img, rect)
return resize_image(img, size)
Binary file modified images/screenshot.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit ddd78d1

Please sign in to comment.