forked from nickdelgrosso/crab_pipeline
-
Notifications
You must be signed in to change notification settings - Fork 2
/
vids.py
139 lines (98 loc) · 3.6 KB
/
vids.py
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
from itertools import islice
from typing import Optional
import cv2
import numpy as np
from PIL import Image
import dask.array as da
# import av
class Video:
def __init__(self, filename: str, thumbnail_height: int = 200):
# self.cont = av.open(filename)
self.cap = cv2.VideoCapture(filename)
self.thumbnail_height = thumbnail_height
@property
def height(self) -> int:
return int(self.cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
@property
def width(self) -> int:
return int(self.cap.get(cv2.CAP_PROP_FRAME_WIDTH))
@property
def nchans(self) -> int:
return 3
@property
def nframes(self) -> int:
return int(self.cap.get(cv2.CAP_PROP_FRAME_COUNT))
def __len__(self):
return self.nframes
def _repr_png_(self):
height = self.cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
w = self.thumbnail_height
h = int(self.height / (self.width / w))
full_img = Image.new('RGB', (w * 5, h))
for rep, idx in enumerate(np.linspace(0, self.nframes - 1, 5, dtype=int)):
self.cap.set(cv2.CAP_PROP_POS_FRAMES, idx)
res, frame = self.cap.read()
img = Image.fromarray(frame, 'RGB')
img.thumbnail((w, h))
full_img.paste(img, (w * rep, 0))
return full_img._repr_png_()
def __getitem__(self, idx: int) -> np.ndarray:
# allow slicing
if type(idx) is slice:
return islice(self, idx.start, idx.stop, idx.step)
self.seek(idx)
frame = self.read()
return frame
def __iter__(self):
self.seek(0)
return self
def __next__(self):
try:
return self.read()
except EOFError:
raise StopIteration
def seek(self, idx: int) -> None:
nframes = self.nframes
# Alllow negative indices
if -nframes <= idx < 0:
idx = nframes + idx
if not 0 <= idx < nframes:
raise IndexError(f"requested frame {idx}, video has only {nframes} frames.")
self.cap.set(cv2.CAP_PROP_POS_FRAMES, idx)
def read(self):
res, frame = self.cap.read()
if not res:
if self.cap.get(cv2.CAP_PROP_POS_FRAMES) == self.cap.get(cv2.CAP_PROP_FRAME_COUNT):
raise EOFError("reached end of file.")
else:
raise IOError("unknown error")
return frame
def preview(frame: np.ndarray, width=400, height=400):
img = Image.fromarray(frame, 'RGB')
img.thumbnail((height, width))
return img
from time import perf_counter_ns, sleep
class Timer:
def __init__(self):
self.measured = None
self.measurements = []
self.zero_offset = 0.
self.reset()
def __enter__(self):
self.reset()
return self
def __exit__(self, type, value, traceback):
self.measured = self.read()
def calibrate(self, reps=7, wait=.02) -> None:
timer = Timer()
for _ in range(reps):
sleep(wait)
timer.write()
timer.reset()
self.zero_offset = sum(timer.measurements) / len(timer.measurements) - wait
def reset(self) -> None:
self.__time = perf_counter_ns()
def read(self) -> float:
return (perf_counter_ns() - self.__time) * 1e-9 - self.zero_offset
def write(self) -> None:
self.measurements.append(self.read())