Free Motion Capture for Everyone πβ¨
TypeScript
pep8 black reformat
jon matthisa909ac15f4e1
diff --git a/docs/source/conf.py b/docs/source/conf.pyindex 8af18068..5ff90304 100644--- a/docs/source/conf.py+++ b/docs/source/conf.py@@ -32,17 +32,17 @@# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom# ones.extensions = [- 'sphinx.ext.autodoc',- 'sphinx.ext.autosummary',- 'sphinx.ext.githubpages',- 'sphinx.ext.intersphinx',- 'sphinx.ext.mathjax',- 'sphinx.ext.viewcode',- 'IPython.sphinxext.ipython_directive',- 'IPython.sphinxext.ipython_console_highlighting',- 'matplotlib.sphinxext.plot_directive',- 'numpydoc',- 'sphinx_copybutton',+ "sphinx.ext.autodoc",+ "sphinx.ext.autosummary",+ "sphinx.ext.githubpages",+ "sphinx.ext.intersphinx",+ "sphinx.ext.mathjax",+ "sphinx.ext.viewcode",+ "IPython.sphinxext.ipython_directive",+ "IPython.sphinxext.ipython_console_highlighting",+ "matplotlib.sphinxext.plot_directive",+ "numpydoc",+ "sphinx_copybutton",]# Configuration options for plot_directive. See:@@ -55,27 +55,28 @@ autosummary_generate = Truenumpydoc_show_class_members = False# Add any paths that contain templates here, relative to this directory.-templates_path = ['_templates']+templates_path = ["_templates"]# The suffix(es) of source filenames.# You can specify multiple suffix as a list of string:## source_suffix = ['.rst', '.md']-source_suffix = '.rst'+source_suffix = ".rst"# The master toctree document.-master_doc = 'index'+master_doc = "index"# General information about the project.-project = 'The FreeMoCap Project'-copyright = '2021, Jonathan Samir Matthis'-author = 'Jonathan Samir Matthis'+project = "The FreeMoCap Project"+copyright = "2021, Jonathan Samir Matthis"+author = "Jonathan Samir Matthis"# The version info for the project you're documenting, acts as replacement for# |version| and |release|, also used in various other places throughout the# built documents.#import freemocap+# The short X.Y version.version = freemocap.__version__# The full version, including alpha/beta/rc tags.@@ -94,7 +95,7 @@ language = Noneexclude_patterns = []# The name of the Pygments (syntax highlighting) style to use.-pygments_style = 'sphinx'+pygments_style = "sphinx"# If true, `todo` and `todoList` produce output, else they produce nothing.todo_include_todos = False@@ -105,8 +106,9 @@ todo_include_todos = False# The theme to use for HTML and HTML Help pages. See the documentation for# a list of builtin themes.#-html_theme = 'sphinx_rtd_theme'+html_theme = "sphinx_rtd_theme"import sphinx_rtd_theme+html_theme_path = [sphinx_rtd_theme.get_html_theme_path()]# Theme options are theme-specific and customize the look and feel of a theme@@ -118,7 +120,7 @@ html_theme_path = [sphinx_rtd_theme.get_html_theme_path()]# Add any paths that contain custom static files (such as style sheets) here,# relative to this directory. They are copied after the builtin static files,# so a file named "default.css" will overwrite the builtin "default.css".-html_static_path = ['_static']+html_static_path = ["_static"]# Custom sidebar templates, must be a dictionary that maps document names# to template names.@@ -126,9 +128,9 @@ html_static_path = ['_static']# This is required for the alabaster theme# refs: http://alabaster.readthedocs.io/en/latest/installation.html#sidebarshtml_sidebars = {- '**': [- 'relations.html', # needs 'show_related': True theme option to display- 'searchbox.html',+ "**": [+ "relations.html", # needs 'show_related': True theme option to display+ "searchbox.html",]}@@ -136,7 +138,7 @@ html_sidebars = {# -- Options for HTMLHelp output ------------------------------------------# Output file base name for HTML help builder.-htmlhelp_basename = 'freemocap'+htmlhelp_basename = "freemocap"# -- Options for LaTeX output ---------------------------------------------@@ -145,15 +147,12 @@ latex_elements = {# The paper size ('letterpaper' or 'a4paper').## 'papersize': 'letterpaper',-# The font size ('10pt', '11pt' or '12pt').## 'pointsize': '10pt',-# Additional stuff for the LaTeX preamble.## 'preamble': '',-# Latex figure (float) alignment## 'figure_align': 'htbp',@@ -163,8 +162,13 @@ latex_elements = {# (source start file, target name, title,# author, documentclass [howto, manual, or own class]).latex_documents = [- (master_doc, 'freemocap.tex', 'The FreeMoCap Project Documentation',- 'Contributors', 'manual'),+ (+ master_doc,+ "freemocap.tex",+ "The FreeMoCap Project Documentation",+ "Contributors",+ "manual",+ ),]@@ -173,8 +177,7 @@ latex_documents = [# One entry per manual page. List of tuples# (source start file, name, description, authors, manual section).man_pages = [- (master_doc, 'freemocap', 'The FreeMoCap Project Documentation',- [author], 1)+ (master_doc, "freemocap", "The FreeMoCap Project Documentation", [author], 1)]@@ -184,19 +187,23 @@ man_pages = [# (source start file, target name, title, author,# dir menu entry, description, category)texinfo_documents = [- (master_doc, 'freemocap', 'The FreeMoCap Project Documentation',- author, 'freemocap', 'The FreeMoCap Project: A free-and-open-source, hardware-and-software-agnostic, research-grade, minimal-cost motion capture system and platform for decentralized scientific research, education, and training',- 'Miscellaneous'),+ (+ master_doc,+ "freemocap",+ "The FreeMoCap Project Documentation",+ author,+ "freemocap",+ "The FreeMoCap Project: A free-and-open-source, hardware-and-software-agnostic, research-grade, minimal-cost motion capture system and platform for decentralized scientific research, education, and training",+ "Miscellaneous",+ ),]--# Example configuration for intersphinx: refer to the Python standard library.intersphinx_mapping = {- 'python': ('https://docs.python.org/3/', None),- 'numpy': ('https://numpy.org/doc/stable/', None),- 'scipy': ('https://docs.scipy.org/doc/scipy/reference/', None),- 'pandas': ('https://pandas.pydata.org/pandas-docs/stable', None),- 'matplotlib': ('https://matplotlib.org/stable', None),+ "python": ("https://docs.python.org/3/", None),+ "numpy": ("https://numpy.org/doc/stable/", None),+ "scipy": ("https://docs.scipy.org/doc/scipy/reference/", None),+ "pandas": ("https://pandas.pydata.org/pandas-docs/stable", None),+ "matplotlib": ("https://matplotlib.org/stable", None),}diff --git a/freemocap/__init__.py b/freemocap/__init__.pyindex 74f4e668..80edaf05 100644--- a/freemocap/__init__.py+++ b/freemocap/__init__.py@@ -1,4 +1,4 @@-from ._version import get_versions-__version__ = get_versions()['version']++__version__ = get_versions()["version"]del get_versionsdiff --git a/freemocap/_version.py b/freemocap/_version.pyindex d658cdc1..3398cacf 100644--- a/freemocap/_version.py+++ b/freemocap/_version.py@@ -1,4 +1,3 @@-# This file helps to compute a version number in source trees obtained from# git-archive tarball (such as those provided by githubs download-from-tag# feature). Distribution tarballs (built by setup.py sdist) and build@@ -58,17 +57,18 @@ HANDLERS = {}def register_vcs_handler(vcs, method): # decorator"""Decorator to mark a method as the handler for a particular VCS."""+def decorate(f):"""Store f in HANDLERS[vcs][method]."""if vcs not in HANDLERS:HANDLERS[vcs] = {}HANDLERS[vcs][method] = freturn f+return decorate-def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False,- env=None):+def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False, env=None):"""Call the given command(s)."""assert isinstance(commands, list)p = None@@ -76,10 +76,13 @@ def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False,try:dispcmd = str([c] + args)# remember shell=False, so use git.cmd on windows, not just git- p = subprocess.Popen([c] + args, cwd=cwd, env=env,- stdout=subprocess.PIPE,- stderr=(subprocess.PIPE if hide_stderr- else None))+ p = subprocess.Popen(+ [c] + args,+ cwd=cwd,+ env=env,+ stdout=subprocess.PIPE,+ stderr=(subprocess.PIPE if hide_stderr else None),+ )breakexcept EnvironmentError:e = sys.exc_info()[1]@@ -116,16 +119,22 @@ def versions_from_parentdir(parentdir_prefix, root, verbose):for i in range(3):dirname = os.path.basename(root)if dirname.startswith(parentdir_prefix):- return {"version": dirname[len(parentdir_prefix):],- "full-revisionid": None,- "dirty": False, "error": None, "date": None}+ return {+ "version": dirname[len(parentdir_prefix) :],+ "full-revisionid": None,+ "dirty": False,+ "error": None,+ "date": None,+ }else:rootdirs.append(root)root = os.path.dirname(root) # up a levelif verbose:- print("Tried directories %s but none started with prefix %s" %- (str(rootdirs), parentdir_prefix))+ print(+ "Tried directories %s but none started with prefix %s"+ % (str(rootdirs), parentdir_prefix)+ )raise NotThisMethod("rootdir doesn't start with parentdir_prefix")@@ -181,7 +190,7 @@ def git_versions_from_keywords(keywords, tag_prefix, verbose):# starting in git-1.8.3, tags are listed as "tag: foo-1.0" instead of# just "foo-1.0". If we see a "tag: " prefix, prefer those.TAG = "tag: "- tags = set([r[len(TAG):] for r in refs if r.startswith(TAG)])+ tags = set([r[len(TAG) :] for r in refs if r.startswith(TAG)])if not tags:# Either we're using git < 1.8.3, or there really are no tags. We use# a heuristic: assume all version tags have a digit. The old git %d@@ -190,7 +199,7 @@ def git_versions_from_keywords(keywords, tag_prefix, verbose):# between branches and tags. By ignoring refnames without digits, we# filter out many common branch names like "release" and# "stabilization", as well as "HEAD" and "master".- tags = set([r for r in refs if re.search(r'\d', r)])+ tags = set([r for r in refs if re.search(r"\d", r)])if verbose:print("discarding '%s', no digits" % ",".join(refs - tags))if verbose:@@ -198,19 +207,26 @@ def git_versions_from_keywords(keywords, tag_prefix, verbose):for ref in sorted(tags):# sorting will prefer e.g. "2.0" over "2.0rc1"if ref.startswith(tag_prefix):- r = ref[len(tag_prefix):]+ r = ref[len(tag_prefix) :]if verbose:print("picking %s" % r)- return {"version": r,- "full-revisionid": keywords["full"].strip(),- "dirty": False, "error": None,- "date": date}+ return {+ "version": r,+ "full-revisionid": keywords["full"].strip(),+ "dirty": False,+ "error": None,+ "date": date,+ }# no suitable tags, so version is "0+unknown", but full hex is still thereif verbose:print("no suitable tags, using unknown + full revision id")- return {"version": "0+unknown",- "full-revisionid": keywords["full"].strip(),- "dirty": False, "error": "no suitable tags", "date": None}+ return {+ "version": "0+unknown",+ "full-revisionid": keywords["full"].strip(),+ "dirty": False,+ "error": "no suitable tags",+ "date": None,+ }@register_vcs_handler("git", "pieces_from_vcs")@@ -225,8 +241,7 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command):if sys.platform == "win32":GITS = ["git.cmd", "git.exe"]- out, rc = run_command(GITS, ["rev-parse", "--git-dir"], cwd=root,- hide_stderr=True)+ out, rc = run_command(GITS, ["rev-parse", "--git-dir"], cwd=root, hide_stderr=True)if rc != 0:if verbose:print("Directory %s not under git control" % root)@@ -234,10 +249,19 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command):# if there is a tag matching tag_prefix, this yields TAG-NUM-gHEX[-dirty]# if there isn't one, this yields HEX[-dirty] (no NUM)- describe_out, rc = run_command(GITS, ["describe", "--tags", "--dirty",- "--always", "--long",- "--match", "%s*" % tag_prefix],- cwd=root)+ describe_out, rc = run_command(+ GITS,+ [+ "describe",+ "--tags",+ "--dirty",+ "--always",+ "--long",+ "--match",+ "%s*" % tag_prefix,+ ],+ cwd=root,+ )# --long was added in git-1.5.5if describe_out is None:raise NotThisMethod("'git describe' failed")@@ -260,17 +284,16 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command):dirty = git_describe.endswith("-dirty")pieces["dirty"] = dirtyif dirty:- git_describe = git_describe[:git_describe.rindex("-dirty")]+ git_describe = git_describe[: git_describe.rindex("-dirty")]# now we have TAG-NUM-gHEX or HEXif "-" in git_describe:# TAG-NUM-gHEX- mo = re.search(r'^(.+)-(\d+)-g([0-9a-f]+)$', git_describe)+ mo = re.search(r"^(.+)-(\d+)-g([0-9a-f]+)$", git_describe)if not mo:# unparseable. Maybe git-describe is misbehaving?- pieces["error"] = ("unable to parse git-describe output: '%s'"- % describe_out)+ pieces["error"] = "unable to parse git-describe output: '%s'" % describe_outreturn pieces# tag@@ -279,10 +302,12 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command):if verbose:fmt = "tag '%s' doesn't start with prefix '%s'"print(fmt % (full_tag, tag_prefix))- pieces["error"] = ("tag '%s' doesn't start with prefix '%s'"- % (full_tag, tag_prefix))+ pieces["error"] = "tag '%s' doesn't start with prefix '%s'" % (+ full_tag,+ tag_prefix,+ )return pieces- pieces["closest-tag"] = full_tag[len(tag_prefix):]+ pieces["closest-tag"] = full_tag[len(tag_prefix) :]# distance: number of commits since tagpieces["distance"] = int(mo.group(2))@@ -293,13 +318,13 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command):else:# HEX: no tagspieces["closest-tag"] = None- count_out, rc = run_command(GITS, ["rev-list", "HEAD", "--count"],- cwd=root)+ count_out, rc = run_command(GITS, ["rev-list", "HEAD", "--count"], cwd=root)pieces["distance"] = int(count_out) # total number of commits# commit date: see ISO-8601 comment in git_versions_from_keywords()- date = run_command(GITS, ["show", "-s", "--format=%ci", "HEAD"],- cwd=root)[0].strip()+ date = run_command(GITS, ["show", "-s", "--format=%ci", "HEAD"], cwd=root)[+ 0+ ].strip()pieces["date"] = date.strip().replace(" ", "T", 1).replace(" ", "", 1)return pieces@@ -330,8 +355,7 @@ def render_pep440(pieces):rendered += ".dirty"else:# exception #1- rendered = "0+untagged.%d.g%s" % (pieces["distance"],- pieces["short"])+ rendered = "0+untagged.%d.g%s" % (pieces["distance"], pieces["short"])if pieces["dirty"]:rendered += ".dirty"return rendered@@ -445,11 +469,13 @@ def render_git_describe_long(pieces):def render(pieces, style):"""Render the given version pieces into the requested style."""if pieces["error"]:- return {"version": "unknown",- "full-revisionid": pieces.get("long"),- "dirty": None,- "error": pieces["error"],- "date": None}+ return {+ "version": "unknown",+ "full-revisionid": pieces.get("long"),+ "dirty": None,+ "error": pieces["error"],+ "date": None,+ }if not style or style == "default":style = "pep440" # the default@@ -469,9 +495,13 @@ def render(pieces, style):else:raise ValueError("unknown style '%s'" % style)- return {"version": rendered, "full-revisionid": pieces["long"],- "dirty": pieces["dirty"], "error": None,- "date": pieces.get("date")}+ return {+ "version": rendered,+ "full-revisionid": pieces["long"],+ "dirty": pieces["dirty"],+ "error": None,+ "date": pieces.get("date"),+ }def get_versions():@@ -485,8 +515,7 @@ def get_versions():verbose = cfg.verbosetry:- return git_versions_from_keywords(get_keywords(), cfg.tag_prefix,- verbose)+ return git_versions_from_keywords(get_keywords(), cfg.tag_prefix, verbose)except NotThisMethod:pass@@ -495,13 +524,16 @@ def get_versions():# versionfile_source is the relative path from the top of the source# tree (where the .git directory might live) to this file. Invert# this to find the root from __file__.- for i in cfg.versionfile_source.split('/'):+ for i in cfg.versionfile_source.split("/"):root = os.path.dirname(root)except NameError:- return {"version": "0+unknown", "full-revisionid": None,- "dirty": None,- "error": "unable to find root of source tree",- "date": None}+ return {+ "version": "0+unknown",+ "full-revisionid": None,+ "dirty": None,+ "error": "unable to find root of source tree",+ "date": None,+ }try:pieces = git_pieces_from_vcs(cfg.tag_prefix, root, verbose)@@ -515,6 +547,10 @@ def get_versions():except NotThisMethod:pass- return {"version": "0+unknown", "full-revisionid": None,- "dirty": None,- "error": "unable to compute version", "date": None}+ return {+ "version": "0+unknown",+ "full-revisionid": None,+ "dirty": None,+ "error": "unable to compute version",+ "date": None,+ }diff --git a/freemocap/calibrate.py b/freemocap/calibrate.pyindex 528b83d3..ca5f60dd 100644--- a/freemocap/calibrate.py+++ b/freemocap/calibrate.py@@ -10,28 +10,27 @@ import osfrom rich.progress import track+def CalibrateCaptureVolume(session, board):-def CalibrateCaptureVolume(session,board):-calVideoFrameLength = 60- createCalibrationVideos(session,calVideoFrameLength)+ createCalibrationVideos(session, calVideoFrameLength)- vidnames = []- cam_names = []- for count,thisVidPath in enumerate(session.calVidPath.glob('*.mp4'),start=1):+ vidnames = []+ cam_names = []+ for count, thisVidPath in enumerate(session.calVidPath.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)+ 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)+ calibrationFile = "{}_calibration.yaml".format(session.sessionID)- calibrationFile = '{}_calibration.yaml'.format(session.sessionID)-- session.cameraCalFilePath = session.sessionPath/calibrationFile+ session.cameraCalFilePath = session.sessionPath / calibrationFile- forceRunCameraExtrinsicsCalibration = True #set this to True to re-run the camera calibration and saveover the existing config file+ forceRunCameraExtrinsicsCalibration = True # set this to True to re-run the camera calibration and saveover the existing config file# if not session.cameraCalFilePath.exists() or forceRunCameraExtrinsicsCalibration: #if that config file doesn't exist, run the camera calibration whosists. If it does, then just load the toml## this will take a few minutes@@ -39,7 +38,7 @@ def CalibrateCaptureVolume(session,board):## then calibrate the cameras based on the detections, using iterative bundle adjustment# error,all_rows,merged = cgroup.calibrate_videos(vidnames, board) #JSM NOTE - in its original form, this method doesn't throw an error when a video fails to load. This is an easy fix!- #%% This next bit is a copy-paste of the inner bits of `cgroup.calibrate_videos` - Part 1 of 2+ #%% This next bit is a copy-paste of the inner bits of `cgroup.calibrate_videos` - Part 1 of 2"""Takes as input a list of list of video filenames, one list of each camera.Also takes a board which specifies what should be detected in the videos"""@@ -50,14 +49,14 @@ def CalibrateCaptureVolume(session,board):if session.debug:fig = plt.figure(47290)for camNum in range(len(all_rows)):- ax = fig.add_subplot(2,2,camNum+1)+ ax = fig.add_subplot(2, 2, camNum + 1)# cap = cv2.VideoCapture(vidnames[camNum][0]) #this is crazy inefficient - we should save out the first image somewhere and save it in an accessible location# ret, frame = cap.read()# plt.imshow(frame)# cap.release()for frNum in range(len(all_rows[camNum])):- corners = np.squeeze(all_rows[camNum][frNum]['filled'])- plt.plot(corners[:,0], corners[:,1],'.-')+ corners = np.squeeze(all_rows[camNum][frNum]["filled"])+ plt.plot(corners[:, 0], corners[:, 1], ".-")# xmin = np.nanmin(corners[:,0])*.9# xmax = np.nanmax(corners[:,0])*1.1# ymin = np.nanmin(corners[:,1])*.9@@ -66,53 +65,55 @@ def CalibrateCaptureVolume(session,board):# ax.set_xlim(ymin, ymax)plt.show()---#%% This next bit is a copy-paste of the inner bits of `cgroup.calibrate_videos` - Part 2 of 2- #error, merged = cgroup.calibrate_rows(all_rows, board,- # init_intrinsics=True,- # init_extrinsics=True)-#%% End of the copy-paste of the inner bits of `cgroup.calibrate_videos` - END-- #JSM NOTE - need add method to extract Charuco board points from the calibrate_videos pipeline-- ## if you need to save and load- ## example saving and loading for later- if not session.cameraCalFilePath.is_file():- error,merged = cgroup.calibrate_videos(vidnames, board)- cgroup.dump(session.cameraCalFilePath) #JSM NOTE - let's just use .yaml's unless there is some reason to use .toml- mergename = session.sessionPath/'merged.npy'- np.save(mergename,merged)- else:- cgroup = CameraGroup.load(session.cameraCalFilePath) #load previous calibration config-+ #%% This next bit is a copy-paste of the inner bits of `cgroup.calibrate_videos` - Part 2 of 2+ # error, merged = cgroup.calibrate_rows(all_rows, board,+ # init_intrinsics=True,+ # init_extrinsics=True)+ #%% End of the copy-paste of the inner bits of `cgroup.calibrate_videos` - END++ # JSM NOTE - need add method to extract Charuco board points from the calibrate_videos pipeline++ ## if you need to save and load+ ## example saving and loading for later+ if not session.cameraCalFilePath.is_file():+ error, merged = cgroup.calibrate_videos(vidnames, board)+ cgroup.dump(+ session.cameraCalFilePath+ ) # JSM NOTE - let's just use .yaml's unless there is some reason to use .toml+ mergename = session.sessionPath / "merged.npy"+ np.save(mergename, merged)+ else:+ cgroup = CameraGroup.load(+ session.cameraCalFilePath+ ) # load previous calibration configsession.cgroup = cgroup- n_frames= 40+ n_frames = 40startframe = 0n_trackedPoints = 24- framelist = range(startframe,startframe+n_frames)+ framelist = range(startframe, startframe + n_frames)- charucoarray = np.empty([session.numCams,n_frames,n_trackedPoints,1,2])+ charucoarray = np.empty([session.numCams, n_frames, n_trackedPoints, 1, 2])charucoarray[:] = np.nan- data = np.load(session.sessionPath/'merged.npy',allow_pickle = True)+ data = np.load(session.sessionPath / "merged.npy", allow_pickle=True)for cam in range(session.numCams):- for count,frame in enumerate(framelist):+ for count, frame in enumerate(framelist):try:- charucoarray[cam][count] = data[frame][cam]['filled']- except:- print('failed frame:', frame)+ charucoarray[cam][count] = data[frame][cam]["filled"]+ except:+ print("failed frame:", frame)continuecharuco_nCams_nFrames_nImgPts_XY = np.squeeze(np.array(charucoarray))-+session.charuco_nCams_nFrames_nImgPts_XY = charuco_nCams_nFrames_nImgPts_XY-- charuco_fr_mar_dim = reconstruct3D.reconstruct3D(session,charuco_nCams_nFrames_nImgPts_XY)-- mean_charuco_fr_mar_dim = np.nanmean(charuco_fr_mar_dim,axis = 0)+ charuco_fr_mar_dim = reconstruct3D.reconstruct3D(+ session, charuco_nCams_nFrames_nImgPts_XY+ )++ mean_charuco_fr_mar_dim = np.nanmean(charuco_fr_mar_dim, axis=0)# charry_reshaped = charucoarray.reshape(session.numCams, -1, 2)@@ -120,42 +121,44 @@ def CalibrateCaptureVolume(session,board):# charReprojerr_flat = cgroup.reprojection_error(char_flat, charry_reshaped, mean=True)# char_fr_mar_dim = char_flat.reshape(n_frames, n_trackedPoints, 3)- # charReprojErr_fr_mar_err = charReprojerr_flat.reshape(n_frames, n_trackedPoints)-+ # charReprojErr_fr_mar_err = charReprojerr_flat.reshape(n_frames, n_trackedPoints)if session.debug:fig = plt.figure()- #mean charuco position- ax1 = fig.add_subplot(111,projection='3d')+ # mean charuco position+ ax1 = fig.add_subplot(111, projection="3d")ax1.cla()- x = mean_charuco_fr_mar_dim[:][:,0]- y = mean_charuco_fr_mar_dim[:][:,1]- z = mean_charuco_fr_mar_dim[:][:,2]+ x = mean_charuco_fr_mar_dim[:][:, 0]+ y = mean_charuco_fr_mar_dim[:][:, 1]+ z = mean_charuco_fr_mar_dim[:][:, 2]mx = np.nanmean(x)my = np.nanmean(y)mz = np.nanmean(z)---- ax1.scatter(x,y,z, marker='o')- ax1.set_title('Mean Charuco Point Positions')-- ax1.set_xlabel('x')- ax1.set_ylabel('y')- ax1.set_zlabel('z')++ ax1.scatter(x, y, z, marker="o")+ ax1.set_title("Mean Charuco Point Positions")++ ax1.set_xlabel("x")+ ax1.set_ylabel("y")+ ax1.set_zlabel("z")for camNum in range(len(cgroup.cameras)):np.append(mx, cgroup.cameras[camNum].tvec[0])np.append(my, cgroup.cameras[camNum].tvec[1])np.append(mz, cgroup.cameras[camNum].tvec[2])- ax1.scatter(cgroup.cameras[camNum].tvec[0], cgroup.cameras[camNum].tvec[1], cgroup.cameras[camNum].tvec[2], marker = 'p')-- axRange = board.square_length*5+ ax1.scatter(+ cgroup.cameras[camNum].tvec[0],+ cgroup.cameras[camNum].tvec[1],+ cgroup.cameras[camNum].tvec[2],+ marker="p",+ )++ axRange = board.square_length * 5- ax1.set_xlim(mx-axRange,mx+axRange)- ax1.set_ylim(my-axRange,my+axRange)- ax1.set_zlim(mz-axRange,mz+axRange)+ ax1.set_xlim(mx - axRange, mx + axRange)+ ax1.set_ylim(my - axRange, my + axRange)+ ax1.set_zlim(mz - axRange, mz + axRange)# #charuco points over time# ax2 = fig.add_subplot(122)@@ -166,34 +169,41 @@ def CalibrateCaptureVolume(session,board):# ax2.set_title('Charuco Point positions over time')plt.show()- path_to_charuco_array = session.dataArrayPath/'charuco_points.npy'+ path_to_charuco_array = session.dataArrayPath / "charuco_points.npy"np.save(path_to_charuco_array, mean_charuco_fr_mar_dim)- return cgroup,mean_charuco_fr_mar_dim+ return cgroup, mean_charuco_fr_mar_dim-def createCalibrationVideos(session,calVideoFrameLength):++def createCalibrationVideos(session, calVideoFrameLength):vidList = os.listdir(session.syncedVidPath)framelist = list(range(calVideoFrameLength))- codec = 'DIVX'- for count,vid in enumerate(vidList,start=1):+ codec = "DIVX"+ for count, vid in enumerate(vidList, start=1):cam_name = "Cam{}".format(count)- cap = cv2.VideoCapture(str(session.syncedVidPath/vid))+ cap = cv2.VideoCapture(str(session.syncedVidPath / vid))fourcc = cv2.VideoWriter_fourcc(*codec)-- #grab resolution parameters from the videos- resWidth = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))++ # grab resolution parameters from the videos+ resWidth = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))resHeight = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))framerate = int(cap.get(cv2.CAP_PROP_FPS))-- saveName = session.sessionID +'_trimmed_' + cam_name + '.mp4' #create a name for the trimmed video- saveCalVidPath = str(session.calVidPath/saveName) #create an output path for the function-- success, image = cap.read() #start reading frames-- out = cv2.VideoWriter(saveCalVidPath, fourcc, framerate, (resWidth,resHeight))- print('Trimming ' + cam_name)++ saveName = (+ session.sessionID + "_trimmed_" + cam_name + ".mp4"+ ) # create a name for the trimmed video+ saveCalVidPath = str(+ session.calVidPath / saveName+ ) # create an output path for the function++ success, image = cap.read() # start reading frames++ out = cv2.VideoWriter(saveCalVidPath, fourcc, framerate, (resWidth, resHeight))+ print("Trimming " + cam_name)for frame in track(framelist):- cap.set(cv2.CAP_PROP_POS_FRAMES, frame) #set the video to the frame that we need+ cap.set(+ cv2.CAP_PROP_POS_FRAMES, frame+ ) # set the video to the frame that we needsuccess, image = cap.read()out.write(image)cap.release()- out.release()\ No newline at end of file+ out.release()diff --git a/freemocap/createvideo.py b/freemocap/createvideo.pyindex 165bc5eb..d5aeb3f8 100644--- a/freemocap/createvideo.py+++ b/freemocap/createvideo.py@@ -6,22 +6,22 @@ import osdef createVideo(session):- vidSavePath = str(session.sessionPath/'{}_outVid.mp4'.format(session.sessionID))+ vidSavePath = str(session.sessionPath / "{}_outVid.mp4".format(session.sessionID))fps = 30- shape = 1078,647+ shape = 1078, 647frame_array = []- for filename in session.imOutPath.glob('*.png'):- filename= str(filename)+ for filename in session.imOutPath.glob("*.png"):+ filename = str(filename)- #reading each files+ # reading each filesimg = cv2.imread(filename)- #resized = cv2.resize(img,shape)- #shape = img.shape[:2]- resized = cv2.resize(img,shape)- #inserting the frames into an image array+ # resized = cv2.resize(img,shape)+ # shape = img.shape[:2]+ resized = cv2.resize(img, shape)+ # inserting the frames into an image arrayframe_array.append(resized)- out = cv2.VideoWriter(vidSavePath,cv2.VideoWriter_fourcc(*'DIVX'), fps, shape)+ out = cv2.VideoWriter(vidSavePath, cv2.VideoWriter_fourcc(*"DIVX"), fps, shape)for i in range(len(frame_array)):# writing to a image arrayout.write(frame_array[i])@@ -30,29 +30,35 @@ def createVideo(session):def createBodyTrackingVideos(session):- vidSavePath = str(session.sessionPath/'{}_outVid.mp4'.format(session.sessionID))+ vidSavePath = str(session.sessionPath / "{}_outVid.mp4".format(session.sessionID))fps = 30- shape = 1078,647- #frame_array = []+ shape = 1078, 647+ # frame_array = []if session.useOpenPose:- for count,videoPath in enumerate(session.config_settings['openPose_imgPathList']):+ for count, videoPath in enumerate(+ session.config_settings["openPose_imgPathList"]+ ):frame_array = []- vidSavePath = str(session.openPoseDataPath/'openPoseVideo_cam{}.mp4'.format(count))+ vidSavePath = str(+ session.openPoseDataPath / "openPoseVideo_cam{}.mp4".format(count)+ )videoPath = Path(videoPath)- for filename in videoPath.glob('*.png'):-- filename= str(filename)- #reading each files+ for filename in videoPath.glob("*.png"):++ filename = str(filename)+ # reading each filesimg = cv2.imread(filename)- #resized = cv2.resize(img,shape)- #shape = img.shape[:2]- resized = cv2.resize(img,shape)- #inserting the frames into an image array+ # resized = cv2.resize(img,shape)+ # shape = img.shape[:2]+ resized = cv2.resize(img, shape)+ # inserting the frames into an image arrayframe_array.append(resized)- out = cv2.VideoWriter(vidSavePath,cv2.VideoWriter_fourcc(*'DIVX'), fps, shape)+ out = cv2.VideoWriter(+ vidSavePath, cv2.VideoWriter_fourcc(*"DIVX"), fps, shape+ )for i in range(len(frame_array)):# writing to a image arrayout.write(frame_array[i])- out.release()\ No newline at end of file+ out.release()diff --git a/freemocap/fmc_anipose.py b/freemocap/fmc_anipose.pyindex d703bc40..a2f6d138 100644--- a/freemocap/fmc_anipose.py+++ b/freemocap/fmc_anipose.py@@ -12,10 +12,14 @@ from tqdm import trangefrom pprint import pprintimport time-from aniposelib.boards import merge_rows, extract_points, \- extract_rtvecs, get_video_params-from aniposelib.utils import get_initial_extrinsics, make_M, get_rtvec, \- get_connections+from aniposelib.boards import (+ merge_rows,+ extract_points,+ extract_rtvecs,+ get_video_params,+)+from aniposelib.utils import get_initial_extrinsics, make_M, get_rtvec, get_connections+@jit(nopython=True, parallel=True)def triangulate_simple(points, camera_mats):@@ -24,8 +28,8 @@ def triangulate_simple(points, camera_mats):for i in range(num_cams):x, y = points[i]mat = camera_mats[i]- A[(i * 2):(i * 2 + 1)] = x * mat[2] - mat[0]- A[(i * 2 + 1):(i * 2 + 2)] = y * mat[2] - mat[1]+ A[(i * 2) : (i * 2 + 1)] = x * mat[2] - mat[0]+ A[(i * 2 + 1) : (i * 2 + 2)] = y * mat[2] - mat[1]u, s, vh = np.linalg.svd(A, full_matrices=True)p3d = vh[-1]p3d = p3d[:3] / p3d[3]@@ -41,7 +45,7 @@ def get_error_dict(errors_full, min_points=10):error_dict = dict()for i in range(n_cams):- for j in range(i+1, n_cams):+ for j in range(i + 1, n_cams):subset = good[i] & good[j]err_subset = errors_norm[:, subset][[i, j]]err_subset_mean = np.mean(err_subset, axis=0)@@ -51,45 +55,50 @@ def get_error_dict(errors_full, min_points=10):error_dict[(i, j)] = (err_subset.shape[1], percents)return error_dict+def check_errors(cgroup, imgp):p3ds = cgroup.triangulate(imgp)errors_full = cgroup.reprojection_error(p3ds, imgp, mean=False)return get_error_dict(errors_full)+def subset_extra(extra, ixs):if extra is None:return Nonenew_extra = {- 'objp': extra['objp'][ixs],- 'ids': extra['ids'][ixs],- 'rvecs': extra['rvecs'][:, ixs],- 'tvecs': extra['tvecs'][:, ixs]+ "objp": extra["objp"][ixs],+ "ids": extra["ids"][ixs],+ "rvecs": extra["rvecs"][:, ixs],+ "tvecs": extra["tvecs"][:, ixs],}return new_extra+def resample_points_extra(imgp, extra, n_samp=25):n_cams, n_points, _ = imgp.shape- ids = remap_ids(extra['ids'])- n_ids = np.max(ids)+1+ ids = remap_ids(extra["ids"])+ n_ids = np.max(ids) + 1good = ~np.isnan(imgp[:, :, 0])ixs = np.arange(n_points)- cam_counts = np.zeros((n_ids, n_cams), dtype='int64')+ cam_counts = np.zeros((n_ids, n_cams), dtype="int64")for idnum in range(n_ids):cam_counts[idnum] = np.sum(good[:, ids == idnum], axis=1)cam_counts_random = cam_counts + np.random.random(size=cam_counts.shape)best_boards = np.argsort(-cam_counts_random, axis=0)- cam_totals = np.zeros(n_cams, dtype='int64')+ cam_totals = np.zeros(n_cams, dtype="int64")include = set()for cam_num in range(n_cams):for board_id in best_boards[:, cam_num]:include.update(ixs[ids == board_id])cam_totals += cam_counts[board_id]- if cam_totals[cam_num] >= n_samp or \- cam_counts_random[board_id, cam_num] < 1:+ if (+ cam_totals[cam_num] >= n_samp+ or cam_counts_random[board_id, cam_num] < 1+ ):breakfinal_ixs = sorted(include)@@ -97,6 +106,7 @@ def resample_points_extra(imgp, extra, n_samp=25):extra = subset_extra(extra, final_ixs)return newp, extra+def resample_points(imgp, extra=None, n_samp=25):# if extra is not None:# return resample_points_extra(imgp, extra, n_samp)@@ -110,12 +120,12 @@ def resample_points(imgp, extra=None, n_samp=25):include = set()for i in range(n_cams):- for j in range(i+1, n_cams):+ for j in range(i + 1, n_cams):subset = good[i] & good[j]n_good = np.sum(subset)if n_good > 0:## pick points, prioritizing points seen by more cameras- arr = np.copy(num_cams[subset]).astype('float64')+ arr = np.copy(num_cams[subset]).astype("float64")arr += np.random.random(size=arr.shape)picked_ix = np.argsort(-arr)[:n_samp]picked = ixs[subset][picked_ix]@@ -126,15 +136,18 @@ def resample_points(imgp, extra=None, n_samp=25):extra = subset_extra(extra, final_ixs)return newp, extra+def medfilt_data(values, size=15):- padsize = size+5- vpad = np.pad(values, (padsize, padsize), mode='reflect')+ padsize = size + 5+ vpad = np.pad(values, (padsize, padsize), mode="reflect")vpadf = signal.medfilt(vpad, kernel_size=size)return vpadf[padsize:-padsize]+def nan_helper(y):return np.isnan(y), lambda z: z.nonzero()[0]+def interpolate_data(vals):nans, ix = nan_helper(vals)out = np.copy(vals)@@ -144,6 +157,7 @@ def interpolate_data(vals):out[:] = 0return out+def remap_ids(ids):unique_ids = np.unique(ids)ids_out = np.copy(ids)@@ -151,34 +165,37 @@ def remap_ids(ids):ids_out[ids == num] = ireturn ids_out+def transform_points(points, rvecs, tvecs):"""Rotate points by given rotation vectors and translate.Rodrigues' rotation formula is used."""theta = np.linalg.norm(rvecs, axis=1)[:, np.newaxis]- with np.errstate(invalid='ignore'):+ with np.errstate(invalid="ignore"):v = rvecs / thetav = np.nan_to_num(v)dot = np.sum(points * v, axis=1)[:, np.newaxis]cos_theta = np.cos(theta)sin_theta = np.sin(theta)- rotated = cos_theta * points + \- sin_theta * np.cross(v, points) + \- dot * (1 - cos_theta) * v+ rotated = (+ cos_theta * points + sin_theta * np.cross(v, points) + dot * (1 - cos_theta) * v+ )return rotated + tvecsclass Camera:- def __init__(self,- matrix=np.eye(3),- dist=np.zeros(5),- size=None,- rvec=np.zeros(3),- tvec=np.zeros(3),- name=None,- extra_dist=False):+ def __init__(+ self,+ matrix=np.eye(3),+ dist=np.zeros(5),+ size=None,+ rvec=np.zeros(3),+ tvec=np.zeros(3),+ name=None,+ extra_dist=False,+ ):self.set_camera_matrix(matrix)self.set_distortions(dist)@@ -190,21 +207,21 @@ class Camera:def get_dict(self):return {- 'name': self.get_name(),- 'size': list(self.get_size()),- 'matrix': self.get_camera_matrix().tolist(),- 'distortions': self.get_distortions().tolist(),- 'rotation': self.get_rotation().tolist(),- 'translation': self.get_translation().tolist(),+ "name": self.get_name(),+ "size": list(self.get_size()),+ "matrix": self.get_camera_matrix().tolist(),+ "distortions": self.get_distortions().tolist(),+ "rotation": self.get_rotation().tolist(),+ "translation": self.get_translation().tolist(),}def load_dict(self, d):- self.set_camera_matrix(d['matrix'])- self.set_rotation(d['rotation'])- self.set_translation(d['translation'])- self.set_distortions(d['distortions'])- self.set_name(d['name'])- self.set_size(d['size'])+ self.set_camera_matrix(d["matrix"])+ self.set_rotation(d["rotation"])+ self.set_translation(d["translation"])+ self.set_distortions(d["distortions"])+ self.set_name(d["name"])+ self.set_size(d["size"])def from_dict(d):cam = Camera()@@ -218,7 +235,7 @@ class Camera:return self.distdef set_camera_matrix(self, matrix):- self.matrix = np.array(matrix, dtype='float64')+ self.matrix = np.array(matrix, dtype="float64")def set_focal_length(self, fx, fy=None):if fy is None:@@ -235,16 +252,16 @@ class Camera:return (fx + fy) / 2.0def set_distortions(self, dist):- self.dist = np.array(dist, dtype='float64').ravel()+ self.dist = np.array(dist, dtype="float64").ravel()def set_rotation(self, rvec):- self.rvec = np.array(rvec, dtype='float64').ravel()+ self.rvec = np.array(rvec, dtype="float64").ravel()def get_rotation(self):return self.rvecdef set_translation(self, tvec):- self.tvec = np.array(tvec, dtype='float64').ravel()+ self.tvec = np.array(tvec, dtype="float64").ravel()def get_translation(self):return self.tvec@@ -277,7 +294,7 @@ class Camera:self.set_camera_matrix(new_matrix)def get_params(self):- params = np.zeros(8 + self.extra_dist, dtype='float64')+ params = np.zeros(8 + self.extra_dist, dtype="float64")params[0:3] = self.get_rotation()params[3:6] = self.get_translation()params[6] = self.get_focal_length()@@ -292,7 +309,7 @@ class Camera:self.set_translation(params[3:6])self.set_focal_length(params[6])- dist = np.zeros(5, dtype='float64')+ dist = np.zeros(5, dtype="float64")dist[0] = params[7]if self.extra_dist:dist[1] = params[8]@@ -302,24 +319,32 @@ class Camera:shape = points.shapepoints = points.reshape(-1, 1, 2)new_points = np.dstack([points, np.ones((points.shape[0], 1, 1))])- out, _ = cv2.projectPoints(new_points, np.zeros(3), np.zeros(3),- self.matrix.astype('float64'),- self.dist.astype('float64'))+ out, _ = cv2.projectPoints(+ new_points,+ np.zeros(3),+ np.zeros(3),+ self.matrix.astype("float64"),+ self.dist.astype("float64"),+ )return out.reshape(shape)def undistort_points(self, points):shape = points.shapepoints = points.reshape(-1, 1, 2)- out = cv2.undistortPoints(points,- self.matrix.astype('float64'),- self.dist.astype('float64'))+ out = cv2.undistortPoints(+ points, self.matrix.astype("float64"), self.dist.astype("float64")+ )return out.reshape(shape)def project(self, points):points = points.reshape(-1, 1, 3)- out, _ = cv2.projectPoints(points, self.rvec, self.tvec,- self.matrix.astype('float64'),- self.dist.astype('float64'))+ out, _ = cv2.projectPoints(+ points,+ self.rvec,+ self.tvec,+ self.matrix.astype("float64"),+ self.dist.astype("float64"),+ )return outdef reprojection_error(self, p3d, p2d):@@ -327,24 +352,28 @@ class Camera:return p2d - projdef copy(self):- return \- Camera(matrix=self.get_camera_matrix().copy(),- dist=self.get_distortions().copy(),- size=self.get_size(),- rvec=self.get_rotation().copy(),- tvec=self.get_translation().copy(),- name=self.get_name(),- extra_dist=self.extra_dist)+ return Camera(+ matrix=self.get_camera_matrix().copy(),+ dist=self.get_distortions().copy(),+ size=self.get_size(),+ rvec=self.get_rotation().copy(),+ tvec=self.get_translation().copy(),+ name=self.get_name(),+ extra_dist=self.extra_dist,+ )+class FisheyeCamera(Camera):- def __init__(self,- matrix=np.eye(3),- dist=np.zeros(4),- size=None,- rvec=np.zeros(3),- tvec=np.zeros(3),- name=None,- extra_dist=False):+ def __init__(+ self,+ matrix=np.eye(3),+ dist=np.zeros(4),+ size=None,+ rvec=np.zeros(3),+ tvec=np.zeros(3),+ name=None,+ extra_dist=False,+ ):self.set_camera_matrix(matrix)self.set_distortions(dist)self.set_size(size)@@ -360,33 +389,41 @@ class FisheyeCamera(Camera):def get_dict(self):d = super().get_dict()- d['fisheye'] = True+ d["fisheye"] = Truereturn ddef distort_points(self, points):shape = points.shapepoints = points.reshape(-1, 1, 2)new_points = np.dstack([points, np.ones((points.shape[0], 1, 1))])- out, _ = cv2.fisheye.projectPoints(new_points,- np.zeros(3), np.zeros(3),- self.matrix.astype('float64'),- self.dist.astype('float64'))+ out, _ = cv2.fisheye.projectPoints(+ new_points,+ np.zeros(3),+ np.zeros(3),+ self.matrix.astype("float64"),+ self.dist.astype("float64"),+ )return out.reshape(shape)def undistort_points(self, points):shape = points.shapepoints = points.reshape(-1, 1, 2)- out = cv2.fisheye.undistortPoints(points.astype('float64'),- self.matrix.astype('float64'),- self.dist.astype('float64'))+ out = cv2.fisheye.undistortPoints(+ points.astype("float64"),+ self.matrix.astype("float64"),+ self.dist.astype("float64"),+ )return out.reshape(shape)def project(self, points):points = points.reshape(-1, 1, 3)- out, _ = cv2.fisheye.projectPoints(points,- self.rvec, self.tvec,- self.matrix.astype('float64'),- self.dist.astype('float64'))+ out, _ = cv2.fisheye.projectPoints(+ points,+ self.rvec,+ self.tvec,+ self.matrix.astype("float64"),+ self.dist.astype("float64"),+ )return outdef set_params(self, params):@@ -394,7 +431,7 @@ class FisheyeCamera(Camera):self.set_translation(params[3:6])self.set_focal_length(params[6])- dist = np.zeros(4, dtype='float64')+ dist = np.zeros(4, dtype="float64")dist[0] = params[7]if self.extra_dist:dist[1] = params[8]@@ -403,7 +440,7 @@ class FisheyeCamera(Camera):self.set_distortions(dist)def get_params(self):- params = np.zeros(8+self.extra_dist, dtype='float64')+ params = np.zeros(8 + self.extra_dist, dtype="float64")params[0:3] = self.get_rotation()params[3:6] = self.get_translation()params[6] = self.get_focal_length()@@ -423,7 +460,9 @@ class FisheyeCamera(Camera):rvec=self.get_rotation().copy(),tvec=self.get_translation().copy(),name=self.get_name(),- extra_dist=self.extra_dist)+ extra_dist=self.extra_dist,+ )+class CameraGroup:def __init__(self, cameras, metadata={}):@@ -441,9 +480,8 @@ class CameraGroup:for name in names:if name not in cur_names_dict:raise IndexError(- "name {} not part of camera names: {}".format(- name, cur_names- ))+ "name {} not part of camera names: {}".format(name, cur_names)+ )indices.append(cur_names_dict[name])return self.subset_cameras(indices)@@ -454,7 +492,7 @@ class CameraGroup:n_points = points.shape[0]n_cams = len(self.cameras)- out = np.empty((n_cams, n_points, 2), dtype='float64')+ out = np.empty((n_cams, n_points, 2), dtype="float64")for cnum, cam in enumerate(self.cameras):out[cnum] = cam.project(points).reshape(n_points, 2)@@ -464,11 +502,12 @@ class CameraGroup:"""Given an CxNx2 array, this returns an Nx3 array of points,where N is the number of points and C is the number of cameras"""- assert points.shape[0] == len(self.cameras), \- "Invalid points shape, first dim should be equal to" \+ assert points.shape[0] == len(self.cameras), (+ "Invalid points shape, first dim should be equal to"" number of cameras ({}), but shape is {}".format(len(self.cameras), points.shape)+ )one_point = Falseif len(points.shape) == 2:@@ -506,8 +545,9 @@ class CameraGroup:return out- def triangulate_possible(self, points, undistort=True,- min_cams=2, progress=False, threshold=0.5):+ def triangulate_possible(+ self, points, undistort=True, min_cams=2, progress=False, threshold=0.5+ ):"""Given an CxNxPx2 array, this returns an Nx3 array of pointsby triangulating all possible points and picking the ones withbest reprojection error@@ -517,21 +557,22 @@ class CameraGroup:P: number of possible options per point"""- assert points.shape[0] == len(self.cameras), \- "Invalid points shape, first dim should be equal to" \+ assert points.shape[0] == len(self.cameras), (+ "Invalid points shape, first dim should be equal to"" number of cameras ({}), but shape is {}".format(len(self.cameras), points.shape)+ )n_cams, n_points, n_possible, _ = points.shape- cam_nums, point_nums, possible_nums = np.where(- ~np.isnan(points[:, :, :, 0]))+ cam_nums, point_nums, possible_nums = np.where(~np.isnan(points[:, :, :, 0]))all_iters = defaultdict(dict)- for cam_num, point_num, possible_num in zip(cam_nums, point_nums,- possible_nums):+ for cam_num, point_num, possible_num in zip(+ cam_nums, point_nums, possible_nums+ ):if cam_num not in all_iters[point_num]:all_iters[point_num][cam_num] = []all_iters[point_num][cam_num].append((cam_num, possible_num))@@ -540,10 +581,10 @@ class CameraGroup:for cam_num in all_iters[point_num].keys():all_iters[point_num][cam_num].append(None)- out = np.full((n_points, 3), np.nan, dtype='float64')- picked_vals = np.zeros((n_cams, n_points, n_possible), dtype='bool')- errors = np.zeros(n_points, dtype='float64')- points_2d = np.full((n_cams, n_points, 2), np.nan, dtype='float64')+ out = np.full((n_points, 3), np.nan, dtype="float64")+ picked_vals = np.zeros((n_cams, n_points, n_possible), dtype="bool")+ errors = np.zeros(n_points, dtype="float64")+ points_2d = np.full((n_cams, n_points, 2), np.nan, dtype="float64")if progress:iterator = trange(n_points, ncols=70)@@ -572,24 +613,24 @@ class CameraGroup:if err < best_error:best_point = {- 'error': err,- 'point': p3d[:3],- 'points': pts,- 'picked': picked,- 'joint_ix': point_ix+ "error": err,+ "point": p3d[:3],+ "points": pts,+ "picked": picked,+ "joint_ix": point_ix,}best_error = errif best_error < threshold:breakif best_point is not None:- out[point_ix] = best_point['point']- picked = best_point['picked']+ out[point_ix] = best_point["point"]+ picked = best_point["picked"]cnums = [p[0] for p in picked]xnums = [p[1] for p in picked]picked_vals[cnums, point_ix, xnums] = True- errors[point_ix] = best_point['error']- points_2d[cnums, point_ix] = best_point['points']+ errors[point_ix] = best_point["error"]+ points_2d[cnums, point_ix] = best_point["points"]return out, picked_vals, points_2d, errors@@ -597,21 +638,20 @@ class CameraGroup:"""Given an CxNx2 array, this returns an Nx3 array of points,where N is the number of points and C is the number of cameras"""- assert points.shape[0] == len(self.cameras), \- "Invalid points shape, first dim should be equal to" \+ assert points.shape[0] == len(self.cameras), (+ "Invalid points shape, first dim should be equal to"" number of cameras ({}), but shape is {}".format(len(self.cameras), points.shape)+ )n_cams, n_points, _ = points.shapepoints_ransac = points.reshape(n_cams, n_points, 1, 2)- return self.triangulate_possible(points_ransac,- undistort=undistort,- min_cams=min_cams,- progress=progress)-+ return self.triangulate_possible(+ points_ransac, undistort=undistort, min_cams=min_cams, progress=progress+ )@jit(nopython=True, parallel=True, forceobj=True)def reprojection_error(self, p3ds, p2ds, mean=False):@@ -627,9 +667,12 @@ class CameraGroup:one_point = Truen_cams, n_points, _ = p2ds.shape- assert p3ds.shape == (n_points, 3), \- "shapes of 2D and 3D points are not consistent: " \- "2D={}, 3D={}".format(p2ds.shape, p3ds.shape)+ assert p3ds.shape == (+ n_points,+ 3,+ ), "shapes of 2D and 3D points are not consistent: " "2D={}, 3D={}".format(+ p2ds.shape, p3ds.shape+ )errors = np.empty((n_cams, n_points, 2))@@ -640,7 +683,7 @@ class CameraGroup:errors_norm = np.linalg.norm(errors, axis=2)good = ~np.isnan(errors_norm)errors_norm[~good] = 0- denom = np.sum(good, axis=0).astype('float64')+ denom = np.sum(good, axis=0).astype("float64")denom[denom < 1.5] = np.nanerrors = np.sum(errors_norm, axis=0) / denom@@ -652,13 +695,20 @@ class CameraGroup:return errors-- def bundle_adjust_iter(self, p2ds, extra=None,- n_iters=10, start_mu=15, end_mu=1,- max_nfev=200, ftol=1e-4,- n_samp_iter=100, n_samp_full=1000,- error_threshold=0.3,- verbose=False):+ def bundle_adjust_iter(+ self,+ p2ds,+ extra=None,+ n_iters=10,+ start_mu=15,+ end_mu=1,+ max_nfev=200,+ ftol=1e-4,+ n_samp_iter=100,+ n_samp_full=1000,+ error_threshold=0.3,+ verbose=False,+ ):"""Given an CxNx2 array of 2D points,where N is the number of points and C is the number of cameras,this performs iterative bundle adjustsment to fine-tune the parameters of the cameras.@@ -667,30 +717,29 @@ class CameraGroup:This is inspired by the algorithm for Fast Global Registration by Zhou, Park, and Koltun"""- assert p2ds.shape[0] == len(self.cameras), \- "Invalid points shape, first dim should be equal to" \+ assert p2ds.shape[0] == len(self.cameras), (+ "Invalid points shape, first dim should be equal to"" number of cameras ({}), but shape is {}".format(len(self.cameras), p2ds.shape)+ )p2ds_full = p2dsextra_full = extra- p2ds, extra = resample_points(p2ds_full, extra_full,- n_samp=n_samp_full)+ p2ds, extra = resample_points(p2ds_full, extra_full, n_samp=n_samp_full)error = self.average_error(p2ds, median=True)if verbose:- print('error: ', error)+ print("error: ", error)mus = np.exp(np.linspace(np.log(start_mu), np.log(end_mu), num=n_iters))if verbose:- print('n_samples: {}'.format(n_samp_iter))+ print("n_samples: {}".format(n_samp_iter))for i in range(n_iters):- p2ds, extra = resample_points(p2ds_full, extra_full,- n_samp=n_samp_full)+ p2ds, extra = resample_points(p2ds_full, extra_full, n_samp=n_samp_full)p3ds = self.triangulate(p2ds)errors_full = self.reprojection_error(p3ds, p2ds, mean=False)errors_norm = self.reprojection_error(p3ds, p2ds, mean=True)@@ -707,7 +756,8 @@ class CameraGroup:good = errors_norm < muextra_good = subset_extra(extra, good)p2ds_samp, extra_samp = resample_points(- p2ds[:, good], extra_good, n_samp=n_samp_iter)+ p2ds[:, good], extra_good, n_samp=n_samp_iter+ )error = np.median(errors_norm)@@ -716,16 +766,22 @@ class CameraGroup:if verbose:pprint(error_dict)- print('error: {:.2f}, mu: {:.1f}, ratio: {:.3f}'.format(error, mu, np.mean(good)))-- self.bundle_adjust(p2ds_samp, extra_samp,- loss='linear', ftol=ftol,- max_nfev=max_nfev,- verbose=verbose)-+ print(+ "error: {:.2f}, mu: {:.1f}, ratio: {:.3f}".format(+ error, mu, np.mean(good)+ )+ )++ self.bundle_adjust(+ p2ds_samp,+ extra_samp,+ loss="linear",+ ftol=ftol,+ max_nfev=max_nfev,+ verbose=verbose,+ )- p2ds, extra = resample_points(p2ds_full, extra_full,- n_samp=n_samp_full)+ p2ds, extra = resample_points(p2ds_full, extra_full, n_samp=n_samp_full)p3ds = self.triangulate(p2ds)errors_full = self.reprojection_error(p3ds, p2ds, mean=False)errors_norm = self.reprojection_error(p3ds, p2ds, mean=True)@@ -743,10 +799,14 @@ class CameraGroup:good = errors_norm < muextra_good = subset_extra(extra, good)- self.bundle_adjust(p2ds[:, good], extra_good,- loss='linear',- ftol=ftol, max_nfev=max(200, max_nfev),- verbose=verbose)+ self.bundle_adjust(+ p2ds[:, good],+ extra_good,+ loss="linear",+ ftol=ftol,+ max_nfev=max(200, max_nfev),+ verbose=verbose,+ )error = self.average_error(p2ds, median=True)@@ -757,30 +817,35 @@ class CameraGroup:pprint(error_dict)if verbose:- print('error: ', error)+ print("error: ", error)return error- def bundle_adjust(self, p2ds, extra=None,- loss='linear',- threshold=50,- ftol=1e-4,- max_nfev=1000,- weights=None,- start_params=None,- verbose=True):+ def bundle_adjust(+ self,+ p2ds,+ extra=None,+ loss="linear",+ threshold=50,+ ftol=1e-4,+ max_nfev=1000,+ weights=None,+ start_params=None,+ verbose=True,+ ):"""Given an CxNx2 array of 2D points,where N is the number of points and C is the number of cameras,this performs bundle adjustsment to fine-tune the parameters of the cameras"""- assert p2ds.shape[0] == len(self.cameras), \- "Invalid points shape, first dim should be equal to" \+ assert p2ds.shape[0] == len(self.cameras), (+ "Invalid points shape, first dim should be equal to"" number of cameras ({}), but shape is {}".format(len(self.cameras), p2ds.shape)+ )if extra is not None:- extra['ids_map'] = remap_ids(extra['ids'])+ extra["ids_map"] = remap_ids(extra["ids"])x0, n_cam_params = self._initialize_params_bundle(p2ds, extra)@@ -793,18 +858,20 @@ class CameraGroup:jac_sparse = self._jac_sparsity_bundle(p2ds, n_cam_params, extra)f_scale = threshold- opt = optimize.least_squares(error_fun,- x0,- jac_sparsity=jac_sparse,- f_scale=f_scale,- x_scale='jac',- loss=loss,- ftol=ftol,- method='trf',- tr_solver='lsmr',- verbose=2 * verbose,- max_nfev=max_nfev,- args=(p2ds, n_cam_params, extra))+ opt = optimize.least_squares(+ error_fun,+ x0,+ jac_sparsity=jac_sparse,+ f_scale=f_scale,+ x_scale="jac",+ loss=loss,+ ftol=ftol,+ method="trf",+ tr_solver="lsmr",+ verbose=2 * verbose,+ max_nfev=max_nfev,+ args=(p2ds, n_cam_params, extra),+ )best_params = opt.xfor i, cam in enumerate(self.cameras):@@ -830,18 +897,18 @@ class CameraGroup:n_cams = len(self.cameras)sub = n_cam_params * n_camsn3d = p2ds.shape[1] * 3- p3ds_test = params[sub:sub+n3d].reshape(-1, 3)+ p3ds_test = params[sub : sub + n3d].reshape(-1, 3)errors = self.reprojection_error(p3ds_test, p2ds)errors_reproj = errors[good]if extra is not None:- ids = extra['ids_map']- objp = extra['objp']+ ids = extra["ids_map"]+ objp = extra["objp"]min_scale = np.min(objp[objp > 0])n_boards = int(np.max(ids)) + 1- a = sub+n3d- rvecs = params[a:a+n_boards*3].reshape(-1, 3)- tvecs = params[a+n_boards*3:a+n_boards*6].reshape(-1, 3)+ a = sub + n3d+ rvecs = params[a : a + n_boards * 3].reshape(-1, 3)+ tvecs = params[a + n_boards * 3 : a + n_boards * 6].reshape(-1, 3)expected = transform_points(objp, rvecs[ids], tvecs[ids])errors_obj = 2 * (p3ds_test - expected).ravel() / min_scaleelse:@@ -849,14 +916,13 @@ class CameraGroup:return np.hstack([errors_reproj, errors_obj])-def _jac_sparsity_bundle(self, p2ds, n_cam_params, extra):"""Given an CxNx2 array of 2D points,where N is the number of points and C is the number of cameras,compute the sparsity structure of the jacobian for bundle adjustment"""- point_indices = np.zeros(p2ds.shape, dtype='int32')- cam_indices = np.zeros(p2ds.shape, dtype='int32')+ point_indices = np.zeros(p2ds.shape, dtype="int32")+ cam_indices = np.zeros(p2ds.shape, dtype="int32")for i in range(p2ds.shape[1]):point_indices[:, i] = i@@ -867,9 +933,9 @@ class CameraGroup:good = ~np.isnan(p2ds)if extra is not None:- ids = extra['ids_map']+ ids = extra["ids_map"]n_boards = int(np.max(ids)) + 1- total_board_params = n_boards * (3 + 3) # rvecs + tvecs+ total_board_params = n_boards * (3 + 3) # rvecs + tvecselse:n_boards = 0total_board_params = 0@@ -885,7 +951,7 @@ class CameraGroup:else:n_errors = n_good_values- A_sparse = dok_matrix((n_errors, n_params), dtype='int16')+ A_sparse = dok_matrix((n_errors, n_params), dtype="int16")cam_indices_good = cam_indices[good]point_indices_good = point_indices[good]@@ -912,17 +978,21 @@ class CameraGroup:## update board rotation and translation based on error from expectedfor i in range(3):for j in range(3):- A_sparse[n_good_values + point_ix*3 + i,- total_params_reproj + ids*3 + j] = 1- A_sparse[n_good_values + point_ix*3 + i,- total_params_reproj + n_boards*3 + ids*3 + j] = 1-+ A_sparse[+ n_good_values + point_ix * 3 + i,+ total_params_reproj + ids * 3 + j,+ ] = 1+ A_sparse[+ n_good_values + point_ix * 3 + i,+ total_params_reproj + n_boards * 3 + ids * 3 + j,+ ] = 1## update point position based on error from expectedfor i in range(3):- A_sparse[n_good_values + point_ix*3 + i,- n_cams*n_cam_params + point_ix*3 + i] = 1-+ A_sparse[+ n_good_values + point_ix * 3 + i,+ n_cams * n_cam_params + point_ix * 3 + i,+ ] = 1return A_sparse@@ -937,59 +1007,67 @@ class CameraGroup:total_cam_params = len(cam_params)n_cams, n_points, _ = p2ds.shape- assert n_cams == len(self.cameras), \- "number of cameras in CameraGroup does not " \+ assert n_cams == len(self.cameras), (+ "number of cameras in CameraGroup does not ""match number of cameras in 2D points given"+ )p3ds = self.triangulate(p2ds)if extra is not None:- ids = extra['ids_map']+ ids = extra["ids_map"]n_boards = int(np.max(ids[~np.isnan(ids)])) + 1- total_board_params = n_boards * (3 + 3) # rvecs + tvecs+ total_board_params = n_boards * (3 + 3) # rvecs + tvecs# initialize to 0- rvecs = np.zeros((n_boards, 3), dtype='float64')- tvecs = np.zeros((n_boards, 3), dtype='float64')+ rvecs = np.zeros((n_boards, 3), dtype="float64")+ tvecs = np.zeros((n_boards, 3), dtype="float64")- if 'rvecs' in extra and 'tvecs' in extra:- rvecs_all = extra['rvecs']- tvecs_all = extra['tvecs']+ if "rvecs" in extra and "tvecs" in extra:+ rvecs_all = extra["rvecs"]+ tvecs_all = extra["tvecs"]for board_num in range(n_boards):point_id = np.where(ids == board_num)[0][0]cam_ids_possible = np.where(~np.isnan(p2ds[:, point_id, 0]))[0]cam_id = np.random.choice(cam_ids_possible)M_cam = self.cameras[cam_id].get_extrinsics_mat()- M_board_cam = make_M(rvecs_all[cam_id, point_id],- tvecs_all[cam_id, point_id])+ M_board_cam = make_M(+ rvecs_all[cam_id, point_id], tvecs_all[cam_id, point_id]+ )M_board = np.matmul(np.linalg.inv(M_cam), M_board_cam)rvec, tvec = get_rtvec(M_board)rvecs[board_num] = rvectvecs[board_num] = tvec-else:total_board_params = 0x0 = np.zeros(total_cam_params + p3ds.size + total_board_params)x0[:total_cam_params] = cam_params- x0[total_cam_params:total_cam_params+p3ds.size] = p3ds.ravel()+ x0[total_cam_params : total_cam_params + p3ds.size] = p3ds.ravel()if extra is not None:- start_board = total_cam_params+p3ds.size- x0[start_board:start_board + n_boards*3] = rvecs.ravel()- x0[start_board + n_boards*3:start_board + n_boards*6] = \- tvecs.ravel()+ start_board = total_cam_params + p3ds.size+ x0[start_board : start_board + n_boards * 3] = rvecs.ravel()+ x0[start_board + n_boards * 3 : start_board + n_boards * 6] = tvecs.ravel()return x0, n_cam_params- def optim_points(self, points, p3ds,- constraints=[],- constraints_weak=[],- scale_smooth=4,- scale_length=2, scale_length_weak=0.5,- reproj_error_threshold=15, reproj_loss='soft_l1',- n_deriv_smooth=1, scores=None, verbose=False):+ def optim_points(+ self,+ points,+ p3ds,+ constraints=[],+ constraints_weak=[],+ scale_smooth=4,+ scale_length=2,+ scale_length_weak=0.5,+ reproj_error_threshold=15,+ reproj_loss="soft_l1",+ n_deriv_smooth=1,+ scores=None,+ verbose=False,+ ):"""Take in an array of 2D points of shape CxNxJx2,an array of 3D points of shape NxJx3,@@ -1006,11 +1084,12 @@ class CameraGroup:(meaning that lengths of segments 0->1, 1->2, 2->3 are all constant)"""- assert points.shape[0] == len(self.cameras), \- "Invalid points shape, first dim should be equal to" \+ assert points.shape[0] == len(self.cameras), (+ "Invalid points shape, first dim should be equal to"" number of cameras ({}), but shape is {}".format(len(self.cameras), points.shape)+ )n_cams, n_frames, n_joints, _ = points.shapeconstraints = np.array(constraints)@@ -1020,52 +1099,66 @@ class CameraGroup:p3ds_med = np.apply_along_axis(medfilt_data, 0, p3ds_intp, size=7)- default_smooth = 1.0/np.mean(np.abs(np.diff(p3ds_med, axis=0)))+ default_smooth = 1.0 / np.mean(np.abs(np.diff(p3ds_med, axis=0)))scale_smooth_full = scale_smooth * default_smootht1 = time.time()x0 = self._initialize_params_triangulation(- p3ds_intp, constraints, constraints_weak)+ p3ds_intp, constraints, constraints_weak+ )x0[~np.isfinite(x0)] = 0jac = self._jac_sparsity_triangulation(- points, constraints, constraints_weak, n_deriv_smooth)-- opt2 = optimize.least_squares(self._error_fun_triangulation,- x0=x0, jac_sparsity=jac,- loss='linear',- ftol=1e-3,- verbose=2*verbose,- args=(points,- constraints,- constraints_weak,- scores,- scale_smooth_full,- scale_length,- scale_length_weak,- reproj_error_threshold,- reproj_loss,- n_deriv_smooth))-- p3ds_new2 = opt2.x[:p3ds.size].reshape(p3ds.shape)+ points, constraints, constraints_weak, n_deriv_smooth+ )++ opt2 = optimize.least_squares(+ self._error_fun_triangulation,+ x0=x0,+ jac_sparsity=jac,+ loss="linear",+ ftol=1e-3,+ verbose=2 * verbose,+ args=(+ points,+ constraints,+ constraints_weak,+ scores,+ scale_smooth_full,+ scale_length,+ scale_length_weak,+ reproj_error_threshold,+ reproj_loss,+ n_deriv_smooth,+ ),+ )++ p3ds_new2 = opt2.x[: p3ds.size].reshape(p3ds.shape)t2 = time.time()if verbose:- print('optimization took {:.2f} seconds'.format(t2 - t1))+ print("optimization took {:.2f} seconds".format(t2 - t1))return p3ds_new2-- def optim_points_possible(self, points, p3ds,- constraints=[],- constraints_weak=[],- scale_smooth=4,- scale_length=2, scale_length_weak=0.5,- reproj_error_threshold=15, reproj_loss='soft_l1',- n_deriv_smooth=1, scores=None, verbose=False):+ def optim_points_possible(+ self,+ points,+ p3ds,+ constraints=[],+ constraints_weak=[],+ scale_smooth=4,+ scale_length=2,+ scale_length_weak=0.5,+ reproj_error_threshold=15,+ reproj_loss="soft_l1",+ n_deriv_smooth=1,+ scores=None,+ verbose=False,+ ):"""Take in an array of 2D points of shape CxNxJxPx2,an array of 3D points of shape NxJx3,@@ -1083,11 +1176,12 @@ class CameraGroup:(meaning that lengths of segments 0->1, 1->2, 2->3 are all constant)"""- assert points.shape[0] == len(self.cameras), \- "Invalid points shape, first dim should be equal to" \+ assert points.shape[0] == len(self.cameras), (+ "Invalid points shape, first dim should be equal to"" number of cameras ({}), but shape is {}".format(len(self.cameras), points.shape)+ )n_cams, n_frames, n_joints, n_possible, _ = points.shapeconstraints = np.array(constraints)@@ -1097,50 +1191,60 @@ class CameraGroup:p3ds_med = np.apply_along_axis(medfilt_data, 0, p3ds_intp, size=7)- default_smooth = 1.0/np.mean(np.abs(np.diff(p3ds_med, axis=0)))+ default_smooth = 1.0 / np.mean(np.abs(np.diff(p3ds_med, axis=0)))scale_smooth_full = scale_smooth * default_smootht1 = time.time()x0 = self._initialize_params_triangulation_possible(- p3ds_intp, points, constraints=constraints, constraints_weak=constraints_weak)+ p3ds_intp,+ points,+ constraints=constraints,+ constraints_weak=constraints_weak,+ )- print('getting jacobian...')+ print("getting jacobian...")jac = self._jac_sparsity_triangulation_possible(points,constraints=constraints,constraints_weak=constraints_weak,- n_deriv_smooth=n_deriv_smooth)+ n_deriv_smooth=n_deriv_smooth,+ )beta = 5- print('starting optimization...')- opt2 = optimize.least_squares(self._error_fun_triangulation_possible,- x0=x0, jac_sparsity=jac,- loss='linear',- ftol=1e-3,- verbose=2*verbose,- args=(points,- beta,- constraints,- constraints_weak,- scores,- scale_smooth_full,- scale_length,- scale_length_weak,- reproj_error_threshold,- reproj_loss,- n_deriv_smooth))+ print("starting optimization...")+ opt2 = optimize.least_squares(+ self._error_fun_triangulation_possible,+ x0=x0,+ jac_sparsity=jac,+ loss="linear",+ ftol=1e-3,+ verbose=2 * verbose,+ args=(+ points,+ beta,+ constraints,+ constraints_weak,+ scores,+ scale_smooth_full,+ scale_length,+ scale_length_weak,+ reproj_error_threshold,+ reproj_loss,+ n_deriv_smooth,+ ),+ )params = opt2.x- p3ds_new2 = params[:p3ds.size].reshape(p3ds.shape)+ p3ds_new2 = params[: p3ds.size].reshape(p3ds.shape)bad = np.isnan(points[:, :, :, :, 0])all_bad = np.all(bad, axis=3)n_params_norm = p3ds.size + len(constraints) + len(constraints_weak)- alphas = np.zeros((n_cams, n_frames, n_joints, n_possible), dtype='float64')+ alphas = np.zeros((n_cams, n_frames, n_joints, n_possible), dtype="float64")alphas[~bad] = params[n_params_norm:]alphas_exp = np.exp(beta * alphas)@@ -1153,13 +1257,13 @@ class CameraGroup:t2 = time.time()if verbose:- print('optimization took {:.2f} seconds'.format(t2 - t1))+ print("optimization took {:.2f} seconds".format(t2 - t1))return p3ds_new2, alphas_norm-- def triangulate_optim(self, points, init_ransac=False, init_progress=False,- **kwargs):+ def triangulate_optim(+ self, points, init_ransac=False, init_progress=False, **kwargs+ ):"""Take in an array of 2D points of shape CxNxJx2, and an array of constraints of shape Kx2, whereC: number of camera@@ -1175,19 +1279,22 @@ class CameraGroup:"""- assert points.shape[0] == len(self.cameras), \- "Invalid points shape, first dim should be equal to" \+ assert points.shape[0] == len(self.cameras), (+ "Invalid points shape, first dim should be equal to"" number of cameras ({}), but shape is {}".format(len(self.cameras), points.shape)+ )n_cams, n_frames, n_joints, _ = points.shape# constraints = np.array(constraints)# constraints_weak = np.array(constraints_weak)- points_shaped = points.reshape(n_cams, n_frames*n_joints, 2)+ points_shaped = points.reshape(n_cams, n_frames * n_joints, 2)if init_ransac:- p3ds, picked, p2ds, errors = self.triangulate_ransac(points_shaped, progress=init_progress)+ p3ds, picked, p2ds, errors = self.triangulate_ransac(+ points_shaped, progress=init_progress+ )points = p2ds.reshape(points.shape)else:p3ds = self.triangulate(points_shaped, progress=init_progress)@@ -1200,29 +1307,31 @@ class CameraGroup:return self.optim_points(points, p3ds, **kwargs)--@jit(nopython=True, forceobj=True, parallel=True)- def _error_fun_triangulation(self, params, p2ds,- constraints=[],- constraints_weak=[],- scores=None,- scale_smooth=10000,- scale_length=1,- scale_length_weak=0.2,- reproj_error_threshold=100,- reproj_loss='soft_l1',- n_deriv_smooth=1):+ def _error_fun_triangulation(+ self,+ params,+ p2ds,+ constraints=[],+ constraints_weak=[],+ scores=None,+ scale_smooth=10000,+ scale_length=1,+ scale_length_weak=0.2,+ reproj_error_threshold=100,+ reproj_loss="soft_l1",+ n_deriv_smooth=1,+ ):n_cams, n_frames, n_joints, _ = p2ds.shape- n_3d = n_frames*n_joints*3+ n_3d = n_frames * n_joints * 3n_constraints = len(constraints)n_constraints_weak = len(constraints_weak)# load paramsp3ds = params[:n_3d].reshape((n_frames, n_joints, 3))- joint_lengths = np.array(params[n_3d:n_3d+n_constraints])- joint_lengths_weak = np.array(params[n_3d+n_constraints:])+ joint_lengths = np.array(params[n_3d : n_3d + n_constraints])+ joint_lengths_weak = np.array(params[n_3d + n_constraints :])# reprojection errorsp3ds_flat = p3ds.reshape(-1, 3)@@ -1235,40 +1344,39 @@ class CameraGroup:rp = reproj_error_thresholderrors_reproj = np.abs(errors_reproj)- if reproj_loss == 'huber':+ if reproj_loss == "huber":bad = errors_reproj > rp- errors_reproj[bad] = rp*(2*np.sqrt(errors_reproj[bad]/rp) - 1)- elif reproj_loss == 'linear':+ errors_reproj[bad] = rp * (2 * np.sqrt(errors_reproj[bad] / rp) - 1)+ elif reproj_loss == "linear":pass- elif reproj_loss == 'soft_l1':- errors_reproj = rp*2*(np.sqrt(1+errors_reproj/rp)-1)+ elif reproj_loss == "soft_l1":+ errors_reproj = rp * 2 * (np.sqrt(1 + errors_reproj / rp) - 1)# temporal constrainterrors_smooth = np.diff(p3ds, n=n_deriv_smooth, axis=0).ravel() * scale_smooth# joint length constraint- errors_lengths = np.empty((n_constraints, n_frames), dtype='float64')+ errors_lengths = np.empty((n_constraints, n_frames), dtype="float64")for cix, (a, b) in enumerate(constraints):lengths = np.linalg.norm(p3ds[:, a] - p3ds[:, b], axis=1)expected = joint_lengths[cix]- errors_lengths[cix] = 100*(lengths - expected)/expected+ errors_lengths[cix] = 100 * (lengths - expected) / expectederrors_lengths = errors_lengths.ravel() * scale_length- errors_lengths_weak = np.empty((n_constraints_weak, n_frames), dtype='float64')+ errors_lengths_weak = np.empty((n_constraints_weak, n_frames), dtype="float64")for cix, (a, b) in enumerate(constraints_weak):lengths = np.linalg.norm(p3ds[:, a] - p3ds[:, b], axis=1)expected = joint_lengths_weak[cix]- errors_lengths_weak[cix] = 100*(lengths - expected)/expected+ errors_lengths_weak[cix] = 100 * (lengths - expected) / expectederrors_lengths_weak = errors_lengths_weak.ravel() * scale_length_weak- return np.hstack([errors_reproj, errors_smooth,- errors_lengths, errors_lengths_weak])+ return np.hstack(+ [errors_reproj, errors_smooth, errors_lengths, errors_lengths_weak]+ )- def _error_fun_triangulation_possible(self, params, p2ds,- beta=2,- constraints=[],- constraints_weak=[],- *args):+ def _error_fun_triangulation_possible(+ self, params, p2ds, beta=2, constraints=[], constraints_weak=[], *args+ ):# extract alphas from end of params# soft argmax for picking the appropriate points from p2ds# pass the points to error_fun_triangulate_possible for residuals@@ -1277,16 +1385,16 @@ class CameraGroup:n_cams, n_frames, n_joints, n_possible, _ = p2ds.shape- n_3d = n_frames*n_joints*3+ n_3d = n_frames * n_joints * 3n_constraints = len(constraints)n_constraints_weak = len(constraints_weak)- n_params_norm = n_3d+n_constraints+n_constraints_weak+ n_params_norm = n_3d + n_constraints + n_constraints_weak# load paramsbad = np.isnan(p2ds[:, :, :, :, 0])all_bad = np.all(bad, axis=3)- alphas = np.zeros((n_cams, n_frames, n_joints, n_possible), dtype='float64')+ alphas = np.zeros((n_cams, n_frames, n_joints, n_possible), dtype="float64")alphas[~bad] = params[n_params_norm:]params_rest = np.array(params[:n_params_norm])@@ -1303,26 +1411,25 @@ class CameraGroup:p2ds_adj = np.sum(alphas_norm[:, :, :, :, None] * p2ds_test, axis=3)p2ds_adj[all_bad] = np.nan- errors = self._error_fun_triangulation(params_rest, p2ds_adj,- constraints, constraints_weak, *args)+ errors = self._error_fun_triangulation(+ params_rest, p2ds_adj, constraints, constraints_weak, *args+ )alphas_test = alphas_norm[~all_bad]errors_alphas = (1 - np.std(alphas_test, axis=1)) * 10return np.hstack([errors, errors_alphas])-- def _initialize_params_triangulation(self, p3ds,- constraints=[],- constraints_weak=[]):- joint_lengths = np.empty(len(constraints), dtype='float64')- joint_lengths_weak = np.empty(len(constraints_weak), dtype='float64')+ def _initialize_params_triangulation(+ self, p3ds, constraints=[], constraints_weak=[]+ ):+ joint_lengths = np.empty(len(constraints), dtype="float64")+ joint_lengths_weak = np.empty(len(constraints_weak), dtype="float64")for cix, (a, b) in enumerate(constraints):lengths = np.linalg.norm(p3ds[:, a] - p3ds[:, b], axis=1)joint_lengths[cix] = np.median(lengths)-for cix, (a, b) in enumerate(constraints_weak):lengths = np.linalg.norm(p3ds[:, a] - p3ds[:, b], axis=1)joint_lengths_weak[cix] = np.median(lengths)@@ -1336,8 +1443,8 @@ class CameraGroup:joint_lengths[joint_lengths == 0] = medjoint_lengths_weak[joint_lengths_weak == 0] = med- joint_lengths[joint_lengths > med+mad*5] = med- joint_lengths_weak[joint_lengths_weak > med+mad*5] = med+ joint_lengths[joint_lengths > med + mad * 5] = med+ joint_lengths_weak[joint_lengths_weak > med + mad * 5] = medreturn np.hstack([p3ds.ravel(), joint_lengths, joint_lengths_weak])@@ -1348,7 +1455,7 @@ class CameraGroup:n_cams, n_frames, n_joints, n_possible, _ = p2ds.shapegood = ~np.isnan(p2ds[:, :, :, :, 0])- alphas = np.zeros((n_cams, n_frames, n_joints, n_possible), dtype='float64')+ alphas = np.zeros((n_cams, n_frames, n_joints, n_possible), dtype="float64")alphas[:, :, :, 0] = 0params = self._initialize_params_triangulation(p3ds, **kwargs)@@ -1356,38 +1463,37 @@ class CameraGroup:return params_full- def _jac_sparsity_triangulation(self, p2ds,- constraints=[],- constraints_weak=[],- n_deriv_smooth=1):+ def _jac_sparsity_triangulation(+ self, p2ds, constraints=[], constraints_weak=[], n_deriv_smooth=1+ ):n_cams, n_frames, n_joints, _ = p2ds.shapen_constraints = len(constraints)n_constraints_weak = len(constraints_weak)p2ds_flat = p2ds.reshape((n_cams, -1, 2))- point_indices = np.zeros(p2ds_flat.shape, dtype='int32')+ point_indices = np.zeros(p2ds_flat.shape, dtype="int32")for i in range(p2ds_flat.shape[1]):point_indices[:, i] = i- point_indices_3d = np.arange(n_frames*n_joints)\- .reshape((n_frames, n_joints))+ point_indices_3d = np.arange(n_frames * n_joints).reshape((n_frames, n_joints))good = ~np.isnan(p2ds_flat)n_errors_reproj = np.sum(good)- n_errors_smooth = (n_frames-n_deriv_smooth) * n_joints * 3+ n_errors_smooth = (n_frames - n_deriv_smooth) * n_joints * 3n_errors_lengths = n_constraints * n_framesn_errors_lengths_weak = n_constraints_weak * n_frames- n_errors = n_errors_reproj + n_errors_smooth + \- n_errors_lengths + n_errors_lengths_weak+ n_errors = (+ n_errors_reproj + n_errors_smooth + n_errors_lengths + n_errors_lengths_weak+ )- n_3d = n_frames*n_joints*3+ n_3d = n_frames * n_joints * 3n_params = n_3d + n_constraints + n_constraints_weakpoint_indices_good = point_indices[good]- A_sparse = dok_matrix((n_errors, n_params), dtype='int16')+ A_sparse = dok_matrix((n_errors, n_params), dtype="int16")# constraints for reprojection errorsix_reproj = np.arange(n_errors_reproj)@@ -1395,20 +1501,20 @@ class CameraGroup:A_sparse[ix_reproj, point_indices_good * 3 + k] = 1# sparse constraints for smoothness in time- frames = np.arange(n_frames-n_deriv_smooth)+ frames = np.arange(n_frames - n_deriv_smooth)for j in range(n_joints):- for n in range(n_deriv_smooth+1):+ for n in range(n_deriv_smooth + 1):pa = point_indices_3d[frames, j]- pb = point_indices_3d[frames+n, j]+ pb = point_indices_3d[frames + n, j]for k in range(3):- A_sparse[n_errors_reproj + pa*3 + k, pb*3 + k] = 1+ A_sparse[n_errors_reproj + pa * 3 + k, pb * 3 + k] = 1## -- strong constraints --# joint lengths should change with joint lengths errorsstart = n_errors_reproj + n_errors_smoothframes = np.arange(n_frames)for cix, (a, b) in enumerate(constraints):- A_sparse[start + cix*n_frames + frames, n_3d+cix] = 1+ A_sparse[start + cix * n_frames + frames, n_3d + cix] = 1# points should change accordingly to match joint lengths tooframes = np.arange(n_frames)@@ -1416,15 +1522,15 @@ class CameraGroup:pa = point_indices_3d[frames, a]pb = point_indices_3d[frames, b]for k in range(3):- A_sparse[start + cix*n_frames + frames, pa*3 + k] = 1- A_sparse[start + cix*n_frames + frames, pb*3 + k] = 1+ A_sparse[start + cix * n_frames + frames, pa * 3 + k] = 1+ A_sparse[start + cix * n_frames + frames, pb * 3 + k] = 1## -- weak constraints --# joint lengths should change with joint lengths errorsstart = n_errors_reproj + n_errors_smooth + n_errors_lengthsframes = np.arange(n_frames)for cix, (a, b) in enumerate(constraints_weak):- A_sparse[start + cix*n_frames + frames, n_3d + n_constraints + cix] = 1+ A_sparse[start + cix * n_frames + frames, n_3d + n_constraints + cix] = 1# points should change accordingly to match joint lengths tooframes = np.arange(n_frames)@@ -1432,8 +1538,8 @@ class CameraGroup:pa = point_indices_3d[frames, a]pb = point_indices_3d[frames, b]for k in range(3):- A_sparse[start + cix*n_frames + frames, pa*3 + k] = 1- A_sparse[start + cix*n_frames + frames, pb*3 + k] = 1+ A_sparse[start + cix * n_frames + frames, pa * 3 + k] = 1+ A_sparse[start + cix * n_frames + frames, pb * 3 + k] = 1return A_sparse@@ -1454,17 +1560,22 @@ class CameraGroup:n_errors, n_params = A_sparse.shape- B_sparse = dok_matrix((n_errors + n_errors_alphas, n_params + n_alphas), dtype='int16')+ B_sparse = dok_matrix(+ (n_errors + n_errors_alphas, n_params + n_alphas), dtype="int16"+ )for r, c in zip(*A_sparse.nonzero()):B_sparse[r, c] = A_sparse[r, c]- point_indices_2d = np.arange(n_cams*n_frames*n_joints)\- .reshape(n_cams, n_frames, n_joints)+ point_indices_2d = np.arange(n_cams * n_frames * n_joints).reshape(+ n_cams, n_frames, n_joints+ )point_indices_2d_rep = np.repeat(point_indices_2d[:, :, :, None], 2, axis=3)point_indices_2d_good = point_indices_2d_rep[~np.isnan(p2ds)]point_indices_good = point_indices_2d[any_good]- alpha_indices = np.zeros((n_cams, n_frames, n_joints, n_possible), dtype='int64')+ alpha_indices = np.zeros(+ (n_cams, n_frames, n_joints, n_possible), dtype="int64"+ )for pnum in range(n_possible):alpha_indices[:, :, :, pnum] = point_indices_2d@@ -1476,8 +1587,7 @@ class CameraGroup:point_indices_2d_good_find[p].append(ix)for ix, alpha_index in enumerate(alpha_indices_good):- B_sparse[point_indices_2d_good_find[alpha_index],- n_params + ix] = 1+ B_sparse[point_indices_2d_good_find[alpha_index], n_params + ix] = 1# alphas should change according to the alpha errorspoint_indices_good_find = dict()@@ -1533,15 +1643,24 @@ class CameraGroup:else:return np.mean(errors)- def calibrate_rows(self, all_rows, board,- init_intrinsics=True, init_extrinsics=True, verbose=True,- **kwargs):+ def calibrate_rows(+ self,+ all_rows,+ board,+ init_intrinsics=True,+ init_extrinsics=True,+ verbose=True,+ **kwargs+ ):"""Assumes camera sizes are set properly"""for rows, camera in zip(all_rows, self.cameras):size = camera.get_size()- assert size is not None, \- "Camera with name {} has no specified frame size".format(camera.get_name())+ assert (+ size is not None+ ), "Camera with name {} has no specified frame size".format(+ camera.get_name()+ )if init_intrinsics:objp, imgp = board.get_all_calibration_points(rows)@@ -1566,7 +1685,7 @@ class CameraGroup:error = self.bundle_adjust_iter(imgp, extra, verbose=verbose, **kwargs)- return error,merged+ return error, mergeddef get_rows_videos(self, videos, board, verbose=True):all_rows = []@@ -1574,9 +1693,11 @@ class CameraGroup:for cix, (cam, cam_videos) in enumerate(zip(self.cameras, videos)):rows_cam = []for vnum, vidname in enumerate(cam_videos):- if verbose: print(vidname)+ if verbose:+ print(vidname)rows = board.detect_video(vidname, prefix=vnum, progress=verbose)- if verbose: print("{} boards detected".format(len(rows)))+ if verbose:+ print("{} boards detected".format(len(rows)))rows_cam.extend(rows)all_rows.append(rows_cam)@@ -1587,12 +1708,18 @@ class CameraGroup:rows_cam = []for vnum, vidname in enumerate(cam_videos):params = get_video_params(vidname)- size = (params['width'], params['height'])+ size = (params["width"], params["height"])cam.set_size(size)- def calibrate_videos(self, videos, board,- init_intrinsics=True, init_extrinsics=True, verbose=True,- **kwargs):+ def calibrate_videos(+ self,+ videos,+ board,+ init_intrinsics=True,+ init_extrinsics=True,+ verbose=True,+ **kwargs+ ):"""Takes as input a list of list of video filenames, one list of each camera.Also takes a board which specifies what should be detected in the videos"""@@ -1600,10 +1727,14 @@ class CameraGroup:if init_extrinsics:self.set_camera_sizes_videos(videos)- error,merged = self.calibrate_rows(all_rows, board,- init_intrinsics=init_intrinsics,- init_extrinsics=init_extrinsics,- verbose=verbose, **kwargs)+ error, merged = self.calibrate_rows(+ all_rows,+ board,+ init_intrinsics=init_intrinsics,+ init_extrinsics=init_extrinsics,+ verbose=verbose,+ **kwargs+ )return error, mergeddef get_dicts(self):@@ -1615,7 +1746,7 @@ class CameraGroup:def from_dicts(arr):cameras = []for d in arr:- if 'fisheye' in d and d['fisheye']:+ if "fisheye" in d and d["fisheye"]:cam = FisheyeCamera.from_dict(d)else:cam = Camera.from_dict(d)@@ -1638,19 +1769,19 @@ class CameraGroup:def dump(self, fname):dicts = self.get_dicts()- names = ['cam_{}'.format(i) for i in range(len(dicts))]+ names = ["cam_{}".format(i) for i in range(len(dicts))]master_dict = dict(zip(names, dicts))- master_dict['metadata'] = self.metadata- with open(fname, 'w') as f:+ master_dict["metadata"] = self.metadata+ with open(fname, "w") as f:toml.dump(master_dict, f)def load(fname):master_dict = toml.load(fname)keys = sorted(master_dict.keys())- items = [master_dict[k] for k in keys if k != 'metadata']+ items = [master_dict[k] for k in keys if k != "metadata"]cgroup = CameraGroup.from_dicts(items)- if 'metadata' in master_dict:- cgroup.metadata = master_dict['metadata']+ if "metadata" in master_dict:+ cgroup.metadata = master_dict["metadata"]return cgroupdef resize_cameras(self, scale):diff --git a/freemocap/fmc_deeplabcut.py b/freemocap/fmc_deeplabcut.pyindex 1893b304..6a914103 100644--- a/freemocap/fmc_deeplabcut.py+++ b/freemocap/fmc_deeplabcut.py@@ -13,46 +13,50 @@ from pathlib import Pathimport h5py+def parseDeepLabCut(+ session,+): # NOTE this is assuming both the dlc config file and dlc data h5 foles are in the same folder specified in the session.dlcDataPath, if that is not the case either dlcconfig or h5files variable need to be changed---def parseDeepLabCut(session): # NOTE this is assuming both the dlc config file and dlc data h5 foles are in the same folder specified in the session.dlcDataPath, if that is not the case either dlcconfig or h5files variable need to be changed-- dlcConfig = session.dlcConfigPath #File path for dlcConfig file-+ dlcConfig = session.dlcConfigPath # File path for dlcConfig filewith open(dlcConfig) as configFile:- #Open the config file- configList = yaml.load(configFile, Loader = yaml.FullLoader)- for item, doc in configList.items(): #For each item in the config file- if item == 'bodyparts': #If the item is bodyparts- bodypartList = doc #Set that list to a variable- if item == 'skeleton': #If the item is skeleton- skeletonList = doc #Set that list to a variable-- h5files =session.dlcDataPath.glob('*.h5') #Take all h5 files from folder-- numCams = int(session.numCams)#Get num of cams- numFrames = session.numFrames+10#Get num of frames-- numPoints = len(bodypartList)# Get amount of points tracked- dlcData_nCams_nFrames_nImgPts_XYC = np.ndarray([numCams, numFrames, numPoints, 3]) # Create empty array for dlc points-- nn=0#counter for each camera- for data in h5files:#Loop throuxgh each camera- with h5py.File(data) as f:#Open each h5 file- fullDataGroup = f.get('df_with_missing')#Open main h5 group- dataTable = fullDataGroup.get('table')#Open datatable with all DLC tracked points- for ii in range(len(dataTable)):#Loop through each frame- dataFromOneFrame = dataTable[ii] #Assign frame to varible- idx =(len(dataFromOneFrame[1])/3) #index for reshaping data- sortedFrame = np.array(dataFromOneFrame[1]) #Put data in numoy array- dlcData_nCams_nFrames_nImgPts_XYC[nn,ii,:,:] =sortedFrame.reshape(int(idx),3) #Reshape the data into the correct form- nn+=1-+ # Open the config file+ configList = yaml.load(configFile, Loader=yaml.FullLoader)+ for item, doc in configList.items(): # For each item in the config file+ if item == "bodyparts": # If the item is bodyparts+ bodypartList = doc # Set that list to a variable+ if item == "skeleton": # If the item is skeleton+ skeletonList = doc # Set that list to a variable++ h5files = session.dlcDataPath.glob("*.h5") # Take all h5 files from folder++ numCams = int(session.numCams) # Get num of cams+ numFrames = session.numFrames + 10 # Get num of frames++ numPoints = len(bodypartList) # Get amount of points tracked+ dlcData_nCams_nFrames_nImgPts_XYC = np.ndarray(+ [numCams, numFrames, numPoints, 3]+ ) # Create empty array for dlc points++ nn = 0 # counter for each camera+ for data in h5files: # Loop throuxgh each camera+ with h5py.File(data) as f: # Open each h5 file+ fullDataGroup = f.get("df_with_missing") # Open main h5 group+ dataTable = fullDataGroup.get(+ "table"+ ) # Open datatable with all DLC tracked points+ for ii in range(len(dataTable)): # Loop through each frame+ dataFromOneFrame = dataTable[ii] # Assign frame to varible+ idx = len(dataFromOneFrame[1]) / 3 # index for reshaping data+ sortedFrame = np.array(dataFromOneFrame[1]) # Put data in numoy array+ dlcData_nCams_nFrames_nImgPts_XYC[nn, ii, :, :] = sortedFrame.reshape(+ int(idx), 3+ ) # Reshape the data into the correct form+ nn += 1+if session.debug:fig = plt.figure()-+ax1 = fig.add_subplot(221)ax2 = fig.add_subplot(222)ax3 = fig.add_subplot(223)@@ -62,21 +66,36 @@ def parseDeepLabCut(session): # NOTE this is assuming both the dlc config file acam2im = session.firstImage_nCams_list[1]cam3im = session.firstImage_nCams_list[2]cam4im = session.firstImage_nCams_list[3]-+ax1.imshow(cam1im)- ax1.plot(dlcData_nCams_nFrames_nImgPts_XYC[0,0,:,0],dlcData_nCams_nFrames_nImgPts_XYC[0,0,:,1])+ ax1.plot(+ dlcData_nCams_nFrames_nImgPts_XYC[0, 0, :, 0],+ dlcData_nCams_nFrames_nImgPts_XYC[0, 0, :, 1],+ )ax2.imshow(cam2im)- ax2.plot(dlcData_nCams_nFrames_nImgPts_XYC[1,0,:,0],dlcData_nCams_nFrames_nImgPts_XYC[1,0,:,1])+ ax2.plot(+ dlcData_nCams_nFrames_nImgPts_XYC[1, 0, :, 0],+ dlcData_nCams_nFrames_nImgPts_XYC[1, 0, :, 1],+ )ax3.imshow(cam3im)- ax3.plot(dlcData_nCams_nFrames_nImgPts_XYC[2,0,:,0],dlcData_nCams_nFrames_nImgPts_XYC[2,0,:,1])+ ax3.plot(+ dlcData_nCams_nFrames_nImgPts_XYC[2, 0, :, 0],+ dlcData_nCams_nFrames_nImgPts_XYC[2, 0, :, 1],+ )ax4.imshow(cam4im)- ax4.plot(dlcData_nCams_nFrames_nImgPts_XYC[3,0,:,0],dlcData_nCams_nFrames_nImgPts_XYC[3,0,:,1])+ ax4.plot(+ dlcData_nCams_nFrames_nImgPts_XYC[3, 0, :, 0],+ dlcData_nCams_nFrames_nImgPts_XYC[3, 0, :, 1],+ )plt.show()- np.save(session.dataArrayPath/'deepLabCutData_2d.npy', dlcData_nCams_nFrames_nImgPts_XYC)+ np.save(+ session.dataArrayPath / "deepLabCutData_2d.npy",+ dlcData_nCams_nFrames_nImgPts_XYC,+ )return dlcData_nCams_nFrames_nImgPts_XYCdiff --git a/freemocap/fmc_mediapipe.py b/freemocap/fmc_mediapipe.pyindex 255a66cb..ccf719c1 100644--- a/freemocap/fmc_mediapipe.py+++ b/freemocap/fmc_mediapipe.py@@ -6,63 +6,80 @@ from rich.progress import Progressfrom ruamel.yaml import YAMLimport cv2import mediapipe as mp+# from numba import jitmp_drawing = mp.solutions.drawing_utilsmp_holistic = mp.solutions.holistic+def runMediaPipe(session, dummyRun=False):- mediaPipe_jsonPathList = [] #list to hold the paths to the json files+ mediaPipe_jsonPathList = [] # list to hold the paths to the json filesmediaPipe_imgPathList = []mediaPipe_imgPathList_yaml = []mediaPipe_jsonPathList_yaml = []-+with mp_holistic.Holistic() as holistic:- eachCamerasData = [] #Create an empty list that holds each cameras data- for thisVidPath in session.syncedVidPath.iterdir(): #Run MediaPipe 'Holistic' (body, hands, face) tracker on each video in the raw video folder- if thisVidPath.suffix =='.mp4': #NOTE - at some point we should build some list of 'synced video names' and check against that-+ eachCamerasData = [] # Create an empty list that holds each cameras data+ for (+ thisVidPath+ ) in (+ session.syncedVidPath.iterdir()+ ): # Run MediaPipe 'Holistic' (body, hands, face) tracker on each video in the raw video folder+ if (+ thisVidPath.suffix == ".mp4"+ ): # NOTE - at some point we should build some list of 'synced video names' and check against that+vidPath = session.mediaPipeDataPath / thisVidPath.stem- jsonPath = vidPath / 'json'- jsonPath.mkdir(parents=True, exist_ok=True) #this camera's json files (with keypoints)- imgPath = vidPath / 'images'- imgPath.mkdir(parents=True, exist_ok=True)+ jsonPath = vidPath / "json"+ jsonPath.mkdir(+ parents=True, exist_ok=True+ ) # this camera's json files (with keypoints)+ imgPath = vidPath / "images"+ imgPath.mkdir(parents=True, exist_ok=True)mediaPipe_jsonPathList.append(jsonPath)mediaPipe_imgPathList.append(imgPath)mediaPipe_imgPathList_yaml.append(str(imgPath))mediaPipe_jsonPathList_yaml.append(str(jsonPath))- mediaPipe_dataList = [] #Create an empty list for mediapipes data+ mediaPipe_dataList = [] # Create an empty list for mediapipes dataif not dummyRun:cap = cv2.VideoCapture(str(thisVidPath))-+frameNum = -1numFrames = cap.get(cv2.CAP_PROP_FRAME_COUNT)- success, image = cap.read() #load first image from video+ success, image = cap.read() # load first image from videowith Progress() as progress:- progressBar = progress.add_task('[magenta]MediaPiping: {}'.format(thisVidPath.name), total=numFrames) #make progressbar+ progressBar = progress.add_task(+ "[magenta]MediaPiping: {}".format(thisVidPath.name),+ total=numFrames,+ ) # make progressbarwhile success:-- if frameNum%5==0:- progress.update(progressBar, advance=5) #increment progress bar everyh 5th frame++ if frameNum % 5 == 0:+ progress.update(+ progressBar, advance=5+ ) # increment progress bar everyh 5th frameframeNum += 1image_height, image_width, _ = image.shape-- results = holistic.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) # THIS IS WHERE THE MAGIC HAPENS (ps. Convert the BGR image to RGB before processing.)-++ results = holistic.process(+ cv2.cvtColor(image, cv2.COLOR_BGR2RGB)+ ) # THIS IS WHERE THE MAGIC HAPENS (ps. Convert the BGR image to RGB before processing.)+try:- results.pose_landmarks.landmark+ results.pose_landmarks.landmarktry:- results.left_hand_landmarks.landmark+ results.left_hand_landmarks.landmarktry:- results.right_hand_landmarks.landmark+ results.right_hand_landmarks.landmarktry:results.face_landmarks.landmark- f=9+ f = 9except:passexcept:@@ -72,128 +89,200 @@ def runMediaPipe(session, dummyRun=False):except:pass- mediaPipe_dataList.append(results)#Append data to mediapipe data list-+ mediaPipe_dataList.append(+ results+ ) # Append data to mediapipe data listsaveImages = Trueif saveImages:# Draw pose, left and right hands, and face landmarks on the image.annotated_image = image.copy()mp_drawing.draw_landmarks(- annotated_image, results.face_landmarks, mp_holistic.FACE_CONNECTIONS)+ annotated_image,+ results.face_landmarks,+ mp_holistic.FACE_CONNECTIONS,+ )mp_drawing.draw_landmarks(- annotated_image, results.left_hand_landmarks, mp_holistic.HAND_CONNECTIONS)+ annotated_image,+ results.left_hand_landmarks,+ mp_holistic.HAND_CONNECTIONS,+ )mp_drawing.draw_landmarks(- annotated_image, results.right_hand_landmarks, mp_holistic.HAND_CONNECTIONS)+ annotated_image,+ results.right_hand_landmarks,+ mp_holistic.HAND_CONNECTIONS,+ )mp_drawing.draw_landmarks(- annotated_image, results.pose_landmarks, mp_holistic.POSE_CONNECTIONS)+ annotated_image,+ results.pose_landmarks,+ mp_holistic.POSE_CONNECTIONS,+ )- #save annotated image+ # save annotated imageframeName = str(frameNum).zfill(6)- cv2.imwrite(str(imgPath)+ '/' + frameName + '.png', annotated_image)-- #load the next image (will break `while-loop` on the last frame)- success, image = cap.read()#load next image from video- f=9- eachCamerasData.append(mediaPipe_dataList)#Append that cameras data for every frame to the camera datalist+ cv2.imwrite(+ str(imgPath) + "/" + frameName + ".png",+ annotated_image,+ )++ # load the next image (will break `while-loop` on the last frame)+ success, image = cap.read() # load next image from video+ f = 9+ eachCamerasData.append(+ mediaPipe_dataList+ ) # Append that cameras data for every frame to the camera datalistsession.image_height = image_heightsession.image_width = image_width-+session.mediaPipe_jsonPathList = mediaPipe_jsonPathListsession.mediaPipe_imgPathList = mediaPipe_imgPathListyaml = YAML()data = yaml.load(session.yamlPath)- data['mediaPipe_imgPathList'] = mediaPipe_imgPathList_yaml- data['mediaPipe_jsonPathList'] = mediaPipe_jsonPathList_yaml+ data["mediaPipe_imgPathList"] = mediaPipe_imgPathList_yaml+ data["mediaPipe_jsonPathList"] = mediaPipe_jsonPathList_yamlyaml.dump(data, session.yamlPath)session.mediaPipeData = eachCamerasData-++# @jit(nopython=True)def parseMediaPipe(session):- numCams = len(session.mediaPipeData) #Get number of cameras- numFrames = len(session.mediaPipeData[0]) #Get number of frames- #numBodyPoints = len(np.max(session.mediaPipeData[0][:].pose_landmarks.landmark[:]))#Get number of body points- #numFacePoints = len(np.max(session.mediaPipeData[0][:].face_landmarks.landmark[:]))#Get number of face points- #numLeftHandPoints = len(np.max(session.mediaPipeData[0][:].left_hand_landmarks.landmark[:]))#Get number of right hand points- #numRightHandPoints = len(np.max(session.mediaPipeData[0][:].right_hand_landmarks.landmark[:]))#Get number of left hand points- numBodyPoints = 33- numFacePoints = 468- numHandPoints = 21-- numTrackedPoints = numBodyPoints+numHandPoints*2+numFacePoints #Get total points-- #Create array of nans the size of number of cams, frame, points, XYC- mediaPipeData_nCams_nFrames_nImgPts_XYC = np.empty((int(numCams),int(numFrames),int(numTrackedPoints),3)) # create empty array- mediaPipeData_nCams_nFrames_nImgPts_XYC[:] = np.NaN #Fill it with NaNs!-- for camNum in range(numCams):#Loop through each camera- for frNum in range(numFrames): #Loop through each frame- # make empty arrays for thisFrame's data- thisFrame_X = np.empty(numTrackedPoints)- thisFrame_X[:] = np.nan- thisFrame_Y = thisFrame_X.copy()- thisFrame_C = thisFrame_X.copy()-- fullFrame = True- if frNum == 107:- f=9- try:- #pull out ThisFrame's mediapipe data (`mpData.pose_landmarks.landmark` returns something iterable Β―\_(γ)_/Β―)- thisFrame_poseDataLandMarks = session.mediaPipeData[camNum][frNum].pose_landmarks.landmark # body ('pose') data- #stuff body data into pre-allocated nan array- thisFrame_X[:numBodyPoints] = [pp.x for pp in thisFrame_poseDataLandMarks] #PoseX data - Normalized screen coords (in range [0, 1]) - need multiply by image resultion for pixels- thisFrame_Y[:numBodyPoints] = [pp.y for pp in thisFrame_poseDataLandMarks] #PoseY data- thisFrame_C[:numBodyPoints] = [pp.visibility for pp in thisFrame_poseDataLandMarks] #'visibility' is MediaPose's 'confidence' measure in range [0,1]- except:- fullFrame = False--- # Right hand data- try:- thisFrame_rHandDataLandMarks = session.mediaPipeData[camNum][frNum].right_hand_landmarks.landmark #right hand data- thisFrame_X[numBodyPoints+1:numBodyPoints+1+numHandPoints] = [pp.x for pp in thisFrame_rHandDataLandMarks] #PoseX data - Normalized screen coords (in range [0, 1]) - need multiply by image resultion for pixels- thisFrame_Y[numBodyPoints+1:numBodyPoints+1+numHandPoints] = [pp.y for pp in thisFrame_rHandDataLandMarks] #PoseY data- thisFrame_C[numBodyPoints+1:numBodyPoints+1+numHandPoints] = [pp.visibility for pp in thisFrame_rHandDataLandMarks] #'visibility' is MediaPose's 'confidence' measure in range [0,1]- except:- fullFrame = False-- # Left hand data- try:- thisFrame_lHandDataLandMarks = session.mediaPipeData[camNum][frNum].left_hand_landmarks.landmark #left hand data- thisFrame_X[numBodyPoints+numHandPoints+1:numBodyPoints+1+numHandPoints*2] = [pp.x for pp in thisFrame_lHandDataLandMarks] #PoseX data - Normalized screen coords (in range [0, 1]) - need multiply by image resultion for pixels- thisFrame_Y[numBodyPoints+numHandPoints+1:numBodyPoints+1+numHandPoints*2] = [pp.y for pp in thisFrame_lHandDataLandMarks] #PoseY data- thisFrame_C[numBodyPoints+numHandPoints+1:numBodyPoints+1+numHandPoints*2] = [pp.visibility for pp in thisFrame_lHandDataLandMarks] #'visibility' is MediaPose's 'confidence' measure in range [0,1]- except:- fullFrame = False-- #FaceMeshData- try:- thisFrame_faceDataLandMarks = session.mediaPipeData[camNum][frNum].face_landmarks.landmark # face (mesh) data- thisFrame_X[numBodyPoints+numHandPoints*2+1:-1] = [pp.x for pp in thisFrame_faceDataLandMarks] #PoseX data - Normalized screen coords (in range [0, 1]) - need multiply by image resultion for pixels- thisFrame_Y[numBodyPoints+numHandPoints*2:-1] = [pp.y for pp in thisFrame_faceDataLandMarks] #PoseY data- #NOTE - There's also Z data in here- thisFrame_C[numBodyPoints+numHandPoints*2:-1] = [pp.visibility for pp in thisFrame_faceDataLandMarks] #'visibility' is MediaPose's 'confidence' measure in range [0,1]- except:- fullFrame = False-- if fullFrame:- f=9-- #stuff this frame's data into pre-allocated mediaPipeData_.... array- mediaPipeData_nCams_nFrames_nImgPts_XYC[camNum,frNum,:,0] = thisFrame_X- mediaPipeData_nCams_nFrames_nImgPts_XYC[camNum,frNum,:,1] = thisFrame_Y- mediaPipeData_nCams_nFrames_nImgPts_XYC[camNum,frNum,:,2] = thisFrame_C--- #convert from normalized screen coordinates to pixel coordinates- mediaPipeData_nCams_nFrames_nImgPts_XYC[:,:,:,0] *= session.image_height- mediaPipeData_nCams_nFrames_nImgPts_XYC[:,:,:,1] *= session.image_width-- np.save(session.dataArrayPath/'mediaPipe_2d.npy', mediaPipeData_nCams_nFrames_nImgPts_XYC)---- return mediaPipeData_nCams_nFrames_nImgPts_XYC\ No newline at end of file+ numCams = len(session.mediaPipeData) # Get number of cameras+ numFrames = len(session.mediaPipeData[0]) # Get number of frames+ # numBodyPoints = len(np.max(session.mediaPipeData[0][:].pose_landmarks.landmark[:]))#Get number of body points+ # numFacePoints = len(np.max(session.mediaPipeData[0][:].face_landmarks.landmark[:]))#Get number of face points+ # numLeftHandPoints = len(np.max(session.mediaPipeData[0][:].left_hand_landmarks.landmark[:]))#Get number of right hand points+ # numRightHandPoints = len(np.max(session.mediaPipeData[0][:].right_hand_landmarks.landmark[:]))#Get number of left hand points+ numBodyPoints = 33+ numFacePoints = 468+ numHandPoints = 21++ numTrackedPoints = (+ numBodyPoints + numHandPoints * 2 + numFacePoints+ ) # Get total points++ # Create array of nans the size of number of cams, frame, points, XYC+ mediaPipeData_nCams_nFrames_nImgPts_XYC = np.empty(+ (int(numCams), int(numFrames), int(numTrackedPoints), 3)+ ) # create empty array+ mediaPipeData_nCams_nFrames_nImgPts_XYC[:] = np.NaN # Fill it with NaNs!++ for camNum in range(numCams): # Loop through each camera+ for frNum in range(numFrames): # Loop through each frame+ # make empty arrays for thisFrame's data+ thisFrame_X = np.empty(numTrackedPoints)+ thisFrame_X[:] = np.nan+ thisFrame_Y = thisFrame_X.copy()+ thisFrame_C = thisFrame_X.copy()++ fullFrame = True+ if frNum == 107:+ f = 9+ try:+ # pull out ThisFrame's mediapipe data (`mpData.pose_landmarks.landmark` returns something iterable Β―\_(γ)_/Β―)+ thisFrame_poseDataLandMarks = session.mediaPipeData[camNum][+ frNum+ ].pose_landmarks.landmark # body ('pose') data+ # stuff body data into pre-allocated nan array+ thisFrame_X[:numBodyPoints] = [+ pp.x for pp in thisFrame_poseDataLandMarks+ ] # PoseX data - Normalized screen coords (in range [0, 1]) - need multiply by image resultion for pixels+ thisFrame_Y[:numBodyPoints] = [+ pp.y for pp in thisFrame_poseDataLandMarks+ ] # PoseY data+ thisFrame_C[:numBodyPoints] = [+ pp.visibility for pp in thisFrame_poseDataLandMarks+ ] #'visibility' is MediaPose's 'confidence' measure in range [0,1]+ except:+ fullFrame = False++ # Right hand data+ try:+ thisFrame_rHandDataLandMarks = session.mediaPipeData[camNum][+ frNum+ ].right_hand_landmarks.landmark # right hand data+ thisFrame_X[numBodyPoints + 1 : numBodyPoints + 1 + numHandPoints] = [+ pp.x for pp in thisFrame_rHandDataLandMarks+ ] # PoseX data - Normalized screen coords (in range [0, 1]) - need multiply by image resultion for pixels+ thisFrame_Y[numBodyPoints + 1 : numBodyPoints + 1 + numHandPoints] = [+ pp.y for pp in thisFrame_rHandDataLandMarks+ ] # PoseY data+ thisFrame_C[numBodyPoints + 1 : numBodyPoints + 1 + numHandPoints] = [+ pp.visibility for pp in thisFrame_rHandDataLandMarks+ ] #'visibility' is MediaPose's 'confidence' measure in range [0,1]+ except:+ fullFrame = False++ # Left hand data+ try:+ thisFrame_lHandDataLandMarks = session.mediaPipeData[camNum][+ frNum+ ].left_hand_landmarks.landmark # left hand data+ thisFrame_X[+ numBodyPoints+ + numHandPoints+ + 1 : numBodyPoints+ + 1+ + numHandPoints * 2+ ] = [+ pp.x for pp in thisFrame_lHandDataLandMarks+ ] # PoseX data - Normalized screen coords (in range [0, 1]) - need multiply by image resultion for pixels+ thisFrame_Y[+ numBodyPoints+ + numHandPoints+ + 1 : numBodyPoints+ + 1+ + numHandPoints * 2+ ] = [+ pp.y for pp in thisFrame_lHandDataLandMarks+ ] # PoseY data+ thisFrame_C[+ numBodyPoints+ + numHandPoints+ + 1 : numBodyPoints+ + 1+ + numHandPoints * 2+ ] = [+ pp.visibility for pp in thisFrame_lHandDataLandMarks+ ] #'visibility' is MediaPose's 'confidence' measure in range [0,1]+ except:+ fullFrame = False++ # FaceMeshData+ try:+ thisFrame_faceDataLandMarks = session.mediaPipeData[camNum][+ frNum+ ].face_landmarks.landmark # face (mesh) data+ thisFrame_X[numBodyPoints + numHandPoints * 2 + 1 : -1] = [+ pp.x for pp in thisFrame_faceDataLandMarks+ ] # PoseX data - Normalized screen coords (in range [0, 1]) - need multiply by image resultion for pixels+ thisFrame_Y[numBodyPoints + numHandPoints * 2 : -1] = [+ pp.y for pp in thisFrame_faceDataLandMarks+ ] # PoseY data+ # NOTE - There's also Z data in here+ thisFrame_C[numBodyPoints + numHandPoints * 2 : -1] = [+ pp.visibility for pp in thisFrame_faceDataLandMarks+ ] #'visibility' is MediaPose's 'confidence' measure in range [0,1]+ except:+ fullFrame = False++ if fullFrame:+ f = 9++ # stuff this frame's data into pre-allocated mediaPipeData_.... array+ mediaPipeData_nCams_nFrames_nImgPts_XYC[camNum, frNum, :, 0] = thisFrame_X+ mediaPipeData_nCams_nFrames_nImgPts_XYC[camNum, frNum, :, 1] = thisFrame_Y+ mediaPipeData_nCams_nFrames_nImgPts_XYC[camNum, frNum, :, 2] = thisFrame_C++ # convert from normalized screen coordinates to pixel coordinates+ mediaPipeData_nCams_nFrames_nImgPts_XYC[:, :, :, 0] *= session.image_height+ mediaPipeData_nCams_nFrames_nImgPts_XYC[:, :, :, 1] *= session.image_width++ np.save(+ session.dataArrayPath / "mediaPipe_2d.npy",+ mediaPipeData_nCams_nFrames_nImgPts_XYC,+ )++ return mediaPipeData_nCams_nFrames_nImgPts_XYCdiff --git a/freemocap/fmc_openpose.py b/freemocap/fmc_openpose.pyindex 7294161d..033ed2bb 100644--- a/freemocap/fmc_openpose.py+++ b/freemocap/fmc_openpose.py@@ -6,124 +6,193 @@ import numpy as npfrom rich.progress import trackfrom ruamel.yaml import YAMLfrom pathlib import Path+# from numba import jit+def runOpenPose(session, dummyRun=False):if not dummyRun:os.chdir(session.openPoseExePath)-- openPose_jsonPathList = [] #list to hold the paths to the json files++ openPose_jsonPathList = [] # list to hold the paths to the json filesopenPose_imgPathList = []openPose_imgPathList_yaml = []openPose_jsonPathList_yaml = []-- for thisVidPath in session.syncedVidPath.iterdir(): #Run OpenPose ('Windows Portable Demo') on each video in the raw video folder- if thisVidPath.suffix =='.mp4': #NOTE - build some list of 'synced video names' and check against that- print('OpenPosing: ', thisVidPath.name )++ for (+ thisVidPath+ ) in (+ session.syncedVidPath.iterdir()+ ): # Run OpenPose ('Windows Portable Demo') on each video in the raw video folder+ if (+ thisVidPath.suffix == ".mp4"+ ): # NOTE - build some list of 'synced video names' and check against that+ print("OpenPosing: ", thisVidPath.name)vidPath = session.openPoseDataPath / thisVidPath.stem- jsonPath = vidPath / 'json'- jsonPath.mkdir(parents=True, exist_ok=True) #this camera's json files (with keypoints)- imgPath = vidPath / 'img'- imgPath.mkdir(parents=True, exist_ok=True)+ jsonPath = vidPath / "json"+ jsonPath.mkdir(+ parents=True, exist_ok=True+ ) # this camera's json files (with keypoints)+ imgPath = vidPath / "img"+ imgPath.mkdir(parents=True, exist_ok=True)openPose_jsonPathList.append(jsonPath)openPose_imgPathList.append(imgPath)openPose_imgPathList_yaml.append(str(imgPath))openPose_jsonPathList_yaml.append(str(jsonPath))if not dummyRun:- subprocess.run(['bin\OpenPoseDemo.exe', '--video', str(thisVidPath), '--write_json', str(jsonPath),'--write_images', str(imgPath),'--write_images_format','png','--net_resolution','-1x320','--hand','--face', '--number_people_max', '1'],shell = True)+ subprocess.run(+ [+ "bin\OpenPoseDemo.exe",+ "--video",+ str(thisVidPath),+ "--write_json",+ str(jsonPath),+ "--write_images",+ str(imgPath),+ "--write_images_format",+ "png",+ "--net_resolution",+ "-1x320",+ "--hand",+ "--face",+ "--number_people_max",+ "1",+ ],+ shell=True,+ )else:- print('Skipping: ', thisVidPath.name )+ print("Skipping: ", thisVidPath.name)session.openPose_jsonPathList = openPose_jsonPathListsession.openPose_imgPathList = openPose_imgPathListyaml = YAML()data = yaml.load(session.yamlPath)- data['openPose_imgPathList'] = openPose_imgPathList_yaml- data['openPose_jsonPathList'] = openPose_jsonPathList_yaml+ data["openPose_imgPathList"] = openPose_imgPathList_yaml+ data["openPose_jsonPathList"] = openPose_jsonPathList_yamlyaml.dump(data, session.yamlPath)-+os.chdir(session.sessionPath)-++# @jit(nopython=True)def parseOpenPose(session):- thisCamNum = -1-- ## %%- #build header for dataframe - NOTE - #openpose data comes in a line ordered 'pixel x location (px)', 'pixel y (py)', 'confidence (conf)' for each keypoint-- dataFrameHeader = []- bodyCols = 75- handCols = 63 #per hand- faceCols = 210 #das alotta face!- headerLength = bodyCols + 2*handCols + faceCols#should be 411 for whatever version of openpose i was using on 11 Jan 2021- numImgPoints = headerLength-- for bb in range(0,int(bodyCols/3)): #loop through the number of body markers (i.e. #bodyCols/3)- dataFrameHeader.append('body_' + str(bb).zfill(3) + '_pixx')- dataFrameHeader.append('body_' + str(bb).zfill(3) + '_pixy')- dataFrameHeader.append('body_' + str(bb).zfill(3) + '_conf')-- for hr in range(0,int(handCols/3)): #loop through the number of handR markers (i.e. #handCols/3)- dataFrameHeader.append('handR_' + str(hr).zfill(3) + '_pixx')- dataFrameHeader.append('handR_' + str(hr).zfill(3) + '_pixy')- dataFrameHeader.append('handR_' + str(hr).zfill(3) + '_conf')-- for hl in range(0,int(handCols/3)): #loop through the number of handL markers (i.e. #handCols/3)- dataFrameHeader.append('handL_' + str(hl).zfill(3) + '_pixx')- dataFrameHeader.append('handL_' + str(hl).zfill(3) + '_pixy')- dataFrameHeader.append('handL_' + str(hl).zfill(3) + '_conf')-- for ff in range(0,int(faceCols/3)): #loop through the number of Face markers (i.e. #faceCols/3)- dataFrameHeader.append('face_' + str(ff).zfill(3) + '_pixx')- dataFrameHeader.append('face_' + str(ff).zfill(3) + '_pixy')- dataFrameHeader.append('face_' + str(ff).zfill(3) + '_conf')-- assert len(dataFrameHeader) == headerLength, ['Header is the wrong length! Should be ' + str(headerLength) + ' but it is ' + str(len(dataFrameHeader)) + ' Check version of OpenPose?']-- ## %%- ## load in data from json files- numFrames = int(len(list(Path(session.openPose_jsonPathList[0]).glob('*')))) #lol- numMarkers= int(int(len(dataFrameHeader)/3))- numCams = int(session.numCams)-- openPoseData_nCams_nFrames_nImgPts_XYC = np.ndarray([numCams,numFrames,numMarkers,3]) #hardcoding for now because I am a bad person-- for thisCams_JsonFolderPath in track(session.openPose_jsonPathList, description='Parsing json\'s into a dataframe (per cam)' ):- thisCamNum += 1- # print('Parsing into a dataframe: ', thisCams_JsonFolderPath.name )- jsonPaths = sorted(Path(thisCams_JsonFolderPath).glob('*.json')) #glob is a "generator(?)" for paths to all the jason for THIS camara-- for thisJsonPath in jsonPaths: #loop throug all the json files and save their 'people' data to a dictionary (which will then be formatted into a pandas dataframe). NOTE - will be empty array if no hoomans visible in frame- # print('loading: ', thisJsonPath.name)- frameNum = int(thisJsonPath.stem.split('_')[-2]) #frame number we're on- thisJsonData = json.loads(thisJsonPath.read_bytes())- thisJsonData = thisJsonData['people'] # #FEATURE_REQUEST - at some point, we should check the openpose version (save it with the data somehow, verify everything is the same version, use different markernamlists for different versions, etc)-- if thisJsonData: #if this json has data- bodyData = np.array(thisJsonData[0]['pose_keypoints_2d'])- 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- else: #if this json is empty, just stuff it fulla NaNs- thisFrameRow = np.empty([headerLength])- thisFrameRow.fill(np.nan)--- assert thisFrameRow.size == headerLength, ['Header is the wrong length! Should be ' + str(headerLength) + ' but it is ' + str(thisFrameRow.size) + ' Check version of OpenPose?']-- if frameNum < openPoseData_nCams_nFrames_nImgPts_XYC.shape[1]: #NOTE: THIS SHOULDN"T BE NECESSARY. CURRENT Vid Trim Methods can (apparently) produce vids with off-by-one numbers of frames- openPoseData_nCams_nFrames_nImgPts_XYC[thisCamNum, frameNum, :, :] = np.reshape(thisFrameRow, [137,3]) #hard coding b/c I'm a bad person----- # session.dataFrameHeader = dataFrameHeader- # session.numImgPoints = numImgPoints- session.numFrames = frameNum #NOTE - Need to find a safer way to get this number-- path_to_openpose_2d = session.dataArrayPath/'openPoseData_2d.npy'- np.save(path_to_openpose_2d, openPoseData_nCams_nFrames_nImgPts_XYC)-- return openPoseData_nCams_nFrames_nImgPts_XYC\ No newline at end of file+ thisCamNum = -1++ ## %%+ # build header for dataframe - NOTE - #openpose data comes in a line ordered 'pixel x location (px)', 'pixel y (py)', 'confidence (conf)' for each keypoint++ dataFrameHeader = []+ bodyCols = 75+ handCols = 63 # per hand+ faceCols = 210 # das alotta face!+ headerLength = (+ bodyCols + 2 * handCols + faceCols+ ) # should be 411 for whatever version of openpose i was using on 11 Jan 2021+ numImgPoints = headerLength++ for bb in range(+ 0, int(bodyCols / 3)+ ): # loop through the number of body markers (i.e. #bodyCols/3)+ dataFrameHeader.append("body_" + str(bb).zfill(3) + "_pixx")+ dataFrameHeader.append("body_" + str(bb).zfill(3) + "_pixy")+ dataFrameHeader.append("body_" + str(bb).zfill(3) + "_conf")++ for hr in range(+ 0, int(handCols / 3)+ ): # loop through the number of handR markers (i.e. #handCols/3)+ dataFrameHeader.append("handR_" + str(hr).zfill(3) + "_pixx")+ dataFrameHeader.append("handR_" + str(hr).zfill(3) + "_pixy")+ dataFrameHeader.append("handR_" + str(hr).zfill(3) + "_conf")++ for hl in range(+ 0, int(handCols / 3)+ ): # loop through the number of handL markers (i.e. #handCols/3)+ dataFrameHeader.append("handL_" + str(hl).zfill(3) + "_pixx")+ dataFrameHeader.append("handL_" + str(hl).zfill(3) + "_pixy")+ dataFrameHeader.append("handL_" + str(hl).zfill(3) + "_conf")++ for ff in range(+ 0, int(faceCols / 3)+ ): # loop through the number of Face markers (i.e. #faceCols/3)+ dataFrameHeader.append("face_" + str(ff).zfill(3) + "_pixx")+ dataFrameHeader.append("face_" + str(ff).zfill(3) + "_pixy")+ dataFrameHeader.append("face_" + str(ff).zfill(3) + "_conf")++ assert len(dataFrameHeader) == headerLength, [+ "Header is the wrong length! Should be "+ + str(headerLength)+ + " but it is "+ + str(len(dataFrameHeader))+ + " Check version of OpenPose?"+ ]++ ## %%+ ## load in data from json files+ numFrames = int(len(list(Path(session.openPose_jsonPathList[0]).glob("*")))) # lol+ numMarkers = int(int(len(dataFrameHeader) / 3))+ numCams = int(session.numCams)++ openPoseData_nCams_nFrames_nImgPts_XYC = np.ndarray(+ [numCams, numFrames, numMarkers, 3]+ ) # hardcoding for now because I am a bad person++ for thisCams_JsonFolderPath in track(+ session.openPose_jsonPathList,+ description="Parsing json's into a dataframe (per cam)",+ ):+ thisCamNum += 1+ # print('Parsing into a dataframe: ', thisCams_JsonFolderPath.name )+ jsonPaths = sorted(+ Path(thisCams_JsonFolderPath).glob("*.json")+ ) # glob is a "generator(?)" for paths to all the jason for THIS camara++ for (+ thisJsonPath+ ) in (+ jsonPaths+ ): # loop throug all the json files and save their 'people' data to a dictionary (which will then be formatted into a pandas dataframe). NOTE - will be empty array if no hoomans visible in frame+ # print('loading: ', thisJsonPath.name)+ frameNum = int(thisJsonPath.stem.split("_")[-2]) # frame number we're on+ thisJsonData = json.loads(thisJsonPath.read_bytes())+ thisJsonData = thisJsonData[+ "people"+ ] # #FEATURE_REQUEST - at some point, we should check the openpose version (save it with the data somehow, verify everything is the same version, use different markernamlists for different versions, etc)++ if thisJsonData: # if this json has data+ bodyData = np.array(thisJsonData[0]["pose_keypoints_2d"])+ 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+ else: # if this json is empty, just stuff it fulla NaNs+ thisFrameRow = np.empty([headerLength])+ thisFrameRow.fill(np.nan)++ assert thisFrameRow.size == headerLength, [+ "Header is the wrong length! Should be "+ + str(headerLength)+ + " but it is "+ + str(thisFrameRow.size)+ + " Check version of OpenPose?"+ ]++ if (+ frameNum < openPoseData_nCams_nFrames_nImgPts_XYC.shape[1]+ ): # NOTE: THIS SHOULDN"T BE NECESSARY. CURRENT Vid Trim Methods can (apparently) produce vids with off-by-one numbers of frames+ openPoseData_nCams_nFrames_nImgPts_XYC[+ thisCamNum, frameNum, :, :+ ] = np.reshape(+ thisFrameRow, [137, 3]+ ) # hard coding b/c I'm a bad person++ # session.dataFrameHeader = dataFrameHeader+ # session.numImgPoints = numImgPoints+ session.numFrames = frameNum # NOTE - Need to find a safer way to get this number++ path_to_openpose_2d = session.dataArrayPath / "openPoseData_2d.npy"+ np.save(path_to_openpose_2d, openPoseData_nCams_nFrames_nImgPts_XYC)++ return openPoseData_nCams_nFrames_nImgPts_XYCdiff --git a/freemocap/fmc_pyqtgraph.py b/freemocap/fmc_pyqtgraph.pyindex 7bba8893..8c8eddc1 100644--- a/freemocap/fmc_pyqtgraph.py+++ b/freemocap/fmc_pyqtgraph.py@@ -3,119 +3,216 @@ import pyqtgraph.opengl as glimport pyqtgraph as pgimport pyqtgraph.consolefrom pyqtgraph.dockarea import Dock, DockArea-import cv2+import cv2import numpy as npfrom pathlib import Pathimport sys-class PlaySkeletonWidget():+class PlaySkeletonWidget:def __init__(self, session):- #set up PyQT widget (the window that pops up and shows the plots, I think ? cribbed from - https://gist.github.com/markjay4k/da2f55e28514be7160a7c5fbf95bd243)+ # set up PyQT widget (the window that pops up and shows the plots, I think ? cribbed from - https://gist.github.com/markjay4k/da2f55e28514be7160a7c5fbf95bd243)# self.app = QtGui.QApplication(sys.argv)self.Skel3dViewWidget = gl.GLViewWidget()- self.Skel3dViewWidget.opts['distance'] = 2000- self.Skel3dViewWidget.setWindowTitle('SessionID: {}'.format(session.sessionID))+ self.Skel3dViewWidget.opts["distance"] = 2000+ self.Skel3dViewWidget.setWindowTitle("SessionID: {}".format(session.sessionID))self.Skel3dViewWidget.setGeometry(0, 110, 1920, 1080)self.Skel3dViewWidget.show()-- #load data to be plotted+ # load data to be plottedself.useOpenPose = session.useOpenPoseif self.useOpenPose:- self.skel_fr_mar_dim = np.load(session.dataArrayPath/'openPoseSkel_3d.npy')- self.skelScatterItem = gl.GLScatterPlotItem(pos=self.skel_fr_mar_dim, color=(1,0,1,.8), size=10, pxMode=False)+ self.skel_fr_mar_dim = np.load(+ session.dataArrayPath / "openPoseSkel_3d.npy"+ )+ self.skelScatterItem = gl.GLScatterPlotItem(+ pos=self.skel_fr_mar_dim, color=(1, 0, 1, 0.8), size=10, pxMode=False+ )-self.Skel3dViewWidget.addItem(self.skelScatterItem)-- #make skeleton line plots (numbers specify what order to connects the dots)+ # make skeleton line plots (numbers specify what order to connects the dots)self.head = [17, 15, 0, 1, 0, 16, 18]- self.spine = [1,8]+ self.spine = [1, 8]self.rArm = [17, 15, 0, 16, 18]- self.rArm = [4 ,3 ,2 ,1]+ self.rArm = [4, 3, 2, 1]self.lArm = [1, 5, 6, 7]- self.rLeg = [11 ,10, 9, 8]- self.lLeg = [14 ,13 ,12, 8]- self.rFoot = [11, 23,22, 11, 24]+ self.rLeg = [11, 10, 9, 8]+ self.lLeg = [14, 13, 12, 8]+ self.rFoot = [11, 23, 22, 11, 24]self.lFoot = [14, 20, 19, 14, 21]- #Make some handy maps ;D- rHandIDstart = 25- lHandIDstart = rHandIDstart+21-- self.rHand = np.array([0, 1, 2, 3, 4, False, 0, 5, 6, 7, 8, False, 0, 9, 10, 11, 12, False, 0, 13, 14, 15, 16, False, 0, 17, 18, 19, 20,]) + rHandIDstart- self.lHand = self.rHand + lHandIDstart--- self.headLineItem = gl.GLLinePlotItem(pos=self.skel_fr_mar_dim[:,self.head,:], color=(1,1,1,.2), width=4., antialias=True)+ # Make some handy maps ;D+ rHandIDstart = 25+ lHandIDstart = rHandIDstart + 21++ self.rHand = (+ np.array(+ [+ 0,+ 1,+ 2,+ 3,+ 4,+ False,+ 0,+ 5,+ 6,+ 7,+ 8,+ False,+ 0,+ 9,+ 10,+ 11,+ 12,+ False,+ 0,+ 13,+ 14,+ 15,+ 16,+ False,+ 0,+ 17,+ 18,+ 19,+ 20,+ ]+ )+ + rHandIDstart+ )+ self.lHand = self.rHand + lHandIDstart++ self.headLineItem = gl.GLLinePlotItem(+ pos=self.skel_fr_mar_dim[:, self.head, :],+ color=(1, 1, 1, 0.2),+ width=4.0,+ antialias=True,+ )self.Skel3dViewWidget.addItem(self.headLineItem)- self.spineLineItem = gl.GLLinePlotItem(pos=self.skel_fr_mar_dim[:,self.spine,:], color=(1,1,1,.9), width=4., antialias=True)+ self.spineLineItem = gl.GLLinePlotItem(+ pos=self.skel_fr_mar_dim[:, self.spine, :],+ color=(1, 1, 1, 0.9),+ width=4.0,+ antialias=True,+ )self.Skel3dViewWidget.addItem(self.spineLineItem)- self.rArmLineItem = gl.GLLinePlotItem(pos=self.skel_fr_mar_dim[:,self.rArm,:], color=(1,.2,.15,.9), width=4., antialias=True)+ self.rArmLineItem = gl.GLLinePlotItem(+ pos=self.skel_fr_mar_dim[:, self.rArm, :],+ color=(1, 0.2, 0.15, 0.9),+ width=4.0,+ antialias=True,+ )self.Skel3dViewWidget.addItem(self.rArmLineItem)- self.lArmLineItem = gl.GLLinePlotItem(pos=self.skel_fr_mar_dim[:,self.lArm,:], color=(.4,.6,1,.9), width=4., antialias=True)+ self.lArmLineItem = gl.GLLinePlotItem(+ pos=self.skel_fr_mar_dim[:, self.lArm, :],+ color=(0.4, 0.6, 1, 0.9),+ width=4.0,+ antialias=True,+ )self.Skel3dViewWidget.addItem(self.lArmLineItem)- self.rLegLineItem = gl.GLLinePlotItem(pos=self.skel_fr_mar_dim[:,self.rLeg,:], color=(1,.2,.15,.9), width=4., antialias=True)+ self.rLegLineItem = gl.GLLinePlotItem(+ pos=self.skel_fr_mar_dim[:, self.rLeg, :],+ color=(1, 0.2, 0.15, 0.9),+ width=4.0,+ antialias=True,+ )self.Skel3dViewWidget.addItem(self.rLegLineItem)- self.lLegLineItem = gl.GLLinePlotItem(pos=self.skel_fr_mar_dim[:,self.lLeg,:], color=(.4,.6,1,.9), width=4., antialias=True)+ self.lLegLineItem = gl.GLLinePlotItem(+ pos=self.skel_fr_mar_dim[:, self.lLeg, :],+ color=(0.4, 0.6, 1, 0.9),+ width=4.0,+ antialias=True,+ )self.Skel3dViewWidget.addItem(self.lLegLineItem)- self.rFootLineItem = gl.GLLinePlotItem(pos=self.skel_fr_mar_dim[:,self.rFoot,:], color=(1,.2,.15,.9), width=4., antialias=True)+ self.rFootLineItem = gl.GLLinePlotItem(+ pos=self.skel_fr_mar_dim[:, self.rFoot, :],+ color=(1, 0.2, 0.15, 0.9),+ width=4.0,+ antialias=True,+ )self.Skel3dViewWidget.addItem(self.rFootLineItem)- self.lFootLineItem = gl.GLLinePlotItem(pos=self.skel_fr_mar_dim[:,self.lFoot,:], color=(.4,.6,1,.9), width=4., antialias=True)+ self.lFootLineItem = gl.GLLinePlotItem(+ pos=self.skel_fr_mar_dim[:, self.lFoot, :],+ color=(0.4, 0.6, 1, 0.9),+ width=4.0,+ antialias=True,+ )self.Skel3dViewWidget.addItem(self.lFootLineItem)# plot handybois- self.rHandLineItem = gl.GLLinePlotItem(pos=self.skel_fr_mar_dim[:,self.rHand,:], color=(.2,.4,1,.9), width=1., antialias=True)+ self.rHandLineItem = gl.GLLinePlotItem(+ pos=self.skel_fr_mar_dim[:, self.rHand, :],+ color=(0.2, 0.4, 1, 0.9),+ width=1.0,+ antialias=True,+ )# self.Skel3dViewWidget.addItem(self.rHandLineItem)-- self.lHandLineItem = gl.GLLinePlotItem(pos=self.skel_fr_mar_dim[:,self.lHand,:], color=(1,.1,.05,.9), width=1., antialias=True)- # self.Skel3dViewWidget.addItem(self.lHandLineItem)--+ self.lHandLineItem = gl.GLLinePlotItem(+ pos=self.skel_fr_mar_dim[:, self.lHand, :],+ color=(1, 0.1, 0.05, 0.9),+ width=1.0,+ antialias=True,+ )+ # self.Skel3dViewWidget.addItem(self.lHandLineItem)-self.useDLC = session.useDLCif self.useDLC:# self.dlc_fr_mar_dim = np.load(session.dataArrayPath/'mediaPipeSkel_3d.npy')- self.dlc_fr_mar_dim = np.load(session.dataArrayPath/'deepLabCut_3d.npy')- self.dlcScatterItem = gl.GLScatterPlotItem(pos=self.dlc_fr_mar_dim, color=(1,0,0,.8), size=60, pxMode=False)+ self.dlc_fr_mar_dim = np.load(session.dataArrayPath / "deepLabCut_3d.npy")+ self.dlcScatterItem = gl.GLScatterPlotItem(+ pos=self.dlc_fr_mar_dim, color=(1, 0, 0, 0.8), size=60, pxMode=False+ )self.Skel3dViewWidget.addItem(self.dlcScatterItem)-- self.dlc0LineItem = gl.GLLinePlotItem(pos=self.dlc_fr_mar_dim[:,0,:], color=(0,1,1,.5), width=4., antialias=True)- self.dlc1LineItem = gl.GLLinePlotItem(pos=self.dlc_fr_mar_dim[:,1,:], color=(1,0,1,.5), width=4., antialias=True)- self.dlc2LineItem = gl.GLLinePlotItem(pos=self.dlc_fr_mar_dim[:,2,:], color=(1,1,0,.5), width=4., antialias=True)++ self.dlc0LineItem = gl.GLLinePlotItem(+ pos=self.dlc_fr_mar_dim[:, 0, :],+ color=(0, 1, 1, 0.5),+ width=4.0,+ antialias=True,+ )+ self.dlc1LineItem = gl.GLLinePlotItem(+ pos=self.dlc_fr_mar_dim[:, 1, :],+ color=(1, 0, 1, 0.5),+ width=4.0,+ antialias=True,+ )+ self.dlc2LineItem = gl.GLLinePlotItem(+ pos=self.dlc_fr_mar_dim[:, 2, :],+ color=(1, 1, 0, 0.5),+ width=4.0,+ antialias=True,+ )self.Skel3dViewWidget.addItem(self.dlc0LineItem)self.Skel3dViewWidget.addItem(self.dlc1LineItem)self.Skel3dViewWidget.addItem(self.dlc2LineItem)self.tailLength = 10---- self.charuco_mar_dim = np.load(session.dataArrayPath/'charuco_points.npy')- self.charucoScatterItem = gl.GLScatterPlotItem(pos=self.charuco_mar_dim, color=(1,1,1,.8), size=10, pxMode=False)+ self.charuco_mar_dim = np.load(session.dataArrayPath / "charuco_points.npy")+ self.charucoScatterItem = gl.GLScatterPlotItem(+ pos=self.charuco_mar_dim, color=(1, 1, 1, 0.8), size=10, pxMode=False+ )self.Skel3dViewWidget.addItem(self.charucoScatterItem)- #plot videos Frames!+ # plot videos Frames!- #set frame counter+ # set frame counterself.currentFrameNumber = 0- self.framerate = 50 #frames per second- self.frameDuration = 1000/self.framerate #milliseconds+ self.framerate = 50 # frames per second+ self.frameDuration = 1000 / self.framerate # milliseconds# def start(self):# if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):@@ -123,33 +220,64 @@ class PlaySkeletonWidget():def update(self):- #Plot Mr. Skreleton :D- #Plots the dots- self.skelScatterItem.setData(pos=self.skel_fr_mar_dim[self.currentFrameNumber,:,:])-- #connect the dottos- self.headLineItem.setData(pos=self.skel_fr_mar_dim[self.currentFrameNumber, self.head,:])- self.spineLineItem.setData(pos=self.skel_fr_mar_dim[self.currentFrameNumber, self.spine,:])- self.rArmLineItem.setData(pos=self.skel_fr_mar_dim[self.currentFrameNumber, self.rArm,:])- self.lArmLineItem.setData(pos=self.skel_fr_mar_dim[self.currentFrameNumber, self.lArm,:])- self.rLegLineItem.setData(pos=self.skel_fr_mar_dim[self.currentFrameNumber, self.rLeg,:])- self.lLegLineItem.setData(pos=self.skel_fr_mar_dim[self.currentFrameNumber, self.lLeg,:])- self.rFootLineItem.setData(pos=self.skel_fr_mar_dim[self.currentFrameNumber, self.rFoot,:])- self.lFootLineItem.setData(pos=self.skel_fr_mar_dim[self.currentFrameNumber, self.lFoot,:])-- self.rHandLineItem.setData(pos=self.skel_fr_mar_dim[self.currentFrameNumber, self.rHand,:])- self.lHandLineItem.setData(pos=self.skel_fr_mar_dim[self.currentFrameNumber, self.lHand,:])+ # Plot Mr. Skreleton :D+ # Plots the dots+ self.skelScatterItem.setData(+ pos=self.skel_fr_mar_dim[self.currentFrameNumber, :, :]+ )++ # connect the dottos+ self.headLineItem.setData(+ pos=self.skel_fr_mar_dim[self.currentFrameNumber, self.head, :]+ )+ self.spineLineItem.setData(+ pos=self.skel_fr_mar_dim[self.currentFrameNumber, self.spine, :]+ )+ self.rArmLineItem.setData(+ pos=self.skel_fr_mar_dim[self.currentFrameNumber, self.rArm, :]+ )+ self.lArmLineItem.setData(+ pos=self.skel_fr_mar_dim[self.currentFrameNumber, self.lArm, :]+ )+ self.rLegLineItem.setData(+ pos=self.skel_fr_mar_dim[self.currentFrameNumber, self.rLeg, :]+ )+ self.lLegLineItem.setData(+ pos=self.skel_fr_mar_dim[self.currentFrameNumber, self.lLeg, :]+ )+ self.rFootLineItem.setData(+ pos=self.skel_fr_mar_dim[self.currentFrameNumber, self.rFoot, :]+ )+ self.lFootLineItem.setData(+ pos=self.skel_fr_mar_dim[self.currentFrameNumber, self.lFoot, :]+ )++ self.rHandLineItem.setData(+ pos=self.skel_fr_mar_dim[self.currentFrameNumber, self.rHand, :]+ )+ self.lHandLineItem.setData(+ pos=self.skel_fr_mar_dim[self.currentFrameNumber, self.lHand, :]+ )if self.useDLC:- #Plot DeepLabCut data- self.dlcScatterItem.setData(pos=self.dlc_fr_mar_dim[self.currentFrameNumber,:,:])-- if self.currentFrameNumber > self.tailLength:- self.tailIndexes = range(self.currentFrameNumber-self.tailLength , self.currentFrameNumber)- self.dlc0LineItem.setData(pos=self.dlc_fr_mar_dim[self.tailIndexes,0,:])- self.dlc1LineItem.setData(pos=self.dlc_fr_mar_dim[self.tailIndexes,1,:])- self.dlc2LineItem.setData(pos=self.dlc_fr_mar_dim[self.tailIndexes,2,:])+ # Plot DeepLabCut data+ self.dlcScatterItem.setData(+ pos=self.dlc_fr_mar_dim[self.currentFrameNumber, :, :]+ )+ if self.currentFrameNumber > self.tailLength:+ self.tailIndexes = range(+ self.currentFrameNumber - self.tailLength, self.currentFrameNumber+ )+ self.dlc0LineItem.setData(+ pos=self.dlc_fr_mar_dim[self.tailIndexes, 0, :]+ )+ self.dlc1LineItem.setData(+ pos=self.dlc_fr_mar_dim[self.tailIndexes, 1, :]+ )+ self.dlc2LineItem.setData(+ pos=self.dlc_fr_mar_dim[self.tailIndexes, 2, :]+ )self.currentFrameNumber += 1if self.currentFrameNumber >= self.skel_fr_mar_dim.shape[0]:@@ -165,27 +293,28 @@ class PlaySkeletonWidget():class VideoWindowWidget:def __init__(self, vidNum, videoPath, session):- #set up video capture+ # set up video captureself.vidCap = cv2.VideoCapture(str(videoPath))success, image = self.vidCap.read()- self.vidCap.set(cv2.CAP_PROP_POS_FRAMES, 0) #reset video capture so things start on the first frame-+ self.vidCap.set(+ cv2.CAP_PROP_POS_FRAMES, 0+ ) # reset video capture so things start on the first frame+self.videoWidget = pg.GraphicsLayoutWidget()self.videoWidget.show() ## show widget alone in its own window- self.videoWidget.setWindowTitle('Video Name')+ self.videoWidget.setWindowTitle("Video Name")self.view = self.videoWidget.addViewBox()## lock the aspect ratio so pixels are always squareself.view.setAspectLocked(True)## Create image item- self.imgItem = pg.ImageItem(border='w')+ self.imgItem = pg.ImageItem(border="w")self.view.addItem(self.imgItem)## Set initial view boundsself.view.setRange(QtCore.QRectF(0, 0, image.shape[0], image.shape[1]))-- #set image, I guess?+ # set image, I guess?self.imgItem.setImage(image)def update(self):@@ -196,16 +325,16 @@ class VideoWindowWidget:image = cv2.rotate(image, cv2.ROTATE_90_CLOCKWISE)self.imgItem.setImage(image)else:- self.vidCap.set(cv2.CAP_PROP_POS_FRAMES, 0) #reset video capture so video will loop- success, image = self.vidCap.read()+ self.vidCap.set(+ cv2.CAP_PROP_POS_FRAMES, 0+ ) # reset video capture so video will loop+ success, image = self.vidCap.read()self.imgItem.setImage(image)-class PlayerDockedWindow:- def __init__(self, session,displayVid):+ def __init__(self, session, displayVid):-app = pg.mkQApp("DockArea Example")self.displayVid = displayVidself.win = QtGui.QMainWindow()@@ -213,8 +342,8 @@ class PlayerDockedWindow:self.win.setCentralWidget(area)winHeight = 1000winWidth = 1900- self.win.resize(winHeight,winWidth)- self.win.setWindowTitle('FreeMoCap')+ self.win.resize(winHeight, winWidth)+ self.win.setWindowTitle("FreeMoCap")## Create docks, place them into the window one at a time.## Note that size arguments are only a suggestion; docks will still have to@@ -227,92 +356,101 @@ class PlayerDockedWindow:path_to_search = session.syncedVidPathelif displayVid == 1:path_to_search = session.openPoseDataPath- for count,vidPath in enumerate(path_to_search.glob('*.mp4')):+ for count, vidPath in enumerate(path_to_search.glob("*.mp4")):self.VidPathList.append(vidPath)- #dock_name = 'dock_video{}'.format(count)+ # dock_name = 'dock_video{}'.format(count)dock_name = countself.dock_name_dictionary[dock_name] = None+ dock_3dView = Dock(+ "Session: {}".format(session.sessionID), size=(1, 1), closable=True+ ) ## give this dock the minimum possible size+ dock_Console = Dock(+ "Dock2 - Console", size=(winHeight, winWidth), closable=True+ )- dock_3dView = Dock('Session: {}'.format(session.sessionID), size=(1, 1), closable=True) ## give this dock the minimum possible size- dock_Console = Dock("Dock2 - Console", size=(winHeight,winWidth), closable=True)-for key in self.dock_name_dictionary:- self.dock_name_dictionary[key] = Dock(str(key), size=(1,1), closable=True)- #dock_video0 = Dock("Video0", size=(1,1), closable=True)- #dock_video1 = Dock("Video1", size=(1,1), closable=True)- #dock_video2 = Dock("Video2", size=(1,1), closable=True)- #dock_video3 = Dock("Video3", size=(1,1), closable=True)-- area.addDock(dock_3dView, 'left') ## place d1 at left edge of dock area (it will fill the whole space since there are no other docks yet)- direction_list = ['top','left','bottom','top']+ self.dock_name_dictionary[key] = Dock(str(key), size=(1, 1), closable=True)+ # dock_video0 = Dock("Video0", size=(1,1), closable=True)+ # dock_video1 = Dock("Video1", size=(1,1), closable=True)+ # dock_video2 = Dock("Video2", size=(1,1), closable=True)+ # dock_video3 = Dock("Video3", size=(1,1), closable=True)++ area.addDock(+ dock_3dView, "left"+ ) ## place d1 at left edge of dock area (it will fill the whole space since there are no other docks yet)+ direction_list = ["top", "left", "bottom", "top"]for key, value in self.dock_name_dictionary.items():if (key % 2) == 0:- area.addDock(value,'right')+ area.addDock(value, "right")previous_dock = valueelse:- area.addDock(value,'bottom',previous_dock)- #previous_dock = value-- #area.addDock(dock_video0, 'right') ## place d2 at right edge of dock area- #area.addDock(dock_video1, 'right', dock_video0)## place d3 at bottom edge of d1- #area.addDock(dock_video2, 'bottom',dock_video0) ## place d4 at right edge of dock area- #area.addDock(dock_video3, 'left', dock_video2) ## place d5 at left edge of d1- #area.moveDock(dock_video1, 'right', dock_video0)+ area.addDock(value, "bottom", previous_dock)+ # previous_dock = value++ # area.addDock(dock_video0, 'right') ## place d2 at right edge of dock area+ # area.addDock(dock_video1, 'right', dock_video0)## place d3 at bottom edge of d1+ # area.addDock(dock_video2, 'bottom',dock_video0) ## place d4 at right edge of dock area+ # area.addDock(dock_video3, 'left', dock_video2) ## place d5 at left edge of d1+ # area.moveDock(dock_video1, 'right', dock_video0)## Add widgets into each dock## first dock gets save/restore buttonsw1 = pg.LayoutWidget()- label = QtGui.QLabel(""" -- DockArea Example --+ label = QtGui.QLabel(+ """ -- DockArea Example --This window has 6 Dock widgets in it. Each dock can be draggedby its title bar to occupy a different space within the windowbut note that one dock has its title bar hidden). Additionally,the borders between docks may be dragged to resize. Docks that are dragged on topof one another are stacked in a tabbed layout. Double-click a dock titlebar to place it in its own window.- """)+ """+ )w2 = pg.console.ConsoleWidget()dock_Console.addWidget(w2)self.playSkel = PlaySkeletonWidget(session)-+dock_3dView.addWidget(self.playSkel.Skel3dViewWidget)- #self.VidPathList = []- #for vidPath in session.syncedVidPath.glob('*.mp4'):+ # self.VidPathList = []+ # for vidPath in session.syncedVidPath.glob('*.mp4'):# self.VidPathList.append(vidPath)self.widget_list = []for key, dock_video in self.dock_name_dictionary.items():- self.videoWidget = VideoWindowWidget(key,str(self.VidPathList[key]),session)+ self.videoWidget = VideoWindowWidget(+ key, str(self.VidPathList[key]), session+ )self.widget_list.append(self.videoWidget)dock_video.addWidget(self.videoWidget.videoWidget)- #self.video0Widget = VideoWindowWidget(0,str(self.VidPathList[0]), session)- #dock_video0.addWidget(self.video0Widget.videoWidget )+ # self.video0Widget = VideoWindowWidget(0,str(self.VidPathList[0]), session)+ # dock_video0.addWidget(self.video0Widget.videoWidget )- #self.video1Widget = VideoWindowWidget(1,str(self.VidPathList[1]),session)- #dock_video1.addWidget(self.video1Widget.videoWidget )+ # self.video1Widget = VideoWindowWidget(1,str(self.VidPathList[1]),session)+ # dock_video1.addWidget(self.video1Widget.videoWidget )- #self.video2Widget = VideoWindowWidget(2,str(self.VidPathList[2]), session)- #dock_video2.addWidget(self.video2Widget.videoWidget )+ # self.video2Widget = VideoWindowWidget(2,str(self.VidPathList[2]), session)+ # dock_video2.addWidget(self.video2Widget.videoWidget )- #self.video3Widget = VideoWindowWidget(3,str(self.VidPathList[3]),session)- #dock_video3.addWidget(self.video3Widget.videoWidget )+ # self.video3Widget = VideoWindowWidget(3,str(self.VidPathList[3]),session)+ # dock_video3.addWidget(self.video3Widget.videoWidget )self.win.show()-+def update(self):self.playSkel.update()for widget in self.widget_list:widget.update()- #self.video0Widget.update()- #self.video1Widget.update()- #self.video2Widget.update()- #self.video3Widget.update()-+ # self.video0Widget.update()+ # self.video1Widget.update()+ # self.video2Widget.update()+ # self.video3Widget.update()+def animate(self):timer = QtCore.QTimer()timer.timeout.connect(self.update)@@ -320,7 +458,5 @@ class PlayerDockedWindow:self.start()def start(self):- if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):- QtGui.QApplication.instance().exec_()--\ No newline at end of file+ if (sys.flags.interactive != 1) or not hasattr(QtCore, "PYQT_VERSION"):+ QtGui.QApplication.instance().exec_()diff --git a/freemocap/initialization.py b/freemocap/initialization.pyindex e005c6b1..5db9a02c 100644--- a/freemocap/initialization.py+++ b/freemocap/initialization.py@@ -1,4 +1,3 @@-from pathlib import Pathfrom ruamel.yaml import YAMLimport datetime@@ -6,109 +5,122 @@ import sysimport pandas as pdimport numpy as np-from freemocap.webcam import recordGUI,camsetup+from freemocap.webcam import recordGUI, camsetupfrom freemocap import recordingconfig, fmc_anipose+def initialize(session, stage, board):- print('Starting initialization for stage {}'.format(stage))- #if stage == 1:+ print("Starting initialization for stage {}".format(stage))+ # if stage == 1:filepath = Path.cwd()session.board = board-# %% Stage One Initialization+ # %% Stage One Initializationif stage == 1:-- #create session ID+ # create session IDsessionID_in = datetime.datetime.now().strftime("sesh_%y-%m-%d_%H%M%S")session.sessionID = sessionID_in- #load the parameters YAML and extract the saved parameters if possible, and the default otherwise- parameter_path = filepath/'parameters.yaml'+ # load the parameters YAML and extract the saved parameters if possible, and the default otherwise+ parameter_path = filepath / "parameters.yaml"parameters_yaml = YAML()parameters = parameters_yaml.load(parameter_path)- try:- rotation_entry = parameters['saved']['rotations']- parameter_entry = parameters['saved']['parameters']+ try:+ rotation_entry = parameters["saved"]["rotations"]+ parameter_entry = parameters["saved"]["parameters"]except:print("Using Default Config")- rotation_entry = parameters['default']['rotations']- parameter_entry = parameters['default']['parameters']-- proceedToRecording = False #create this boolean, set it to false, and if the user wants to record- #later in the pipeline, it will be set to true-- #run the GUI to get the tasks, the cams chosen, the camera settings, and the session ID- task, cam_inputs, rotDict, paramDict, session.sessionID = recordGUI.RunGUI(sessionID_in,rotation_entry,parameter_entry)-- #update the saved parameters in the YAML- parameters['saved']['rotations'] = rotDict- parameters['saved']['parameters'] = paramDict+ rotation_entry = parameters["default"]["rotations"]+ parameter_entry = parameters["default"]["parameters"]++ proceedToRecording = False # create this boolean, set it to false, and if the user wants to record+ # later in the pipeline, it will be set to true++ # run the GUI to get the tasks, the cams chosen, the camera settings, and the session ID+ task, cam_inputs, rotDict, paramDict, session.sessionID = recordGUI.RunGUI(+ sessionID_in, rotation_entry, parameter_entry+ )++ # update the saved parameters in the YAML+ parameters["saved"]["rotations"] = rotDict+ parameters["saved"]["parameters"] = paramDictparameters_yaml.dump(parameters, parameter_path)-- #create a list from the rotation dictionary to be used in running webcams++ # create a list from the rotation dictionary to be used in running webcamsrotation_input = list(rotDict.values())- if task == 'setup':- #run setup processes, and then check if th user wants to proceed to recording- camsetup.RunSetup(cam_inputs,rotation_input,paramDict)- proceedToRecording,session.sessionID = recordGUI.RunProceedtoRecordGUI(sessionID_in)+ if task == "setup":+ # run setup processes, and then check if th user wants to proceed to recording+ camsetup.RunSetup(cam_inputs, rotation_input, paramDict)+ proceedToRecording, session.sessionID = recordGUI.RunProceedtoRecordGUI(+ sessionID_in+ )- elif task == 'record':+ elif task == "record":proceedToRecording = True-+if proceedToRecording:- #create these session properties to be used later in the pipeline+ # create these session properties to be used later in the pipelinesession.cam_inputs = cam_inputssession.parameterDictionary = paramDictsession.rotationInputs = rotation_input- #create a config yaml and text file for this session- config_yaml_path = recordingconfig.createSession(session,filepath)- recordingconfig.createSessionTxt(session,paramDict,rotDict)-- print('Proceeding to Stage One')-- else:-- sys.exit('Recording Cancelled')-# %% Stage Two Initalization- if stage == 2:- session.sessionPath = filepath/'Data'/session.sessionID #create a session path based on the sessionID-- #load the config yaml for this session, and add all the paths to the session file- session.yamlPath = session.sessionPath/'{}_config.yaml'.format(session.sessionID)- session.config_settings = recordingconfig.load_config_yaml(session.yamlPath) #config settings = paths and camera parameter inputs- recordingconfig.load_session_paths(session,session.config_settings) #add paths to session class-- #from the config settings add the camera input parameters to parameter dictionary- session.parameterDictionary = session.config_settings['CamInputs']['ParameterDict']- session.rotationInputs = session.config_settings['CamInputs']['RotationInputs']+ # create a config yaml and text file for this session+ config_yaml_path = recordingconfig.createSession(session, filepath)+ recordingconfig.createSessionTxt(session, paramDict, rotDict)- #initialize the path to the timestamp csv- csvName = session.sessionID + '.csv'- csvPath = session.sessionPath/csvName+ print("Proceeding to Stage One")- #read CSV data, turn it into a data frame- timeStampData = pd.read_csv (csvPath)- timeStampData = timeStampData.iloc[:,1:]+ else:- #get the camIDs and number of cameras (numCamRange) from the dataframe+ sys.exit("Recording Cancelled")+ # %% Stage Two Initalization+ if stage == 2:+ session.sessionPath = (+ filepath / "Data" / session.sessionID+ ) # create a session path based on the sessionID++ # load the config yaml for this session, and add all the paths to the session file+ session.yamlPath = session.sessionPath / "{}_config.yaml".format(+ session.sessionID+ )+ session.config_settings = recordingconfig.load_config_yaml(+ session.yamlPath+ ) # config settings = paths and camera parameter inputs+ recordingconfig.load_session_paths(+ session, session.config_settings+ ) # add paths to session class++ # from the config settings add the camera input parameters to parameter dictionary+ session.parameterDictionary = session.config_settings["CamInputs"][+ "ParameterDict"+ ]+ session.rotationInputs = session.config_settings["CamInputs"]["RotationInputs"]++ # initialize the path to the timestamp csv+ csvName = session.sessionID + ".csv"+ csvPath = session.sessionPath / csvName++ # read CSV data, turn it into a data frame+ timeStampData = pd.read_csv(csvPath)+ timeStampData = timeStampData.iloc[:, 1:]++ # get the camIDs and number of cameras (numCamRange) from the dataframecamIDs = list(timeStampData.columns)numCams = len(camIDs)- numCamRange = range(len(camIDs))-- #create names for each of the raw videos+ numCamRange = range(len(camIDs))++ # create names for each of the raw videosvidNames = []for x in numCamRange:- singleVidName = 'raw_cam{}.mp4'.format(x+1)- vidNames.append(singleVidName)-+ singleVidName = "raw_cam{}.mp4".format(x + 1)+ vidNames.append(singleVidName)- #initialize all the session variables we'll need to run the rest of the pipeline+ # initialize all the session variables we'll need to run the rest of the pipelinesession.timeStampData = timeStampDatasession.camIDs = camIDssession.numCamRange = numCamRange@@ -116,44 +128,70 @@ def initialize(session, stage, board):session.numCams = numCamsif stage == 3:-- session.sessionPath = filepath/'Data'/session.sessionID #create a session path based on the sessionID- #load the config yaml for this session, and add all the paths to the session file- session.yamlPath = session.sessionPath/'{}_config.yaml'.format(session.sessionID)-- if session.yamlPath.is_file(): #if the config yaml exists (from a webcam recording)- session.config_settings = recordingconfig.load_config_yaml(session.yamlPath) #config settings = paths and camera parameter inputs- recordingconfig.load_session_paths(session,session.config_settings) #add paths to session class- else: #if it doesn't exist (because of a GoPro/external camera recording)- recordingconfig.createSession(session,filepath)+ session.sessionPath = (+ filepath / "Data" / session.sessionID+ ) # create a session path based on the sessionID++ # load the config yaml for this session, and add all the paths to the session file+ session.yamlPath = session.sessionPath / "{}_config.yaml".format(+ session.sessionID+ )++ if (+ session.yamlPath.is_file()+ ): # if the config yaml exists (from a webcam recording)+ session.config_settings = recordingconfig.load_config_yaml(+ session.yamlPath+ ) # config settings = paths and camera parameter inputs+ recordingconfig.load_session_paths(+ session, session.config_settings+ ) # add paths to session class+ else: # if it doesn't exist (because of a GoPro/external camera recording)+ recordingconfig.createSession(session, filepath)if stage == 4:- session.sessionPath = filepath/'Data'/session.sessionID-- session.yamlPath = session.sessionPath/'{}_config.yaml'.format(session.sessionID) #path to the configuration yaml- session.config_settings = recordingconfig.load_config_yaml(session.yamlPath) #config settings = paths and camera parameter inputs- recordingconfig.load_session_paths(session,session.config_settings) #add paths to session class-- for count,thisVidPath in enumerate(session.syncedVidPath.glob('*.mp4'),start=1):+ session.sessionPath = filepath / "Data" / session.sessionID++ session.yamlPath = session.sessionPath / "{}_config.yaml".format(+ session.sessionID+ ) # path to the configuration yaml+ session.config_settings = recordingconfig.load_config_yaml(+ session.yamlPath+ ) # config settings = paths and camera parameter inputs+ recordingconfig.load_session_paths(+ session, session.config_settings+ ) # add paths to session class++ for count, thisVidPath in enumerate(+ session.syncedVidPath.glob("*.mp4"), start=1+ ):session.numCams = count-+if stage == 6:- session.sessionPath = filepath/'Data'/session.sessionID+ session.sessionPath = filepath / "Data" / session.sessionID- session.yamlPath = session.sessionPath/'{}_config.yaml'.format(session.sessionID) #path to the configuration yaml- session.config_settings = recordingconfig.load_config_yaml(session.yamlPath) #config settings = paths and camera parameter inputs- recordingconfig.load_session_paths(session,session.config_settings) #add paths to session class+ session.yamlPath = session.sessionPath / "{}_config.yaml".format(+ session.sessionID+ ) # path to the configuration yaml+ session.config_settings = recordingconfig.load_config_yaml(+ session.yamlPath+ ) # config settings = paths and camera parameter inputs+ recordingconfig.load_session_paths(+ session, session.config_settings+ ) # add paths to session classif session.useOpenPose:- session.openPose_imgPathList = session.config_settings['openPose_imgPathList']-- if session.useMediaPipe:- session.mediaPipe_imgPathList = session.config_settings['mediaPipe_imgPathList']-+ session.openPose_imgPathList = session.config_settings[+ "openPose_imgPathList"+ ]+ if session.useMediaPipe:+ session.mediaPipe_imgPathList = session.config_settings[+ "mediaPipe_imgPathList"+ ]# if stage == 5:@@ -163,7 +201,7 @@ def initialize(session, stage, board):# session.config_settings = recordingconfig.load_config_yaml(session.yamlPath) #config settings = paths and camera parameter inputs# recordingconfig.load_session_paths(session,session.config_settings) #add paths to session class- # for count,thisVidPath in enumerate(session.syncedVidPath.glob('*.mp4'),start=1):+ # for count,thisVidPath in enumerate(session.syncedVidPath.glob('*.mp4'),start=1):# session.numCams = count# session.mediaPipe_jsonPathList = session.config_settings['mediaPipe_jsonPathList']@@ -178,32 +216,40 @@ def initialize(session, stage, board):# recordingconfig.load_session_paths(session,session.config_settings) #add paths to session class# session.mediaPipeData_nCams_nFrames_nImgPts_XY = np.load(session.dataArrayPath/'mediaPipeData_nCams_nFrames_nImgPts_XY.npy')-if stage == 7:- session.sessionPath = filepath/'Data'/session.sessionID-- session.yamlPath = session.sessionPath/'{}_config.yaml'.format(session.sessionID) #path to the configuration yaml- session.config_settings = recordingconfig.load_config_yaml(session.yamlPath) #config settings = paths and camera parameter inputs- recordingconfig.load_session_paths(session,session.config_settings) #add paths to session class+ session.sessionPath = filepath / "Data" / session.sessionID-- session.mediaPipe_imgPathList = session.config_settings['mediaPipe_imgPathList']- session.openPose_imgPathList = session.config_settings['openPose_imgPathList']+ session.yamlPath = session.sessionPath / "{}_config.yaml".format(+ session.sessionID+ ) # path to the configuration yaml+ session.config_settings = recordingconfig.load_config_yaml(+ session.yamlPath+ ) # config settings = paths and camera parameter inputs+ recordingconfig.load_session_paths(+ session, session.config_settings+ ) # add paths to session class-- #session.openPose_imgPathList = session.config_settings['openPose_imgPathList']- #session.numCams = len(session.openPose_imgPathList)+ session.mediaPipe_imgPathList = session.config_settings["mediaPipe_imgPathList"]+ session.openPose_imgPathList = session.config_settings["openPose_imgPathList"]-+ # session.openPose_imgPathList = session.config_settings['openPose_imgPathList']+ # session.numCams = len(session.openPose_imgPathList)if stage == 8:- session.sessionPath = filepath/'Data'/session.sessionID+ session.sessionPath = filepath / "Data" / session.sessionID++ session.yamlPath = session.sessionPath / "{}_config.yaml".format(+ session.sessionID+ ) # path to the configuration yaml+ session.config_settings = recordingconfig.load_config_yaml(+ session.yamlPath+ ) # config settings = paths and camera parameter inputs+ recordingconfig.load_session_paths(+ session, session.config_settings+ ) # add paths to session class- session.yamlPath = session.sessionPath/'{}_config.yaml'.format(session.sessionID) #path to the configuration yaml- session.config_settings = recordingconfig.load_config_yaml(session.yamlPath) #config settings = paths and camera parameter inputs- recordingconfig.load_session_paths(session,session.config_settings) #add paths to session class# %%diff --git a/freemocap/playskeleton.py b/freemocap/playskeleton.pyindex 32d4d05e..e8eae815 100644--- a/freemocap/playskeleton.py+++ b/freemocap/playskeleton.py@@ -5,370 +5,588 @@ Created on Fri Feb 26 12:51:21 2021@author: Rontc"""-import numpy as np-#from mpl_toolkits.mplot3d import Axes3D+import numpy as np++# from mpl_toolkits.mplot3d import Axes3Dimport matplotlib.pyplot as pltimport cv2from pathlib import Pathimport pyqtgraph as pg-################################################################################################################################################################################################################################################################################-##-## ββββ ββββ ββββββ ββββββββββββββββ βββ βββββββ ββββββββββββ ββββββββββ-## βββββ βββββββββββββββββββββββββββββββββ βββββββββββββββββββββ βββββββββββ-## βββββββββββββββββββ βββ βββββββββββ βββ βββ βββ βββ βββββββββββ-## βββββββββββββββββββ βββ βββββββ βββ βββ βββ βββ βββ βββββββββββ-## βββ βββ ββββββ βββ βββ βββ βββββββββββββββββ βββ βββββββββββββββββββ-## βββ ββββββ βββ βββ βββ ββββββββ βββββββ βββ ββββββββββββββββββ-################################################################################################################################################################################################################################################################################+##+## ββββ ββββ ββββββ ββββββββββββββββ βββ βββββββ ββββββββββββ βββββββββ+## βββββ βββββββββββββββββββββββββββββββββ βββββββββββββββββββββ ββββββββββ+## βββββββββββββββββββ βββ βββββββββββ βββ βββ βββ βββ βββββββββ+## βββββββββββββββββββ βββ βββββββ βββ βββ βββ βββ βββ ββββββββββ+## βββ βββ ββββββ βββ βββ βββ βββββββββββββββββ βββ βββββββββββββββββ+##+########################################################################################################################################+########################################################################################################################################+-def ReplaySkeleton_matplotlib( session,- vidType=1,- startFrame=40,- azimuth=-90,- elevation=-80,- useOpenPose=True,- useMediaPipe=False,- useDLC=False):+def ReplaySkeleton_matplotlib(+ session,+ vidType=1,+ startFrame=40,+ azimuth=-90,+ elevation=-80,+ useOpenPose=True,+ useMediaPipe=False,+ useDLC=False,+):-- mean_charuco_fr_mar_dim = np.load(session.dataArrayPath/'charuco_points.npy')+ mean_charuco_fr_mar_dim = np.load(session.dataArrayPath / "charuco_points.npy")if useOpenPose:- skel_fr_mar_dim = np.load(session.dataArrayPath/'openPoseSkel_3d.npy')+ skel_fr_mar_dim = np.load(session.dataArrayPath / "openPoseSkel_3d.npy")if useMediaPipe:- mediaPipe_skel_fr_mar_dim = np.load(session.dataArrayPath/'mediaPipeSkel_3d.npy')-+ mediaPipe_skel_fr_mar_dim = np.load(+ session.dataArrayPath / "mediaPipeSkel_3d.npy"+ )+if useDLC:- dlc_fr_mar_dim = np.load(session.dataArrayPath/'deepLabCut_3d.npy')- dlc0 = np.squeeze(dlc_fr_mar_dim[:,0,:])- dlc1 = np.squeeze(dlc_fr_mar_dim[:,1,:])- dlc2 = np.squeeze(dlc_fr_mar_dim[:,2,:])+ dlc_fr_mar_dim = np.load(session.dataArrayPath / "deepLabCut_3d.npy")+ dlc0 = np.squeeze(dlc_fr_mar_dim[:, 0, :])+ dlc1 = np.squeeze(dlc_fr_mar_dim[:, 1, :])+ dlc2 = np.squeeze(dlc_fr_mar_dim[:, 2, :])ballTrailLen = 4-if useOpenPose:- imgPathList = session.config_settings['openPose_imgPathList']+ imgPathList = session.config_settings["openPose_imgPathList"]session.numCams = len(session.openPose_imgPathList)-+camImgPathList = {}for cam in range(session.numCams):- camImgPathList[cam] = list(sorted(Path(imgPathList[cam]).glob('*.png')))- session.numFrames = len(camImgPathList[cam]) #will need to perhaps put a check in on whether numFrames between cameras are the same---# define Skeleton connections-# head = [17, 15, 0, 16, 18]- head = [ 15, 0, 16]- spine = [0,1,8]+ camImgPathList[cam] = list(sorted(Path(imgPathList[cam]).glob("*.png")))+ session.numFrames = len(+ camImgPathList[cam]+ ) # will need to perhaps put a check in on whether numFrames between cameras are the same++ # define Skeleton connections+ # head = [17, 15, 0, 16, 18]+ head = [15, 0, 16]+ spine = [0, 1, 8]rArm = [17, 15, 0, 16, 18]- rArm = [4 ,3 ,2 ,1]+ rArm = [4, 3, 2, 1]lArm = [1, 5, 6, 7]- rLeg = [11 ,10, 9, 8]- lLeg = [14 ,13 ,12, 8]- rFoot = [11, 23,22, 11, 24]+ rLeg = [11, 10, 9, 8]+ lLeg = [14, 13, 12, 8]+ rFoot = [11, 23, 22, 11, 24]lFoot = [14, 20, 19, 14, 21]- #Make some handy maps ;D- rHandIDstart = 25- lHandIDstart = rHandIDstart+21-- thumb = np.array([0,1,2,3,4])- index = np.array([0, 5,6,7,8])- bird = np.array([0, 9,10,11,12])- ring = np.array([0, 13,14,15,16])- pinky = np.array([0, 17,18,19,20])+ # Make some handy maps ;D+ rHandIDstart = 25+ lHandIDstart = rHandIDstart + 21+ thumb = np.array([0, 1, 2, 3, 4])+ index = np.array([0, 5, 6, 7, 8])+ bird = np.array([0, 9, 10, 11, 12])+ ring = np.array([0, 13, 14, 15, 16])+ pinky = np.array([0, 17, 18, 19, 20])- fig = plt.figure(figsize=(11,8))+ fig = plt.figure(figsize=(11, 8))if vidType == 0:- axMain = fig.add_subplot(111,projection='3d')+ axMain = fig.add_subplot(111, projection="3d")elif vidType == 1:- axMain = fig.add_subplot(121,projection='3d')+ axMain = fig.add_subplot(121, projection="3d")axCam1 = fig.add_subplot(122)# fig.tight_layout(h_pad = 5)- axMain.view_init(azim = azimuth, elev= elevation)+ axMain.view_init(azim=azimuth, elev=elevation)plt.ion()- for fr in range(startFrame,session.numFrames):- fig.suptitle(['Frame# - ', str(fr)])+ for fr in range(startFrame, session.numFrames):+ fig.suptitle(["Frame# - ", str(fr)])axMain.cla()if vidType == 1:axCam1.cla()-- char_x = mean_charuco_fr_mar_dim[:][:,0]- char_y = mean_charuco_fr_mar_dim[:][:,1]- char_z = mean_charuco_fr_mar_dim[:][:,2]--- #plot skeleton (openPose by default)-- sk_x = skel_fr_mar_dim[fr,:,0] #skeleton x data- sk_y = skel_fr_mar_dim[fr,:,1] #skeleton y data- sk_z = skel_fr_mar_dim[fr,:,2] #skeleton z data-- axMain.scatter3D(sk_x,sk_y,sk_z, marker='.',color = 'k', s=4.)- axMain.plot(sk_x[head],sk_y[head],sk_z[head], linestyle='-', color='g', linewidth = 1.)- axMain.plot(sk_x[spine],sk_y[spine],sk_z[spine], linestyle='-', color = 'g', linewidth = 1.)- axMain.plot(sk_x[rArm],sk_y[rArm],sk_z[rArm], linestyle='-', color = 'r', linewidth = 1.)- axMain.plot(sk_x[lArm],sk_y[lArm],sk_z[lArm], linestyle='-', color = 'b', linewidth = 1.)- axMain.plot(sk_x[rLeg],sk_y[rLeg],sk_z[rLeg], linestyle='-', color = 'r', linewidth = 1.)- axMain.plot(sk_x[lLeg],sk_y[lLeg],sk_z[lLeg], linestyle='-', color = 'b', linewidth = 1.)- axMain.plot(sk_x[rFoot],sk_y[rFoot],sk_z[rFoot], linestyle='-', color = 'r', linewidth = 1.)- axMain.plot(sk_x[lFoot],sk_y[lFoot],sk_z[lFoot], linestyle='-', color = 'b', linewidth = 1.)+ char_x = mean_charuco_fr_mar_dim[:][:, 0]+ char_y = mean_charuco_fr_mar_dim[:][:, 1]+ char_z = mean_charuco_fr_mar_dim[:][:, 2]++ # plot skeleton (openPose by default)++ sk_x = skel_fr_mar_dim[fr, :, 0] # skeleton x data+ sk_y = skel_fr_mar_dim[fr, :, 1] # skeleton y data+ sk_z = skel_fr_mar_dim[fr, :, 2] # skeleton z data++ axMain.scatter3D(sk_x, sk_y, sk_z, marker=".", color="k", s=4.0)+ axMain.plot(+ sk_x[head], sk_y[head], sk_z[head], linestyle="-", color="g", linewidth=1.0+ )+ axMain.plot(+ sk_x[spine],+ sk_y[spine],+ sk_z[spine],+ linestyle="-",+ color="g",+ linewidth=1.0,+ )+ axMain.plot(+ sk_x[rArm], sk_y[rArm], sk_z[rArm], linestyle="-", color="r", linewidth=1.0+ )+ axMain.plot(+ sk_x[lArm], sk_y[lArm], sk_z[lArm], linestyle="-", color="b", linewidth=1.0+ )+ axMain.plot(+ sk_x[rLeg], sk_y[rLeg], sk_z[rLeg], linestyle="-", color="r", linewidth=1.0+ )+ axMain.plot(+ sk_x[lLeg], sk_y[lLeg], sk_z[lLeg], linestyle="-", color="b", linewidth=1.0+ )+ axMain.plot(+ sk_x[rFoot],+ sk_y[rFoot],+ sk_z[rFoot],+ linestyle="-",+ color="r",+ linewidth=1.0,+ )+ axMain.plot(+ sk_x[lFoot],+ sk_y[lFoot],+ sk_z[lFoot],+ linestyle="-",+ color="b",+ linewidth=1.0,+ )# plot handybois# right hand- axMain.plot(sk_x[thumb+rHandIDstart],sk_y[thumb+rHandIDstart],sk_z[thumb+rHandIDstart], linestyle='-', color = 'r', linewidth = 1.)- axMain.plot(sk_x[index+rHandIDstart],sk_y[index+rHandIDstart],sk_z[index+rHandIDstart], linestyle='-', color = 'r', linewidth = 1.)- axMain.plot(sk_x[bird+rHandIDstart],sk_y[bird+rHandIDstart],sk_z[bird+rHandIDstart], linestyle='-', color = 'r', linewidth = 1.)- axMain.plot(sk_x[ring+rHandIDstart],sk_y[ring+rHandIDstart],sk_z[ring+rHandIDstart], linestyle='-', color = 'r', linewidth = 1.)- axMain.plot(sk_x[pinky+rHandIDstart],sk_y[pinky+rHandIDstart],sk_z[pinky+rHandIDstart], linestyle='-', color = 'r', linewidth = 1.)-- #left hand- axMain.plot(sk_x[thumb+lHandIDstart],sk_y[thumb+lHandIDstart],sk_z[thumb+lHandIDstart], linestyle='-', color = 'b', linewidth = 1.)- axMain.plot(sk_x[index+lHandIDstart],sk_y[index+lHandIDstart],sk_z[index+lHandIDstart], linestyle='-', color = 'b', linewidth = 1.)- axMain.plot(sk_x[bird+lHandIDstart],sk_y[bird+lHandIDstart],sk_z[bird+lHandIDstart], linestyle='-', color = 'b', linewidth = 1.)- axMain.plot(sk_x[ring+lHandIDstart],sk_y[ring+lHandIDstart],sk_z[ring+lHandIDstart], linestyle='-', color = 'b', linewidth = 1.)- axMain.plot(sk_x[pinky+lHandIDstart],sk_y[pinky+lHandIDstart],sk_z[pinky+lHandIDstart], linestyle='-', color = 'b', linewidth = 1.)--- #plot mediapipe- mp_sk_x = mediaPipe_skel_fr_mar_dim[fr,:,0] #skeleton x data- mp_sk_y = mediaPipe_skel_fr_mar_dim[fr,:,1] #skeleton y data- mp_sk_z = mediaPipe_skel_fr_mar_dim[fr,:,2] #skeleton z data- axMain.scatter3D(mp_sk_x, mp_sk_y, mp_sk_z, marker='.',color = 'g', s=8.)-- #plot deeplabcut-- dlc_x = dlc_fr_mar_dim[fr,:,0]- dlc_y = dlc_fr_mar_dim[fr,:,1]- dlc_z = dlc_fr_mar_dim[fr,:,2]-- axMain.scatter3D(dlc_x,dlc_y,dlc_z, marker='o',color = 'r', s=24.)-- #plot charuco grid- axMain.scatter(char_x,char_y,char_z, marker='o')+ axMain.plot(+ sk_x[thumb + rHandIDstart],+ sk_y[thumb + rHandIDstart],+ sk_z[thumb + rHandIDstart],+ linestyle="-",+ color="r",+ linewidth=1.0,+ )+ axMain.plot(+ sk_x[index + rHandIDstart],+ sk_y[index + rHandIDstart],+ sk_z[index + rHandIDstart],+ linestyle="-",+ color="r",+ linewidth=1.0,+ )+ axMain.plot(+ sk_x[bird + rHandIDstart],+ sk_y[bird + rHandIDstart],+ sk_z[bird + rHandIDstart],+ linestyle="-",+ color="r",+ linewidth=1.0,+ )+ axMain.plot(+ sk_x[ring + rHandIDstart],+ sk_y[ring + rHandIDstart],+ sk_z[ring + rHandIDstart],+ linestyle="-",+ color="r",+ linewidth=1.0,+ )+ axMain.plot(+ sk_x[pinky + rHandIDstart],+ sk_y[pinky + rHandIDstart],+ sk_z[pinky + rHandIDstart],+ linestyle="-",+ color="r",+ linewidth=1.0,+ )++ # left hand+ axMain.plot(+ sk_x[thumb + lHandIDstart],+ sk_y[thumb + lHandIDstart],+ sk_z[thumb + lHandIDstart],+ linestyle="-",+ color="b",+ linewidth=1.0,+ )+ axMain.plot(+ sk_x[index + lHandIDstart],+ sk_y[index + lHandIDstart],+ sk_z[index + lHandIDstart],+ linestyle="-",+ color="b",+ linewidth=1.0,+ )+ axMain.plot(+ sk_x[bird + lHandIDstart],+ sk_y[bird + lHandIDstart],+ sk_z[bird + lHandIDstart],+ linestyle="-",+ color="b",+ linewidth=1.0,+ )+ axMain.plot(+ sk_x[ring + lHandIDstart],+ sk_y[ring + lHandIDstart],+ sk_z[ring + lHandIDstart],+ linestyle="-",+ color="b",+ linewidth=1.0,+ )+ axMain.plot(+ sk_x[pinky + lHandIDstart],+ sk_y[pinky + lHandIDstart],+ sk_z[pinky + lHandIDstart],+ linestyle="-",+ color="b",+ linewidth=1.0,+ )++ # plot mediapipe+ mp_sk_x = mediaPipe_skel_fr_mar_dim[fr, :, 0] # skeleton x data+ mp_sk_y = mediaPipe_skel_fr_mar_dim[fr, :, 1] # skeleton y data+ mp_sk_z = mediaPipe_skel_fr_mar_dim[fr, :, 2] # skeleton z data+ axMain.scatter3D(mp_sk_x, mp_sk_y, mp_sk_z, marker=".", color="g", s=8.0)++ # plot deeplabcut++ dlc_x = dlc_fr_mar_dim[fr, :, 0]+ dlc_y = dlc_fr_mar_dim[fr, :, 1]+ dlc_z = dlc_fr_mar_dim[fr, :, 2]++ axMain.scatter3D(dlc_x, dlc_y, dlc_z, marker="o", color="r", s=24.0)++ # plot charuco grid+ axMain.scatter(char_x, char_y, char_z, marker="o")# #plot camera positions (I think...)# for camNum in range(len(session.cgroup.cameras)):# axMain.scatter(session.cgroup.cameras[camNum].tvec[0], session.cgroup.cameras[camNum].tvec[1], session.cgroup.cameras[camNum].tvec[2], marker = 'p')- axRange = session.board.square_length*10+ axRange = session.board.square_length * 10mx = np.nanmean(char_x)my = np.nanmean(char_y)mz = np.nanmean(char_z)-- axMain.set_xlim(mx-axRange,mx+axRange)- axMain.set_ylim(my-axRange,my+axRange)- axMain.set_zlim(mz-axRange,mz+axRange)- axMain.set_xlabel('x')- axMain.set_ylabel('y')- axMain.set_zlabel('z')+ axMain.set_xlim(mx - axRange, mx + axRange)+ axMain.set_ylim(my - axRange, my + axRange)+ axMain.set_zlim(mz - axRange, mz + axRange)+ axMain.set_xlabel("x")+ axMain.set_ylabel("y")+ axMain.set_zlabel("z")-- if vidType ==0:- pass+ if vidType == 0:+ passelif vidType == 1:- axCam1.imshow(cv2.cvtColor(cv2.imread(str(camImgPathList[0][fr])),cv2.COLOR_BGR2RGB))- axCam1.axis('off')+ axCam1.imshow(+ cv2.cvtColor(cv2.imread(str(camImgPathList[0][fr])), cv2.COLOR_BGR2RGB)+ )+ axCam1.axis("off")plt.pause(0.01)plt.show()frameName = str(fr).zfill(6)- saveFrameName = '{}.png'.format(frameName)- saveFramePath = session.imOutPath/saveFrameName- fig.savefig(saveFramePath )-+ saveFrameName = "{}.png".format(frameName)+ saveFramePath = session.imOutPath / saveFrameName+ fig.savefig(saveFramePath)---################################################################################################################################################################################################################################################################################-### βββββββ βββββββ ββββββββββββ βββ βββββββ βββ-### ββββββββββββββββββββββββββββββ βββ ββββββββ βββ-### βββ βββββββββββββββββ ββββββ βββ βββ βββββββ-### βββ ββββββββββ ββββββ ββββββββββ βββ ββββββ-### ββββββββββββ βββββββββββ ββββββ βββββββββββββββββ-########################################################################################################################################+########################################################################################################################################+### βββββββ βββββββ ββββββββββββ βββ βββββββ ββ+### ββββββββββββββββββββββββββββββ βββ ββββββββ ββ+### βββ βββββββββββββββββ ββββββ βββ βββ ββββββ+### βββ ββββββββββ ββββββ ββββββββββ βββ βββββ+### ββββββββββββ βββββββββββ ββββββ ββββββββββββββββ+################################################################################################################################################################################################################################################################################+def ReplaySkeleton_matplotlib(+ session,+ vidType=1,+ startFrame=40,+ azimuth=-90,+ elevation=-80,+ useOpenPose=True,+ useMediaPipe=False,+ useDLC=False,+):---def ReplaySkeleton_matplotlib( session,- vidType=1,- startFrame=40,- azimuth=-90,- elevation=-80,- useOpenPose=True,- useMediaPipe=False,- useDLC=False):--- mean_charuco_fr_mar_dim = np.load(session.dataArrayPath/'charuco_points.npy')-+ mean_charuco_fr_mar_dim = np.load(session.dataArrayPath / "charuco_points.npy")if session.useMediaPipe:- mediaPipe_skel_fr_mar_dim = np.load(session.dataArrayPath/'mediaPipeSkel_3d.npy')- imgPathList = session.config_settings['mediaPipe_imgPathList']+ mediaPipe_skel_fr_mar_dim = np.load(+ session.dataArrayPath / "mediaPipeSkel_3d.npy"+ )+ imgPathList = session.config_settings["mediaPipe_imgPathList"]session.numCams = len(session.mediaPipe_imgPathList)-if session.useOpenPose:- skel_fr_mar_dim = np.load(session.dataArrayPath/'openPoseSkel_3d.npy')- imgPathList = session.config_settings['openPose_imgPathList']- session.numCams = len(session.openPose_imgPathList)-+ skel_fr_mar_dim = np.load(session.dataArrayPath / "openPoseSkel_3d.npy")+ imgPathList = session.config_settings["openPose_imgPathList"]+ session.numCams = len(session.openPose_imgPathList)+if session.useDLC:- dlc_fr_mar_dim = np.load(session.dataArrayPath/'deepLabCut_3d.npy')- dlc0 = np.squeeze(dlc_fr_mar_dim[:,0,:])- dlc1 = np.squeeze(dlc_fr_mar_dim[:,1,:])- dlc2 = np.squeeze(dlc_fr_mar_dim[:,2,:])+ dlc_fr_mar_dim = np.load(session.dataArrayPath / "deepLabCut_3d.npy")+ dlc0 = np.squeeze(dlc_fr_mar_dim[:, 0, :])+ dlc1 = np.squeeze(dlc_fr_mar_dim[:, 1, :])+ dlc2 = np.squeeze(dlc_fr_mar_dim[:, 2, :])ballTrailLen = 4---camImgPathList = {}for cam in range(session.numCams):- camImgPathList[cam] = list(sorted(Path(imgPathList[cam]).glob('*.png')))- session.numFrames = len(camImgPathList[cam]) #will need to perhaps put a check in on whether numFrames between cameras are the same---# define Skeleton connections-# head = [17, 15, 0, 16, 18]- head = [ 15, 0, 16]- spine = [0,1,8]- rArm = [4 ,3 ,2 ,1]+ camImgPathList[cam] = list(sorted(Path(imgPathList[cam]).glob("*.png")))+ session.numFrames = len(+ camImgPathList[cam]+ ) # will need to perhaps put a check in on whether numFrames between cameras are the same++ # define Skeleton connections+ # head = [17, 15, 0, 16, 18]+ head = [15, 0, 16]+ spine = [0, 1, 8]+ rArm = [4, 3, 2, 1]lArm = [1, 5, 6, 7]- rLeg = [11 ,10, 9, 8]- lLeg = [14 ,13 ,12, 8]- rFoot = [11, 23,22, 11, 24]+ rLeg = [11, 10, 9, 8]+ lLeg = [14, 13, 12, 8]+ rFoot = [11, 23, 22, 11, 24]lFoot = [14, 20, 19, 14, 21]- #Make some handy maps ;D- rHandIDstart = 25- lHandIDstart = rHandIDstart+21-- thumb = np.array([0,1,2,3,4])- index = np.array([0, 5,6,7,8])- bird = np.array([0, 9,10,11,12])- ring = np.array([0, 13,14,15,16])- pinky = np.array([0, 17,18,19,20])+ # Make some handy maps ;D+ rHandIDstart = 25+ lHandIDstart = rHandIDstart + 21+ thumb = np.array([0, 1, 2, 3, 4])+ index = np.array([0, 5, 6, 7, 8])+ bird = np.array([0, 9, 10, 11, 12])+ ring = np.array([0, 13, 14, 15, 16])+ pinky = np.array([0, 17, 18, 19, 20])- fig = plt.figure(figsize=(11,8))+ fig = plt.figure(figsize=(11, 8))if vidType == 0:- axMain = fig.add_subplot(111,projection='3d')+ axMain = fig.add_subplot(111, projection="3d")elif vidType == 1:- axMain = fig.add_subplot(121,projection='3d')+ axMain = fig.add_subplot(121, projection="3d")axCam1 = fig.add_subplot(122)# fig.tight_layout(h_pad = 5)- axMain.view_init(azim = azimuth, elev= elevation)+ axMain.view_init(azim=azimuth, elev=elevation)plt.ion()- for fr in range(startFrame,session.numFrames):- fig.suptitle(['Frame# - ', str(fr)])+ for fr in range(startFrame, session.numFrames):+ fig.suptitle(["Frame# - ", str(fr)])axMain.cla()if vidType == 1:axCam1.cla()+ char_x = mean_charuco_fr_mar_dim[:][:, 0]+ char_y = mean_charuco_fr_mar_dim[:][:, 1]+ char_z = mean_charuco_fr_mar_dim[:][:, 2]- char_x = mean_charuco_fr_mar_dim[:][:,0]- char_y = mean_charuco_fr_mar_dim[:][:,1]- char_z = mean_charuco_fr_mar_dim[:][:,2]--- #plot skeleton (openPose by default)+ # plot skeleton (openPose by default)if session.useOpenPose:- sk_x = skel_fr_mar_dim[fr,:,0] #skeleton x data- sk_y = skel_fr_mar_dim[fr,:,1] #skeleton y data- sk_z = skel_fr_mar_dim[fr,:,2] #skeleton z data-- axMain.scatter3D(sk_x,sk_y,sk_z, marker='.',color = 'k', s=4.)- axMain.plot(sk_x[head],sk_y[head],sk_z[head], linestyle='-', color='g', linewidth = 1.)- axMain.plot(sk_x[spine],sk_y[spine],sk_z[spine], linestyle='-', color = 'g', linewidth = 1.)- axMain.plot(sk_x[rArm],sk_y[rArm],sk_z[rArm], linestyle='-', color = 'r', linewidth = 1.)- axMain.plot(sk_x[lArm],sk_y[lArm],sk_z[lArm], linestyle='-', color = 'b', linewidth = 1.)- axMain.plot(sk_x[rLeg],sk_y[rLeg],sk_z[rLeg], linestyle='-', color = 'r', linewidth = 1.)- axMain.plot(sk_x[lLeg],sk_y[lLeg],sk_z[lLeg], linestyle='-', color = 'b', linewidth = 1.)- axMain.plot(sk_x[rFoot],sk_y[rFoot],sk_z[rFoot], linestyle='-', color = 'r', linewidth = 1.)- axMain.plot(sk_x[lFoot],sk_y[lFoot],sk_z[lFoot], linestyle='-', color = 'b', linewidth = 1.)+ sk_x = skel_fr_mar_dim[fr, :, 0] # skeleton x data+ sk_y = skel_fr_mar_dim[fr, :, 1] # skeleton y data+ sk_z = skel_fr_mar_dim[fr, :, 2] # skeleton z data++ axMain.scatter3D(sk_x, sk_y, sk_z, marker=".", color="k", s=4.0)+ axMain.plot(+ sk_x[head],+ sk_y[head],+ sk_z[head],+ linestyle="-",+ color="g",+ linewidth=1.0,+ )+ axMain.plot(+ sk_x[spine],+ sk_y[spine],+ sk_z[spine],+ linestyle="-",+ color="g",+ linewidth=1.0,+ )+ axMain.plot(+ sk_x[rArm],+ sk_y[rArm],+ sk_z[rArm],+ linestyle="-",+ color="r",+ linewidth=1.0,+ )+ axMain.plot(+ sk_x[lArm],+ sk_y[lArm],+ sk_z[lArm],+ linestyle="-",+ color="b",+ linewidth=1.0,+ )+ axMain.plot(+ sk_x[rLeg],+ sk_y[rLeg],+ sk_z[rLeg],+ linestyle="-",+ color="r",+ linewidth=1.0,+ )+ axMain.plot(+ sk_x[lLeg],+ sk_y[lLeg],+ sk_z[lLeg],+ linestyle="-",+ color="b",+ linewidth=1.0,+ )+ axMain.plot(+ sk_x[rFoot],+ sk_y[rFoot],+ sk_z[rFoot],+ linestyle="-",+ color="r",+ linewidth=1.0,+ )+ axMain.plot(+ sk_x[lFoot],+ sk_y[lFoot],+ sk_z[lFoot],+ linestyle="-",+ color="b",+ linewidth=1.0,+ )# plot handybois# right hand- axMain.plot(sk_x[thumb+rHandIDstart],sk_y[thumb+rHandIDstart],sk_z[thumb+rHandIDstart], linestyle='-', color = 'r', linewidth = 1.)- axMain.plot(sk_x[index+rHandIDstart],sk_y[index+rHandIDstart],sk_z[index+rHandIDstart], linestyle='-', color = 'r', linewidth = 1.)- axMain.plot(sk_x[bird+rHandIDstart],sk_y[bird+rHandIDstart],sk_z[bird+rHandIDstart], linestyle='-', color = 'r', linewidth = 1.)- axMain.plot(sk_x[ring+rHandIDstart],sk_y[ring+rHandIDstart],sk_z[ring+rHandIDstart], linestyle='-', color = 'r', linewidth = 1.)- axMain.plot(sk_x[pinky+rHandIDstart],sk_y[pinky+rHandIDstart],sk_z[pinky+rHandIDstart], linestyle='-', color = 'r', linewidth = 1.)-- #left hand- axMain.plot(sk_x[thumb+lHandIDstart],sk_y[thumb+lHandIDstart],sk_z[thumb+lHandIDstart], linestyle='-', color = 'b', linewidth = 1.)- axMain.plot(sk_x[index+lHandIDstart],sk_y[index+lHandIDstart],sk_z[index+lHandIDstart], linestyle='-', color = 'b', linewidth = 1.)- axMain.plot(sk_x[bird+lHandIDstart],sk_y[bird+lHandIDstart],sk_z[bird+lHandIDstart], linestyle='-', color = 'b', linewidth = 1.)- axMain.plot(sk_x[ring+lHandIDstart],sk_y[ring+lHandIDstart],sk_z[ring+lHandIDstart], linestyle='-', color = 'b', linewidth = 1.)- axMain.plot(sk_x[pinky+lHandIDstart],sk_y[pinky+lHandIDstart],sk_z[pinky+lHandIDstart], linestyle='-', color = 'b', linewidth = 1.)--- #plot mediapipe+ axMain.plot(+ sk_x[thumb + rHandIDstart],+ sk_y[thumb + rHandIDstart],+ sk_z[thumb + rHandIDstart],+ linestyle="-",+ color="r",+ linewidth=1.0,+ )+ axMain.plot(+ sk_x[index + rHandIDstart],+ sk_y[index + rHandIDstart],+ sk_z[index + rHandIDstart],+ linestyle="-",+ color="r",+ linewidth=1.0,+ )+ axMain.plot(+ sk_x[bird + rHandIDstart],+ sk_y[bird + rHandIDstart],+ sk_z[bird + rHandIDstart],+ linestyle="-",+ color="r",+ linewidth=1.0,+ )+ axMain.plot(+ sk_x[ring + rHandIDstart],+ sk_y[ring + rHandIDstart],+ sk_z[ring + rHandIDstart],+ linestyle="-",+ color="r",+ linewidth=1.0,+ )+ axMain.plot(+ sk_x[pinky + rHandIDstart],+ sk_y[pinky + rHandIDstart],+ sk_z[pinky + rHandIDstart],+ linestyle="-",+ color="r",+ linewidth=1.0,+ )++ # left hand+ axMain.plot(+ sk_x[thumb + lHandIDstart],+ sk_y[thumb + lHandIDstart],+ sk_z[thumb + lHandIDstart],+ linestyle="-",+ color="b",+ linewidth=1.0,+ )+ axMain.plot(+ sk_x[index + lHandIDstart],+ sk_y[index + lHandIDstart],+ sk_z[index + lHandIDstart],+ linestyle="-",+ color="b",+ linewidth=1.0,+ )+ axMain.plot(+ sk_x[bird + lHandIDstart],+ sk_y[bird + lHandIDstart],+ sk_z[bird + lHandIDstart],+ linestyle="-",+ color="b",+ linewidth=1.0,+ )+ axMain.plot(+ sk_x[ring + lHandIDstart],+ sk_y[ring + lHandIDstart],+ sk_z[ring + lHandIDstart],+ linestyle="-",+ color="b",+ linewidth=1.0,+ )+ axMain.plot(+ sk_x[pinky + lHandIDstart],+ sk_y[pinky + lHandIDstart],+ sk_z[pinky + lHandIDstart],+ linestyle="-",+ color="b",+ linewidth=1.0,+ )++ # plot mediapipeif session.useMediaPipe:- mp_sk_x = mediaPipe_skel_fr_mar_dim[fr,:,0] #skeleton x data- mp_sk_y = mediaPipe_skel_fr_mar_dim[fr,:,1] #skeleton y data- mp_sk_z = mediaPipe_skel_fr_mar_dim[fr,:,2] #skeleton z data- axMain.scatter3D(mp_sk_x, mp_sk_y, mp_sk_z, marker='.',color = 'g', s=8.)+ mp_sk_x = mediaPipe_skel_fr_mar_dim[fr, :, 0] # skeleton x data+ mp_sk_y = mediaPipe_skel_fr_mar_dim[fr, :, 1] # skeleton y data+ mp_sk_z = mediaPipe_skel_fr_mar_dim[fr, :, 2] # skeleton z data+ axMain.scatter3D(mp_sk_x, mp_sk_y, mp_sk_z, marker=".", color="g", s=8.0)- #plot deeplabcut+ # plot deeplabcutif session.useDLC:- dlc_x = dlc_fr_mar_dim[fr,:,0]- dlc_y = dlc_fr_mar_dim[fr,:,1]- dlc_z = dlc_fr_mar_dim[fr,:,2]-- axMain.scatter3D(dlc_x,dlc_y,dlc_z, marker='o',color = 'r', s=24.)+ dlc_x = dlc_fr_mar_dim[fr, :, 0]+ dlc_y = dlc_fr_mar_dim[fr, :, 1]+ dlc_z = dlc_fr_mar_dim[fr, :, 2]++ axMain.scatter3D(dlc_x, dlc_y, dlc_z, marker="o", color="r", s=24.0)- #plot charuco grid- axMain.scatter(char_x,char_y,char_z, marker='o')+ # plot charuco grid+ axMain.scatter(char_x, char_y, char_z, marker="o")# #plot camera positions (I think...)# for camNum in range(len(session.cgroup.cameras)):# axMain.scatter(session.cgroup.cameras[camNum].tvec[0], session.cgroup.cameras[camNum].tvec[1], session.cgroup.cameras[camNum].tvec[2], marker = 'p')- axRange = session.board.square_length*10+ axRange = session.board.square_length * 10mx = np.nanmean(char_x)my = np.nanmean(char_y)mz = np.nanmean(char_z)-- axMain.set_xlim(mx-axRange,mx+axRange)- axMain.set_ylim(my-axRange,my+axRange)- axMain.set_zlim(mz-axRange,mz+axRange)-- axMain.set_xlabel('x')- axMain.set_ylabel('y')- axMain.set_zlabel('z')--- if vidType ==0:- pass++ axMain.set_xlim(mx - axRange, mx + axRange)+ axMain.set_ylim(my - axRange, my + axRange)+ axMain.set_zlim(mz - axRange, mz + axRange)++ axMain.set_xlabel("x")+ axMain.set_ylabel("y")+ axMain.set_zlabel("z")++ if vidType == 0:+ passelif vidType == 1:- axCam1.imshow(cv2.cvtColor(cv2.imread(str(camImgPathList[0][fr])),cv2.COLOR_BGR2RGB))- axCam1.axis('off')+ axCam1.imshow(+ cv2.cvtColor(cv2.imread(str(camImgPathList[0][fr])), cv2.COLOR_BGR2RGB)+ )+ axCam1.axis("off")plt.pause(0.01)plt.show()frameName = str(fr).zfill(6)- saveFrameName = '{}.png'.format(frameName)- saveFramePath = session.imOutPath/saveFrameName- fig.savefig(saveFramePath )+ saveFrameName = "{}.png".format(frameName)+ saveFramePath = session.imOutPath / saveFrameName+ fig.savefig(saveFramePath)diff --git a/freemocap/reconstruct3D.py b/freemocap/reconstruct3D.pyindex 838ed202..deeb552d 100644--- a/freemocap/reconstruct3D.py+++ b/freemocap/reconstruct3D.py@@ -1,38 +1,54 @@from freemocap import fmc_anipose-import numpy as np+import numpy as np+# from numba import jit# @jit(nopython=True)-def reconstruct3D(session, data_nCams_nFrames_nImgPts_XYC, confidenceThreshold = .3):-- if session.cgroup is None: #load the calibration settings into the session class if it hasn't been already- calibrationFile = '{}_calibration.yaml'.format(session.sessionID)- session.cameraConfigFilePath = session.sessionPath/calibrationFile+def reconstruct3D(session, data_nCams_nFrames_nImgPts_XYC, confidenceThreshold=0.3):++ if (+ session.cgroup is None+ ): # load the calibration settings into the session class if it hasn't been already+ calibrationFile = "{}_calibration.yaml".format(session.sessionID)+ session.cameraConfigFilePath = session.sessionPath / calibrationFilesession.cgroup = fmc_anipose.CameraGroup.load(session.cameraConfigFilePath)--+nCams, nFrames, nImgPts, nDims = data_nCams_nFrames_nImgPts_XYC.shapeif nDims == 3:-- data_nCams_nFrames_nImgPts_X = np.squeeze(data_nCams_nFrames_nImgPts_XYC[:,:,:,0].copy())- data_nCams_nFrames_nImgPts_Y = np.squeeze(data_nCams_nFrames_nImgPts_XYC[:,:,:,1].copy())- confidence = np.squeeze(data_nCams_nFrames_nImgPts_XYC[:,:,:,2].copy())- data_nCams_nFrames_nImgPts_X[confidence < confidenceThreshold] = np.nan #replace low confidence points with 'nan'+ data_nCams_nFrames_nImgPts_X = np.squeeze(+ data_nCams_nFrames_nImgPts_XYC[:, :, :, 0].copy()+ )+ data_nCams_nFrames_nImgPts_Y = np.squeeze(+ data_nCams_nFrames_nImgPts_XYC[:, :, :, 1].copy()+ )+ confidence = np.squeeze(data_nCams_nFrames_nImgPts_XYC[:, :, :, 2].copy())++ data_nCams_nFrames_nImgPts_X[+ confidence < confidenceThreshold+ ] = np.nan # replace low confidence points with 'nan'data_nCams_nFrames_nImgPts_Y[confidence < confidenceThreshold] = np.nan- data_nCams_nFrames_nImgPts_XY =np.stack((data_nCams_nFrames_nImgPts_X, data_nCams_nFrames_nImgPts_Y), axis=3)-+ data_nCams_nFrames_nImgPts_XY = np.stack(+ (data_nCams_nFrames_nImgPts_X, data_nCams_nFrames_nImgPts_Y), axis=3+ )+if nDims == 2:data_nCams_nFrames_nImgPts_XY = data_nCams_nFrames_nImgPts_XYC- dataFlat_nCams_nTotalPoints_XY = data_nCams_nFrames_nImgPts_XY.reshape(nCams, -1, 2) #reshape data to collapse across 'frames' so it becomes [numCams, numFrames*numPoints, XY]+ dataFlat_nCams_nTotalPoints_XY = data_nCams_nFrames_nImgPts_XY.reshape(+ nCams, -1, 2+ ) # reshape data to collapse across 'frames' so it becomes [numCams, numFrames*numPoints, XY]- data3d_flat = session.cgroup.triangulate(dataFlat_nCams_nTotalPoints_XY, progress=True)- dataReprojerr_flat = session.cgroup.reprojection_error(data3d_flat, dataFlat_nCams_nTotalPoints_XY, mean=True)+ data3d_flat = session.cgroup.triangulate(+ dataFlat_nCams_nTotalPoints_XY, progress=True+ )+ dataReprojerr_flat = session.cgroup.reprojection_error(+ data3d_flat, dataFlat_nCams_nTotalPoints_XY, mean=True+ )##return:data_fr_mar_dim = data3d_flat.reshape(nFrames, nImgPts, 3)dataReprojErr_fr_mar_err = dataReprojerr_flat.reshape(nFrames, nImgPts)- return data_fr_mar_dim+ return data_fr_mar_dimdiff --git a/freemocap/recordingconfig.py b/freemocap/recordingconfig.pyindex bddfcbcc..2539e609 100644--- a/freemocap/recordingconfig.py+++ b/freemocap/recordingconfig.py@@ -7,81 +7,79 @@ Created on Thu Mar 11 11:54:12 2021from ruamel.yaml import YAMLfrom pathlib import Path-def createSession(session,filepath, existing_data = False):- #create file paths for the session, raw and synced videos-++def createSession(session, filepath, existing_data=False):+ # create file paths for the session, raw and synced videos+pathList = []-- recordPath = filepath/'Data'- recordPath.mkdir(exist_ok= True)-- session.sessionPath = recordPath/session.sessionID- #if existing_data:- #session.sessionPath.mkdir(exist_ok = True)- #else:- session.sessionPath.mkdir(exist_ok= True)++ recordPath = filepath / "Data"+ recordPath.mkdir(exist_ok=True)++ session.sessionPath = recordPath / session.sessionID+ # if existing_data:+ # session.sessionPath.mkdir(exist_ok = True)+ # else:+ session.sessionPath.mkdir(exist_ok=True)pathList.append(session.sessionPath)-- session.rawVidPath = session.sessionPath/'RawVideos'- session.rawVidPath.mkdir(exist_ok = True)++ session.rawVidPath = session.sessionPath / "RawVideos"+ session.rawVidPath.mkdir(exist_ok=True)pathList.append(session.rawVidPath)- session.syncedVidPath = session.sessionPath/'SyncedVideos'- session.syncedVidPath.mkdir(exist_ok = True)+ session.syncedVidPath = session.sessionPath / "SyncedVideos"+ session.syncedVidPath.mkdir(exist_ok=True)pathList.append(session.syncedVidPath)- session.calVidPath = session.sessionPath/'CalVideos'- session.calVidPath.mkdir(exist_ok = True)+ session.calVidPath = session.sessionPath / "CalVideos"+ session.calVidPath.mkdir(exist_ok=True)pathList.append(session.calVidPath)- session.openPoseDataPath = session.sessionPath/'OpenPoseData'- session.openPoseDataPath.mkdir(exist_ok = True)+ session.openPoseDataPath = session.sessionPath / "OpenPoseData"+ session.openPoseDataPath.mkdir(exist_ok=True)pathList.append(session.openPoseDataPath)- session.mediaPipeDataPath = session.sessionPath/'MediaPipeData'- session.mediaPipeDataPath.mkdir(exist_ok = True)+ session.mediaPipeDataPath = session.sessionPath / "MediaPipeData"+ session.mediaPipeDataPath.mkdir(exist_ok=True)pathList.append(session.mediaPipeDataPath)-- session.dlcDataPath = session.sessionPath / 'DLCdata'- session.dlcDataPath.mkdir(exist_ok = True, parents = True)++ session.dlcDataPath = session.sessionPath / "DLCdata"+ session.dlcDataPath.mkdir(exist_ok=True, parents=True)pathList.append(session.dlcDataPath)-- session.imOutPath = session.sessionPath / 'imOut'++ session.imOutPath = session.sessionPath / "imOut"session.imOutPath.mkdir(exist_ok=True)pathList.append(session.imOutPath)- session.dataArrayPath = session.sessionPath/'Data Arrays'+ session.dataArrayPath = session.sessionPath / "Data Arrays"session.dataArrayPath.mkdir(exist_ok=True)pathList.append(session.dataArrayPath)-- #create a list of paths that is passed to the recording script- #pathList = [session.sessionPath,session.rawVidPath,session.syncedVidPath,session.openPoseDataPath,session.dlcDataPath,session.imOutPath, session.dataArrayPath]--- config_settings,config_yaml = create_config_yaml()++ # create a list of paths that is passed to the recording script+ # pathList = [session.sessionPath,session.rawVidPath,session.syncedVidPath,session.openPoseDataPath,session.dlcDataPath,session.imOutPath, session.dataArrayPath]++ config_settings, config_yaml = create_config_yaml()session.config_settings = config_settings- yaml_name = session.sessionPath/'{}_config.yaml'.format(session.sessionID)+ yaml_name = session.sessionPath / "{}_config.yaml".format(session.sessionID)session.yamlPath = yaml_name- # print(yaml_name)- write_config_yaml(session,config_settings,config_yaml,pathList,yaml_name)-+ # print(yaml_name)+ write_config_yaml(session, config_settings, config_yaml, pathList, yaml_name)+return yaml_name-def createSessionTxt(session,paramDict,rotDict):- #create a text file listing recording parameters- parameter_text = session.sessionPath/'sessionSettings.txt'- text = open(parameter_text, 'w')- text.write("Session ID = %s\n" %(session.sessionID))- text.write("%s = %s\n" %("Parameters", paramDict))- text.write("%s = %s\n" %("Rotations", rotDict))++def createSessionTxt(session, paramDict, rotDict):+ # create a text file listing recording parameters+ parameter_text = session.sessionPath / "sessionSettings.txt"+ text = open(parameter_text, "w")+ text.write("Session ID = %s\n" % (session.sessionID))+ text.write("%s = %s\n" % ("Parameters", paramDict))+ text.write("%s = %s\n" % ("Rotations", rotDict))text.close()----def create_config_yaml():+def create_config_yaml():yaml_str = """\# Camera Inputs@@ -105,37 +103,37 @@ def create_config_yaml():config_yaml = YAML()config_settings = config_yaml.load(yaml_str)- return config_settings,config_yaml+ return config_settings, config_yaml++def write_config_yaml(session, config_settings, config_yaml, pathList, yaml_name):-def write_config_yaml(session,config_settings,config_yaml,pathList,yaml_name):-- for count,key in enumerate(config_settings['Paths'].keys()):- config_settings['Paths'][key] = str(pathList[count])-- config_settings['CamInputs']['RotationInputs'] = session.rotationInputs- config_settings['CamInputs']['ParameterDict'] = session.parameterDictionary-+ for count, key in enumerate(config_settings["Paths"].keys()):+ config_settings["Paths"][key] = str(pathList[count])- #yamlPath = sessionPath/'config_yaml'- #print(config_settings,yaml_name)- with open(yaml_name, 'w') as outfile:+ config_settings["CamInputs"]["RotationInputs"] = session.rotationInputs+ config_settings["CamInputs"]["ParameterDict"] = session.parameterDictionary++ # yamlPath = sessionPath/'config_yaml'+ # print(config_settings,yaml_name)+ with open(yaml_name, "w") as outfile:config_yaml.dump(config_settings, outfile)def load_config_yaml(yaml_path):config_yaml = YAML()- with open(yaml_path,'r') as fp:+ with open(yaml_path, "r") as fp:config_settings = config_yaml.load(fp)return config_settings-def load_session_paths(session,config_settings):- configPaths = config_settings['Paths']- session.rawVidPath = Path(configPaths['rawVidPath'])- session.syncedVidPath = Path(configPaths['syncedVidPath'])- session.calVidPath = Path(configPaths['calVidPath'])- session.mediaPipeDataPath = Path(configPaths['mediaPipeDataPath'])- session.openPoseDataPath = Path(configPaths['openPoseDataPath'])- session.dlcDataPath = Path(configPaths['dlcDataPath'])- session.imOutPath = Path(configPaths['imOutPath'])- session.dataArrayPath = Path(configPaths['dataArrayPath'])++def load_session_paths(session, config_settings):+ configPaths = config_settings["Paths"]+ session.rawVidPath = Path(configPaths["rawVidPath"])+ session.syncedVidPath = Path(configPaths["syncedVidPath"])+ session.calVidPath = Path(configPaths["calVidPath"])+ session.mediaPipeDataPath = Path(configPaths["mediaPipeDataPath"])+ session.openPoseDataPath = Path(configPaths["openPoseDataPath"])+ session.dlcDataPath = Path(configPaths["dlcDataPath"])+ session.imOutPath = Path(configPaths["imOutPath"])+ session.dataArrayPath = Path(configPaths["dataArrayPath"])diff --git a/freemocap/runcams.py b/freemocap/runcams.pyindex 96c2dee9..e0699f03 100644--- a/freemocap/runcams.py+++ b/freemocap/runcams.py@@ -1,61 +1,76 @@-from freemocap.webcam import startcamrecording, timesync, videotrim-+from pathlib import Pathimport timeimport pickle-import pandas as pd+import pandas as pdimport numpy as npfrom tkinter import Tk-def RecordCams(session,camInputs,parameterDictionary,rotationInputs):-+def RecordCams(session, camInputs, parameterDictionary, rotationInputs):+#%% Setting up recordingsbeginTime = time.time()- numCams = len(camInputs) #number of cameras- numCamRange = range(numCams) #a range for the number of cameras that we have+ numCams = len(camInputs) # number of cameras+ numCamRange = range(numCams) # a range for the number of cameras that we havevidNames = []camIDs = []- for x in numCamRange: #create names for each of the initial untrimmed videos- singleCamID = 'Cam{}'.format(x+1)- camIDs.append(singleCamID) #creates IDs for each camera based on the number of cameras entered- singleVidName = 'raw_cam{}.mp4'.format(x+1)- vidNames.append(singleVidName)+ for x in numCamRange: # create names for each of the initial untrimmed videos+ singleCamID = "Cam{}".format(x + 1)+ camIDs.append(+ singleCamID+ ) # creates IDs for each camera based on the number of cameras entered+ singleVidName = "raw_cam{}.mp4".format(x + 1)+ vidNames.append(singleVidName)#%% Starting the thread recordings for each camera- threads = []- for n in numCamRange: #starts recording video, opens threads for each camera- camRecordings = startcamrecording.CamRecordingThread(camIDs[n],camInputs[n],vidNames[n],session.rawVidPath,beginTime,parameterDictionary)+ threads = []+ for n in numCamRange: # starts recording video, opens threads for each camera+ camRecordings = startcamrecording.CamRecordingThread(+ camIDs[n],+ camInputs[n],+ vidNames[n],+ session.rawVidPath,+ beginTime,+ parameterDictionary,+ )camRecordings.start()-- threads.append(camRecordings)-++ threads.append(camRecordings)+for camRecordings in threads:- camRecordings.join() #make sure that one thread ending doesn't immediately end all the others (before they can dump data in a pickle file)-- print('finished recordings')-- timeStampList = []-- for e in numCamRange: #open the saved pickle file for each camera, and add the timestamps to the dataList list- with open(camIDs[e], 'rb') as f:+ camRecordings.join() # make sure that one thread ending doesn't immediately end all the others (before they can dump data in a pickle file)++ print("finished recordings")++ timeStampList = []++ for (+ e+ ) in (+ numCamRange+ ): # open the saved pickle file for each camera, and add the timestamps to the dataList list+ with open(camIDs[e], "rb") as f:camTimeList = pickle.load(f)timeStampList.append(camTimeList)-- timeDictionary = {}-- id_and_time = zip(camIDs,timeStampList)-- for cam,data in id_and_time:- timeDictionary[cam] = np.array(data) #create a dictionary that holds the timestamps for each camera--- df = pd.DataFrame.from_dict(timeDictionary, orient = 'index') #create a data frame from this dictionary++ timeDictionary = {}++ id_and_time = zip(camIDs, timeStampList)++ for cam, data in id_and_time:+ timeDictionary[cam] = np.array(+ data+ ) # create a dictionary that holds the timestamps for each camera++ df = pd.DataFrame.from_dict(+ timeDictionary, orient="index"+ ) # create a data frame from this dictionarytimeStampData = df.transpose()- csvName = session.sessionID + '.csv' #create our csv filename- csvPath = session.sessionPath/csvName- timeStampData.to_csv(csvPath) #turn dataframe into a CSV+ csvName = session.sessionID + ".csv" # create our csv filename+ csvPath = session.sessionPath / csvName+ timeStampData.to_csv(csvPath) # turn dataframe into a CSVsession.numCams = numCamssession.timeStampData = timeStampData@@ -63,19 +78,31 @@ def RecordCams(session,camInputs,parameterDictionary,rotationInputs):session.numCamRange = numCamRangesession.vidNames = vidNames-def SyncCams(session, timeStampData,numCamRange,vidNames,camIDs):- #start the timesync process- frameTable,timeTable,frameRate,resultsTable,plots = timesync.TimeSync(timeStampData,numCamRange,camIDs)-- #this message shows you your percentages and asks if you would like to continue or not. shuts down the program if no+def SyncCams(session, timeStampData, numCamRange, vidNames, camIDs):++ # start the timesync process+ frameTable, timeTable, frameRate, resultsTable, plots = timesync.TimeSync(+ timeStampData, numCamRange, camIDs+ )++ # this message shows you your percentages and asks if you would like to continue or not. shuts down the program if noroot = Tk()- proceed = timesync.proceedGUI(root,resultsTable,plots) #create a GUI instance called proceed+ proceed = timesync.proceedGUI(+ root, resultsTable, plots+ ) # create a GUI instance called proceedroot.mainloop()- if proceed.proceed == True:+ if proceed.proceed == True:print()- print('Starting editing')- videotrim.VideoTrim(session,vidNames,frameTable,session.parameterDictionary,session.rotationInputs,numCamRange)- #videotrim.createCalibrationVideos(session,60,parameterDictionary)- print('all done')\ No newline at end of file+ print("Starting editing")+ videotrim.VideoTrim(+ session,+ vidNames,+ frameTable,+ session.parameterDictionary,+ session.rotationInputs,+ numCamRange,+ )+ # videotrim.createCalibrationVideos(session,60,parameterDictionary)+ print("all done")diff --git a/freemocap/session.py b/freemocap/session.pyindex 7975528d..58cf9eef 100644--- a/freemocap/session.py+++ b/freemocap/session.py@@ -1,20 +1,22 @@-class Session: #session like "recording session"+class Session: # session like "recording session"def __init__(self):- self.sessionID = '' #The sessionID tag will be used to generate files names and whatnot- self.sessionPath = '' #The folder where the to-be-processed videos live (in a folder called "synced Vids")- self.numCams = ''#The number of cameras used in this recording session- self.openPoseDataPath = ''#Where the open pose data lives- self.dlcDataPath = ''#Where the DLC data lives- self.openPoseExePath = r'C:\openpose'+ self.sessionID = (+ "" # The sessionID tag will be used to generate files names and whatnot+ )+ self.sessionPath = "" # The folder where the to-be-processed videos live (in a folder called "synced Vids")+ self.numCams = "" # The number of cameras used in this recording session+ self.openPoseDataPath = "" # Where the open pose data lives+ self.dlcDataPath = "" # Where the DLC data lives+ self.openPoseExePath = r"C:\openpose"self.cam_inputs = Noneself.parameterDictionary = Noneself.rotationInputs = Noneself.cgroup = None-+self.mean_charuco_fr_mar_dim = Noneself.skel_fr_mar_dim = Noneself.mediaPipe_imgPathList = None- self.openPose_imgPathList = None\ No newline at end of file+ self.openPose_imgPathList = Nonediff --git a/freemocap/webcam/__init__.py b/freemocap/webcam/__init__.pyindex ef4b4f30..9059c793 100644--- a/freemocap/webcam/__init__.py+++ b/freemocap/webcam/__init__.py@@ -4,4 +4,3 @@ Created on Mon Mar 1 13:39:34 2021@author: Rontc"""-diff --git a/freemocap/webcam/camsetup.py b/freemocap/webcam/camsetup.pyindex df333ec7..5f252c4b 100644--- a/freemocap/webcam/camsetup.py+++ b/freemocap/webcam/camsetup.py@@ -1,56 +1,58 @@--import threadingimport cv2import imutils+class VideoSetup(threading.Thread):- def __init__(self, camID,parameterDictionary,rotNum):- self.camID = camID- self.parameterDictionary = parameterDictionary- self.rotNum = rotNum- threading.Thread.__init__(self)- def run(self):- #print("Starting " + self.previewName)- self.record(self.parameterDictionary,self.rotNum)- def record(self,parameterDictionary,rotNum):- exposure = parameterDictionary.get('exposure')- resWidth = parameterDictionary.get('resWidth')- resHeight = parameterDictionary.get('resHeight')- camName = "Camera" + str(self.camID)-- cv2.namedWindow(camName)- cap = cv2.VideoCapture(self.camID,cv2.CAP_DSHOW)- cap.set(cv2.CAP_PROP_FRAME_WIDTH, resWidth)- cap.set(cv2.CAP_PROP_FRAME_HEIGHT, resHeight)- cap.set(cv2.CAP_PROP_EXPOSURE, exposure)- #print('exposure',cap.get(cv2.CAP_PROP_EXPOSURE))- #if not cap.isOpened():- # raise RuntimeError('No camera found at input '+ str(self.camID))- while True:- ret1,frame1 = cap.read()- if ret1 ==True:- if rotNum is not None:- frame1 = imutils.rotate_bound(frame1, angle= rotNum)- cv2.imshow(camName,frame1)- if cv2.waitKey(1) & 0xFF== 27:- # == ord('q') for q- break-- else:+ def __init__(self, camID, parameterDictionary, rotNum):+ self.camID = camID+ self.parameterDictionary = parameterDictionary+ self.rotNum = rotNum+ threading.Thread.__init__(self)++ def run(self):+ # print("Starting " + self.previewName)+ self.record(self.parameterDictionary, self.rotNum)++ def record(self, parameterDictionary, rotNum):+ exposure = parameterDictionary.get("exposure")+ resWidth = parameterDictionary.get("resWidth")+ resHeight = parameterDictionary.get("resHeight")+ camName = "Camera" + str(self.camID)++ cv2.namedWindow(camName)+ cap = cv2.VideoCapture(self.camID, cv2.CAP_DSHOW)+ cap.set(cv2.CAP_PROP_FRAME_WIDTH, resWidth)+ cap.set(cv2.CAP_PROP_FRAME_HEIGHT, resHeight)+ cap.set(cv2.CAP_PROP_EXPOSURE, exposure)+ # print('exposure',cap.get(cv2.CAP_PROP_EXPOSURE))+ # if not cap.isOpened():+ # raise RuntimeError('No camera found at input '+ str(self.camID))+ while True:+ ret1, frame1 = cap.read()+ if ret1 == True:+ if rotNum is not None:+ frame1 = imutils.rotate_bound(frame1, angle=rotNum)+ cv2.imshow(camName, frame1)+ if cv2.waitKey(1) & 0xFF == 27:+ # == ord('q') for q+ break++ else:break- cv2.destroyWindow(camName)+ cv2.destroyWindow(camName)+-def RunSetup(cam_inputs,rotation_input,paramDict):+def RunSetup(cam_inputs, rotation_input, paramDict):if not cam_inputs:- raise ValueError('Camera input list (cam_inputs) is empty')-+ raise ValueError("Camera input list (cam_inputs) is empty")+ulist = []- for cam_input,cam_rotation in zip(cam_inputs,rotation_input):- u = VideoSetup(cam_input,paramDict,cam_rotation)- u.start()- ulist.append(u)-+ for cam_input, cam_rotation in zip(cam_inputs, rotation_input):+ u = VideoSetup(cam_input, paramDict, cam_rotation)+ u.start()+ ulist.append(u)+for k in ulist:- k.join()\ No newline at end of file+ k.join()diff --git a/freemocap/webcam/checkcams.py b/freemocap/webcam/checkcams.pyindex ed466a06..dda1348c 100644--- a/freemocap/webcam/checkcams.py+++ b/freemocap/webcam/checkcams.py@@ -1,27 +1,29 @@from tqdm import tqdmimport cv2+def TestDevice(source):- cap = cv2.VideoCapture(source,cv2.CAP_DSHOW)- #if cap is None or not cap.isOpened():- #print('Warning: unable to open video source: ', source)-- if cap.isOpened():- #print('Opened: ',source)- #print('Exposure: '+ str(cap.get(cv2.CAP_PROP_EXPOSURE)))- #time.sleep(3)+ cap = cv2.VideoCapture(source, cv2.CAP_DSHOW)+ # if cap is None or not cap.isOpened():+ # print('Warning: unable to open video source: ', source)++ if cap.isOpened():+ # print('Opened: ',source)+ # print('Exposure: '+ str(cap.get(cv2.CAP_PROP_EXPOSURE)))+ # time.sleep(3)cap.release()- cv2.destroyAllWindows()+ cv2.destroyAllWindows()open_cam = sourcereturn open_cam- else:- return None+ else:+ return None+def CreateAvailableCamList():openCamList = []- for x in tqdm(range(20)):#range 20 right now to be safe- openCamera = TestDevice(x)- if openCamera is not None:- openCamList.append(openCamera)-- return openCamList\ No newline at end of file+ for x in tqdm(range(20)): # range 20 right now to be safe+ openCamera = TestDevice(x)+ if openCamera is not None:+ openCamList.append(openCamera)++ return openCamListdiff --git a/freemocap/webcam/recordGUI.py b/freemocap/webcam/recordGUI.pyindex 4b91847f..112e6a4f 100644--- a/freemocap/webcam/recordGUI.py+++ b/freemocap/webcam/recordGUI.py@@ -5,208 +5,225 @@ Created on Wed Mar 3 12:18:46 2021@author: Rontc"""import tkinter as tk-from tkinter import Tk,Label, Button, Frame, Listbox, Entry+from tkinter import Tk, Label, Button, Frame, Listbox, Entryfrom freemocap.webcam import checkcamsimport pickleimport datetimeclass recordGUI:- #this GUI detects how many cameras you have available and presents them in- #a listbox, and allows you to pick a task for the cameras+ # this GUI detects how many cameras you have available and presents them in+ # a listbox, and allows you to pick a task for the camerasdef __init__(self, master):self.master = master- #Detect how many inputs you have available+ # Detect how many inputs you have availableavailable_inputs = checkcams.CreateAvailableCamList()number_of_cams = len(available_inputs)- cam_statement = "You have " + str(number_of_cams) + " cameras available at inputs " + str(available_inputs)-+ cam_statement = (+ "You have "+ + str(number_of_cams)+ + " cameras available at inputs "+ + str(available_inputs)+ )+self.master.title("Choose Your Cameras/Task")- bottom_frame = Frame(self.master, height = 100)- bottom_frame.pack(side = tk.BOTTOM)- #---creates a listbox with all the available camera choices- self.list_box = Listbox(self.master,selectmode = 'multiple')+ bottom_frame = Frame(self.master, height=100)+ bottom_frame.pack(side=tk.BOTTOM)+ # ---creates a listbox with all the available camera choices+ self.list_box = Listbox(self.master, selectmode="multiple")for i in available_inputs:- self.list_box.insert(i,str(i))-- #---setting up the task radiobutton- #creates the different options and the values associated with them- options = [("Setup", 'setup'),- ("Record", 'record')]-- #initializes currentTask as a string variable and defaults it to setup+ self.list_box.insert(i, str(i))++ # ---setting up the task radiobutton+ # creates the different options and the values associated with them+ options = [("Setup", "setup"), ("Record", "record")]++ # initializes currentTask as a string variable and defaults it to setupself.currentTask = tk.StringVar()- self.currentTask.set('setup')-- #---display the detection results- tk.Label(self.master, text = cam_statement, justify = tk.RIGHT).pack()- tk.Label(self.master, text = 'Select Your Cameras', justify = tk.RIGHT).pack()+ self.currentTask.set("setup")++ # ---display the detection results+ tk.Label(self.master, text=cam_statement, justify=tk.RIGHT).pack()+ tk.Label(self.master, text="Select Your Cameras", justify=tk.RIGHT).pack()self.list_box.pack()-- #---create the task radiobutton- tk.Label(self.master,- text="""Choose your task""",- justify = tk.LEFT,- padx = 20).pack()-++ # ---create the task radiobutton+ tk.Label(+ self.master, text="""Choose your task""", justify=tk.LEFT, padx=20+ ).pack()+for option, val in options:- tk.Radiobutton(self.master,- text=option,- padx = 20,- variable= self.currentTask,- value=val).pack(anchor=tk.W)--- self.sub_button = Button(bottom_frame, text="Submit", command=self.Submit)- self.sub_button.pack(side = tk.RIGHT)---+ tk.Radiobutton(+ self.master, text=option, padx=20, variable=self.currentTask, value=val+ ).pack(anchor=tk.W)++ self.sub_button = Button(bottom_frame, text="Submit", command=self.Submit)+ self.sub_button.pack(side=tk.RIGHT)+def Submit(self):self.taskChoice = self.currentTask.get()- self.selected = [int(self.list_box.get(pos)) for pos in self.list_box.curselection()]+ self.selected = [+ int(self.list_box.get(pos)) for pos in self.list_box.curselection()+ ]self.master.destroy()--class SettingsGUI:- def __init__(self, master,cam_inputs,parameter_dictionary,rotation_dictionary,sessionID_in,task):- self.master = master++class SettingsGUI:+ def __init__(+ self,+ master,+ cam_inputs,+ parameter_dictionary,+ rotation_dictionary,+ sessionID_in,+ task,+ ):+ self.master = masterself.cam_inputs = cam_inputsself.parameter_dictionary = parameter_dictionaryself.rotation_dictionary = rotation_dictionaryself.sessionID_in = sessionID_inself.task = task- #check if any previous parameters exist- if self.parameter_dictionary is not None:+ # check if any previous parameters exist+ if self.parameter_dictionary is not None:existing_parameters = Trueelse:existing_parameters = False-- #check to see if there are any stored rotations for the cameras chosen++ # check to see if there are any stored rotations for the cameras chosenrotationValues = self.RotationRetrieval(cam_inputs, rotation_dictionary)--- #create frames to organize rotation inputs, resolution inputs, and parameter inputs++ # create frames to organize rotation inputs, resolution inputs, and parameter inputstopFrame = Frame(self.master)- topFrame.pack(side = tk.TOP)+ topFrame.pack(side=tk.TOP)rotLabelFrame = Frame(self.master)rotLabelFrame.pack()resFrame = Frame(self.master)resFrame.pack()parametersFrame = Frame(self.master)parametersFrame.pack()- bottomFrame = Frame(self.master).pack(side = tk.BOTTOM)+ bottomFrame = Frame(self.master).pack(side=tk.BOTTOM)-- taskText = "Current Task: " + self.task- taskLabel = Label(topFrame,text = taskText, font = 'bold')+ taskText = "Current Task: " + self.task+ taskLabel = Label(topFrame, text=taskText, font="bold")taskLabel.pack()- #---Rotation Options. Dynamically creates radio button options depending- #---on how many cameras there are- rotation_options = [("rotate 0",0),("rotate 90",90),("rotate 180",180),("rotate 270",270)]- #radVar = tk.IntVar() #Radio Button Variable- self.rotation_list = []+ # ---Rotation Options. Dynamically creates radio button options depending+ # ---on how many cameras there are+ rotation_options = [+ ("rotate 0", 0),+ ("rotate 90", 90),+ ("rotate 180", 180),+ ("rotate 270", 270),+ ]+ # radVar = tk.IntVar() #Radio Button Variable+ self.rotation_list = []rotationRadioButton = {}- #creates radio buttons for each cam and stores the chosen rotation degree in a list- for cam,rotDegree in zip(self.cam_inputs,rotationValues):+ # creates radio buttons for each cam and stores the chosen rotation degree in a list+ for cam, rotDegree in zip(self.cam_inputs, rotationValues):current_rotation = tk.IntVar()current_rotation.set(rotDegree)self.rotation_list.append(current_rotation)- tk.Label(rotLabelFrame, text = "Choose rotation for Cam " + str(cam)).pack()- for option,degree in rotation_options:- rotationRadioButton[cam] = tk.Radiobutton(rotLabelFrame,text=option,value = degree,variable = current_rotation ).pack()-- #---Resolution Height and Resolution Width entry- widthLabel = Label(resFrame, text = 'Resolution Width').pack(side = 'left')+ tk.Label(rotLabelFrame, text="Choose rotation for Cam " + str(cam)).pack()+ for option, degree in rotation_options:+ rotationRadioButton[cam] = tk.Radiobutton(+ rotLabelFrame, text=option, value=degree, variable=current_rotation+ ).pack()++ # ---Resolution Height and Resolution Width entry+ widthLabel = Label(resFrame, text="Resolution Width").pack(side="left")self.resWidthEntry = Entry(resFrame)- self.resWidthEntry.pack(side = 'left')- heightLabel = Label(resFrame, text = 'Resolution Height').pack(side = 'left')+ self.resWidthEntry.pack(side="left")+ heightLabel = Label(resFrame, text="Resolution Height").pack(side="left")self.resHeightEntry = Entry(resFrame)- self.resHeightEntry.pack(side = 'left')-- #---Exposure,FPS, and codec entry- exposureLabel = Label(parametersFrame, text = 'Exposure').pack(side= 'left')+ self.resHeightEntry.pack(side="left")++ # ---Exposure,FPS, and codec entry+ exposureLabel = Label(parametersFrame, text="Exposure").pack(side="left")self.exposureEntry = Entry(parametersFrame)- self.exposureEntry.pack(side = 'left')- FPSLabel = Label(parametersFrame, text = 'FPS').pack(side= 'left')- self. FPSEntry = Entry(parametersFrame)- self.FPSEntry.pack( side = 'left')- codecLabel = Label(parametersFrame, text = 'codec').pack(side = 'left')+ self.exposureEntry.pack(side="left")+ FPSLabel = Label(parametersFrame, text="FPS").pack(side="left")+ self.FPSEntry = Entry(parametersFrame)+ self.FPSEntry.pack(side="left")+ codecLabel = Label(parametersFrame, text="codec").pack(side="left")self.codecEntry = Entry(parametersFrame)- self.codecEntry.pack(side = 'left')-- #---SessionID entry- default is the date/time- if self.task == 'record':- sessionText = 'Change SessionID if desired'- sessionLabel = Label(parametersFrame, text = sessionText).pack()+ self.codecEntry.pack(side="left")++ # ---SessionID entry- default is the date/time+ if self.task == "record":+ sessionText = "Change SessionID if desired"+ sessionLabel = Label(parametersFrame, text=sessionText).pack()self.sessionIDEntry = Entry(parametersFrame)self.sessionIDEntry.pack()- self.sessionIDEntry.insert(0,self.sessionID_in)-- #---If previous parameters were recieved, insert them into the entry boxes+ self.sessionIDEntry.insert(0, self.sessionID_in)++ # ---If previous parameters were recieved, insert them into the entry boxesif existing_parameters == True:-- self.exposureEntry.insert(0,parameter_dictionary['exposure'])- self.resWidthEntry.insert(0,parameter_dictionary['resWidth'])- self.resHeightEntry.insert(0,parameter_dictionary['resHeight'])- self.codecEntry.insert(0,parameter_dictionary['codec'])- self.FPSEntry.insert(0,parameter_dictionary['framerate'])-- #---Submit button to hit when things are finalized- self.sub_button = Button(bottomFrame, text="Submit", command= self.Submit)- self.sub_button.pack(side = tk.BOTTOM)--++ self.exposureEntry.insert(0, parameter_dictionary["exposure"])+ self.resWidthEntry.insert(0, parameter_dictionary["resWidth"])+ self.resHeightEntry.insert(0, parameter_dictionary["resHeight"])+ self.codecEntry.insert(0, parameter_dictionary["codec"])+ self.FPSEntry.insert(0, parameter_dictionary["framerate"])++ # ---Submit button to hit when things are finalized+ self.sub_button = Button(bottomFrame, text="Submit", command=self.Submit)+ self.sub_button.pack(side=tk.BOTTOM)+def Submit(self):- #get the final entries for all parameters- #get the rotations for each camera and save them as values of a dictionary+ # get the final entries for all parameters+ # get the rotations for each camera and save them as values of a dictionaryself.rotation_output = {}- for count,cam in enumerate(self.cam_inputs):+ for count, cam in enumerate(self.cam_inputs):self.rotation_output[cam] = self.rotation_list[count].get()-- #built a parameter dictionary by .get()-ing the values from each entry- self.paramDict = {'exposure':int(self.exposureEntry.get()),"resWidth":int(self.resWidthEntry.get()),"resHeight":int(self.resHeightEntry.get()),- 'framerate':int(self.FPSEntry.get()),'codec':str(self.codecEntry.get())}- #spit out the sessionID- if self.task =='record':++ # built a parameter dictionary by .get()-ing the values from each entry+ self.paramDict = {+ "exposure": int(self.exposureEntry.get()),+ "resWidth": int(self.resWidthEntry.get()),+ "resHeight": int(self.resHeightEntry.get()),+ "framerate": int(self.FPSEntry.get()),+ "codec": str(self.codecEntry.get()),+ }+ # spit out the sessionID+ if self.task == "record":self.sessionID_out = self.sessionIDEntry.get()-+self.master.destroy()- def RotationRetrieval(self,cam_inputs,rotation_dictionary):- #takes the input cameras and compares them to the dictionary of- #rotation values from the last session. For any matching cameras,- #grab the rotation values. Otherwise, the rotation value is None++ def RotationRetrieval(self, cam_inputs, rotation_dictionary):+ # takes the input cameras and compares them to the dictionary of+ # rotation values from the last session. For any matching cameras,+ # grab the rotation values. Otherwise, the rotation value is NonerotationValues = []for cam in cam_inputs:if cam in rotation_dictionary.keys():rotationValues.append(rotation_dictionary[cam])- else:+ else:rotationValues.append(0)return rotationValues---class ProceedToRecordGUI():- def __init__(self, master,sessionID_in):++class ProceedToRecordGUI:+ def __init__(self, master, sessionID_in):self.master = masterself.sessionID_in = sessionID_in- self.continueToRecording = ''- self.sessionID_out = ''+ self.continueToRecording = ""+ self.sessionID_out = ""- sessionText = "Proceed to recording with this sessionID?"- sessionLabel = Label(master,text = sessionText)- sessionLabel.pack(side = 'top')+ sessionText = "Proceed to recording with this sessionID?"+ sessionLabel = Label(master, text=sessionText)+ sessionLabel.pack(side="top")self.sessionIDEntry = Entry(master)self.sessionIDEntry.pack()- self.sessionIDEntry.insert(0,self.sessionID_in)-- #master.title("Proceed to Recording?")+ self.sessionIDEntry.insert(0, self.sessionID_in)++ # master.title("Proceed to Recording?")self.proceed_button = Button(text="Proceed", command=self.destroy)- self.stop_button = Button(text="Quit", command= self.stop)- self.stop_button.pack(side = tk.RIGHT)- self.proceed_button.pack(side = tk.RIGHT)-+ self.stop_button = Button(text="Quit", command=self.stop)+ self.stop_button.pack(side=tk.RIGHT)+ self.proceed_button.pack(side=tk.RIGHT)+def stop(self):self.master.destroy()@@ -215,39 +232,42 @@ class ProceedToRecordGUI():self.sessionID_out = self.sessionIDEntry.get()self.master.destroy()-def RunGUI(sessionID_in, rotation_entry, parameter_entry):-- #---Get the camera inputs and the task++def RunGUI(sessionID_in, rotation_entry, parameter_entry):++ # ---Get the camera inputs and the taskroot = tk.Tk()camera_choice = recordGUI(root)root.mainloop()-- cam_inputs = camera_choice.selected++ cam_inputs = camera_choice.selectedif not cam_inputs:- raise ValueError('No camera inputs selected')+ raise ValueError("No camera inputs selected")task = camera_choice.taskChoice--- #---Get all the necessary parameters++ # ---Get all the necessary parametersroot = tk.Tk()- recording_settings = SettingsGUI(root,cam_inputs,parameter_entry,rotation_entry,sessionID_in,task)+ recording_settings = SettingsGUI(+ root, cam_inputs, parameter_entry, rotation_entry, sessionID_in, task+ )root.mainloop()-- #---Create the parameters we'll need to run the records- #rotation_input = list(test.rotation_output.values())-++ # ---Create the parameters we'll need to run the records+ # rotation_input = list(test.rotation_output.values())+paramDict = recording_settings.paramDict-- if task == 'record':- sessionID = recording_settings.sessionID_out++ if task == "record":+ sessionID = recording_settings.sessionID_outelse:sessionID = None-+return task, cam_inputs, recording_settings.rotation_output, paramDict, sessionID+def RunProceedtoRecordGUI(sessionID_in):root = tk.Tk()- goToRecording = ProceedToRecordGUI(root,sessionID_in)+ goToRecording = ProceedToRecordGUI(root, sessionID_in)root.mainloop()- return goToRecording.continueToRecording,goToRecording.sessionID_out+ return goToRecording.continueToRecording, goToRecording.sessionID_outdiff --git a/freemocap/webcam/startcamrecording.py b/freemocap/webcam/startcamrecording.pyindex 9a433d08..bfd194d3 100644--- a/freemocap/webcam/startcamrecording.py+++ b/freemocap/webcam/startcamrecording.py@@ -1,10 +1,13 @@import threadingimport cv2import time-import pickle+import pickle-class CamRecordingThread(threading.Thread):- def __init__(self,camID,camInput,videoName,rawVidPath,beginTime,parameterDictionary):++class CamRecordingThread(threading.Thread):+ def __init__(+ self, camID, camInput, videoName, rawVidPath, beginTime, parameterDictionary+ ):threading.Thread.__init__(self)self.camID = camIDself.camInput = camInput@@ -15,66 +18,83 @@ class CamRecordingThread(threading.Thread):def run(self):print("Starting " + self.camID)- self.timeStamps = CamRecording(self.camID, self.camInput, self.videoName,self.rawVidPath,self.beginTime,self.parameterDictionary)+ self.timeStamps = CamRecording(+ self.camID,+ self.camInput,+ self.videoName,+ self.rawVidPath,+ self.beginTime,+ self.parameterDictionary,+ )+def getStamps(self):return self.timeStamps-#the recording function that each threaded camera object runs-def CamRecording(camID, camInput, videoName,rawVidPath,beginTime,parameterDictionary):- #the flag is triggered when the user shuts down one webcam to shut down the rest.- #normally I'd try to avoid global variables, but in this case it's- #necessary, since each webcam runs as it's own object.- global flag- flag = False-- cv2.namedWindow(camID) #name the preview window for the camera its showing- cam = cv2.VideoCapture(camInput,cv2.CAP_DSHOW) #create the video capture object- #if not cam.isOpened():- # raise RuntimeError('No camera found at input '+ str(camID))- #pulling out all the dictionary paramters- exposure = parameterDictionary.get('exposure')- resWidth = parameterDictionary.get('resWidth')- resHeight = parameterDictionary.get('resHeight')- framerate = parameterDictionary.get('framerate')- codec = parameterDictionary.get('codec')-+++# the recording function that each threaded camera object runs+def CamRecording(+ camID, camInput, videoName, rawVidPath, beginTime, parameterDictionary+):+ # the flag is triggered when the user shuts down one webcam to shut down the rest.+ # normally I'd try to avoid global variables, but in this case it's+ # necessary, since each webcam runs as it's own object.+ global flag+ flag = False++ cv2.namedWindow(camID) # name the preview window for the camera its showing+ cam = cv2.VideoCapture(camInput, cv2.CAP_DSHOW) # create the video capture object+ # if not cam.isOpened():+ # raise RuntimeError('No camera found at input '+ str(camID))+ # pulling out all the dictionary paramters+ exposure = parameterDictionary.get("exposure")+ resWidth = parameterDictionary.get("resWidth")+ resHeight = parameterDictionary.get("resHeight")+ framerate = parameterDictionary.get("framerate")+ codec = parameterDictionary.get("codec")+cam.set(cv2.CAP_PROP_FRAME_WIDTH, resWidth)cam.set(cv2.CAP_PROP_FRAME_HEIGHT, resHeight)cam.set(cv2.CAP_PROP_EXPOSURE, exposure)fourcc = cv2.VideoWriter_fourcc(*codec)- #rawPath = filepath/'RawVideos' #creating a RawVideos folder- #rawPath.mkdir(parents = True, exist_ok = True)+ # rawPath = filepath/'RawVideos' #creating a RawVideos folder+ # rawPath.mkdir(parents = True, exist_ok = True)width = cam.get(cv2.CAP_PROP_FRAME_WIDTH)height = cam.get(cv2.CAP_PROP_FRAME_HEIGHT)- print('width:',width, 'height:',height)- saveRawVidPath = str(rawVidPath/videoName) #create a save path for each video to the RawVideos folders- out = cv2.VideoWriter(saveRawVidPath,fourcc, framerate, (resWidth,resHeight))- timeStamps = [] #holds the timestamps+ print("width:", width, "height:", height)+ saveRawVidPath = str(+ rawVidPath / videoName+ ) # create a save path for each video to the RawVideos folders+ out = cv2.VideoWriter(saveRawVidPath, fourcc, framerate, (resWidth, resHeight))+ timeStamps = [] # holds the timestampsif cam.isOpened():success, frame = cam.read()else:success = False- while success: #while the camera is opened, record the data until the escape button is hit- if flag: #when the flag is triggered, stop recording and dump the data- with open(camID, 'wb') as f:- pickle.dump(timeStamps, f)+ while (+ success+ ): # while the camera is opened, record the data until the escape button is hit+ if flag: # when the flag is triggered, stop recording and dump the data+ with open(camID, "wb") as f:+ pickle.dump(timeStamps, f)breaksuccess, frame = cam.read()-+cv2.imshow(camID, frame)- frame_sized = cv2.resize(frame,(resWidth,resHeight))- #frame_sized = frame+ frame_sized = cv2.resize(frame, (resWidth, resHeight))+ # frame_sized = frameout.write(frame_sized)- timeStamps.append(time.time()-beginTime) #add each timestamp to the list-+ timeStamps.append(time.time() - beginTime) # add each timestamp to the list+key = cv2.waitKey(20)if key == 27: # exit on ESC- flag = True #set flag to true to shut down all other webcams- with open(camID, 'wb') as f:- pickle.dump(timeStamps, f) #dump the data+ flag = True # set flag to true to shut down all other webcams+ with open(camID, "wb") as f:+ pickle.dump(timeStamps, f) # dump the databreakcv2.destroyWindow(camID)return timeStamps-#this is how we sync our time frames, based on our recorded timestamps\ No newline at end of file++# this is how we sync our time frames, based on our recorded timestampsdiff --git a/freemocap/webcam/timesync.py b/freemocap/webcam/timesync.pyindex 814fe391..d954e21c 100644--- a/freemocap/webcam/timesync.py+++ b/freemocap/webcam/timesync.py@@ -1,187 +1,225 @@import numpy as npimport pandas as pd-import sys+import sysfrom matplotlib.figure import Figurefrom matplotlib.backends.backend_tkagg import FigureCanvasTkAggimport tkinter as tk-from tkinter import Tk,Label, Button, Frame+from tkinter import Tk, Label, Button, Frame-def TimeSync(timeStampData,numCamRange,camNames):-- def CloseNeighb(camera,point):- closestPoint = (np.abs(camera - point)).argmin()- return closestPoint-- #this section auto-finds the start and end points for our master timeline to the nearest second- masterTimelineBegin = np.ceil(max(timeStampData.iloc[0]))+def TimeSync(timeStampData, numCamRange, camNames):+ def CloseNeighb(camera, point):+ closestPoint = (np.abs(camera - point)).argmin()+ return closestPoint++ # this section auto-finds the start and end points for our master timeline to the nearest second+ masterTimelineBegin = np.ceil(max(timeStampData.iloc[0]))lastPoints = []for name in camNames:- cameraLastPoint = timeStampData[name][timeStampData[name].last_valid_index()] #find the last non-nan value in each camera+ cameraLastPoint = timeStampData[name][+ timeStampData[name].last_valid_index()+ ] # find the last non-nan value in each cameralastPoints.append(cameraLastPoint)-- masterTimelineEnd = np.floor(min(lastPoints)) #find where the fastest camera ended, round down, use that as the end point--- #print('end',endList)- print('intervals:',masterTimelineBegin,masterTimelineEnd)--- totalFrameIntvl= [] #list for storing frame rates for each camera--- n = 0; #counter for going through camera names++ masterTimelineEnd = np.floor(+ min(lastPoints)+ ) # find where the fastest camera ended, round down, use that as the end point++ # print('end',endList)+ print("intervals:", masterTimelineBegin, masterTimelineEnd)++ totalFrameIntvl = [] # list for storing frame rates for each camera++ n = 0+ # counter for going through camera namesfor x in numCamRange:- currentCam = timeStampData[camNames[x]]- #finds the closest point in each camera to the start and end points of our timeline- currentCamStart = CloseNeighb(currentCam,masterTimelineBegin)- currentCamEnd = CloseNeighb(currentCam,masterTimelineEnd)- print("using frames:", currentCamStart,"-",currentCamEnd,"for",camNames[n])- n +=1- currentCamTimeline = currentCam[currentCamStart:currentCamEnd] #grab the times from start to finish for each camera- currentCamFrameInterval = np.mean(np.diff(currentCamTimeline)) #calculate the interval between each frame and take the mean- totalFrameIntvl.append(currentCamFrameInterval) #add interval to list- #print(camNames[x],currentCamStart,currentCamEnd)- totalAverageIntvl = np.mean(totalFrameIntvl) #find the total average interval across all cameras- masterTimeline = np.arange(masterTimelineBegin,masterTimelineEnd,totalAverageIntvl) #build a master timeline with the average interval-- #now we start the syncing process-- frameList = masterTimeline #start a list of frames, with the first row being our master timeline- timeList = masterTimeline #start a list of timestamps,with the first row being our master timeline-- delNum= []; #stores number of deleted frames/camera- bufNum= []; #stores number of buffered frames/camera- bufPercentList = []; #stores percentage of deleted frames/camera- delPercentList = []; #stores percentage of buffered frames/camera-- count = 0 #Keeps track of what frame we're on (I think?)- n = 0; #I don't remember why I did this but I'm sure I'll figure it out later-+ currentCam = timeStampData[camNames[x]]+ # finds the closest point in each camera to the start and end points of our timeline+ currentCamStart = CloseNeighb(currentCam, masterTimelineBegin)+ currentCamEnd = CloseNeighb(currentCam, masterTimelineEnd)+ print("using frames:", currentCamStart, "-", currentCamEnd, "for", camNames[n])+ n += 1+ currentCamTimeline = currentCam[+ currentCamStart:currentCamEnd+ ] # grab the times from start to finish for each camera+ currentCamFrameInterval = np.mean(+ np.diff(currentCamTimeline)+ ) # calculate the interval between each frame and take the mean+ totalFrameIntvl.append(currentCamFrameInterval) # add interval to list+ # print(camNames[x],currentCamStart,currentCamEnd)+ totalAverageIntvl = np.mean(+ totalFrameIntvl+ ) # find the total average interval across all cameras+ masterTimeline = np.arange(+ masterTimelineBegin, masterTimelineEnd, totalAverageIntvl+ ) # build a master timeline with the average interval++ # now we start the syncing process++ frameList = masterTimeline # start a list of frames, with the first row being our master timeline+ timeList = masterTimeline # start a list of timestamps,with the first row being our master timeline++ delNum = []+ # stores number of deleted frames/camera+ bufNum = []+ # stores number of buffered frames/camera+ bufPercentList = []+ # stores percentage of deleted frames/camera+ delPercentList = []+ # stores percentage of buffered frames/camera++ count = 0 # Keeps track of what frame we're on (I think?)+ n = 0+ # I don't remember why I did this but I'm sure I'll figure it out later+for y in numCamRange:thisCam = timeStampData[camNames[y]]- camTimes = []; #stored times- camFrames =[]; #stored frames-+ camTimes = []+ # stored times+ camFrames = []+ # stored frames+count += 1- #--------------Adjust each camera to start at the first point in the master timeline- beginFrame = CloseNeighb(thisCam,masterTimeline[0])+ # --------------Adjust each camera to start at the first point in the master timeline+ beginFrame = CloseNeighb(thisCam, masterTimeline[0])timeDif = masterTimeline[0] - thisCam[beginFrame]thisCam = thisCam + timeDif-- for z in masterTimeline: #for each point in the master timeline- closestFrame = CloseNeighb(thisCam, z) #find the closest frame in this camera to each point of the master timeline- camFrames.append(closestFrame) #add that frame to the list- camTimes.append(thisCam[closestFrame]) #find the time corresponding to this frame-- print("starting detection:",camNames[n]) #now to start finding deleted/buffered slides- frameList = np.column_stack((frameList,camFrames)) #update our framelist with our new frames- timeList = np.column_stack((timeList,camTimes)) #update our timelist--- #start counters for the number of buffered and deleted slides- bufCount = 0;- delCount = 0;-- for i in range(0,len(camFrames)-1):- distanceBetweenFrames = camFrames[i+1] - camFrames[i] #find the distance between adjacent frames- if distanceBetweenFrames == 1: #these frames are consecutive, do nothing- None- elif distanceBetweenFrames == 0: #we have a buffered slide++ for z in masterTimeline: # for each point in the master timeline+ closestFrame = CloseNeighb(+ thisCam, z+ ) # find the closest frame in this camera to each point of the master timeline+ camFrames.append(closestFrame) # add that frame to the list+ camTimes.append(+ thisCam[closestFrame]+ ) # find the time corresponding to this frame++ print(+ "starting detection:", camNames[n]+ ) # now to start finding deleted/buffered slides+ frameList = np.column_stack(+ (frameList, camFrames)+ ) # update our framelist with our new frames+ timeList = np.column_stack((timeList, camTimes)) # update our timelist++ # start counters for the number of buffered and deleted slides+ bufCount = 0+ delCount = 0++ for i in range(0, len(camFrames) - 1):+ distanceBetweenFrames = (+ camFrames[i + 1] - camFrames[i]+ ) # find the distance between adjacent frames+ if distanceBetweenFrames == 1: # these frames are consecutive, do nothing+ None+ elif distanceBetweenFrames == 0: # we have a buffered slidebufCount += 1- #this section looks at our two timepoints, and finds the which point on the master timeline is closest- frame1 = abs(masterTimeline[i]-camTimes[i])- frame2 = abs(masterTimeline[i+1]-camTimes[i])- #finds which frame is closest, and sets the other frame as a buffer (indicated by the -1)- if frame1>frame2:- frameList[i,count] = -1- timeList[i,count] = -1+ # this section looks at our two timepoints, and finds the which point on the master timeline is closest+ frame1 = abs(masterTimeline[i] - camTimes[i])+ frame2 = abs(masterTimeline[i + 1] - camTimes[i])+ # finds which frame is closest, and sets the other frame as a buffer (indicated by the -1)+ if frame1 > frame2:+ frameList[i, count] = -1+ timeList[i, count] = -1else:- frameList[i+1,count] = -1;- timeList[i+1,count] = -1;- elif distanceBetweenFrames > 1: #deletion- delCount += 1+ frameList[i + 1, count] = -1+ timeList[i + 1, count] = -1+ elif distanceBetweenFrames > 1: # deletion+ delCount += 1else:-+print("something else happened")- #update and calculate our percentages and numbers for buffers/deletions- delNum.append(round(delCount,1))- bufNum.append(round(bufCount,1))- #print(bufCount,len(frameList))- bufPercent = (bufCount/len(frameList))*100- delPercent = (delCount/len(frameList))*100- bufPercentList.append(round(bufPercent,2))- delPercentList.append(round(delPercent,2))-- #print(delNum,bufNum,bufPercentList,delPercentList)- #print("deleted frames:",delCount)- #print("buffered frames:",bufCount)- n +=1-- #create our data frame for both times and frames- frameTable = pd.DataFrame(frameList)- columnNames = ['Master Timeline'] + camNames+ # update and calculate our percentages and numbers for buffers/deletions+ delNum.append(round(delCount, 1))+ bufNum.append(round(bufCount, 1))+ # print(bufCount,len(frameList))+ bufPercent = (bufCount / len(frameList)) * 100+ delPercent = (delCount / len(frameList)) * 100+ bufPercentList.append(round(bufPercent, 2))+ delPercentList.append(round(delPercent, 2))++ # print(delNum,bufNum,bufPercentList,delPercentList)+ # print("deleted frames:",delCount)+ # print("buffered frames:",bufCount)+ n += 1++ # create our data frame for both times and frames+ frameTable = pd.DataFrame(frameList)+ columnNames = ["Master Timeline"] + camNamesframeTable.columns = columnNamestimeTable = pd.DataFrame(timeList)timeTable.columns = columnNames- totalFrameRate = [round(1/intvl,1) for intvl in totalFrameIntvl]- frameRate = 1/totalAverageIntvl #calculates our framerate- results = {'Cam':camNames,'#Del':delNum,'%Del':delPercentList,'#Buf':bufNum,'%Buf':bufPercentList,'FPS':totalFrameRate}- resultTable = pd.DataFrame(results,columns = ['Cam','#Del','%Del','#Buf','%Buf','FPS'])-- #============================================== plot data- differenceFrame = timeStampData.diff(axis = 0)+ totalFrameRate = [round(1 / intvl, 1) for intvl in totalFrameIntvl]+ frameRate = 1 / totalAverageIntvl # calculates our framerate+ results = {+ "Cam": camNames,+ "#Del": delNum,+ "%Del": delPercentList,+ "#Buf": bufNum,+ "%Buf": bufPercentList,+ "FPS": totalFrameRate,+ }+ resultTable = pd.DataFrame(+ results, columns=["Cam", "#Del", "%Del", "#Buf", "%Buf", "FPS"]+ )++ # ============================================== plot data+ differenceFrame = timeStampData.diff(axis=0)fpsFrame = differenceFrame.pow(-1)- fig = Figure(figsize = [10,10])- fig.patch.set_facecolor('#F0F0F0')+ fig = Figure(figsize=[10, 10])+ fig.patch.set_facecolor("#F0F0F0")a = fig.add_subplot(221)- a.set_xlabel('Frame')- a.set_ylabel('Time(s)')- timeStampData.plot(ax = a, title = 'Camera timestamps')+ a.set_xlabel("Frame")+ a.set_ylabel("Time(s)")+ timeStampData.plot(ax=a, title="Camera timestamps")b = fig.add_subplot(222)- b.set_xlabel('Frame')- b.set_ylabel('Frame duration (s)')- differenceFrame.plot(ax = b, marker = '.', linestyle = 'none',title = 'Camera frame duration')+ b.set_xlabel("Frame")+ b.set_ylabel("Frame duration (s)")+ differenceFrame.plot(+ ax=b, marker=".", linestyle="none", title="Camera frame duration"+ )c = fig.add_subplot(223)- c.set_xlabel('Time (s)')- differenceFrame.plot.hist(ax = c, bins=6,alpha = .5, xlim = [0,.1], title = 'Frame interval distribution (s)')+ c.set_xlabel("Time (s)")+ differenceFrame.plot.hist(+ ax=c, bins=6, alpha=0.5, xlim=[0, 0.1], title="Frame interval distribution (s)"+ )d = fig.add_subplot(224)- d.set_xlabel('Frames per second')- fpsFrame.plot.hist(ax = d, bins=6,alpha = .5, xlim = [0,40], title = 'Frames per second distribution')- return frameTable,timeTable,frameRate,resultTable,fig-#function to trim our videos+ d.set_xlabel("Frames per second")+ fpsFrame.plot.hist(+ ax=d, bins=6, alpha=0.5, xlim=[0, 40], title="Frames per second distribution"+ )+ return frameTable, timeTable, frameRate, resultTable, fig+++# function to trim our videos+class proceedGUI:def __init__(self, master, results, figure):self.master = masterself.results = resultsself.figure = figure- self.proceed = ''+ self.proceed = ""master.title("Choose File Path")--- bottom_frame = Frame(self.master, height = 1000)- bottom_frame.pack(side = tk.LEFT)- self.label = Label(bottom_frame, text= results.to_string(index = False))- self.label.pack(side = tk.TOP)++ bottom_frame = Frame(self.master, height=1000)+ bottom_frame.pack(side=tk.LEFT)+ self.label = Label(bottom_frame, text=results.to_string(index=False))+ self.label.pack(side=tk.TOP)self.go_button = Button(bottom_frame, text="Proceed", command=self.destroy)- self.stop_button = Button(bottom_frame, text="Quit", command= self.stop)-+ self.stop_button = Button(bottom_frame, text="Quit", command=self.stop)+self.canvas = FigureCanvasTkAgg(self.figure, master=self.master)self.canvas.draw()self.canvas.get_tk_widget().pack(side=tk.LEFT, fill="both", expand=1)-- self.stop_button.pack(side = tk.RIGHT)- self.go_button.pack(side = tk.RIGHT)---+ self.stop_button.pack(side=tk.RIGHT)+ self.go_button.pack(side=tk.RIGHT)def stop(self):- self.master.destroy()- sys.exit("Quitting Program")-+ self.master.destroy()+ sys.exit("Quitting Program")+def destroy(self):- self.proceed = True- self.master.destroy()\ No newline at end of file+ self.proceed = True+ self.master.destroy()diff --git a/freemocap/webcam/videotrim.py b/freemocap/webcam/videotrim.pyindex 4f4e8924..90abd4d5 100644--- a/freemocap/webcam/videotrim.py+++ b/freemocap/webcam/videotrim.py@@ -4,77 +4,98 @@ import imutilsimport numpy as npimport os-def VideoTrim(session,vidList,ft,parameterDictionary,rotationState,numCamRange):- camList = list(ft.columns[1:len(vidList)+1]) #grab the camera identifiers from the data frame- resWidth = parameterDictionary.get('resWidth')- resHeight = parameterDictionary.get('resHeight')- framerate = parameterDictionary.get('framerate')- trueWidth = resWidth #creating true resWidth and resHeight in case we need to rotate our images++def VideoTrim(session, vidList, ft, parameterDictionary, rotationState, numCamRange):+ camList = list(+ ft.columns[1 : len(vidList) + 1]+ ) # grab the camera identifiers from the data frame+ resWidth = parameterDictionary.get("resWidth")+ resHeight = parameterDictionary.get("resHeight")+ framerate = parameterDictionary.get("framerate")+ trueWidth = resWidth # creating true resWidth and resHeight in case we need to rotate our imagestrueHeight = resHeight- codec = parameterDictionary.get('codec')+ codec = parameterDictionary.get("codec")- for vid,cam,camNum in zip(vidList,camList,numCamRange): #iterate in parallel through camera identifiers and matching videos- print('Editing '+cam+' from ' +vid)- cap = cv2.VideoCapture(str(session.rawVidPath/vid)) #initialize OpenCV capture- frameTable = ft[cam] #grab the frames needed for that camera- success, image = cap.read() #start reading frames+ for vid, cam, camNum in zip(+ vidList, camList, numCamRange+ ): # iterate in parallel through camera identifiers and matching videos+ print("Editing " + cam + " from " + vid)+ cap = cv2.VideoCapture(+ str(session.rawVidPath / vid)+ ) # initialize OpenCV capture+ frameTable = ft[cam] # grab the frames needed for that camera+ success, image = cap.read() # start reading framesfourcc = cv2.VideoWriter_fourcc(*codec)- saveName = session.sessionID +'_synced_' + cam + '.mp4'--- saveSyncedVidPath = str(session.syncedVidPath/saveName) #create an output path for the function- if rotationState[camNum] == 90 or rotationState[camNum] == 270:- #if we are rotating to these angles, we need to temporarily swap our resWidth and resHeight+ saveName = session.sessionID + "_synced_" + cam + ".mp4"++ saveSyncedVidPath = str(+ session.syncedVidPath / saveName+ ) # create an output path for the function+ if rotationState[camNum] == 90 or rotationState[camNum] == 270:+ # if we are rotating to these angles, we need to temporarily swap our resWidth and resHeighttempHeight = resHeightresHeight = resWidthresWidth = tempHeight-- out = cv2.VideoWriter(saveSyncedVidPath, fourcc, framerate, (resWidth,resHeight)) #change resolution as needed- for frame in track(frameTable): #start looking through the frames we need- if frame == -1: #this is a buffer frame- blankFrame = np.zeros_like(image) #create a blank frame++ out = cv2.VideoWriter(+ saveSyncedVidPath, fourcc, framerate, (resWidth, resHeight)+ ) # change resolution as needed+ for frame in track(frameTable): # start looking through the frames we need+ if frame == -1: # this is a buffer frame+ blankFrame = np.zeros_like(image) # create a blank frameif rotationState[camNum] is not None:- blankFrame = imutils.rotate_bound(blankFrame, angle= rotationState[camNum])- image = cv2.resize(image,(resWidth,resHeight))- out.write(blankFrame) #write that frame to the video+ blankFrame = imutils.rotate_bound(+ blankFrame, angle=rotationState[camNum]+ )+ image = cv2.resize(image, (resWidth, resHeight))+ out.write(blankFrame) # write that frame to the videoelse:- cap.set(cv2.CAP_PROP_POS_FRAMES, frame) #set the video to the frame that we need+ cap.set(+ cv2.CAP_PROP_POS_FRAMES, frame+ ) # set the video to the frame that we needsuccess, image = cap.read()if rotationState[camNum] is not None:- image = imutils.rotate_bound(image, angle= rotationState[camNum])- image = cv2.resize(image,(resWidth,resHeight))+ image = imutils.rotate_bound(image, angle=rotationState[camNum])+ image = cv2.resize(image, (resWidth, resHeight))out.write(image)resWidth = trueWidthresHeight = trueHeightcap.release()out.release()- print('Saved '+ saveSyncedVidPath)+ print("Saved " + saveSyncedVidPath)print()-def createCalibrationVideos(session,calVideoFrameLength,parameterDictionary):++def createCalibrationVideos(session, calVideoFrameLength, parameterDictionary):vidList = os.listdir(session.syncedVidPath)framelist = list(range(calVideoFrameLength))- codec = parameterDictionary.get('codec')- for count,vid in enumerate(vidList,start=1):+ codec = parameterDictionary.get("codec")+ for count, vid in enumerate(vidList, start=1):cam_name = "Cam{}".format(count)- cap = cv2.VideoCapture(str(session.syncedVidPath/vid))+ cap = cv2.VideoCapture(str(session.syncedVidPath / vid))fourcc = cv2.VideoWriter_fourcc(*codec)-- #grab resolution parameters from the videos- resWidth = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))++ # grab resolution parameters from the videos+ resWidth = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))resHeight = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))framerate = int(cap.get(cv2.CAP_PROP_FPS))-- saveName = session.sessionID +'_trimmed_' + cam_name + '.mp4' #create a name for the trimmed video- saveCalVidPath = str(session.calVidPath/saveName) #create an output path for the function-- success, image = cap.read() #start reading frames-- out = cv2.VideoWriter(saveCalVidPath, fourcc, framerate, (resWidth,resHeight))- print('Trimming ' + cam_name)++ saveName = (+ session.sessionID + "_trimmed_" + cam_name + ".mp4"+ ) # create a name for the trimmed video+ saveCalVidPath = str(+ session.calVidPath / saveName+ ) # create an output path for the function++ success, image = cap.read() # start reading frames++ out = cv2.VideoWriter(saveCalVidPath, fourcc, framerate, (resWidth, resHeight))+ print("Trimming " + cam_name)for frame in track(framelist):- cap.set(cv2.CAP_PROP_POS_FRAMES, frame) #set the video to the frame that we need+ cap.set(+ cv2.CAP_PROP_POS_FRAMES, frame+ ) # set the video to the frame that we needsuccess, image = cap.read()out.write(image)cap.release()- out.release()\ No newline at end of file+ out.release()diff --git a/requirements.txt b/requirements.txtindex 5289505e..12224cf2 100644--- a/requirements.txt+++ b/requirements.txt@@ -1 +1,2 @@# List required packages in this file, one per line.+numpy\ No newline at end of filediff --git a/runme_FreeMoCap.py b/runme_FreeMoCap.pyindex 247fc954..31d107a3 100644--- a/runme_FreeMoCap.py+++ b/runme_FreeMoCap.py@@ -1,4 +1,16 @@-from freemocap import createvideo, initialization, recordingconfig, runcams, calibrate, fmc_mediapipe, fmc_openpose, fmc_deeplabcut, reconstruct3D, playskeleton, session+from freemocap import (+ createvideo,+ initialization,+ recordingconfig,+ runcams,+ calibrate,+ fmc_mediapipe,+ fmc_openpose,+ fmc_deeplabcut,+ reconstruct3D,+ playskeleton,+ session,+)from freemocap.fmc_pyqtgraph import PlayerDockedWindowfrom pathlib import Path@@ -11,102 +23,136 @@ import numpy as npsesh = session.Session()-sesh.useOpenPose= True+sesh.useOpenPose = Truesesh.useMediaPipe = False-sesh.useDLC=False+sesh.useDLC = False-if sesh.useDLC:+if sesh.useDLC:import deeplabcut as dlc- # sesh.dlcConfigPath = Path("C:\\Users\\jonma\\Dropbox\\GitKrakenRepos\\freemocap\\DLC_Models\\PinkGreenRedJugglingBalls-JSM-2021-05-31\\config.yaml")- sesh.dlcConfigPath = Path("C:\\Users\\jonma\\Desktop\\freemocap\\DLC_Models\\PinkGreenRedJugglingBalls-JSM-2021-05-31\\config.yaml")+ # sesh.dlcConfigPath = Path("C:\\Users\\jonma\\Dropbox\\GitKrakenRepos\\freemocap\\DLC_Models\\PinkGreenRedJugglingBalls-JSM-2021-05-31\\config.yaml")+ sesh.dlcConfigPath = Path(+ "C:\\Users\\jonma\\Desktop\\freemocap\\DLC_Models\\PinkGreenRedJugglingBalls-JSM-2021-05-31\\config.yaml"+ )# %% Inputs to edit-stage = 6 #set your starting stage here (stage = 1 will run the pipeline from webcams)+stage = 6 # set your starting stage here (stage = 1 will run the pipeline from webcams)sesh.debug = False-sesh.sessionID = 'sesh_21_06_22_1_CyrWheel' #fill in if you are running from Stage 2 onwards+sesh.sessionID = (+ "sesh_21_06_22_1_CyrWheel" # fill in if you are running from Stage 2 onwards+)if not sesh.sessionID:- dataFolder = Path.cwd()/'Data'- subfolders = [ f.path for f in os.scandir(dataFolder) if f.is_dir() ] #copy-pasta from who knows where- sesh.sessionID = Path(subfolders[-1]).stem #grab the name of the last folder in the list of subfolders--+ dataFolder = Path.cwd() / "Data"+ subfolders = [+ f.path for f in os.scandir(dataFolder) if f.is_dir()+ ] # copy-pasta from who knows where+ sesh.sessionID = Path(+ subfolders[-1]+ ).stem # grab the name of the last folder in the list of subfolders-board = CharucoBoard(7, 5,- #square_length=1, # here, in mm but any unit works (JSM NOTE - just using '1' so resulting units will be in 'charuco squarelenghts`)- #marker_length=.8,- # square_length = 121, #big boi charuco- # marker_length = 98,- square_length = 82,#regular boi charuco- marker_length = 65,- marker_bits=4, dict_size=250)+board = CharucoBoard(+ 7,+ 5,+ # square_length=1, # here, in mm but any unit works (JSM NOTE - just using '1' so resulting units will be in 'charuco squarelenghts`)+ # marker_length=.8,+ # square_length = 121, #big boi charuco+ # marker_length = 98,+ square_length=82, # regular boi charuco+ marker_length=65,+ marker_bits=4,+ dict_size=250,+)-#sesh.input_stage = stage-+# sesh.input_stage = stage# %% Initialization-initialization.initialize(sesh,stage,board)+initialization.initialize(sesh, stage, board)# %% Stage Oneif stage <= 1:print()- print('Starting Video Recordings')- runcams.RecordCams(sesh, sesh.cam_inputs, sesh.parameterDictionary, sesh.rotationInputs)+ print("Starting Video Recordings")+ runcams.RecordCams(+ sesh, sesh.cam_inputs, sesh.parameterDictionary, sesh.rotationInputs+ )else:- print('Skipping Video Recording')+ print("Skipping Video Recording")# %% Stage Twoif stage <= 2:print()- print('Starting Video Syncing')- runcams.SyncCams(sesh, sesh.timeStampData,sesh.numCamRange,sesh.vidNames,sesh.camIDs)+ print("Starting Video Syncing")+ runcams.SyncCams(+ sesh, sesh.timeStampData, sesh.numCamRange, sesh.vidNames, sesh.camIDs+ )else:- print('Skipping Video Syncing')+ print("Skipping Video Syncing")# %% Stage Threeif stage <= 3:print()- print('Starting Calibration')- sesh.cgroup, sesh.mean_charuco_fr_mar_dim = calibrate.CalibrateCaptureVolume(sesh,board)+ print("Starting Calibration")+ sesh.cgroup, sesh.mean_charuco_fr_mar_dim = calibrate.CalibrateCaptureVolume(+ sesh, board+ )else:- print('Skipping Calibration')+ print("Skipping Calibration")# %% Stage Fourif stage <= 4:- print('Starting Track Image Points')+ print("Starting Track Image Points")if sesh.useMediaPipe:fmc_mediapipe.runMediaPipe(sesh)- sesh.mediaPipeData_nCams_nFrames_nImgPts_XYC = fmc_mediapipe.parseMediaPipe(sesh)- sesh.mediaPipeSkel_fr_mar_dim = reconstruct3D.reconstruct3D(sesh,sesh.mediaPipeData_nCams_nFrames_nImgPts_XYC, confidenceThreshold=.1)- np.save(sesh.dataArrayPath/'mediaPipeSkel_3d.npy', sesh.mediaPipeSkel_fr_mar_dim) #save data to npy+ sesh.mediaPipeData_nCams_nFrames_nImgPts_XYC = fmc_mediapipe.parseMediaPipe(+ sesh+ )+ sesh.mediaPipeSkel_fr_mar_dim = reconstruct3D.reconstruct3D(+ sesh, sesh.mediaPipeData_nCams_nFrames_nImgPts_XYC, confidenceThreshold=0.1+ )+ np.save(+ sesh.dataArrayPath / "mediaPipeSkel_3d.npy", sesh.mediaPipeSkel_fr_mar_dim+ ) # save data to npyif sesh.useOpenPose:fmc_openpose.runOpenPose(sesh, dummyRun=False)sesh.openPoseData_nCams_nFrames_nImgPts_XYC = fmc_openpose.parseOpenPose(sesh)- sesh.openPoseSkel_fr_mar_dim = reconstruct3D.reconstruct3D(sesh,sesh.openPoseData_nCams_nFrames_nImgPts_XYC, confidenceThreshold=.1)- np.save(sesh.dataArrayPath/'openPoseSkel_3d.npy', sesh.openPoseSkel_fr_mar_dim) #save data to npy+ sesh.openPoseSkel_fr_mar_dim = reconstruct3D.reconstruct3D(+ sesh, sesh.openPoseData_nCams_nFrames_nImgPts_XYC, confidenceThreshold=0.1+ )+ np.save(+ sesh.dataArrayPath / "openPoseSkel_3d.npy", sesh.openPoseSkel_fr_mar_dim+ ) # save data to npysesh.syncedVidList = []if sesh.useDLC:- for vid in sesh.syncedVidPath.glob('*.mp4'):+ for vid in sesh.syncedVidPath.glob("*.mp4"):sesh.syncedVidList.append(str(vid))-- dlc.analyze_videos(sesh.dlcConfigPath,sesh.syncedVidList, destfolder= sesh.dlcDataPath, save_as_csv=True)++ dlc.analyze_videos(+ sesh.dlcConfigPath,+ sesh.syncedVidList,+ destfolder=sesh.dlcDataPath,+ save_as_csv=True,+ )sesh.dlcData_nCams_nFrames_nImgPts_XYC = fmc_deeplabcut.parseDeepLabCut(sesh)- sesh.dlc_fr_mar_dim = reconstruct3D.reconstruct3D(sesh,sesh.dlcData_nCams_nFrames_nImgPts_XYC, confidenceThreshold=.95)- np.save(sesh.dataArrayPath/'deepLabCut_3d.npy', sesh.dlc_fr_mar_dim) #save data to npy+ sesh.dlc_fr_mar_dim = reconstruct3D.reconstruct3D(+ sesh, sesh.dlcData_nCams_nFrames_nImgPts_XYC, confidenceThreshold=0.95+ )+ np.save(+ sesh.dataArrayPath / "deepLabCut_3d.npy", sesh.dlc_fr_mar_dim+ ) # save data to npyelse:- print('Skipping Run MediaPipe')+ print("Skipping Run MediaPipe")# # %% Stage Five# if not stage > 5:@@ -114,7 +160,6 @@ else:# sesh.mediaPipeData_nCams_nFrames_nImgPts_XYC = fmc_mediapipe.parseMediaPipe(sesh)# else:# print('Skipping Parse MediaPipe')-# # %% Stage Six@@ -127,40 +172,40 @@ else:# np.save(path_to_skel_points, sesh.skel_fr_mar_dim)# else:# print('Skipping Skeleton Reconstruction')--#reupdate the config_settings with mediapipe and openpose image paths+# reupdate the config_settings with mediapipe and openpose image pathssesh.config_settings = recordingconfig.load_config_yaml(sesh.yamlPath)# %% Stage Sixif stage <= 6:- print ('Starting PyQT Animation')- #createvideo.createBodyTrackingVideos(sesh)- displayVid = 1- #if displayVid = 0, will show the synced videos- #if displayVid = 1, will show the openPosed videos- playWin = PlayerDockedWindow(sesh,displayVid)+ print("Starting PyQT Animation")+ # createvideo.createBodyTrackingVideos(sesh)+ displayVid = 1+ # if displayVid = 0, will show the synced videos+ # if displayVid = 1, will show the openPosed videos+ playWin = PlayerDockedWindow(sesh, displayVid)playWin.animate()# %% Stage Sevenif stage <= 7:- print('Starting Skeleton Plotting')+ print("Starting Skeleton Plotting")playskeleton.ReplaySkeleton_matplotlib(- sesh,- vidType=1,- startFrame=40,- azimuth=-90,- elevation=-80,- useOpenPose=sesh.useOpenPose,- useMediaPipe=sesh.useMediaPipe,- useDLC=sesh.useDLC)+ sesh,+ vidType=1,+ startFrame=40,+ azimuth=-90,+ elevation=-80,+ useOpenPose=sesh.useOpenPose,+ useMediaPipe=sesh.useMediaPipe,+ useDLC=sesh.useDLC,+ )# fmc_pyqtgraph.PlaySkeleton(# sesh,# vidType=1,# startFrame=40,- # azimuth=-90,+ # azimuth=-90,# elevation=-80,# useOpenPose=useOpenPose,# useMediaPipe=useMediaPipe,@@ -168,23 +213,20 @@ if stage <= 7:# playSkel = PlaySkeleton(sesh)# playSkel.animate()- #playWin =PlayerDockedWindow(sesh)- #playWin.animate()+ # playWin =PlayerDockedWindow(sesh)+ # playWin.animate()else:- print('Skipping Skeleton Plotting')--+ print("Skipping Skeleton Plotting")# %% Stage Eightif stage <= 8:print()- print('Starting Video Creation')+ print("Starting Video Creation")createvideo.createVideo(sesh)else:- print('Skipping Video Creation')-+ print("Skipping Video Creation")-f = 9\ No newline at end of file+f = 9diff --git a/setup.py b/setup.pyindex 8423238c..668b72a7 100644--- a/setup.py+++ b/setup.py@@ -19,39 +19,44 @@ This may be due to an out-of-date pip. Make sure you have pip >= 9.0.1.Upgrade pip like so:pip install --upgrade pip-""".format(*(sys.version_info[:2] + min_version))+""".format(+ *(sys.version_info[:2] + min_version)+ )sys.exit(error)here = path.abspath(path.dirname(__file__))-with open(path.join(here, 'README.rst'), encoding='utf-8') as readme_file:+with open(path.join(here, "README.rst"), encoding="utf-8") as readme_file:readme = readme_file.read()-with open(path.join(here, 'requirements.txt')) as requirements_file:+with open(path.join(here, "requirements.txt")) as requirements_file:# Parse requirements.txt, ignoring any commented-out lines.- requirements = [line for line in requirements_file.read().splitlines()- if not line.startswith('#')]+ requirements = [+ line+ for line in requirements_file.read().splitlines()+ if not line.startswith("#")+ ]setup(- name='freemocap',+ name="freemocap",version=versioneer.get_version(),cmdclass=versioneer.get_cmdclass(),description="The FreeMoCap Project: A free-and-open-source, hardware-and-software-agnostic, research-grade, minimal-cost motion capture system and platform for decentralized scientific research, education, and training",long_description=readme,author="Jonathan Samir Matthis",- author_email='[email protected]',- url='https://github.com/jonmatthis/freemocap',- python_requires='>={}'.format('.'.join(str(n) for n in min_version)),- packages=find_packages(exclude=['docs', 'tests']),+ author_email="[email protected]",+ url="https://github.com/jonmatthis/freemocap",+ python_requires=">={}".format(".".join(str(n) for n in min_version)),+ packages=find_packages(exclude=["docs", "tests"]),entry_points={- 'console_scripts': [+ "console_scripts": [# 'command = some.module:some_function',],},include_package_data=True,package_data={- 'freemocap': [+ "freemocap": [# When adding files here, remember to update MANIFEST.in as well,# or else they will not be included in the distribution on PyPI!# 'path/to/data_file',@@ -60,8 +65,8 @@ setup(install_requires=requirements,license="BSD (3-clause)",classifiers=[- 'Development Status :: 2 - Pre-Alpha',- 'Natural Language :: English',- 'Programming Language :: Python :: 3',+ "Development Status :: 2 - Pre-Alpha",+ "Natural Language :: English",+ "Programming Language :: Python :: 3",],)diff --git a/versioneer.py b/versioneer.pyindex 64fea1c8..2b545405 100644--- a/versioneer.py+++ b/versioneer.py@@ -1,4 +1,3 @@-# Version: 0.18"""The Versioneer - like a rocketeer, but for versions.@@ -277,6 +276,7 @@ https://creativecommons.org/publicdomain/zero/1.0/ ."""from __future__ import print_function+try:import configparserexcept ImportError:@@ -308,11 +308,13 @@ def get_root():setup_py = os.path.join(root, "setup.py")versioneer_py = os.path.join(root, "versioneer.py")if not (os.path.exists(setup_py) or os.path.exists(versioneer_py)):- err = ("Versioneer was unable to run the project root directory. "- "Versioneer requires setup.py to be executed from "- "its immediate directory (like 'python setup.py COMMAND'), "- "or in a way that lets it use sys.argv[0] to find the root "- "(like 'python path/to/setup.py COMMAND').")+ err = (+ "Versioneer was unable to run the project root directory. "+ "Versioneer requires setup.py to be executed from "+ "its immediate directory (like 'python setup.py COMMAND'), "+ "or in a way that lets it use sys.argv[0] to find the root "+ "(like 'python path/to/setup.py COMMAND')."+ )raise VersioneerBadRootError(err)try:# Certain runtime workflows (setup.py install/develop in a setuptools@@ -325,8 +327,10 @@ def get_root():me_dir = os.path.normcase(os.path.splitext(me)[0])vsr_dir = os.path.normcase(os.path.splitext(versioneer_py)[0])if me_dir != vsr_dir:- print("Warning: build in %s is using versioneer.py from %s"- % (os.path.dirname(me), versioneer_py))+ print(+ "Warning: build in %s is using versioneer.py from %s"+ % (os.path.dirname(me), versioneer_py)+ )except NameError:passreturn root@@ -348,6 +352,7 @@ def get_config_from_root(root):if parser.has_option("versioneer", name):return parser.get("versioneer", name)return None+cfg = VersioneerConfig()cfg.VCS = VCScfg.style = get(parser, "style") or ""@@ -372,17 +377,18 @@ HANDLERS = {}def register_vcs_handler(vcs, method): # decorator"""Decorator to mark a method as the handler for a particular VCS."""+def decorate(f):"""Store f in HANDLERS[vcs][method]."""if vcs not in HANDLERS:HANDLERS[vcs] = {}HANDLERS[vcs][method] = freturn f+return decorate-def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False,- env=None):+def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False, env=None):"""Call the given command(s)."""assert isinstance(commands, list)p = None@@ -390,10 +396,13 @@ def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False,try:dispcmd = str([c] + args)# remember shell=False, so use git.cmd on windows, not just git- p = subprocess.Popen([c] + args, cwd=cwd, env=env,- stdout=subprocess.PIPE,- stderr=(subprocess.PIPE if hide_stderr- else None))+ p = subprocess.Popen(+ [c] + args,+ cwd=cwd,+ env=env,+ stdout=subprocess.PIPE,+ stderr=(subprocess.PIPE if hide_stderr else None),+ )breakexcept EnvironmentError:e = sys.exc_info()[1]@@ -418,7 +427,9 @@ def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False,return stdout, p.returncode-LONG_VERSION_PY['git'] = '''+LONG_VERSION_PY[+ "git"+] = '''# This file helps to compute a version number in source trees obtained from# git-archive tarball (such as those provided by githubs download-from-tag# feature). Distribution tarballs (built by setup.py sdist) and build@@ -993,7 +1004,7 @@ def git_versions_from_keywords(keywords, tag_prefix, verbose):# starting in git-1.8.3, tags are listed as "tag: foo-1.0" instead of# just "foo-1.0". If we see a "tag: " prefix, prefer those.TAG = "tag: "- tags = set([r[len(TAG):] for r in refs if r.startswith(TAG)])+ tags = set([r[len(TAG) :] for r in refs if r.startswith(TAG)])if not tags:# Either we're using git < 1.8.3, or there really are no tags. We use# a heuristic: assume all version tags have a digit. The old git %d@@ -1002,7 +1013,7 @@ def git_versions_from_keywords(keywords, tag_prefix, verbose):# between branches and tags. By ignoring refnames without digits, we# filter out many common branch names like "release" and# "stabilization", as well as "HEAD" and "master".- tags = set([r for r in refs if re.search(r'\d', r)])+ tags = set([r for r in refs if re.search(r"\d", r)])if verbose:print("discarding '%s', no digits" % ",".join(refs - tags))if verbose:@@ -1010,19 +1021,26 @@ def git_versions_from_keywords(keywords, tag_prefix, verbose):for ref in sorted(tags):# sorting will prefer e.g. "2.0" over "2.0rc1"if ref.startswith(tag_prefix):- r = ref[len(tag_prefix):]+ r = ref[len(tag_prefix) :]if verbose:print("picking %s" % r)- return {"version": r,- "full-revisionid": keywords["full"].strip(),- "dirty": False, "error": None,- "date": date}+ return {+ "version": r,+ "full-revisionid": keywords["full"].strip(),+ "dirty": False,+ "error": None,+ "date": date,+ }# no suitable tags, so version is "0+unknown", but full hex is still thereif verbose:print("no suitable tags, using unknown + full revision id")- return {"version": "0+unknown",- "full-revisionid": keywords["full"].strip(),- "dirty": False, "error": "no suitable tags", "date": None}+ return {+ "version": "0+unknown",+ "full-revisionid": keywords["full"].strip(),+ "dirty": False,+ "error": "no suitable tags",+ "date": None,+ }@register_vcs_handler("git", "pieces_from_vcs")@@ -1037,8 +1055,7 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command):if sys.platform == "win32":GITS = ["git.cmd", "git.exe"]- out, rc = run_command(GITS, ["rev-parse", "--git-dir"], cwd=root,- hide_stderr=True)+ out, rc = run_command(GITS, ["rev-parse", "--git-dir"], cwd=root, hide_stderr=True)if rc != 0:if verbose:print("Directory %s not under git control" % root)@@ -1046,10 +1063,19 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command):# if there is a tag matching tag_prefix, this yields TAG-NUM-gHEX[-dirty]# if there isn't one, this yields HEX[-dirty] (no NUM)- describe_out, rc = run_command(GITS, ["describe", "--tags", "--dirty",- "--always", "--long",- "--match", "%s*" % tag_prefix],- cwd=root)+ describe_out, rc = run_command(+ GITS,+ [+ "describe",+ "--tags",+ "--dirty",+ "--always",+ "--long",+ "--match",+ "%s*" % tag_prefix,+ ],+ cwd=root,+ )# --long was added in git-1.5.5if describe_out is None:raise NotThisMethod("'git describe' failed")@@ -1072,17 +1098,16 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command):dirty = git_describe.endswith("-dirty")pieces["dirty"] = dirtyif dirty:- git_describe = git_describe[:git_describe.rindex("-dirty")]+ git_describe = git_describe[: git_describe.rindex("-dirty")]# now we have TAG-NUM-gHEX or HEXif "-" in git_describe:# TAG-NUM-gHEX- mo = re.search(r'^(.+)-(\d+)-g([0-9a-f]+)$', git_describe)+ mo = re.search(r"^(.+)-(\d+)-g([0-9a-f]+)$", git_describe)if not mo:# unparseable. Maybe git-describe is misbehaving?- pieces["error"] = ("unable to parse git-describe output: '%s'"- % describe_out)+ pieces["error"] = "unable to parse git-describe output: '%s'" % describe_outreturn pieces# tag@@ -1091,10 +1116,12 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command):if verbose:fmt = "tag '%s' doesn't start with prefix '%s'"print(fmt % (full_tag, tag_prefix))- pieces["error"] = ("tag '%s' doesn't start with prefix '%s'"- % (full_tag, tag_prefix))+ pieces["error"] = "tag '%s' doesn't start with prefix '%s'" % (+ full_tag,+ tag_prefix,+ )return pieces- pieces["closest-tag"] = full_tag[len(tag_prefix):]+ pieces["closest-tag"] = full_tag[len(tag_prefix) :]# distance: number of commits since tagpieces["distance"] = int(mo.group(2))@@ -1105,13 +1132,13 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command):else:# HEX: no tagspieces["closest-tag"] = None- count_out, rc = run_command(GITS, ["rev-list", "HEAD", "--count"],- cwd=root)+ count_out, rc = run_command(GITS, ["rev-list", "HEAD", "--count"], cwd=root)pieces["distance"] = int(count_out) # total number of commits# commit date: see ISO-8601 comment in git_versions_from_keywords()- date = run_command(GITS, ["show", "-s", "--format=%ci", "HEAD"],- cwd=root)[0].strip()+ date = run_command(GITS, ["show", "-s", "--format=%ci", "HEAD"], cwd=root)[+ 0+ ].strip()pieces["date"] = date.strip().replace(" ", "T", 1).replace(" ", "", 1)return pieces@@ -1167,16 +1194,22 @@ def versions_from_parentdir(parentdir_prefix, root, verbose):for i in range(3):dirname = os.path.basename(root)if dirname.startswith(parentdir_prefix):- return {"version": dirname[len(parentdir_prefix):],- "full-revisionid": None,- "dirty": False, "error": None, "date": None}+ return {+ "version": dirname[len(parentdir_prefix) :],+ "full-revisionid": None,+ "dirty": False,+ "error": None,+ "date": None,+ }else:rootdirs.append(root)root = os.path.dirname(root) # up a levelif verbose:- print("Tried directories %s but none started with prefix %s" %- (str(rootdirs), parentdir_prefix))+ print(+ "Tried directories %s but none started with prefix %s"+ % (str(rootdirs), parentdir_prefix)+ )raise NotThisMethod("rootdir doesn't start with parentdir_prefix")@@ -1205,11 +1238,13 @@ def versions_from_file(filename):contents = f.read()except EnvironmentError:raise NotThisMethod("unable to read _version.py")- mo = re.search(r"version_json = '''\n(.*)''' # END VERSION_JSON",- contents, re.M | re.S)+ mo = re.search(+ r"version_json = '''\n(.*)''' # END VERSION_JSON", contents, re.M | re.S+ )if not mo:- mo = re.search(r"version_json = '''\r\n(.*)''' # END VERSION_JSON",- contents, re.M | re.S)+ mo = re.search(+ r"version_json = '''\r\n(.*)''' # END VERSION_JSON", contents, re.M | re.S+ )if not mo:raise NotThisMethod("no version_json in _version.py")return json.loads(mo.group(1))@@ -1218,8 +1253,7 @@ def versions_from_file(filename):def write_to_version_file(filename, versions):"""Write the given version number to the given _version.py file."""os.unlink(filename)- contents = json.dumps(versions, sort_keys=True,- indent=1, separators=(",", ": "))+ contents = json.dumps(versions, sort_keys=True, indent=1, separators=(",", ": "))with open(filename, "w") as f:f.write(SHORT_VERSION_PY % contents)@@ -1251,8 +1285,7 @@ def render_pep440(pieces):rendered += ".dirty"else:# exception #1- rendered = "0+untagged.%d.g%s" % (pieces["distance"],- pieces["short"])+ rendered = "0+untagged.%d.g%s" % (pieces["distance"], pieces["short"])if pieces["dirty"]:rendered += ".dirty"return rendered@@ -1366,11 +1399,13 @@ def render_git_describe_long(pieces):def render(pieces, style):"""Render the given version pieces into the requested style."""if pieces["error"]:- return {"version": "unknown",- "full-revisionid": pieces.get("long"),- "dirty": None,- "error": pieces["error"],- "date": None}+ return {+ "version": "unknown",+ "full-revisionid": pieces.get("long"),+ "dirty": None,+ "error": pieces["error"],+ "date": None,+ }if not style or style == "default":style = "pep440" # the default@@ -1390,9 +1425,13 @@ def render(pieces, style):else:raise ValueError("unknown style '%s'" % style)- return {"version": rendered, "full-revisionid": pieces["long"],- "dirty": pieces["dirty"], "error": None,- "date": pieces.get("date")}+ return {+ "version": rendered,+ "full-revisionid": pieces["long"],+ "dirty": pieces["dirty"],+ "error": None,+ "date": pieces.get("date"),+ }class VersioneerBadRootError(Exception):@@ -1415,8 +1454,9 @@ def get_versions(verbose=False):handlers = HANDLERS.get(cfg.VCS)assert handlers, "unrecognized VCS '%s'" % cfg.VCSverbose = verbose or cfg.verbose- assert cfg.versionfile_source is not None, \- "please set versioneer.versionfile_source"+ assert (+ cfg.versionfile_source is not None+ ), "please set versioneer.versionfile_source"assert cfg.tag_prefix is not None, "please set versioneer.tag_prefix"versionfile_abs = os.path.join(root, cfg.versionfile_source)@@ -1470,9 +1510,13 @@ def get_versions(verbose=False):if verbose:print("unable to compute version")- return {"version": "0+unknown", "full-revisionid": None,- "dirty": None, "error": "unable to compute version",- "date": None}+ return {+ "version": "0+unknown",+ "full-revisionid": None,+ "dirty": None,+ "error": "unable to compute version",+ "date": None,+ }def get_version():@@ -1521,6 +1565,7 @@ def get_cmdclass():print(" date: %s" % vers.get("date"))if vers["error"]:print(" error: %s" % vers["error"])+cmds["version"] = cmd_version# we override "build_py" in both distutils and setuptools@@ -1553,14 +1598,15 @@ def get_cmdclass():# now locate _version.py in the new build/ directory and replace# it with an updated valueif cfg.versionfile_build:- target_versionfile = os.path.join(self.build_lib,- cfg.versionfile_build)+ target_versionfile = os.path.join(self.build_lib, cfg.versionfile_build)print("UPDATING %s" % target_versionfile)write_to_version_file(target_versionfile, versions)+cmds["build_py"] = cmd_build_pyif "cx_Freeze" in sys.modules: # cx_freeze enabled?from cx_Freeze.dist import build_exe as _build_exe+# nczeczulin reports that py2exe won't like the pep440-style string# as FILEVERSION, but it can be used for PRODUCTVERSION, e.g.# setup(console=[{@@ -1581,17 +1627,21 @@ def get_cmdclass():os.unlink(target_versionfile)with open(cfg.versionfile_source, "w") as f:LONG = LONG_VERSION_PY[cfg.VCS]- f.write(LONG %- {"DOLLAR": "$",- "STYLE": cfg.style,- "TAG_PREFIX": cfg.tag_prefix,- "PARENTDIR_PREFIX": cfg.parentdir_prefix,- "VERSIONFILE_SOURCE": cfg.versionfile_source,- })+ f.write(+ LONG+ % {+ "DOLLAR": "$",+ "STYLE": cfg.style,+ "TAG_PREFIX": cfg.tag_prefix,+ "PARENTDIR_PREFIX": cfg.parentdir_prefix,+ "VERSIONFILE_SOURCE": cfg.versionfile_source,+ }+ )+cmds["build_exe"] = cmd_build_exedel cmds["build_py"]- if 'py2exe' in sys.modules: # py2exe enabled?+ if "py2exe" in sys.modules: # py2exe enabled?try:from py2exe.distutils_buildexe import py2exe as _py2exe # py3except ImportError:@@ -1610,13 +1660,17 @@ def get_cmdclass():os.unlink(target_versionfile)with open(cfg.versionfile_source, "w") as f:LONG = LONG_VERSION_PY[cfg.VCS]- f.write(LONG %- {"DOLLAR": "$",- "STYLE": cfg.style,- "TAG_PREFIX": cfg.tag_prefix,- "PARENTDIR_PREFIX": cfg.parentdir_prefix,- "VERSIONFILE_SOURCE": cfg.versionfile_source,- })+ f.write(+ LONG+ % {+ "DOLLAR": "$",+ "STYLE": cfg.style,+ "TAG_PREFIX": cfg.tag_prefix,+ "PARENTDIR_PREFIX": cfg.parentdir_prefix,+ "VERSIONFILE_SOURCE": cfg.versionfile_source,+ }+ )+cmds["py2exe"] = cmd_py2exe# we override different "sdist" commands for both environments@@ -1643,8 +1697,10 @@ def get_cmdclass():# updated valuetarget_versionfile = os.path.join(base_dir, cfg.versionfile_source)print("UPDATING %s" % target_versionfile)- write_to_version_file(target_versionfile,- self._versioneer_generated_versions)+ write_to_version_file(+ target_versionfile, self._versioneer_generated_versions+ )+cmds["sdist"] = cmd_sdistreturn cmds@@ -1699,11 +1755,13 @@ def do_setup():root = get_root()try:cfg = get_config_from_root(root)- except (EnvironmentError, configparser.NoSectionError,- configparser.NoOptionError) as e:+ except (+ EnvironmentError,+ configparser.NoSectionError,+ configparser.NoOptionError,+ ) as e:if isinstance(e, (EnvironmentError, configparser.NoSectionError)):- print("Adding sample versioneer config to setup.cfg",- file=sys.stderr)+ print("Adding sample versioneer config to setup.cfg", file=sys.stderr)with open(os.path.join(root, "setup.cfg"), "a") as f:f.write(SAMPLE_CONFIG)print(CONFIG_ERROR, file=sys.stderr)@@ -1712,15 +1770,18 @@ def do_setup():print(" creating %s" % cfg.versionfile_source)with open(cfg.versionfile_source, "w") as f:LONG = LONG_VERSION_PY[cfg.VCS]- f.write(LONG % {"DOLLAR": "$",- "STYLE": cfg.style,- "TAG_PREFIX": cfg.tag_prefix,- "PARENTDIR_PREFIX": cfg.parentdir_prefix,- "VERSIONFILE_SOURCE": cfg.versionfile_source,- })-- ipy = os.path.join(os.path.dirname(cfg.versionfile_source),- "__init__.py")+ f.write(+ LONG+ % {+ "DOLLAR": "$",+ "STYLE": cfg.style,+ "TAG_PREFIX": cfg.tag_prefix,+ "PARENTDIR_PREFIX": cfg.parentdir_prefix,+ "VERSIONFILE_SOURCE": cfg.versionfile_source,+ }+ )++ ipy = os.path.join(os.path.dirname(cfg.versionfile_source), "__init__.py")if os.path.exists(ipy):try:with open(ipy, "r") as f:@@ -1762,8 +1823,10 @@ def do_setup():else:print(" 'versioneer.py' already in MANIFEST.in")if cfg.versionfile_source not in simple_includes:- print(" appending versionfile_source ('%s') to MANIFEST.in" %- cfg.versionfile_source)+ print(+ " appending versionfile_source ('%s') to MANIFEST.in"+ % cfg.versionfile_source+ )with open(manifest_in, "a") as f:f.write("include %s\n" % cfg.versionfile_source)else: