pixelflow 0.9.0__tar.gz → 0.10.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (92) hide show
  1. {pixelflow-0.9.0/pixelflow.egg-info → pixelflow-0.10.0}/PKG-INFO +1 -1
  2. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/__init__.py +1 -1
  3. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/media.py +122 -29
  4. {pixelflow-0.9.0 → pixelflow-0.10.0/pixelflow.egg-info}/PKG-INFO +1 -1
  5. {pixelflow-0.9.0 → pixelflow-0.10.0}/LICENSE +0 -0
  6. {pixelflow-0.9.0 → pixelflow-0.10.0}/NOTICE +0 -0
  7. {pixelflow-0.9.0 → pixelflow-0.10.0}/README.md +0 -0
  8. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/annotators/__init__.py +0 -0
  9. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/annotators/anchors.py +0 -0
  10. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/annotators/blur.py +0 -0
  11. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/annotators/box.py +0 -0
  12. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/annotators/classification.py +0 -0
  13. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/annotators/crossing.py +0 -0
  14. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/annotators/dot.py +0 -0
  15. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/annotators/filled_box.py +0 -0
  16. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/annotators/fps_counter.py +0 -0
  17. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/annotators/grid_overlay.py +0 -0
  18. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/annotators/heatmap.py +0 -0
  19. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/annotators/keypoint.py +0 -0
  20. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/annotators/keypoint_skeleton.py +0 -0
  21. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/annotators/label.py +0 -0
  22. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/annotators/mask.py +0 -0
  23. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/annotators/motion_dots.py +0 -0
  24. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/annotators/motion_trails.py +0 -0
  25. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/annotators/oval.py +0 -0
  26. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/annotators/pixelate.py +0 -0
  27. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/annotators/polygon.py +0 -0
  28. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/annotators/scale_bar.py +0 -0
  29. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/annotators/utils.py +0 -0
  30. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/annotators/zones.py +0 -0
  31. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/arrays.py +0 -0
  32. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/buffer.py +0 -0
  33. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/classifications/__init__.py +0 -0
  34. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/classifications/classifications.py +0 -0
  35. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/classifications/converters.py +0 -0
  36. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/colors.py +0 -0
  37. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/crossings.py +0 -0
  38. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/detections/__init__.py +0 -0
  39. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/detections/converters.py +0 -0
  40. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/detections/detections.py +0 -0
  41. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/detections/filters.py +0 -0
  42. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/labels.py +0 -0
  43. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/masks.py +0 -0
  44. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/serialization.py +0 -0
  45. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/slicer.py +0 -0
  46. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/smoother.py +0 -0
  47. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/strategies.py +0 -0
  48. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/timer.py +0 -0
  49. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/tracker/UPSTREAM.md +0 -0
  50. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/tracker/__init__.py +0 -0
  51. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/tracker/base.py +0 -0
  52. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/tracker/botsort/__init__.py +0 -0
  53. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/tracker/botsort/tracker.py +0 -0
  54. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/tracker/botsort/tracklet.py +0 -0
  55. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/tracker/botsort/utils.py +0 -0
  56. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/tracker/bytetrack/__init__.py +0 -0
  57. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/tracker/bytetrack/tracker.py +0 -0
  58. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/tracker/bytetrack/tracklet.py +0 -0
  59. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/tracker/bytetrack/utils.py +0 -0
  60. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/tracker/cbiou/__init__.py +0 -0
  61. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/tracker/cbiou/tracker.py +0 -0
  62. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/tracker/ocsort/__init__.py +0 -0
  63. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/tracker/ocsort/tracker.py +0 -0
  64. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/tracker/ocsort/tracklet.py +0 -0
  65. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/tracker/ocsort/utils.py +0 -0
  66. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/tracker/sort/__init__.py +0 -0
  67. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/tracker/sort/tracker.py +0 -0
  68. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/tracker/sort/tracklet.py +0 -0
  69. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/tracker/sort/utils.py +0 -0
  70. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/tracker/utils/__init__.py +0 -0
  71. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/tracker/utils/base_tracklet.py +0 -0
  72. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/tracker/utils/cmc.py +0 -0
  73. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/tracker/utils/converters.py +0 -0
  74. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/tracker/utils/iou.py +0 -0
  75. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/tracker/utils/kalman_filter.py +0 -0
  76. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/tracker/utils/matrix.py +0 -0
  77. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/tracker/utils/motion_models.py +0 -0
  78. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/tracker/utils/predict_timing.py +0 -0
  79. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/tracker/utils/scale_aware_tracklet.py +0 -0
  80. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/tracker/utils/state_representations.py +0 -0
  81. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/transforms/__init__.py +0 -0
  82. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/transforms/detections.py +0 -0
  83. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/transforms/image.py +0 -0
  84. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/transforms/inverse.py +0 -0
  85. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/validators.py +0 -0
  86. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow/zones.py +0 -0
  87. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow.egg-info/SOURCES.txt +0 -0
  88. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow.egg-info/dependency_links.txt +0 -0
  89. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow.egg-info/requires.txt +0 -0
  90. {pixelflow-0.9.0 → pixelflow-0.10.0}/pixelflow.egg-info/top_level.txt +0 -0
  91. {pixelflow-0.9.0 → pixelflow-0.10.0}/pyproject.toml +0 -0
  92. {pixelflow-0.9.0 → pixelflow-0.10.0}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pixelflow
