def showmotion(src, paths, dest): FPS = get_fps(src) paths = pickle.load(open(paths)) CROP = getRule('crop', src) # divide by number of frames # px/second avg_speeds = [FPS*motion(y)/(paths[idx][0][-1][4]-paths[idx][0][0][4]) for idx,(x,y) in enumerate(paths)] avg_speed = sum(avg_speeds) / len(avg_speeds) fr = None for idx,fr in enumerate(VideoReader(src)): if CROP > 0: fr = fr[CROP:-CROP,CROP:-CROP] if idx == 0: vout = VideoWriter(dest) speeds = [] for p_idx,(raw,smooth) in enumerate(paths): raw = filter(lambda x: x[4] <= idx, raw) smooth = smooth[:max(0,len(raw)-5)] if len(raw) == 0: # print idx, 'path has not yet started' continue raw = numpy.array(raw) pts = raw[:,:2].astype(numpy.int32) cv2.polylines(fr, [pts], False, (255, 0, 0)) tup = tuple(raw[-1,:2].astype(int).tolist()) cv2.circle(fr, tup, raw[-1,2].astype(int), (255, 0, 0)) cv2.circle(fr, tup, 3, (255, 0, 0)) speed = 0 if len(pts) > 1: speed = FPS*numpy.hypot(*(pts[-1] - pts[-2]).T) speeds.append(speed) txt = "%.2f (%.2f)" % (avg_speeds[p_idx], speed) cv2.putText(fr, txt, tup, cv2.FONT_HERSHEY_PLAIN, 1, (0,255,0)) # & smoothed if len(smooth) > 0: pts = numpy.array(smooth).astype(numpy.int32) cv2.polylines(fr, [pts], False, (0, 0, 255)) tup = tuple(pts[-1].astype(int).tolist()) cv2.circle(fr, tup, 3, (0, 0, 255)) txt = "%.2f (%.2f), N=%d" % (avg_speed, sum(speeds)/len(speeds), len(paths)) cv2.putText(fr, txt, (30,30), cv2.FONT_HERSHEY_PLAIN, 1, (255,0,255)) vout.write(fr)
def jsonify(inputdir, outputdir): for f in glob.glob(os.path.join(inputdir, '*.avi')): outbase = os.path.join(outputdir, os.path.basename(f)) FPS = get_fps(f) base = os.path.basename(f) rec[base] = {"filename": os.path.basename(f)} paths = pickle.load(open('%s.path.pkl' % (outbase))) docs = [] store = allpaths for idx,(path,smoothed) in enumerate(paths): uid = '%s-%d' % (base, idx) doc = {"recording": base, "center (x,y)": path[0][:2], "radius (px)": diameter(path)/2, "area (px)": area(path), "raw_motion (px)": motion(path), "motion (px)": motion(smoothed), "distance (px)": distance(path), "speed (px/sec)": speed([path,smoothed], FPS), "rel_motion (% of length)": relativeMotion([path,smoothed]), "rel_speed (%/sec)": relativeSpeed([path,smoothed], FPS), "circularity (radians)": circularity(smoothed), "rel_circularity (radians/%-of-length)": relativeCircularity([path,smoothed]) } docs.append(doc) store[uid] = doc # compute recording-wide averages recinfo = {} nworms = len(paths) recinfo["nworms"] = nworms keys = docs[0].keys() keys.remove("recording") keys.remove("center (x,y)") for key in keys: recinfo[key] = sum([X[key] for X in docs])/float(nworms) recinfo["std_dev:"+key] = np.std([X[key] for X in docs]) rec[base].update(recinfo) json.dump(allpaths, open("Path.json", 'w')) json.dump(rec, open("Recording.json", 'w'))