Spaces:
Runtime error
Runtime error
Upload libs/base_utils.py with huggingface_hub
Browse files- libs/base_utils.py +84 -0
libs/base_utils.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import cv2
|
3 |
+
import torch
|
4 |
+
import numpy as np
|
5 |
+
from PIL import Image
|
6 |
+
|
7 |
+
|
8 |
+
def instantiate_from_config(config):
|
9 |
+
if not "target" in config:
|
10 |
+
raise KeyError("Expected key `target` to instantiate.")
|
11 |
+
return get_obj_from_str(config["target"])(**config.get("params", dict()))
|
12 |
+
|
13 |
+
|
14 |
+
def get_obj_from_str(string, reload=False):
|
15 |
+
import importlib
|
16 |
+
module, cls = string.rsplit(".", 1)
|
17 |
+
if reload:
|
18 |
+
module_imp = importlib.import_module(module)
|
19 |
+
importlib.reload(module_imp)
|
20 |
+
return getattr(importlib.import_module(module, package=None), cls)
|
21 |
+
|
22 |
+
|
23 |
+
def tensor_detail(t):
|
24 |
+
assert type(t) == torch.Tensor
|
25 |
+
print(f"shape: {t.shape} mean: {t.mean():.2f}, std: {t.std():.2f}, min: {t.min():.2f}, max: {t.max():.2f}")
|
26 |
+
|
27 |
+
|
28 |
+
|
29 |
+
def drawRoundRec(draw, color, x, y, w, h, r):
|
30 |
+
drawObject = draw
|
31 |
+
|
32 |
+
'''Rounds'''
|
33 |
+
drawObject.ellipse((x, y, x + r, y + r), fill=color)
|
34 |
+
drawObject.ellipse((x + w - r, y, x + w, y + r), fill=color)
|
35 |
+
drawObject.ellipse((x, y + h - r, x + r, y + h), fill=color)
|
36 |
+
drawObject.ellipse((x + w - r, y + h - r, x + w, y + h), fill=color)
|
37 |
+
|
38 |
+
'''rec.s'''
|
39 |
+
drawObject.rectangle((x + r / 2, y, x + w - (r / 2), y + h), fill=color)
|
40 |
+
drawObject.rectangle((x, y + r / 2, x + w, y + h - (r / 2)), fill=color)
|
41 |
+
|
42 |
+
|
43 |
+
def do_resize_content(original_image: Image, scale_rate):
|
44 |
+
# resize image content wile retain the original image size
|
45 |
+
if scale_rate != 1:
|
46 |
+
# Calculate the new size after rescaling
|
47 |
+
new_size = tuple(int(dim * scale_rate) for dim in original_image.size)
|
48 |
+
# Resize the image while maintaining the aspect ratio
|
49 |
+
resized_image = original_image.resize(new_size)
|
50 |
+
# Create a new image with the original size and black background
|
51 |
+
padded_image = Image.new("RGBA", original_image.size, (0, 0, 0, 0))
|
52 |
+
paste_position = ((original_image.width - resized_image.width) // 2, (original_image.height - resized_image.height) // 2)
|
53 |
+
padded_image.paste(resized_image, paste_position)
|
54 |
+
return padded_image
|
55 |
+
else:
|
56 |
+
return original_image
|
57 |
+
|
58 |
+
def add_stroke(img, color=(255, 255, 255), stroke_radius=3):
|
59 |
+
# color in R, G, B format
|
60 |
+
if isinstance(img, Image.Image):
|
61 |
+
assert img.mode == "RGBA"
|
62 |
+
img = cv2.cvtColor(np.array(img), cv2.COLOR_RGBA2BGRA)
|
63 |
+
else:
|
64 |
+
assert img.shape[2] == 4
|
65 |
+
gray = img[:,:, 3]
|
66 |
+
ret, binary = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
|
67 |
+
contours, hierarchy = cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
|
68 |
+
res = cv2.drawContours(img, contours,-1, tuple(color)[::-1] + (255,), stroke_radius)
|
69 |
+
return Image.fromarray(cv2.cvtColor(res,cv2.COLOR_BGRA2RGBA))
|
70 |
+
|
71 |
+
def make_blob(image_size=(512, 512), sigma=0.2):
|
72 |
+
"""
|
73 |
+
make 2D blob image with:
|
74 |
+
I(x, y)=1-\exp \left(-\frac{(x-H / 2)^2+(y-W / 2)^2}{2 \sigma^2 HS}\right)
|
75 |
+
"""
|
76 |
+
import numpy as np
|
77 |
+
H, W = image_size
|
78 |
+
x = np.arange(0, W, 1, float)
|
79 |
+
y = np.arange(0, H, 1, float)
|
80 |
+
x, y = np.meshgrid(x, y)
|
81 |
+
x0 = W // 2
|
82 |
+
y0 = H // 2
|
83 |
+
img = 1 - np.exp(-((x - x0) ** 2 + (y - y0) ** 2) / (2 * sigma ** 2 * H * W))
|
84 |
+
return (img * 255).astype(np.uint8)
|