def gen_cropped_frames(video_path, coords_path, save_path): cap = cv2.VideoCapture(video_path) fname = video_path.split('/')[-1].split('.')[0] fnum = fname.split("_")[-1] frame_count = 0 coords = open(coords_path).readlines() total_frames = int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT)) if (total_frames-len(coords)) > 0: append_frame_skip = int(total_frames/(total_frames-len(coords))) for f in range(0,total_frames, append_frame_skip): coords.insert(f, coords[f]) vid = my_video_capture(video_path, frame_rate=30) crop_coords_used = open("%s/%s/cropped/crop_coords.txt" % (save_path, fname), "wb") output_vid = my_video_capture(save_path, frame_rate=30, mode="write") prev_fname = fname.split("_")[:-1] prev_fname.append(str(int(fnum) - 1).zfill(4)) prev_fname = "_".join(prev_fname) if os.path.exists("%s/%s/cropped/crop_coords.txt" % (save_path, prev_fname)): use_coord = np.array([int(n) for n in open("%s/%s/cropped/crop_coords.txt" % (save_path, prev_fname)).readlines()[-1].split(",")]) else: use_coord = np.array([0,0,0,0]) while vid.has_next(): frame = vid.read() try: cur_coord = np.array([int(n) for n in coords[frame_count].split(",")]) future_coords = [np.array([int(n) for n in coords[frame_count+f_offset].split(",")]) for f_offset in xrange(5)] except IndexError: pdb.set_trace() diff = np.mean(np.sum(np.abs(future_coord-use_coord)) for future_coord in future_coords) if diff > 100: use_coord = cur_coord crop_coords_used.write(",".join([str(i) for i in use_coord]) + "\n") frame_count += 1 if sum(use_coord) > 0: output_vid.write(frame[use_coord[2]:use_coord[3], use_coord[0]:use_coord[1]]) else: output_vid.write(frame) output_vid.new_img_folder("%s/%s/cropped/" % (save_path, fname)) output_vid.close() vid.close()
def main(sbj_id, day, result, video_loc, disconnect_file, save_fldr, comp): img_fldr = "%s/images" % save_fldr img_fldr_final = "%s/images_final" % save_fldr if not os.path.exists(img_fldr): os.makedirs(img_fldr) if not os.path.exists(img_fldr_final): os.makedirs(img_fldr_final) start_time, end_time, start, end = get_disconnected_times(disconnect_file) duration = 60*60*1 video_starts = pickle.load(open("C:\\Users\\wangnxr\\Documents\\rao_lab\\video_analysis\\vid_real_time\\%s_%i.p" % (sbj_id, day), "rb")) #plot_2d_coords(result, (4,9), (25,55), img_fldr, day, start_time, duration, type="slow") for i, video_start in enumerate(video_starts["start"]): if (start_time+timedelta(seconds=150))<=video_start: vid_counter=i-1 break frame_counter=(start_time+timedelta(seconds=150)-video_starts["start"][vid_counter]).seconds+1 cur_vid = my_video_capture("%s/%s_%i_%04i.avi" %(video_loc, sbj_id, day, vid_counter), 1) movie = my_video_capture(img_fldr_final, 30, mode="write") for s in range(0,duration-150,1): plot = cv2.resize(cv2.imread("%s/%i_%05d.png" % (img_fldr, day, s)), (480, 480)) cur_time = start_time + timedelta(seconds=(s+150)) if not disconnected(start, end, cur_time): if vid_counter < len(video_starts["start"]) and cur_time >= video_starts["start"][vid_counter+1]: vid_counter += 1 frame_counter = 1 cur_vid.close() cur_vid = my_video_capture("%s/%s_%i_%04i.avi" %(video_loc, sbj_id, day, vid_counter), 1) cur_vid.forward_to(frame_counter) frame = cur_vid.read() frame_counter += 1 else: frame = np.zeros((480,640,3), np.uint8) cv2.putText(frame, "Shown at 30X", (50,50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255)) concat = concat_images(plot, frame) movie.write(concat) cur_vid.close() movie.new_vid("%s/movie_comp%i_for_bing3.avi" % (save_fldr, comp)) movie.close()
def face_blur(vid_src): #cascade_fn = "C:\\Python27\\lib\\site-packages\\pyESig\\vid\\haarcascades\\haarcascade_frontalface_alt.xml" #cascade = cv2.CascadeClassifier(cascade_fn) cam = my_video_capture(vid_src, 30) frame_cnt = 1 while cam.has_next(): img = cam.read() height, width, depth = img.shape #gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #gray = cv2.equalizeHist(gray) min_neigh = 1 #t = clock() # if frame_cnt%90==1: # rects = detect(gray[50:-50,50:-50], cascade, min_neigh) # rects = [] # # while len(rects) == 0 and cam.has_next(): # # img_tmp = cam.read() # # height, width, depth = img_tmp.shape # # gray = cv2.cvtColor(img_tmp, cv2.COLOR_BGR2GRAY) # # gray = cv2.equalizeHist(gray) # # rects = detect(gray[50:-50,50:-50], cascade, min_neigh) # cam.rewind_to(frame_cnt) # img = cam.read() # c_x = width/2 # c_y = height/2 # (x1,y1,x2,y2) = find_center_rect(rects, (c_x,c_y)) x1 = 0 x2 = 50 y1 = 60 y2 = 160 sub_face = img[y1:y2, x1:x2] sub_face=cv2.GaussianBlur(sub_face, (23,23),15) img[y1:y2, x1:x2] = sub_face #cv2.imshow('facedetect', img) #pdb.set_trace() if 0xFF & cv2.waitKey(5) == 27: break cam.write(img, frame_cnt) frame_cnt += 1 print frame_cnt fileName, fileExt = os.path.splitext(vid_src) cam.new_vid(fileName + "_2" + fileExt) cam.close() cv2.destroyAllWindows()
def main(joints_file, vid_file, save_dir): poses = np.array( [numerate_coords(row) for row in (open(joints_file)).readlines()]) downsample_fact = 6 difficult_frames = [] for p, pose in enumerate(poses): if p % downsample_fact == 0 and ( len(difficult_frames) == 0 or (p - difficult_frames[-1]) > 30) and np.any(pose[:, 2] < 0.1): difficult_frames.append(p) vid_name = os.path.split(vid_file)[-1].split('.')[0] vid_file = my_video_capture(vid_file, 30 / downsample_fact) for f in difficult_frames: if f % downsample_fact == 0: vid_file.forward_to(f / downsample_fact) img = vid_file.read() cv2.imwrite(os.path.join(save_dir, "%s_%i.jpg" % (vid_name, f)), img)
def main(mv_file, vid_file, save_dir, offset): mv_file = pickle.load(open(mv_file)) if np.sum(mv_file[np.where(mv_file > 0)]) < (0.5 * 3000): #Very little going on in this file print vid_file return vid_name = os.path.split(vid_file)[-1].split('.')[0] vid_file = my_video_capture(vid_file, 30) left_arm_mvmt = np.sum(mv_file[:, (2, 4, 6)], axis=1) right_arm_mvmt = np.sum(mv_file[:, (1, 3, 5)], axis=1) head_mvmt = mv_file[:, 0] train_dir = os.path.join(save_dir, "train") test_dir = os.path.join(save_dir, "test") if not os.path.exists(train_dir): os.makedirs(train_dir) if not os.path.exists(test_dir): os.makedirs(test_dir) if not os.path.exists(os.path.join(train_dir, "l_arm_1")): os.makedirs(os.path.join(train_dir, "head_0")) os.makedirs(os.path.join(train_dir, "head_1")) os.makedirs(os.path.join(train_dir, "r_arm_0")) os.makedirs(os.path.join(train_dir, "r_arm_1")) os.makedirs(os.path.join(train_dir, "l_arm_0")) os.makedirs(os.path.join(train_dir, "l_arm_1")) os.makedirs(os.path.join(train_dir, "mv_1")) os.makedirs(os.path.join(train_dir, "mv_0")) if not os.path.exists(os.path.join(test_dir, "l_arm_1")): os.makedirs(os.path.join(test_dir, "head_0")) os.makedirs(os.path.join(test_dir, "head_1")) os.makedirs(os.path.join(test_dir, "r_arm_0")) os.makedirs(os.path.join(test_dir, "r_arm_1")) os.makedirs(os.path.join(test_dir, "l_arm_0")) os.makedirs(os.path.join(test_dir, "l_arm_1")) os.makedirs(os.path.join(test_dir, "mv_1")) os.makedirs(os.path.join(test_dir, "mv_0")) if int(day) == 9: cur_dir = test_dir else: cur_dir = train_dir for f in range(offset + 1 + 15, len(mv_file), 10): vid_file.forward_to(f - offset) img = vid_file.read() if np.mean(left_arm_mvmt[f:f + 5]) > 2: cv2.imwrite( os.path.join(cur_dir, "l_arm_1", "%s_%i.png" % (vid_name, f - offset)), img) cv2.imwrite( os.path.join(cur_dir, "mv_1", "%s_%i.png" % (vid_name, f - offset)), img) if np.mean(right_arm_mvmt[f:f + 5]) > 2: cv2.imwrite( os.path.join(cur_dir, "r_arm_1", "%s_%i.png" % (vid_name, f - offset)), img) cv2.imwrite( os.path.join(cur_dir, "mv_1", "%s_%i.png" % (vid_name, f - offset)), img) if np.mean(head_mvmt[f:f + 5]) > 1: cv2.imwrite( os.path.join(cur_dir, "head_1", "%s_%i.png" % (vid_name, f - offset)), img) cv2.imwrite( os.path.join(cur_dir, "mv_1", "%s_%i.png" % (vid_name, f - offset)), img) vid_file.rewind() for f in range(offset + 1 + 15, len(mv_file), 60): vid_file.forward_to(f - offset) img = vid_file.read() flag = 0 if np.all(left_arm_mvmt[f:f + 5] >= 0) and np.mean( left_arm_mvmt[f:f + 5]) < 1: cv2.imwrite( os.path.join(cur_dir, "l_arm_0", "%s_%i.png" % (vid_name, f - offset)), img) flag += 1 if np.all(right_arm_mvmt[f:f + 5] >= 0) and np.mean( right_arm_mvmt[f:f + 5]) < 1: cv2.imwrite( os.path.join(cur_dir, "r_arm_0", "%s_%i.png" % (vid_name, f - offset)), img) flag += 1 if np.all( head_mvmt[f:f + 5] >= 0) and np.mean(head_mvmt[f:f + 5]) < 0.5: cv2.imwrite( os.path.join(cur_dir, "head_0", "%s_%i.png" % (vid_name, f - offset)), img) flag += 1 #print (np.mean(left_arm_mvmt[f:f + 5]), np.mean(right_arm_mvmt[f:f + 5]), np.mean(head_mvmt[f:f + 5])) if flag == 3: cv2.imwrite( os.path.join(cur_dir, "mv_0", "%s_%i.png" % (vid_name, f - offset)), img)
def main(mv_file, vid_file, save_dir, offset): mv_file = pickle.load(open(mv_file)) if np.sum(mv_file[np.where(mv_file>0)])<0.5*3000: #Very little going on in this file print vid_file return vid_name = os.path.split(vid_file)[-1].split('.')[0] vid_file = my_video_capture(vid_file, 30) left_arm_mvmt = np.sum(mv_file[:,(2,4,6)], axis=1) right_arm_mvmt = np.sum(mv_file[:,(1,3,5)], axis=1) head_mvmt = mv_file[:,0] train_dir = os.path.join(save_dir, "train") test_dir = os.path.join(save_dir, "test") if not os.path.exists(train_dir): os.makedirs(train_dir) if not os.path.exists(test_dir): os.makedirs(test_dir) if not os.path.exists(os.path.join(train_dir, "l_arm_1")): os.makedirs(os.path.join(train_dir,"head_0")) os.makedirs(os.path.join(train_dir,"head_1")) os.makedirs(os.path.join(train_dir,"r_arm_0")) os.makedirs(os.path.join(train_dir,"r_arm_1")) os.makedirs(os.path.join(train_dir,"l_arm_0")) os.makedirs(os.path.join(train_dir,"l_arm_1")) os.makedirs(os.path.join(train_dir,"mv_1")) os.makedirs(os.path.join(train_dir,"mv_0")) if not os.path.exists(os.path.join(test_dir,"l_arm_1")): os.makedirs(os.path.join(test_dir,"head_0")) os.makedirs(os.path.join(test_dir,"head_1")) os.makedirs(os.path.join(test_dir,"r_arm_0")) os.makedirs(os.path.join(test_dir,"r_arm_1")) os.makedirs(os.path.join(test_dir,"l_arm_0")) os.makedirs(os.path.join(test_dir,"l_arm_1")) os.makedirs(os.path.join(test_dir,"mv_1")) os.makedirs(os.path.join(test_dir,"mv_0")) if np.random.randint(100) < 75: cur_dir = train_dir else: cur_dir = test_dir for f in range(offset+1,len(mv_file), 10): vid_file.forward_to(f - offset) img = vid_file.read() if np.mean(left_arm_mvmt[f:f+5]>5): #cv2.imwrite(os.path.join(cur_dir, "l_arm_1", "%s_%i.png" %(vid_name,f - offset)), img) cv2.imwrite(os.path.join(cur_dir, "mv_1", "%s_%i.png" % (vid_name, f - offset)), img) elif np.mean(right_arm_mvmt[f:f+5]>5): #cv2.imwrite(os.path.join(cur_dir, "r_arm_1", "%s_%i.png" % (vid_name, f - offset)), img) cv2.imwrite(os.path.join(cur_dir, "mv_1", "%s_%i.png" % (vid_name, f - offset)), img) elif np.mean(head_mvmt[f:f+5]>3): #cv2.imwrite(os.path.join(cur_dir, "head_1", "%s_%i.png" % (vid_name, f - offset)), img) cv2.imwrite(os.path.join(cur_dir, "mv_1", "%s_%i.png" % (vid_name, f - offset)), img) vid_file.rewind() for f in range(offset+1, len(mv_file), 60): vid_file.forward_to(f-offset) img = vid_file.read() flag = 0 if np.all(left_arm_mvmt[f:f+5] >= 0) and np.mean(left_arm_mvmt[f:f + 5]) < 1: #cv2.imwrite(os.path.join(cur_dir, "l_arm_0", "%s_%i.png" % (vid_name, f - offset)), img) flag+=1 if np.all(right_arm_mvmt[f:f + 5] >= 0) and np.mean(right_arm_mvmt[f:f + 5]) < 1: #cv2.imwrite(os.path.join(cur_dir, "r_arm_0", "%s_%i.png" % (vid_name, f - offset)), img) flag+=1 if np.all(head_mvmt[f:f + 5] >= 0) and np.mean(head_mvmt[f:f + 5]) < 1: #cv2.imwrite(os.path.join(cur_dir, "head_0", "%s_%i.png" % (vid_name, f - offset)), img) flag+=1 if flag==3: cv2.imwrite(os.path.join(cur_dir, "mv_0", "%s_%i.png" % (vid_name, f - offset)), img)