Skip to content

Commit

Permalink
Move draw to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
jrrodri committed Dec 7, 2024
1 parent ba9284a commit d4732bf
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 34 deletions.
11 changes: 4 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ Identify people on images with face recognition as shown bellow.
import os

from abraia.faces import Recognition
from abraia.utils import load_image, save_image
from abraia.draw import render_results
from abraia.utils import load_image, save_image, render_results

img = load_image('images/rolling-stones.jpg')
out = img.copy()
Expand Down Expand Up @@ -114,9 +113,8 @@ for k, frame in enumerate(video):
Automatically recognize car license plates in images and video streams.

```python
from abraia import draw
from abraia.alpr import ALPR
from abraia.utils import load_image, show_image
from abraia.utils import load_image, show_image, render_results

alpr = ALPR()

Expand All @@ -127,7 +125,7 @@ results = [result for result in results if len(result['lines'])]
for result in results:
result['label'] = '\n'.join([line.get('text', '') for line in result['lines']])
del result['confidence']
frame = draw.render_results(img, results)
frame = render_results(img, results)
show_image(img)
```

Expand Down Expand Up @@ -167,8 +165,7 @@ Model to predict gender and age. It can be useful to anonymize minors faces.

```python
from abraia.faces import Recognition, Attribute
from abraia.utils import load_image, show_image
from abraia.draw import render_results
from abraia.utils import load_image, show_image, render_results

recognition = Recognition()
attribute = Attribute()
Expand Down
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.3'
__version__ = '0.20.4'

from . import config
from .client import Abraia, APIError
Expand Down
3 changes: 1 addition & 2 deletions abraia/detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

from .ops import py_cpu_nms, normalize, mask_to_polygon, softmax
from .utils import download_file, load_json, get_color, get_providers
from .utils import load_image, show_image, save_image, Video
from .draw import render_results
from .utils import load_image, show_image, save_image, Video, render_results


def resize(img, size):
Expand Down
2 changes: 1 addition & 1 deletion abraia/editing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from ..detect import load_model
from ..faces import Recognition
from .. import draw
from ..utils import draw


def detect_faces(img):
Expand Down
6 changes: 1 addition & 5 deletions abraia/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from .video import Video
from .sketcher import Sketcher
from .draw import render_results

tempdir = tempfile.gettempdir()

Expand Down Expand Up @@ -72,11 +73,6 @@ def get_color(idx):
return colors[idx % (len(colors) - 1)]


def hex_to_rgb(hex):
h = hex.lstrip('#')
return tuple(int(h[i:i+2], 16) for i in (0, 2, 4))


def get_providers():
available_providers = ort.get_available_providers()
providers = ["CUDAExecutionProvider", "CoreMLExecutionProvider", "CPUExecutionProvider"]
Expand Down
22 changes: 13 additions & 9 deletions abraia/draw.py → abraia/utils/draw.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import cv2
import math
import numpy as np

from .utils import hex_to_rgb

def hex_to_rgb(hex):
h = hex.lstrip('#')
return tuple(int(h[i:i+2], 16) for i in (0, 2, 4))


def draw_point(img, point, color, thickness = 2):
Expand Down Expand Up @@ -55,19 +57,24 @@ def draw_filled_polygon(img, polygon, color, opacity = 1):
def draw_blurred_mask(img, mask):
w_k = int(0.1 * max(img.shape[:2]))
w_k = w_k + 1 if w_k % 2 == 0 else w_k
# m_k = int(math.sqrt(w_k))
# m_k = m_k + 1 if m_k % 2 == 0 else m_k
# print(w_k, m_k)
mask = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)
blurred_img = cv2.GaussianBlur(img, (w_k, w_k), 0)
# blurred_mask = cv2.blur(mask, (5, 5))
img = np.where(mask==0, img, blurred_img)
# img1 = cv2.multiply(1 - (blurred_mask / 255), img)
# img2 = cv2.multiply(blurred_mask / 255, blurred_img)
# img = (cv2.add(img1, img2)).astype(np.uint8)
return img


def draw_overlay_mask(img, mask, color = (255, 0, 0), opacity = 1):
img_copy = img.copy()
overlay = cv2.cvtColor(mask, cv2.COLOR_GRAY2RGB)
overlay[mask == 255] = color
img_over = cv2.addWeighted(img_copy, 1 - opacity, overlay, opacity, 0)
img_copy[mask == 255] = img_over[mask == 255]
return img_copy


def draw_text(img, text, point, background_color = None, text_color = (255, 255, 255),
text_scale = 0.8, padding = 6):
text_font, text_thickness = cv2.FONT_HERSHEY_DUPLEX, 1
Expand All @@ -89,9 +96,6 @@ def draw_overlay(img, overlay, rect = None, opacity = 1):
alpha_float = (cv2.convertScaleAbs(alpha_channel * opacity).astype(np.float32) / 255)[..., np.newaxis]
blended_roi = cv2.convertScaleAbs((1 - alpha_float) * img[y1:y2, x1:x2] + alpha_float * overlay[:, :, :3])
img[y1:y2, x1:x2] = blended_roi
# img_copy = img.copy()
# cv2.rectangle(img_copy, pt1, pt2, color, -1)
# cv2.addWeighted(img_copy, opacity, img, 1 - opacity, 0, img)
return img


Expand Down
11 changes: 2 additions & 9 deletions abraia/utils/sketcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,7 @@
import cv2
import numpy as np


def draw_mask(img, mask, color, opacity = 1):
img_copy = img.copy()
overlay = cv2.cvtColor(mask, cv2.COLOR_GRAY2RGB)
overlay[mask == 255] = color
img_over = cv2.addWeighted(img_copy, 1 - opacity, overlay, opacity, 0)
img_copy[mask == 255] = img_over[mask == 255]
return img_copy
from .draw import draw_overlay_mask


class Sketcher:
Expand All @@ -42,7 +35,7 @@ def dilate(self, mask):

def show(self, img, mask=None):
if mask is not None:
img = draw_mask(img, mask, (255, 0, 0), 0.5)
img = draw_overlay_mask(img, mask, (255, 0, 0), 0.5)
self.output = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
cv2.imshow(self.win_name, self.output)

Expand Down

0 comments on commit d4732bf

Please sign in to comment.