vidformer 0.7.0__py3-none-any.whl → 0.8.0__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
vidformer/__init__.py CHANGED
@@ -1,5 +1,5 @@
1
1
  """A Python library for creating and viewing videos with vidformer."""
2
2
 
3
- __version__ = "0.7.0"
3
+ __version__ = "0.8.0"
4
4
 
5
5
  from .vf import *
vidformer/cv2/vf_cv2.py CHANGED
@@ -46,6 +46,9 @@ LINE_8 = 8
46
46
  LINE_AA = 16
47
47
 
48
48
  _inline_mat = vf.Filter("_inline_mat")
49
+ _slice_mat = vf.Filter("_slice_mat")
50
+ _slice_write_mat = vf.Filter("_slice_write_mat")
51
+
49
52
 
50
53
  _filter_scale = vf.Filter("Scale")
51
54
  _filter_rectangle = vf.Filter("cv2.rectangle")
@@ -119,6 +122,92 @@ class Frame:
119
122
  frame = frame[:, :, ::-1] # convert RGB to BGR
120
123
  return frame
121
124
 
125
+ def __getitem__(self, key):
126
+ if not isinstance(key, tuple):
127
+ raise NotImplementedError("Only 2D slicing is supported")
128
+
129
+ if len(key) != 2:
130
+ raise NotImplementedError("Only 2D slicing is supported")
131
+
132
+ if not all(isinstance(x, slice) for x in key):
133
+ raise NotImplementedError("Only 2D slicing is supported")
134
+
135
+ miny = key[0].start if key[0].start is not None else 0
136
+ maxy = key[0].stop if key[0].stop is not None else self.shape[0]
137
+ minx = key[1].start if key[1].start is not None else 0
138
+ maxx = key[1].stop if key[1].stop is not None else self.shape[1]
139
+
140
+ # handle negative indices
141
+ if miny < 0:
142
+ miny = self.shape[0] + miny
143
+ if maxy < 0:
144
+ maxy = self.shape[0] + maxy
145
+ if minx < 0:
146
+ minx = self.shape[1] + minx
147
+ if maxx < 0:
148
+ maxx = self.shape[1] + maxx
149
+
150
+ if (
151
+ maxy <= miny
152
+ or maxx <= minx
153
+ or miny < 0
154
+ or minx < 0
155
+ or maxy > self.shape[0]
156
+ or maxx > self.shape[1]
157
+ ):
158
+ raise NotImplementedError("Invalid slice")
159
+
160
+ f = _slice_mat(self._f, miny, maxy, minx, maxx)
161
+ fmt = self._fmt.copy()
162
+ fmt["width"] = maxx - minx
163
+ fmt["height"] = maxy - miny
164
+ return Frame(f, fmt)
165
+
166
+ def __setitem__(self, key, value):
167
+ value = frameify(value, "value")
168
+
169
+ if not isinstance(key, tuple):
170
+ raise NotImplementedError("Only 2D slicing is supported")
171
+
172
+ if len(key) != 2:
173
+ raise NotImplementedError("Only 2D slicing is supported")
174
+
175
+ if not all(isinstance(x, slice) for x in key):
176
+ raise NotImplementedError("Only 2D slicing is supported")
177
+
178
+ miny = key[0].start if key[0].start is not None else 0
179
+ maxy = key[0].stop if key[0].stop is not None else self.shape[0]
180
+ minx = key[1].start if key[1].start is not None else 0
181
+ maxx = key[1].stop if key[1].stop is not None else self.shape[1]
182
+
183
+ # handle negative indices
184
+ if miny < 0:
185
+ miny = self.shape[0] + miny
186
+ if maxy < 0:
187
+ maxy = self.shape[0] + maxy
188
+ if minx < 0:
189
+ minx = self.shape[1] + minx
190
+ if maxx < 0:
191
+ maxx = self.shape[1] + maxx
192
+
193
+ if (
194
+ maxy <= miny
195
+ or maxx <= minx
196
+ or miny < 0
197
+ or minx < 0
198
+ or maxy > self.shape[0]
199
+ or maxx > self.shape[1]
200
+ ):
201
+ raise NotImplementedError("Invalid slice")
202
+
203
+ if value.shape[0] != maxy - miny or value.shape[1] != maxx - minx:
204
+ raise NotImplementedError("Shape mismatch")
205
+
206
+ self._mut()
207
+ value._mut()
208
+
209
+ self._f = _slice_write_mat(self._f, value._f, miny, maxy, minx, maxx)
210
+
122
211
 
