MCPcopy Index your code
hub / github.com/DeepLabCut/DeepLabCut / KmeansbasedFrameselection

Function KmeansbasedFrameselection

deeplabcut/utils/frameselectiontools.py:110–203  ·  view source on GitHub ↗

This code downsamples the video to a width of resizewidth. The video is extracted as a numpy array, which is then clustered with kmeans, whereby each frames is treated as a vector. Frames from different clusters are then selected for labeling. This procedure makes sure that the frames "

(
    clip,
    numframes2pick,
    start,
    stop,
    Index=None,
    step=1,
    resizewidth=30,
    batchsize=100,
    max_iter=50,
    color=False,
)

Source from the content-addressed store, hash-verified

108
109
110def KmeansbasedFrameselection(
111 clip,
112 numframes2pick,
113 start,
114 stop,
115 Index=None,
116 step=1,
117 resizewidth=30,
118 batchsize=100,
119 max_iter=50,
120 color=False,
121):
122 """This code downsamples the video to a width of resizewidth.
123
124 The video is extracted as a numpy array, which is then clustered with kmeans, whereby each frames is treated as a
125 vector.
126 Frames from different clusters are then selected for labeling. This procedure makes sure that the frames "look
127 different",
128 i.e. different postures etc. On large videos this code is slow.
129
130 Consider not extracting the frames from the whole video but rather set start and stop to a period around interesting
131 behavior.
132
133 Note: this method can return fewer images than numframes2pick.
134 """
135
136 print(
137 "Kmeans-quantization based extracting of frames from",
138 round(start * clip.duration, 2),
139 " seconds to",
140 round(stop * clip.duration, 2),
141 " seconds.",
142 )
143 startindex = int(np.floor(clip.fps * clip.duration * start))
144 stopindex = int(np.ceil(clip.fps * clip.duration * stop))
145
146 if Index is None:
147 Index = np.arange(startindex, stopindex, step)
148 else:
149 Index = np.array(Index)
150 Index = Index[(Index > startindex) * (Index < stopindex)] # crop to range!
151
152 nframes = len(Index)
153 if batchsize > nframes:
154 batchsize = int(nframes / 2)
155
156 if len(Index) >= numframes2pick:
157 clipresized = clip.resize(width=resizewidth)
158 ny, nx = clipresized.size
159 frame0 = img_as_ubyte(clip.get_frame(0))
160 if np.ndim(frame0) == 3:
161 ncolors = np.shape(frame0)[2]
162 else:
163 ncolors = 1
164 print("Extracting and downsampling...", nframes, " frames from the video.")
165
166 if color and ncolors > 1:
167 DATA = np.zeros((nframes, nx * 3, ny))

Callers

nothing calls this directly

Calls 2

fitMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected