Motionvideo¶
Motion from video: frame differencing, and what its defaults are for.
MGT's defaults are tuned to produce a legible picture, not a measurement. That is the right choice for what most of this module makes --- motion videos, motiongrams, plots meant to be looked at --- and it is worth stating plainly, because the same functions are increasingly used to produce numbers.
Three defaults are visualisation choices:
threshold=0.05discards small pixel differences, which removes sensor noise and compression shimmer and makes a motiongram read cleanly. It also removes small real motion.
Measured, on 345 clips of a corpus of everyday sound-producing actions, each an event embedded in stillness: no threshold improves how far the action stands above the lead-in it interrupts. More clips lose contrast than gain it at every step, and at the lower thresholds not by chance (202 of 345 at this default, sign test p = 0.002) --- but the median cost at 0.05 is 0.2 %, against a per-clip spread of 0.39 to 2.66. The direction is real and the size is negligible. The picture-legible default is therefore free, for practical purposes, for machine analysis of this kind of material.
At threshold=0.2 the direction stops being significant (187 of 345,
p = 0.13) and the mean ratio rises above one, so heavier filtering is not
reliably worse either --- it simply stops doing anything consistent.
One caution. This scores a single criterion, action against lead-in, and a threshold could be free for that while costing something nobody has measured. Note also how the number moved as the sample grew: 6 clips said no effect, 23 said 1.2 %, 83 said 0.4 %, 345 says 0.2 %. The corpus is heavy-tailed and the small samples were confidently wrong.
A threshold also moves any landmark computed from the same series.
Scored at each threshold's own onset the fall reads as 23 %; with the
boundary held still it is 9 %. The remainder is the onset shifting under the
threshold and dragging frames across the very boundary the statistic is
measured against. That trap is not specific to this measure.
- filtertype='Regular' keeps the magnitude above the threshold. 'Binary'
keeps only whether a pixel moved, which is the fraction of the frame in
motion rather than how much it moved.
- The exported quantity of motion is divided by each clip's own maximum.
Every clip then peaks at exactly 1.0. This is the one that surprises people,
because it is invisible in the numbers themselves: it makes quantity of
motion incomparable between clips, and lets a single bright frame set the
scale for everything around it. Pass normalize=False for the raw sum of
pixel values, exported as QomRaw.
Nothing is smoothed by default. The whole default chain is
format=gray -> tblend=all_mode=difference -> threshold. The smoothing that
exists is off unless asked for: atadenoise (adaptive temporal averaging over
129 frames, the only temporal one), use_median (ffmpeg's spatial median) and
blur (a spatial 10x10 box). A machine-analysis configuration is therefore
mostly a matter of threshold and normalize, not of turning filters off.
mg_motion ¶
mg_motion(self, filtertype='Regular', threshold=0.05, blur='None', kernel_size=5, use_median=False, unit='seconds', atadenoise=False, motion_analysis='all', inverted_motionvideo=False, inverted_motiongram=False, equalize_motiongram=False, audio_descriptors=False, save_plot=True, title=None, save_data=True, data_format='csv', save_motiongrams=True, save_video=True, target_name_video=None, target_name_plot=None, target_name_data=None, target_name_mgx=None, target_name_mgy=None, overwrite=True, normalize=None)
Finds the difference in pixel value from one frame to the next in an input video, and saves the frames into a new video. Describes the motion in the recording. Outputs: a motion video, a plot describing the centroid of motion and the quantity of motion, horizontal and vertical motiongrams, and a text file containing the quantity of motion and the centroid of motion for each frame with timecodes in milliseconds.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filtertype
|
str
|
'Regular' turns all values below |
'Regular'
|
threshold
|
float
|
Eliminates pixel values less than given threshold. Ranges from 0 to 1. Defaults to 0.05. |
0.05
|
blur
|
str
|
'Average' to apply a 10px * 10px blurring filter, 'None' otherwise. Defaults to 'None'. |
'None'
|
kernel_size
|
int
|
Size of structuring element. Defaults to 5. |
5
|
use_median
|
bool
|
If True the algorithm applies a median filter on the thresholded frame-difference stream. Defaults to False. |
False
|
unit
|
str
|
Unit in QoM plot. Accepted values are 'seconds' or 'samples'. Defaults to 'seconds'. |
'seconds'
|
atadenoise
|
bool
|
If True, applies an adaptive temporal averaging denoiser every 129 frames. Defaults to False. |
False
|
normalize
|
bool | None
|
Chooses between data for looking at and data for
measuring with. True (and None, the long-standing default) writes a |
None
|
motion_analysis
|
str
|
Specify which motion analysis to process or all. 'AoM' renders the Area of Motion. 'CoM' renders the Centroid of Motion. 'QoM' renders the Quantity of Motion. 'all' renders all the motion analysis available. Defaults to 'all'. |
'all'
|
inverted_motionvideo
|
bool
|
If True, inverts colors of the motion video. Defaults to False. |
False
|
inverted_motiongram
|
bool
|
If True, inverts colors of the motiongrams. Defaults to False. |
False
|
equalize_motiongram
|
bool
|
If True, converts the motiongrams to hsv-color space and flattens the value channel (v). Defaults to True. |
False
|
save_plot
|
bool
|
If True, outputs motion-plot. Defaults to True. |
True
|
title
|
str
|
Optionally add title to the plot. Defaults to None, which uses the file name as a title. |
None
|
save_data
|
bool
|
If True, outputs motion-data. Defaults to True. |
True
|
data_format
|
str / list
|
Specifies format of motion-data. Accepted values are 'csv', 'tsv' and 'txt'. For multiple output formats, use list, eg. ['csv', 'txt']. Defaults to 'csv'. |
'csv'
|
save_motiongrams
|
bool
|
If True, outputs motiongrams. Defaults to True. |
True
|
save_video
|
bool
|
If True, outputs the motion video. Defaults to True. |
True
|
target_name_video
|
str
|
Target output name for the video. Defaults to None (which assumes that the input filename with the suffix "_motion" should be used). |
None
|
target_name_plot
|
str
|
Target output name for the plot. Defaults to None (which assumes that the input filename with the suffix "_motion_com_aom_qom" should be used). |
None
|
target_name_data
|
str
|
Target output name for the data. Defaults to None (which assumes that the input filename with the suffix "_motion" should be used). |
None
|
target_name_mgx
|
str
|
Target output name for the vertical motiongram. Defaults to None (which assumes that the input filename with the suffix "_mgv" should be used). |
None
|
target_name_mgy
|
str
|
Target output name for the horizontal motiongram. Defaults to None (which assumes that the input filename with the suffix "_mgh" should be used). |
None
|
overwrite
|
bool
|
Whether to allow overwriting existing files or to automatically increment target filenames to avoid overwriting. Defaults to True. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
MgVideo |
'musicalgestures.MgVideo'
|
A new MgVideo pointing to the output video file. If |
Source code in musicalgestures/_motionvideo.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 | |
mg_motiongrams ¶
mg_motiongrams(self, filtertype='Regular', threshold=0.05, blur='None', use_median=False, atadenoise=False, kernel_size=5, inverted_motiongram=False, equalize_motiongram=True, target_name_mgx=None, target_name_mgy=None, overwrite=True, normalize=None)
Shortcut for mg_motion to only render motiongrams.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filtertype
|
str
|
'Regular' turns all values below |
'Regular'
|
threshold
|
float
|
Eliminates pixel values less than given threshold. Ranges from 0 to 1. Defaults to 0.05. |
0.05
|
blur
|
str
|
'Average' to apply a 10px * 10px blurring filter, 'None' otherwise. Defaults to 'None'. |
'None'
|
use_median
|
bool
|
If True the algorithm applies a median filter on the thresholded frame-difference stream. Defaults to False. |
False
|
atadenoise
|
bool
|
If True, applies an adaptive temporal averaging denoiser every 129 frames. Defaults to False. |
False
|
kernel_size
|
int
|
Size of the median filter (if |
5
|
inverted_motiongram
|
bool
|
If True, inverts colors of the motiongrams. Defaults to False. |
False
|
equalize_motiongram
|
bool
|
If True, converts the motiongrams to hsv-color space and flattens the value channel (v). Defaults to True. |
True
|
target_name_mgx
|
str
|
Target output name for the vertical motiongram. Defaults to None (which assumes that the input filename with the suffix "_mgv" should be used). |
None
|
target_name_mgy
|
str
|
Target output name for the horizontal motiongram. Defaults to None (which assumes that the input filename with the suffix "_mgh" should be used). |
None
|
overwrite
|
bool
|
Whether to allow overwriting existing files or to automatically increment target filenames to avoid overwriting. Defaults to True. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
MgList |
'MgList'
|
An MgList pointing to the output motiongram images (as MgImages). |
Source code in musicalgestures/_motionvideo.py
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 | |
mg_motionvideo ¶
mg_motionvideo(self, filtertype='Regular', threshold=0.05, blur='None', use_median=False, kernel_size=5, inverted_motionvideo=False, target_name=None, overwrite=True)
Shortcut to only render the motion video. Uses musicalgestures._utils.motionvideo_ffmpeg. Note that this does not apply median filter by default. If you need it use use_median=True.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filtertype
|
str
|
'Regular' turns all values below |
'Regular'
|
threshold
|
float
|
Eliminates pixel values less than given threshold. Ranges from 0 to 1. Defaults to 0.05. |
0.05
|
blur
|
str
|
'Average' to apply a 10px * 10px blurring filter, 'None' otherwise. Defaults to 'None'. |
'None'
|
use_median
|
bool
|
If True the algorithm applies a median filter on the thresholded frame-difference stream. Defaults to False. |
False
|
kernel_size
|
int
|
Size of the median filter (if |
5
|
inverted_motionvideo
|
bool
|
If True, inverts colors of the motion video. Defaults to False. |
False
|
target_name
|
str
|
Target output name for the video. Defaults to None (which assumes that the input filename with the suffix "_motion" should be used). |
None
|
overwrite
|
bool
|
Whether to allow overwriting existing files or to automatically increment target filenames to avoid overwriting. Defaults to True. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
MgVideo |
'musicalgestures.MgVideo'
|
A new MgVideo pointing to the output '_motion' video file. |
Source code in musicalgestures/_motionvideo.py
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 | |
mg_motiondata ¶
mg_motiondata(self, filtertype='Regular', threshold=0.05, blur='None', kernel_size=5, atadenoise=False, use_median=False, motion_analysis='all', data_format='csv', target_name=None, overwrite=True)
Shortcut for mg_motion to only render motion data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filtertype
|
str
|
'Regular' turns all values below |
'Regular'
|
threshold
|
float
|
Eliminates pixel values less than given threshold. Ranges from 0 to 1. Defaults to 0.05. |
0.05
|
blur
|
str
|
'Average' to apply a 10px * 10px blurring filter, 'None' otherwise. Defaults to 'None'. |
'None'
|
kernel_size
|
int
|
Size of structuring element. Defaults to 5. |
5
|
atadenoise
|
bool
|
If True, applies an adaptive temporal averaging denoiser every 129 frames. Defaults to False. |
False
|
use_median
|
bool
|
If True the algorithm applies a median filter on the thresholded frame-difference stream. Defaults to False. |
False
|
motion_analysis
|
str
|
Specify which motion analysis to process or all. 'AoM' renders the Area of Motion. 'CoM' renders the Centroid of Motion. 'QoM' renders the Quantity of Motion. 'all' renders all the motion analysis available. Defaults to 'all'. |
'all'
|
data_format
|
str / list
|
Specifies format of motion-data. Accepted values are 'csv', 'tsv' and 'txt'. For multiple output formats, use list, eg. ['csv', 'txt']. Defaults to 'csv'. |
'csv'
|
target_name
|
str
|
Target output name for the data. Defaults to None (which assumes that the input filename with the suffix "_motion" should be used). |
None
|
overwrite
|
bool
|
Whether to allow overwriting existing files or to automatically increment target filenames to avoid overwriting. Defaults to True. |
True
|
Returns:
| Type | Description |
|---|---|
'list'
|
str/list: The path(s) to the rendered data file(s). |
Source code in musicalgestures/_motionvideo.py
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 | |
mg_motionplots ¶
mg_motionplots(self, filtertype='Regular', threshold=0.05, blur='None', kernel_size=5, use_median=False, atadenoise=False, motion_analysis='all', audio_descriptors=False, unit='seconds', title=None, target_name=None, overwrite=True)
Shortcut for mg_motion to only render motion plots.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filtertype
|
str
|
'Regular' turns all values below |
'Regular'
|
threshold
|
float
|
Eliminates pixel values less than given threshold. Ranges from 0 to 1. Defaults to 0.05. |
0.05
|
blur
|
str
|
'Average' to apply a 10px * 10px blurring filter, 'None' otherwise. Defaults to 'None'. |
'None'
|
kernel_size
|
int
|
Size of structuring element. Defaults to 5. |
5
|
use_median
|
bool
|
If True the algorithm applies a median filter on the thresholded frame-difference stream. Defaults to False. |
False
|
atadenoise
|
bool
|
If True, applies an adaptive temporal averaging denoiser every 129 frames. Defaults to False. |
False
|
motion_analysis
|
str
|
Specify which motion analysis to process or all. 'AoM' renders the Area of Motion. 'CoM' renders the Centroid of Motion. 'QoM' renders the Quantity of Motion. 'all' renders all the motion analysis available. Defaults to 'all'. |
'all'
|
audio_descriptors
|
bool
|
Whether to plot motion plots together with audio descriptors in order to see possible correlations in the data. Defaults to False. |
False
|
unit
|
str
|
Unit in QoM plot. Accepted values are 'seconds' or 'samples'. Defaults to 'seconds'. |
'seconds'
|
title
|
str
|
Optionally add title to the plot. Defaults to None, which uses the file name as a title. |
None
|
target_name
|
str
|
Target output name for the plot. Defaults to None (which assumes that the input filename with the suffix "_motion_com_aom_qom" should be used). |
None
|
overwrite
|
bool
|
Whether to allow overwriting existing files or to automatically increment target filenames to avoid overwriting. Defaults to True. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
MgImage |
'MgImage'
|
An MgImage pointing to the exported image (png) of the motion plots. |
Source code in musicalgestures/_motionvideo.py
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 | |
mg_motionscore ¶
mg_motionscore(self)
Computes the average VMAF motion score of the video using FFmpeg.
Returns:
| Name | Type | Description |
|---|---|---|
float |
'float'
|
The average VMAF motion score, or None if unavailable. |
Source code in musicalgestures/_motionvideo.py
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 | |
save_analysis ¶
save_analysis(of, fps, aom, com, qom, motion_analysis, audio_descriptors, width, height, unit, title, target_name_plot, overwrite)
Helper function to plot the motion data using matplotlib.
Source code in musicalgestures/_motionvideo.py
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 | |
save_txt ¶
save_txt(of, time, aom, com, qom, motion_analysis, width, height, data_format, target_name_data, overwrite, normalize=True)
Helper function to export motion data as textfile(s).
normalize controls the quantity-of-motion column and is the difference
between data for looking at and data for measuring with.
With normalize=True (the default, and what every earlier version did)
the column is Qom, each clip's quantity of motion divided by that clip's
own maximum. Every clip then peaks at exactly 1.0, which is what a plot on
a 0--1 axis wants and is why the value exists: the same expression appears
in the plotting code. It also means quantity of motion is not comparable
between clips -- a small gesture and a violent one both reach 1.0 -- and
that a single bright frame sets the scale for everything around it.
With normalize=False the column is QomRaw, the untouched sum of pixel
values in the thresholded difference frame. Comparable across clips, and
on the scale the pixels actually had. The column is renamed rather than
merely rescaled so that a file cannot be misread: a Qom column is always
per-clip normalised and a QomRaw column never is, whoever opens it and
however long afterwards.
Source code in musicalgestures/_motionvideo.py
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 | |