123
212
  def _inline_frame(arr):
124
213
  assert arr.dtype == np.uint8
@@ -137,20 +226,6 @@ def _inline_frame(arr):
137
226
  return Frame(f, fmt)
138
227
 
139
228
 
140
- def _framify(obj, field_name=None):
141
- if isinstance(obj, Frame):
142
- return obj
143
- elif isinstance(obj, np.ndarray):
144
- return _inline_frame(obj)
145
- else:
146
- if field_name is not None:
147
- raise Exception(
148
- f"Unsupported type for field {field_name}, expected Frame or np.ndarray"
149
- )
150
- else:
151
- raise Exception("Unsupported type, expected Frame or np.ndarray")
152
-
153
-
154
229
  class VideoCapture:
155
230
  def __init__(self, path):
156
231
  self._path = path
@@ -213,7 +288,7 @@ class VideoWriter:
213
288
  self._pix_fmt = "yuv420p"
214
289
 
215
290
  def write(self, frame):
216
- frame = _framify(frame, "frame")
291
+ frame = frameify(frame, "frame")
217
292
 
218
293
  if frame._fmt["pix_fmt"] != self._pix_fmt:
219
294
  f_obj = _filter_scale(frame._f, pix_fmt=self._pix_fmt)
@@ -245,6 +320,24 @@ class VideoWriter_fourcc:
245
320
  self._args = args
246
321
 
247
322
 
323
+ def frameify(obj, field_name=None):
324
+ """
325
+ Turn an object (e.g., ndarray) into a Frame.
326
+ """
327
+
328
+ if isinstance(obj, Frame):
329
+ return obj
330
+ elif isinstance(obj, np.ndarray):
331
+ return _inline_frame(obj)
332
+ else:
333
+ if field_name is not None:
334
+ raise Exception(
335
+ f"Unsupported type for field {field_name}, expected Frame or np.ndarray"
336
+ )
337
+ else:
338
+ raise Exception("Unsupported type, expected Frame or np.ndarray")
339
+
340
+
248
341
  def imread(path, *args):
249
342
  if len(args) > 0:
250
343
  raise NotImplementedError("imread does not support additional arguments")
@@ -260,7 +353,7 @@ def imwrite(path, img, *args):
260
353
  if len(args) > 0:
261
354
  raise NotImplementedError("imwrite does not support additional arguments")
262
355
 
263
- img = _framify(img)
356
+ img = frameify(img)
264
357
 
265
358
  fmt = img._fmt.copy()
266
359
  width = fmt["width"]
@@ -316,7 +409,7 @@ def rectangle(img, pt1, pt2, color, thickness=None, lineType=None, shift=None):
316
409
  cv.rectangle( img, pt1, pt2, color[, thickness[, lineType[, shift]]] )
317
410
  """
318
411
 
319
- img = _framify(img)
412
+ img = frameify(img)
320
413
  img._mut()
321
414
 
322
415
  assert len(pt1) == 2
@@ -360,7 +453,7 @@ def putText(
360
453
  cv.putText( img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]] )
361
454
  """
362
455
 
363
- img = _framify(img)
456
+ img = frameify(img)
364
457
  img._mut()
365
458
 
366
459
  assert isinstance(text, str)
@@ -399,7 +492,7 @@ def arrowedLine(
399
492
  """
400
493
  cv.arrowedLine( img, pt1, pt2, color[, thickness[, line_type[, shift[, tipLength]]]] )
401
494
  """
402
- img = _framify(img)
495
+ img = frameify(img)
403
496
  img._mut()
404
497
 
405
498
  assert len(pt1) == 2
@@ -433,7 +526,7 @@ def arrowedLine(
433
526
 
434
527
 
435
528
  def line(img, pt1, pt2, color, thickness=None, lineType=None, shift=None):
436
- img = _framify(img)
529
+ img = frameify(img)
437
530
  img._mut()
438
531
 
439
532
  assert len(pt1) == 2
@@ -463,7 +556,7 @@ def line(img, pt1, pt2, color, thickness=None, lineType=None, shift=None):
463
556
 
464
557
 
465
558
  def circle(img, center, radius, color, thickness=None, lineType=None, shift=None):
466
- img = _framify(img)
559
+ img = frameify(img)
467
560
  img._mut()
468
561
 
469
562
  assert len(center) == 2
@@ -514,8 +607,8 @@ def addWeighted(src1, alpha, src2, beta, gamma, dst=None, dtype=-1):
514
607
  """
515
608
  cv.addWeighted( src1, alpha, src2, beta, gamma[, dst[, dtype]] ) -> dst
516
609
  """
517
- src1 = _framify(src1, "src1")
518
- src2 = _framify(src2, "src2")
610
+ src1 = frameify(src1, "src1")
611
+ src2 = frameify(src2, "src2")
519
612
  src1._mut()
520
613
  src2._mut()
521
614
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: vidformer
3
- Version: 0.7.0
3
+ Version: 0.8.0
4
4
  Summary: A Python library for creating and viewing videos with vidformer.
5
5
  Author-email: Dominik Winecki <dominikwinecki@gmail.com>
6
6
  Requires-Python: >=3.8
@@ -0,0 +1,7 @@
1
+ vidformer/__init__.py,sha256=scMlj2LLMBZXdv9ob_Dhb4ObK7RZLzxVgELyaR650xU,113
2
+ vidformer/vf.py,sha256=GjgaKnbk3o75lwrXP3Ue0LDyTxSf-l0njL5X-6QYZvs,31496
3
+ vidformer/cv2/__init__.py,sha256=wOjDsYyUKlP_Hye8-tyz-msu9xwaPMpN2sGMu3Lh3-w,22
4
+ vidformer/cv2/vf_cv2.py,sha256=8shv614pmNHQyHMqjJSZf08j3eNxE_XtfdLd_kyh5no,19396
5
+ vidformer-0.8.0.dist-info/WHEEL,sha256=CpUCUxeHQbRN5UGRQHYRJorO5Af-Qy_fHMctcQ8DSGI,82
6
+ vidformer-0.8.0.dist-info/METADATA,sha256=aXFYRxtNx4hfWlqW7Zx_RW-X2CuN3-adYwRrCUkLtI4,1487
7
+ vidformer-0.8.0.dist-info/RECORD,,
@@ -1,7 +0,0 @@
1
- vidformer/__init__.py,sha256=uCe1BJKzIv5LzLK4TZLlOtw6ru8vCUsf7vJkov5-oCE,113
2
- vidformer/vf.py,sha256=GjgaKnbk3o75lwrXP3Ue0LDyTxSf-l0njL5X-6QYZvs,31496
3
- vidformer/cv2/__init__.py,sha256=wOjDsYyUKlP_Hye8-tyz-msu9xwaPMpN2sGMu3Lh3-w,22
4
- vidformer/cv2/vf_cv2.py,sha256=Rta6wCIVaOAnkGXlpqE06qVjFmnn9FnuP7LUD9xrRHU,16413
5
- vidformer-0.7.0.dist-info/WHEEL,sha256=CpUCUxeHQbRN5UGRQHYRJorO5Af-Qy_fHMctcQ8DSGI,82
6
- vidformer-0.7.0.dist-info/METADATA,sha256=4YKlG7kViJIWONuWeJEkxdIy2iZg4wZJ4NWzaPWATBw,1487
7
- vidformer-0.7.0.dist-info/RECORD,,