def circularity(paths, dest): paths = pickle.load(open(paths)) ims = [draw(smooth) for raw,smooth in paths] sizes = np.array([[x.shape[1], x.shape[0]] for x in ims]) w = sizes[:,0].max() h = sizes[:,1].sum() out = np.zeros((h,w,3), np.uint8) cury = 0 for im in ims: out[cury:cury+im.shape[0],:im.shape[1]] = im cury += im.shape[0] np2image(out, dest)
def contours(src, dst): acc = [] video = None vfr = None BLUR = getRule("blur", src) DILATION = getRule("dilation", src) THRESH = getRule("threshold", src) REVERSED = getRule("reversed", src) MERGE = getRule("merge", src) CROP = getRule("crop", src) merge = [] for idx,fr in enumerate(VideoReader(src)): if CROP > 0: fr = fr[CROP:-CROP,CROP:-CROP] if len(merge) >= MERGE: merge.pop(0) merge.append(fr.mean(axis=2).astype(int)) fr = (array(merge).sum(axis=0) / len(merge)).astype(uint8) if REVERSED: fr = 255 - fr if BLUR > 0: cv2.blur(fr, (BLUR, BLUR), fr) fr = 255*(fr>THRESH).astype(uint8) fr = cv2.dilate(fr, None, iterations=DILATION) if idx == 0: video = VideoWriter(dst.replace('.pkl', '.avi')) vfr = zeros((fr.shape[0],fr.shape[1],3), uint8) np2image(fr, dst.replace('.pkl', '.png')) vfr[:] = fr.reshape((fr.shape[0],fr.shape[1],-1)) video.write(vfr) contours, hierarchy = cv2.findContours(fr, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) acc.append(contours) pickle.dump(acc, open(dst, 'w'))
def first(src, dst): vf = VideoReader(src) fr = vf.next() np2image(fr, dst)