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

IEPolys.py 2.8 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
  1. import numpy as np
  2. import cv2
  3. class IEPolysPoints:
  4. def __init__(self, IEPolys_parent, type):
  5. self.parent = IEPolys_parent
  6. self.type = type
  7. self.points = np.empty( (0,2), dtype=np.int32 )
  8. self.n_max = self.n = 0
  9. def add(self,x,y):
  10. self.points = np.append(self.points[0:self.n], [ (x,y) ], axis=0)
  11. self.n_max = self.n = self.n + 1
  12. self.parent.dirty = True
  13. def n_dec(self):
  14. self.n = max(0, self.n-1)
  15. self.parent.dirty = True
  16. return self.n
  17. def n_inc(self):
  18. self.n = min(len(self.points), self.n+1)
  19. self.parent.dirty = True
  20. return self.n
  21. def n_clip(self):
  22. self.points = self.points[0:self.n]
  23. self.n_max = self.n
  24. def cur_point(self):
  25. return self.points[self.n-1]
  26. def points_to_n(self):
  27. return self.points[0:self.n]
  28. def set_points(self, points):
  29. self.points = np.array(points)
  30. self.n_max = self.n = len(points)
  31. self.parent.dirty = True
  32. class IEPolys:
  33. def __init__(self):
  34. self.list = []
  35. self.n_max = self.n = 0
  36. self.dirty = True
  37. def add(self, type):
  38. self.list = self.list[0:self.n]
  39. l = IEPolysPoints(self, type)
  40. self.list.append ( l )
  41. self.n_max = self.n = self.n + 1
  42. self.dirty = True
  43. return l
  44. def n_dec(self):
  45. self.n = max(0, self.n-1)
  46. self.dirty = True
  47. return self.n
  48. def n_inc(self):
  49. self.n = min(len(self.list), self.n+1)
  50. self.dirty = True
  51. return self.n
  52. def n_list(self):
  53. return self.list[self.n-1]
  54. def n_clip(self):
  55. self.list = self.list[0:self.n]
  56. self.n_max = self.n
  57. if self.n > 0:
  58. self.list[-1].n_clip()
  59. def __iter__(self):
  60. for n in range(self.n):
  61. yield self.list[n]
  62. def switch_dirty(self):
  63. d = self.dirty
  64. self.dirty = False
  65. return d
  66. def overlay_mask(self, mask):
  67. h,w,c = mask.shape
  68. white = (1,)*c
  69. black = (0,)*c
  70. for n in range(self.n):
  71. poly = self.list[n]
  72. if poly.n > 0:
  73. cv2.fillPoly(mask, [poly.points_to_n()], white if poly.type == 1 else black )
  74. def get_total_points(self):
  75. return sum([self.list[n].n for n in range(self.n)])
  76. def dump(self):
  77. result = []
  78. for n in range(self.n):
  79. l = self.list[n]
  80. result += [ (l.type, l.points_to_n().tolist() ) ]
  81. return result
  82. @staticmethod
  83. def load(ie_polys=None):
  84. obj = IEPolys()
  85. if ie_polys is not None and isinstance(ie_polys, list):
  86. for (type, points) in ie_polys:
  87. obj.add(type)
  88. obj.n_list().set_points(points)
  89. return obj
Tip!

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

Comments

Loading...