3
- Version: 0.9.0
3
+ Version: 0.10.0
4
4
  Summary: Computer vision processing library for visualizing and analyzing detection results from ML models
5
5
  Author-email: Emrah Nazif <emrah@datamarkin.com>
6
6
  License: Apache-2.0
@@ -1,4 +1,4 @@
1
- __version__ = "0.9.0"
1
+ __version__ = "0.10.0"
2
2
  __author__ = "Emrah NAZIF"
3
3
 
4
4
  # Import core modules
@@ -337,11 +337,19 @@ class _FrameSource:
337
337
  if self._pending is not None:
338
338
  frame, self._pending = self._pending, None
339
339
  return frame
340
- if self._cap is None or not self._cap.grab():
340
+ if self._cap is None or not self._grab():
341
341
  return None
342
342
  frame = self._retrieve_rgb()
343
343
  return None if frame is None else self._hand_out(frame)
344
344
 
345
+ def _grab(self) -> bool:
346
+ """Decode the next frame without converting it. False when the source ends."""
347
+ return self._cap.grab()
348
+
349
+ def _keeper(self):
350
+ """A test, called after each grab with its index, of whether to yield that frame."""
351
+ return lambda index: index % self._stride == 0
352
+
345
353
  def __iter__(self) -> Iterator[np.ndarray]:
346
354
  """Yield RGB frames from the current position until the source ends.
347
355
 
@@ -354,6 +362,7 @@ class _FrameSource:
354
362
  so they must be walked; they need not be turned into arrays.
355
363
  """
356
364
  index = 0
365
+ keep = self._keeper()
357
366
 
358
367
  # A source that decoded a frame in order to learn its own size hands that
359
368
  # frame out here rather than discarding it. Dropping it would silently skip
@@ -364,9 +373,9 @@ class _FrameSource:
364
373
  index = 1
365
374
 
366
375
  while self._cap is not None:
367
- if not self._cap.grab():
376
+ if not self._grab():
368
377
  break
369
- if index % self._stride == 0:
378
+ if keep(index):
370
379
  frame = self._retrieve_rgb()
371
380
  if frame is None:
372
381
  break
@@ -420,6 +429,12 @@ class VideoReader(_FrameSource):
420
429
  stride: Yield every Nth frame. ``fps`` and ``frames`` are divided to match,
421
430
  so a writer built with ``like=`` stays correct.
422
431
  start: Frame to begin at. Equivalent to ``seek(start)`` after opening.
432
+ rate: Yield this many frames a second instead of every Nth: the first frame of
433
+ every ``1/rate`` seconds, by the frames' own times, so a video whose frame rate
434
+ changes as it plays is sampled evenly too. ``fps`` is the rate. Not with
435
+ ``stride``.
436
+
437
+ After a frame is returned, ``frame_number`` and ``frame_time`` say which frame it is.
423
438
 
424
439
  Example:
425
440
  >>> video = pf.VideoReader("input.mp4", stride=2)
@@ -433,10 +448,15 @@ class VideoReader(_FrameSource):
433
448
  """
434
449
 
435
450
  def __init__(self, source: str, *, stride: int = 1, start: int = 0,
436
- width=_UNSET):
451
+ rate: Optional[float] = None, width=_UNSET):
437
452
  _reject_width(width, "VideoReader")
438
453
  if stride < 1:
439
454
  raise ValueError(f"stride must be at least 1, got {stride}")
455
+ self._rate = None if rate is None else _valid_fps(rate)
456
+ if rate is not None and self._rate is None:
457
+ raise ValueError(f"rate must be a positive number of frames a second, got {rate!r}")
458
+ if rate is not None and stride != 1:
459
+ raise ValueError("give stride or rate, not both: each says which frames to read")
440
460
 
441
461
  path = _resolve_path(source)
442
462
  self._source = str(path)
@@ -460,10 +480,53 @@ class VideoReader(_FrameSource):
460
480
 
461
481
  # Frame times learned by read_frame, in ms; they grow only as far as it is asked.
462
482
  self._times: list[float] = []
483
+ # The number of the next frame a grab decodes, and the time of the last one: see _grab.
484
+ self._next = 0
485
+ self._grabbed_time: Optional[float] = None
486
+ self._frame_number: Optional[int] = None
487
+ self._frame_time: Optional[float] = None
463
488
 
464
489
  if start:
465
490
  self.seek(start)
466
491
 
492
+ @property
493
+ def fps(self) -> Optional[float]:
494
+ """Rate of the frames this reader yields, or None if unknown.
495
+
496
+ Divided by ``stride``. With ``rate``, the rate, capped at the source's own: an
497
+ upper bound for a video with parts slower than the rate, which yield every frame.
498
+ """
499
+ if self._rate is None:
500
+ return super().fps
501
+ return self._rate if self._source_fps is None else min(self._rate, self._source_fps)
502
+
503
+ @property
504
+ def frames(self) -> Optional[int]:
505
+ """Estimated number of frames this reader will yield, or None if unknown."""
506
+ if self._rate is None or self.duration is None:
507
+ return super().frames
508
+ return min(self._source_frames, math.ceil(self.duration * self._rate))
509
+
510
+ @property
511
+ def frame_number(self) -> Optional[int]:
512
+ """Number in the file, from 0, of the frame last returned; None before the first.
513
+
514
+ Counts every frame of the file, whatever ``stride`` or ``rate`` is, so a result
515
+ can be drawn back on the original frame or read again with :meth:`read_frame`.
516
+ After :meth:`seek` in a video whose frame rate changes, it is only as exact as the
517
+ seek; :meth:`read_frame` is always exact.
518
+ """
519
+ return self._frame_number
520
+
521
+ @property
522
+ def frame_time(self) -> Optional[float]:
523
+ """Time in the file, in milliseconds, of the frame last returned; None before the first.
524
+
525
+ The frame's own time, so it stays right when the frame rate changes. A tracker
526
+ takes it as ``timestamp`` (in seconds: ``frame_time / 1000``).
527
+ """
528
+ return self._frame_time
529
+
467
530
  @property
468
531
  def codec(self) -> str:
469
532
  """FourCC of the source stream.
@@ -481,6 +544,39 @@ class VideoReader(_FrameSource):
481
544
  video (common in phone recordings). :meth:`read_frame` is exact.
482
545
  """
483
546
  self._cap.set(cv2.CAP_PROP_POS_FRAMES, frame_number)
547
+ self._next = frame_number
548
+
549
+ def _grab(self) -> bool:
550
+ """Decode the next frame, keeping its number and its time."""
551
+ if not self._cap.grab():
552
+ return False
553
+ self._next += 1
554
+ self._grabbed_time = self._cap.get(cv2.CAP_PROP_POS_MSEC)
555
+ return True
556
+
557
+ def _hand_out(self, frame: np.ndarray) -> np.ndarray:
558
+ self._frame_number, self._frame_time = self._next - 1, self._grabbed_time
559
+ return super()._hand_out(frame)
560
+
561
+ def _keeper(self):
562
+ """With ``rate``: keep the first frame of each 1/rate seconds, by the frames' times."""
563
+ if self._rate is None:
564
+ return super()._keeper()
565
+ first, last = None, -1
566
+
567
+ def keep(index: int) -> bool:
568
+ nonlocal first, last
569
+ time = self._grabbed_time
570
+ if first is None:
571
+ first = time
572
+ # The slack keeps a frame exactly on a boundary in the stretch it starts.
573
+ stretch = int((time - first + _FRAME_TIME_SLACK_MS) * self._rate / 1000)
574
+ if stretch <= last:
575
+ return False
576
+ last = stretch
577
+ return True
578
+
579
+ return keep
484
580
 
485
581
  def timestamps(self) -> list[float]:
