Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

preprocessors.py 2.6 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
  1. import cv2
  2. import numpy as np
  3. def centered_canny(x: np.ndarray, canny_low_threshold, canny_high_threshold):
  4. assert isinstance(x, np.ndarray)
  5. assert x.ndim == 2 and x.dtype == np.uint8
  6. y = cv2.Canny(x, int(canny_low_threshold), int(canny_high_threshold))
  7. y = y.astype(np.float32) / 255.0
  8. return y
  9. def centered_canny_color(x: np.ndarray, canny_low_threshold, canny_high_threshold):
  10. assert isinstance(x, np.ndarray)
  11. assert x.ndim == 3 and x.shape[2] == 3
  12. result = [centered_canny(x[..., i], canny_low_threshold, canny_high_threshold) for i in range(3)]
  13. result = np.stack(result, axis=2)
  14. return result
  15. def pyramid_canny_color(x: np.ndarray, canny_low_threshold, canny_high_threshold):
  16. assert isinstance(x, np.ndarray)
  17. assert x.ndim == 3 and x.shape[2] == 3
  18. H, W, C = x.shape
  19. acc_edge = None
  20. for k in [0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]:
  21. Hs, Ws = int(H * k), int(W * k)
  22. small = cv2.resize(x, (Ws, Hs), interpolation=cv2.INTER_AREA)
  23. edge = centered_canny_color(small, canny_low_threshold, canny_high_threshold)
  24. if acc_edge is None:
  25. acc_edge = edge
  26. else:
  27. acc_edge = cv2.resize(acc_edge, (edge.shape[1], edge.shape[0]), interpolation=cv2.INTER_LINEAR)
  28. acc_edge = acc_edge * 0.75 + edge * 0.25
  29. return acc_edge
  30. def norm255(x, low=4, high=96):
  31. assert isinstance(x, np.ndarray)
  32. assert x.ndim == 2 and x.dtype == np.float32
  33. v_min = np.percentile(x, low)
  34. v_max = np.percentile(x, high)
  35. x -= v_min
  36. x /= v_max - v_min
  37. return x * 255.0
  38. def canny_pyramid(x, canny_low_threshold, canny_high_threshold):
  39. # For some reasons, SAI's Control-lora Canny seems to be trained on canny maps with non-standard resolutions.
  40. # Then we use pyramid to use all resolutions to avoid missing any structure in specific resolutions.
  41. color_canny = pyramid_canny_color(x, canny_low_threshold, canny_high_threshold)
  42. result = np.sum(color_canny, axis=2)
  43. return norm255(result, low=1, high=99).clip(0, 255).astype(np.uint8)
  44. def cpds(x):
  45. # cv2.decolor is not "decolor", it is Cewu Lu's method
  46. # See http://www.cse.cuhk.edu.hk/leojia/projects/color2gray/index.html
  47. # See https://docs.opencv.org/3.0-beta/modules/photo/doc/decolor.html
  48. raw = cv2.GaussianBlur(x, (0, 0), 0.8)
  49. density, boost = cv2.decolor(raw)
  50. raw = raw.astype(np.float32)
  51. density = density.astype(np.float32)
  52. boost = boost.astype(np.float32)
  53. offset = np.sum((raw - boost) ** 2.0, axis=2) ** 0.5
  54. result = density + offset
  55. return norm255(result, low=4, high=96).clip(0, 255).astype(np.uint8)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...