Skip to content

Free Motion Capture for Everyone πŸ’€βœ¨

TypeScript

saving calibration changes

aaroncherian0d27396c1127
diff --git a/freemocap/calibrate.py b/freemocap/calibrate.py
index a0f27bce..b34a9ba4 100644
--- a/freemocap/calibrate.py
+++ b/freemocap/calibrate.py
@@ -3,6 +3,7 @@ import copy
import json
import pickle
import glob
+from pathlib import Path
from aniposelib.cameras import CameraGroup
from aniposelib.utils import load_pose2d_fnames
@@ -32,46 +33,81 @@ def CalibrateCaptureVolume(session,board, calVideoFrameLength = 1):
session.dataArrayPath.mkdir(exist_ok = True)
calibrationVideoPath = session.calVidPath
- if type(calVideoFrameLength)==int or type(calVideoFrameLength)==float:
- if calVideoFrameLength < 0: # if '-1' use the whole video
- cal_vid_frame_range = [0, session.numFrames]
- calibrationVideoPath = session.calVidPath
- else:
- if calVideoFrameLength>0 and calVideoFrameLength<=1: #if between 0 and 1, use as a percentage of the total video length
- cal_vid_frame_range = [0, round(calVideoFrameLength * session.numFrames)]
- else: #otherwise, just use the input value as the number of frames to use
- cal_vid_frame_range = [0, calVideoFrameLength]
- elif type(calVideoFrameLength)==list:
- if len(calVideoFrameLength) ==2:
- cal_vid_frame_range = calVideoFrameLength
- createCalibrationVideos(session, cal_vid_frame_range)
+
+ if session.use_saved_calibration:
+ saved_calibration_folder = session.freemocap_module_path/'fmc_calibration'
+ #saved_calibration_file_name = os.listdir(saved_calibration_folder)[0]
+ #saved_calibration_file_path = os.path.join(saved_calibration_folder, saved_calibration_file_name)
+ #saved_calibration_file_path = list(Path(saved_calibration_folder).glob('*.toml'))
+ saved_calibration_file_path = saved_calibration_folder/'previous_calibration.toml'
+
+
+ cam_names = [i+1 for i in range(session.numCams)]
+ cgroup = fmc_anipose.CameraGroup.from_names( cam_names, fisheye=True)
+
+
+ cgroup = CameraGroup.load(saved_calibration_file_path)
+ charuco_nCams_nFrames_nImgPts_XY = np.load(saved_calibration_folder/'charuco_2d_points.npy')
+
+
+ session.cameraCalFilePath = session.sessionPath /"{}_calibration.toml".format(session.sessionID)
+ cgroup.dump(session.cameraCalFilePath)
+ session.cgroup = cgroup
+ camera_calibration_info_dict = cgroup.get_dicts()
+ camera_calibration_pickle_path = session.sessionPath / "{}_calibration.pickle".format(session.sessionID)
+
+ with open(str(camera_calibration_pickle_path), 'wb') as pickle_file:
+ pickle.dump(camera_calibration_info_dict, pickle_file)
+
+ else:
+
+ if type(calVideoFrameLength)==int or type(calVideoFrameLength)==float:
+ if calVideoFrameLength < 0: # if '-1' use the whole video
+ cal_vid_frame_range = [0, session.numFrames]
+ calibrationVideoPath = session.calVidPath
+ else:
+ if calVideoFrameLength>0 and calVideoFrameLength<=1: #if between 0 and 1, use as a percentage of the total video length
+ cal_vid_frame_range = [0, round(calVideoFrameLength * session.numFrames)]
+ else: #otherwise, just use the input value as the number of frames to use
+ cal_vid_frame_range = [0, calVideoFrameLength]
+ elif type(calVideoFrameLength)==list:
+ if len(calVideoFrameLength) ==2:
+ cal_vid_frame_range = calVideoFrameLength
- vidnames = []
- cam_names = []
- for count, thisVidPath in enumerate(calibrationVideoPath.glob("*.mp4"), start=1):
- vidnames.append([str(thisVidPath)])
- cam_names.append(str(count))
- session.numCams = count
- cgroup = fmc_anipose.CameraGroup.from_names( cam_names, fisheye=True ) # Looking through their code... it looks lke the 'fisheye=True' doesn't do much (see 2020-03-29 obsidian note)
+ createCalibrationVideos(session, cal_vid_frame_range)
- calibrationFile = "{}_calibration.toml".format(session.sessionID)
+ vidnames = []
+ cam_names = []
+
+ for count, thisVidPath in enumerate(calibrationVideoPath.glob("*.mp4"), start=1):
+ vidnames.append([str(thisVidPath)])
+ cam_names.append(str(count))
+ session.numCams = count
+
+
+ cgroup = fmc_anipose.CameraGroup.from_names( cam_names, fisheye=True) # Looking through their code... it looks lke the 'fisheye=True' doesn't do much (see 2020-03-29 obsidian note)
- session.cameraCalFilePath = session.sessionPath / calibrationFile
+ calibrationFile = "{}_calibration.toml".format(session.sessionID)
- error,charuco_data, charuco_frames = cgroup.calibrate_videos(vidnames, board)
-
- cgroup.dump(session.cameraCalFilePath)
+ session.cameraCalFilePath = session.sessionPath / calibrationFile
- camera_calibration_info_dict = cgroup.get_dicts()
- camera_calibration_pickle_path = session.sessionPath / "{}_calibration.pickle".format(session.sessionID)
+ error,charuco_data, charuco_frames = cgroup.calibrate_videos(vidnames, board)
- with open(str(camera_calibration_pickle_path), 'wb') as pickle_file:
- pickle.dump(camera_calibration_info_dict, pickle_file)
+ cgroup.dump(session.cameraCalFilePath)
+
+ camera_calibration_info_dict = cgroup.get_dicts()
+ camera_calibration_pickle_path = session.sessionPath / "{}_calibration.pickle".format(session.sessionID)
+
+ with open(str(camera_calibration_pickle_path), 'wb') as pickle_file:
+ pickle.dump(camera_calibration_info_dict, pickle_file)
+
+
+
# camera_calibration_json_filename = "{}_calibration.json".format(session.sessionID)
# camera_calibration_json_path = session.sessionPath / camera_calibration_json_filename
@@ -83,25 +119,37 @@ def CalibrateCaptureVolume(session,board, calVideoFrameLength = 1):
# json.dumps(this_cam_dict, outfile)
- session.cgroup = cgroup
- n_frames = cal_vid_frame_range[1]-cal_vid_frame_range[0]
- startframe = 0
- n_trackedPoints = 24
- charuco_nCams_nFrames_nImgPts_XY = np.empty([session.numCams, n_frames, n_trackedPoints, 2])
- charuco_nCams_nFrames_nImgPts_XY[:] = np.nan
+ session.cgroup = cgroup
+ n_frames = cal_vid_frame_range[1]-cal_vid_frame_range[0]
+ startframe = 0
+ n_trackedPoints = 24
- for cam in range(session.numCams):
- for charCount, thisCharFrame in enumerate(charuco_frames):
- try:
- charuco_nCams_nFrames_nImgPts_XY[cam, thisCharFrame, :,:] = np.squeeze(charuco_data[charCount][cam]["filled"])
- except:
- # print("failed frame:", frame)
- continue
-
+ charuco_nCams_nFrames_nImgPts_XY = np.empty([session.numCams, n_frames, n_trackedPoints, 2])
+ charuco_nCams_nFrames_nImgPts_XY[:] = np.nan
+
+ for cam in range(session.numCams):
+ for charCount, thisCharFrame in enumerate(charuco_frames):
+ try:
+ charuco_nCams_nFrames_nImgPts_XY[cam, thisCharFrame, :,:] = np.squeeze(charuco_data[charCount][cam]["filled"])
+ except:
+ # print("failed frame:", frame)
+ continue
+
+ path_to_save_calibration_data = session.freemocap_module_path/'fmc_calibration'/'previous_calibration.toml'
+ path_to_save_charuco_data = session.freemocap_module_path/'fmc_calibration'/'charuco_2d_points.npy'
+ cgroup.dump(path_to_save_calibration_data)
+ np.save(path_to_save_charuco_data,charuco_nCams_nFrames_nImgPts_XY)
+
+
charuco2d_filename = session.dataArrayPath/'charuco_2d_points.npy'
np.save(charuco2d_filename,charuco_nCams_nFrames_nImgPts_XY)
+
+
+
+
+
session.charuco_nCams_nFrames_nImgPts_XY = charuco_nCams_nFrames_nImgPts_XY
charuco_fr_mar_xyz, charuco_reprojErr= reconstruct3D.reconstruct3D(
diff --git a/freemocap/fmc_calibration/previous_calibration.toml b/freemocap/fmc_calibration/previous_calibration.toml
new file mode 100644
index 00000000..1de0b21c
--- /dev/null
+++ b/freemocap/fmc_calibration/previous_calibration.toml
@@ -0,0 +1,19 @@
+[cam_0]
+name = "1"
+size = [ 640, 480,]
+matrix = [ [ 631.4624049230893, 0.0, 319.5,], [ 0.0, 631.4624049230893, 239.5,], [ 0.0, 0.0, 1.0,],]
+distortions = [ 0.11452865943070659, 0.0, 0.0, 0.0,]
+rotation = [ -0.004344615178522102, 0.001908507644830412, 0.01413719673449806,]
+translation = [ -3.8152768690487733, -17.987083990695233, -1488.3877421850593,]
+fisheye = true
+
+[cam_1]
+name = "2"
+size = [ 480, 640,]
+matrix = [ [ 577.8765101201407, 0.0, 239.5,], [ 0.0, 577.8765101201407, 319.5,], [ 0.0, 0.0, 1.0,],]
+distortions = [ 0.09435884867237307, 0.0, 0.0, 0.0,]
+rotation = [ -0.09698179190540875, -0.09779468914676767, -0.062021333659871926,]
+translation = [ 298.24509046374243, -418.2189219638704, -2106.5937173440534,]
+fisheye = true
+
+[metadata]
diff --git a/freemocap/fmc_openpose.py b/freemocap/fmc_openpose.py
index 429838d7..da2994b4 100644
--- a/freemocap/fmc_openpose.py
+++ b/freemocap/fmc_openpose.py
@@ -139,7 +139,8 @@ def parseOpenPose(session):
handRData = np.array(thisJsonData[0]['hand_right_keypoints_2d'])
handLData = np.array(thisJsonData[0]['hand_left_keypoints_2d'])
faceData = np.array(thisJsonData[0]['face_keypoints_2d'])
- thisFrameRow = np.hstack((bodyData,handRData, handLData, faceData)) #horizontally concatenate these arrays
+ thisFrameRow = np.hstack((bodyData,handRData, handLData, faceData)) #horizontally concatenate these arrays
+ f=2
else: #if this json is empty, just stuff it fulla NaNs
thisFrameRow = np.empty([headerLength])
thisFrameRow.fill(np.nan)
diff --git a/freemocap/fmc_runme.py b/freemocap/fmc_runme.py
index bddb358c..2adbdd7e 100644
--- a/freemocap/fmc_runme.py
+++ b/freemocap/fmc_runme.py
@@ -62,6 +62,7 @@ def RunMe(sessionID=None,
resetBlenderExe = False,
get_synced_unix_timestamps = True,
good_clean_frame_number = 0,
+ use_saved_calibration = False
):
"""
Starts the freemocap pipeline based on either user-input values, or default values. Creates a new session class instance (called sesh)
@@ -84,8 +85,13 @@ def RunMe(sessionID=None,
sesh.dataFolderName = recordingconfig.dataFolder
sesh.startFrame = startFrame
sesh.get_synced_unix_timestamps = get_synced_unix_timestamps
+ sesh.use_saved_calibration = use_saved_calibration
# %% Startup
+ sesh.freemocap_module_path = Path(__file__).parent
+
+
+
startup.get_user_preferences(sesh,stage)
if sesh.useDLC and stage<5:
@@ -102,9 +108,10 @@ def RunMe(sessionID=None,
console.rule()
print('Running ' + str(sesh.sessionID) + ' from ' + str(sesh.dataFolderPath))
console.rule()
+
if useBlender == True:
- here = Path(__file__).parent
- subprocessPath = here/'fmc_blender.py'
+
+ subprocessPath = sesh.freemocap_module_path/'fmc_blender.py'
blenderPath = startup.get_blender_path(sesh,resetBlenderExe)
diff --git a/freemocap_runme_script.py b/freemocap_runme_script.py
index c6d8ebbf..cd950a32 100644
--- a/freemocap_runme_script.py
+++ b/freemocap_runme_script.py
@@ -1,2 +1,3 @@
+from matplotlib.style import use
import freemocap as fmc
-fmc.RunMe()
+fmc.RunMe(charucoSquareSize=73, use_saved_calibration=True)
diff --git a/runme_FreeMoCap.py b/runme_FreeMoCap.py
index 694ea3e0..5269c96b 100644
--- a/runme_FreeMoCap.py
+++ b/runme_FreeMoCap.py
@@ -1,4 +1,4 @@
import freemocap as fmc
-fmc.RunMe()
+fmc.RunMe(charucoSquareSize = 73, stage = 3, sessionID = 'sesh_2022-04-27_15_35_15')