486
582
  """Every frame's time in the file, in milliseconds, in order.
@@ -512,21 +608,20 @@ class VideoReader(_FrameSource):
512
608
  if number < 0:
513
609
  raise IndexError(f"no frame {number}: frames are counted from 0")
514
610
  if number < len(times):
515
- found = self._seek_to_time(times[number])
611
+ found = self._seek_to_frame(number)
516
612
  else:
517
613
  # Frame times not known this far yet: learn them on the way, on this capture.
518
614
  if times:
519
- found = self._seek_to_time(times[-1])
615
+ found = self._seek_to_frame(len(times) - 1)
520
616
  else:
521
617
  self.seek(0)
522
- found = self._grab_time() is not None
618
+ found = self._grab()
523
619
  if found:
524
- times.append(self._cap.get(cv2.CAP_PROP_POS_MSEC))
620
+ times.append(self._grabbed_time)
525
621
  while found and len(times) <= number:
526
- at = self._grab_time()
527
- found = at is not None
622
+ found = self._grab()
528
623
  if found:
529
- times.append(at)
624
+ times.append(self._grabbed_time)
530
625
  if not found:
531
626
  raise IndexError(f"no frame {number}: the video has {len(times)} frames")
532
627
  frame = self._retrieve_rgb()
@@ -534,19 +629,14 @@ class VideoReader(_FrameSource):
534
629
  raise IndexError(f"frame {number} can't be decoded")
535
630
  return self._hand_out(frame)
536
631
 
537
- def _grab_time(self) -> Optional[float]:
538
- """Decode the next frame without converting it: its time in ms, or None at the end."""
539
- if not self._cap.grab():
540
- return None
541
- return self._cap.get(cv2.CAP_PROP_POS_MSEC)
542
-
543
- def _seek_to_time(self, target: float) -> bool:
544
- """Grab the frame whose time is *target* ms. False if the video ends first.
632
+ def _seek_to_frame(self, number: int) -> bool:
633
+ """Grab frame *number*, whose time is known. False if the video ends first.
545
634
 
546
635
  A seek can't be trusted to land on a frame, but the times OpenCV reports while
547
636
  decoding are the frames' own. So seek to a moment before the target (further
548
637
  back if the seek lands after it), then decode forward to the frame.
549
638
  """
639
+ target = self._times[number]
550
640
  back = 1000.0 # ms: one second covers the gap between keyframes in most files
551
641
  while True:
552
642
  from_start = target - back <= 0
@@ -554,24 +644,26 @@ class VideoReader(_FrameSource):
554
644
  self.seek(0)
555
645
  else:
556
646
  self._cap.set(cv2.CAP_PROP_POS_MSEC, target - back)
557
- at = self._grab_time()
558
- if at is None:
647
+ if not self._grab():
559
648
  return False
560
- if from_start or at <= target + _FRAME_TIME_SLACK_MS:
649
+ if from_start or self._grabbed_time <= target + _FRAME_TIME_SLACK_MS:
561
650
  break
562
651
  back *= 2
563
- while at < target - _FRAME_TIME_SLACK_MS:
564
- at = self._grab_time()
565
- if at is None:
652
+ while self._grabbed_time < target - _FRAME_TIME_SLACK_MS:
653
+ if not self._grab():
566
654
  return False
655
+ self._next = number + 1 # positioned by time, so the count is set, not trusted
567
656
  return True
568
657
 
569
658
  def __repr__(self):
570
659
  rate = "unknown fps" if self.fps is None else f"{self.fps:.2f}fps"
571
660
  count = "unknown length" if self.frames is None else f"{self.frames} frames"
572
- stride = "" if self._stride == 1 else f", stride={self._stride}"
661
+ if self._rate is not None:
662
+ sampling = f", rate={self._rate:g}"
663
+ else:
664
+ sampling = "" if self._stride == 1 else f", stride={self._stride}"
573
665
  return (f"VideoReader({self._source!r}, {self.width}x{self.height}, "
574
- f"{rate}, {count}{stride})")
666
+ f"{rate}, {count}{sampling})")
575
667
 
576
668
 
577
669
  class CameraStream(_FrameSource):
@@ -799,19 +891,20 @@ class VideoWriter:
799
891
  # ---------------------------------------------------------------------------
800
892
 
801
893
  def read_video(source: str, *, stride: int = 1, start: int = 0,
802
- width=_UNSET) -> "VideoReader":
894
+ rate: Optional[float] = None, width=_UNSET) -> "VideoReader":
803
895
  """Open a video file for frame-by-frame reading.
804
896
 
805
897
  Args:
806
898
  source: Path to a video file.
807
899
  stride: Yield every Nth frame.
808
900
  start: Frame to begin at.
901
+ rate: Yield this many frames a second instead, by the frames' own times.
809
902
 
810
903
  Returns:
811
904
  A VideoReader instance.
812
905
  """
813
906
  _reject_width(width, "read_video")
814
- return VideoReader(source, stride=stride, start=start)
907
+ return VideoReader(source, stride=stride, start=start, rate=rate)
815
908
 
816
909
 
817
910
  def read_image(source, width=_UNSET) -> np.ndarray:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pixelflow
3
- Version: 0.9.0
3
+ Version: 0.10.0
4
4
  Summary: Computer vision processing library for visualizing and analyzing detection results from ML models
5
5
  Author-email: Emrah Nazif <emrah@datamarkin.com>
6
6
  License: Apache-2.0
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes