def plot_field(): field_list = get_field_points() for i in range(len(field_list)): if (i <= 4): draw_box(field_list[i]) elif (i == 5): draw_line(field_list[i]) draw_lefthalf_circle() draw_righthalf_circle() draw_middlecircle() # get camera position cam = cam_utils.Camera('camera0', db[0].calib['00118']['A'], db[0].calib['00118']['R'], db[0].calib['00118']['T'], db[0].shape[0], db[0].shape[1]) # initialize figure fig = plt.figure() ax = fig.add_subplot(111, projection='3d') # camera0 x = cam.get_position().item(0, 0) y = cam.get_position().item(1, 0) z = cam.get_position().item(2, 0) ax.scatter(x, y, z, label='camera0', marker='o') # demo field for x-y plane field = io.imread('./demo/data/demo_field.png')
def dump_video(self, vidtype, scale=1, mot_tracks=None, one_color=True): if vidtype not in ['calib', 'poses', 'detections', 'tracks', 'mask']: raise Exception('Uknown video format') if vidtype == 'tracks' and mot_tracks is None: raise Exception('No MOT tracks provided') glog.info('Dumping {0} video'.format(vidtype)) fourcc = cv2.VideoWriter_fourcc(*'mp4v') # MP4V out_file = join(self.path_to_dataset, '{0}.mp4'.format(vidtype)) # 25 FPS out = cv2.VideoWriter(out_file, fourcc, 25.0, (self.shape[1] // scale, self.shape[0] // scale)) font = cv2.FONT_HERSHEY_SIMPLEX cmap = matplotlib.cm.get_cmap('hsv') if mot_tracks is not None: n_tracks = max(np.unique(mot_tracks[:, 1])) for i, basename in enumerate(tqdm(self.frame_basenames)): img = self.get_frame(i, dtype=np.uint8) if vidtype == 'poses': # Pose poses = self.poses[basename] draw_utils.draw_skeleton_on_image(img, poses, cmap, one_color=one_color) if vidtype == 'calib': # Calib cam = cam_utils.Camera('tmp', self.calib[basename]['A'], self.calib[basename] ['R'], self.calib[basename]['T'], self.shape[0], self.shape[1]) canvas, mask = draw_utils.draw_field(cam) canvas = cv2.dilate(canvas.astype(np.uint8), np.ones( (15, 15), dtype=np.uint8)).astype(float) img = img * (1 - canvas)[:, :, None] + np.dstack((canvas * 255, np.zeros_like(canvas), np.zeros_like(canvas))) elif vidtype == 'detections': # Detection bbox = self.bbox[basename].astype(np.int32) if self.ball[basename] is not None: ball = self.ball[basename].astype(np.int32) else: ball = np.zeros((0, 4), dtype=np.int32) for j in range(bbox.shape[0]): cv2.rectangle(img, (bbox[j, 0], bbox[j, 1]), (bbox[j, 2], bbox[j, 3]), (255, 0, 0), 10) for j in range(ball.shape[0]): cv2.rectangle(img, (ball[j, 0], ball[j, 1]), (ball[j, 2], ball[j, 3]), (0, 255, 0), 10) elif vidtype == 'tracks': # Tracks cur_id = mot_tracks[:, 0] - 1 == i current_boxes = mot_tracks[cur_id, :] for j in range(current_boxes.shape[0]): track_id, x, y, w, h = current_boxes[j, 1:6] clr = cmap(track_id / float(n_tracks)) cv2.rectangle(img, (int(x), int(y)), (int(x + w), int(y + h)), (clr[0] * 255, clr[1] * 255, clr[2] * 255), 10) cv2.putText(img, str(int(track_id)), (int(x), int(y)), font, 2, (255, 255, 255), 2, cv2.LINE_AA) elif vidtype == 'mask': # Mask mask = self.get_mask_from_detectron(i)*255 img = np.dstack((mask, mask, mask)) img = cv2.resize(img, (self.shape[1] // scale, self.shape[0] // scale)) out.write(np.uint8(img[:, :, (2, 1, 0)])) # Release everything if job is finished out.release() cv2.destroyAllWindows()
def preprocess_save_to_npy(self, trajectory): ''' Input shape = (n_trajectory, seq_len, features) ===> e.g. (1, ) Features cols = (x, y, z, u_c1, v_c1, u_c2, v_c2) ''' print("[#] Saving 3D tracking by triangulation") calibs = self.load_calibs(suffix='manual') cam1 = calibs[self.video_name[0]] cam1_obj = cam_utils.Camera(name='cam1', A=np.array(cam1['A']), R=np.array(cam1['R']), T=np.array(cam1['T']), h=cam1['H'], w=cam1['W']) cam2 = calibs[self.video_name[1]] cam2_obj = cam_utils.Camera(name='cam2', A=np.array(cam2['A']), R=np.array(cam2['R']), T=np.array(cam2['T']), h=cam2['H'], w=cam2['W']) cam_list = [cam1_obj, cam2_obj] trajectory_list = [[], []] for i in range(trajectory.shape[0]): # Unity convention trajectory[i][:, [2]] *= -1 x, y, z = trajectory[i][:, [0]], trajectory[i][:, [1]], trajectory[ i][:, [2]] for j, cam in enumerate(cam_list): if not self.args.use_tracking: # Do a projection if tracking is not used as an input trajectory_npy = [] u, v, d = transf_utils.project( cam=cam, pts=trajectory[i][..., [0, 1, 2]]) else: trajectory_cam = transf_utils.transform_3d( m=cam.to_unity()[0], pts=trajectory[i][..., [0, 1, 2]], inv=False) d = -trajectory_cam[:, [ 2 ]] # Depth direction should be positive for unity if j == 0: u, v = trajectory[i][:, [3]], trajectory[i][:, [4]] if j == 1: u, v = trajectory[i][:, [5]], trajectory[i][:, [6]] # For raw tracking, unity coordinates start from (0, 0) at the bottom left, so I need to remove with the height v = cam.height - v dummy_eot = np.zeros(shape=(x.shape)) saving_trajectory = np.concatenate( (x, y, z, u, v, d, dummy_eot), axis=1) saving_trajectory = np.vstack( (saving_trajectory[0, :], np.diff(saving_trajectory, axis=0))) trajectory_list[j].append(saving_trajectory) for i, trajectory_cam in enumerate(trajectory_list): trajectory_cam = np.array(trajectory_cam) print("Camera {} : {}".format(i + 1, trajectory_cam.shape)) # np.save(trajectory_cam) # .npy format (For extracting 3d trajectory) self.intiailize_folder(path="{}/{}".format( self.path_to_calibration, 'preprocessed_npy')) preprocessed_npy = join(self.path_to_calibration, 'preprocessed_npy', 'trajectory_{}'.format(self.video_name[i])) preprocessed_npy = join( self.path_to_calibration, 'preprocessed_npy', 'MixedTrajectory_Trial{}'.format(self.video_name[i][-1])) np.save(preprocessed_npy, trajectory_cam)
data = json.load(f) db = soccer3d.YoutubeVideo(opt.path_to_data) db.digest_metadata() db.refine_poses(keypoint_thresh=7, score_thresh=0.4, neck_thresh=0.4) file_utils.mkdir(join(db.path_to_dataset, 'scene3d')) for sel_frame in tqdm(range(db.n_frames)): img = db.get_frame(sel_frame) basename = db.frame_basenames[sel_frame] cam_data = db.calib[basename] cam = cam_utils.Camera(basename, cam_data['A'], cam_data['R'], cam_data['T'], db.shape[0], db.shape[1]) player_list = data[basename] frame_mesh_points = np.zeros((0, 3)) frame_mesh_faces = np.zeros((0, 3)) uvs_atlas = [] textures_atlas = [] cnt = 0 for p in range(len(player_list)): mesh = om.PolyMesh() ply_name = join(opt.path_to_data, 'players', 'meshes', player_list[p]['mesh']+'.ply') vertex_data, face_data, _, _ = io.read_ply(ply_name)
opt, _ = parser.parse_known_args() with open(join(opt.path_to_data, 'players', 'metadata', 'position.json')) as f: data = json.load(f) db = soccer3d.YoutubeVideo(opt.path_to_data) db.digest_metadata() img = db.get_frame(0) mask = db.get_mask_from_detectron(0) cam_npy = db.calib[db.frame_basenames[0]] cam = cam_utils.Camera('tmp', cam_npy['A'], cam_npy['R'], cam_npy['T'], db.shape[0], db.shape[1]) mask = cv2.dilate(mask, np.ones((9, 9), np.int8), iterations=1) img = cv2.inpaint((img[:, :, (2, 1, 0)]*255).astype(np.uint8), (mask*255).astype(np.uint8), 3, cv2.INPAINT_TELEA)[:, :, (2, 1, 0)]/255. W, H = 104.73, 67.74 # a X-------------------------X b # | | # | | # d X-------------------------X c # Whole field p3 = np.array([[-W/2., 0, H/2], [W/2., 0, H/2], [W/2., 0, -H/2] , [-W/2., 0, -H/2]]) p2, _ = cam.project(p3)
# db_Left = soccer.SoccerVideo(os.path.join(opt.path_to_data, 'Left')) # db_Right = soccer.SoccerVideo(os.path.join(opt.path_to_data, 'Right')) db_K9 = soccer.SoccerVideo(os.path.join(opt.path_to_data, 'K9')) db_K1.digest_metadata() db_K8.digest_metadata() # db_Left.digest_metadata() # db_Right.digest_metadata() db_K9.digest_metadata() # plot the field data = [] # K1: cam = cam_utils.Camera("K1", db_K1.calib[db_K1.frame_basenames[0]]['A'], db_K1.calib[db_K1.frame_basenames[0]]['R'], db_K1.calib[db_K1.frame_basenames[0]]['T'], db_K1.shape[0], db_K1.shape[1]) vec = np.array([0, 0, 1]) dir1 = cam.R.T.dot(vec.T) dir1 *= 10 # cam.A[0][0] # 10 good value dir1 = dir1 + [ cam.get_position().item(0, 0), cam.get_position().item(1, 0), cam.get_position().item(2, 0) ] cam_vec1 = np.array([ cam.get_position().item(0, 0), cam.get_position().item(1, 0), cam.get_position().item(2, 0) ]) data.append(
#!/usr/bin/env python from utils import camera, bash, events from itertools import cycle import time import os import sys import readline import random import serial cam = camera.Camera() shell = bash.Bash() events = events.Events() present = False responded = False found = 0 lost = 0 welcomes = cycle(['lazybones', 'alive', 'buddy']) insults = cycle(['facebook', 'homework', 'notpopular']) events = cycle([ 'http://portugal2013sf-es2.eventbrite.com', 'http://gatsbypremiere-es2.eventbrite.com', 'http://cfadrinkupsf-es2.eventbrite.com', 'http://tchotour-es2.eventbrite.com', 'http://unreasonabledrinkssf-es2.eventbrite.com' ])
def calibrate_by_click(img, mask, edge_sfactor=0.5, field_img_path='./demo/data/field.png'): h, w = img.shape[0:2] points2d, points3d = _set_correspondences(img, field_img_path=field_img_path) # ------------------------------------------------------------------------------------------------------------------ # OpenCV initial calibration fx, fy = cam_utils.grid_search_focal_length(points3d, points2d, h, w, same_f=True) A = cam_utils.intrinsic_matrix_from_focal_length(fx, fy, h, w) points_3d_cv = points3d[:, np.newaxis, :].astype(np.float32) points_2d_cv = points2d[:, np.newaxis, :].astype(np.float32) _, rvec, tvec, _ = cv2.solvePnPRansac(points_3d_cv, points_2d_cv, A, None) # 旋轉向量rvec和平移向量tvec得到變換矩陣 rvec, tvec = np.squeeze(rvec), np.squeeze(tvec) R, _ = cv2.Rodrigues(rvec) T = np.array([tvec]).T cam_cv = cam_utils.Camera('tmp', A, R, T, h, w) # ------------------------------------------------------------------------------------------------------------------ # Photometric refinement A__, R__, T__, field3d = calibrate_from_initialization( img, mask, A, R, T, edge_sfactor) cam = cam_utils.Camera('tmp', A__, R__, T__, h, w) # Sanity check, project the soccer field points p2_cv, _ = cam.project(field3d) p2_cv, _ = cam_utils.inside_frame(p2_cv, cam.height, cam.width) p2_opt, _ = cam_cv.project(field3d) p2_opt, valid_id = cam_utils.inside_frame(p2_opt, cam_cv.height, cam_cv.width) A_out, R_out, T_out = [None], [None], [None] class Index(object): def save_opt(self, event): A_out.append(cam.A) R_out.append(cam.R) T_out.append(cam.T) plt.close() def save_pnp(self, event): A_out.append(cam_cv.A) R_out.append(cam_cv.R) T_out.append(cam_cv.T) plt.close() def discard(self, event): plt.close() fig, ax = plt.subplots(1, 2) io.imshow(img, ax=ax[0], points=p2_opt) io.imshow(img, ax=ax[1], points=p2_cv) callback = Index() axdisc = plt.axes([0.6, 0.05, 0.1, 0.075]) axcv = plt.axes([0.7, 0.05, 0.1, 0.075]) axopt = plt.axes([0.81, 0.05, 0.1, 0.075]) bnext = Button(axopt, 'Save opt') bnext.on_clicked(callback.save_opt) bprev = Button(axdisc, 'Discard') bprev.on_clicked(callback.discard) bpcv = Button(axcv, 'Save cv') bpcv.on_clicked(callback.save_pnp) plt.show() return A_out[-1], R_out[-1], T_out[-1]