├── .gitignore ├── LICENSE ├── README.md ├── pdm.lock ├── pyproject.toml ├── src └── offmark │ ├── __init__.py │ ├── common │ ├── __logging.py │ └── __video.py │ ├── degenerator │ ├── de_block_shuffler.py │ ├── de_corr_shuffler.py │ ├── de_grayscale.py │ └── de_shuffler.py │ ├── embed │ ├── dct_encoder.py │ ├── dtcwt_img_encoder.py │ ├── dtcwt_key_encoder.py │ └── dwt_dct_svd_encoder.py │ ├── extract │ ├── dct_decoder.py │ ├── dtcwt_img_decoder.py │ ├── dtcwt_key_decoder.py │ └── dwt_dct_svd_decoder.py │ ├── generator │ ├── block_shuffler.py │ ├── corr_shuffler.py │ ├── grayscale.py │ └── shuffler.py │ └── video │ ├── __init__.py │ ├── embedder.py │ ├── extractor.py │ ├── frame_reader.py │ └── frame_writer.py └── tests ├── detect.py ├── ffmpeg_example.py ├── mark.py ├── media ├── imgs │ └── frame63.jpeg ├── in.mp4 └── wms │ ├── numbers.jpeg │ └── qr.jpeg └── test.py /.gitignore: -------------------------------------------------------------------------------- 1 | **/__pycache__ 2 | build 3 | dist 4 | 5 | # OS 6 | **/.DS_Store 7 | 8 | # PDM 9 | .pdm.toml 10 | 11 | # venv 12 | .venv 13 | 14 | # IntelliJ 15 | .idea 16 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Eluvio, Inc. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # offmark-py 2 | 3 | ## Environment 4 | 5 | ```shell 6 | # Dependencies 7 | pdm install 8 | 9 | # Virtual environment 10 | pdm venv list 11 | eval $(pdm venv activate your_venv_name) 12 | ``` 13 | 14 | ## Run/Test 15 | 16 | ```shell 17 | cd tests 18 | python mark.py 19 | python detect.py 20 | 21 | python test.py 22 | ``` 23 | 24 | ## Build 25 | 26 | ```shell 27 | pdm build 28 | ``` 29 | -------------------------------------------------------------------------------- /pdm.lock: -------------------------------------------------------------------------------- 1 | [[package]] 2 | name = "dtcwt" 3 | version = "0.12.0" 4 | summary = "A port of the Dual-Tree Complex Wavelet Transform MATLAB toolbox." 5 | dependencies = [ 6 | "numpy", 7 | "six", 8 | ] 9 | 10 | [[package]] 11 | name = "ffmpeg-python" 12 | version = "0.2.0" 13 | summary = "Python bindings for FFmpeg - with complex filtering support" 14 | dependencies = [ 15 | "future", 16 | ] 17 | 18 | [[package]] 19 | name = "future" 20 | version = "0.18.2" 21 | requires_python = ">=2.6, !=3.0.*, !=3.1.*, !=3.2.*" 22 | summary = "Clean single-source support for Python 3 and 2" 23 | 24 | [[package]] 25 | name = "numpy" 26 | version = "1.23.3" 27 | requires_python = ">=3.8" 28 | summary = "NumPy is the fundamental package for array computing with Python." 29 | 30 | [[package]] 31 | name = "opencv-python" 32 | version = "4.6.0.66" 33 | requires_python = ">=3.6" 34 | summary = "Wrapper package for OpenCV python bindings." 35 | dependencies = [ 36 | "numpy>=1.14.5; python_version >= \"3.7\"", 37 | "numpy>=1.17.3; python_version >= \"3.8\"", 38 | "numpy>=1.19.3; python_version >= \"3.6\" and platform_system == \"Linux\" and platform_machine == \"aarch64\"", 39 | "numpy>=1.19.3; python_version >= \"3.9\"", 40 | "numpy>=1.21.2; python_version >= \"3.10\"", 41 | "numpy>=1.21.2; python_version >= \"3.6\" and platform_system == \"Darwin\" and platform_machine == \"arm64\"", 42 | ] 43 | 44 | [[package]] 45 | name = "pywavelets" 46 | version = "1.4.1" 47 | requires_python = ">=3.8" 48 | summary = "PyWavelets, wavelet transform module" 49 | dependencies = [ 50 | "numpy>=1.17.3", 51 | ] 52 | 53 | [[package]] 54 | name = "scipy" 55 | version = "1.9.1" 56 | requires_python = ">=3.8,<3.12" 57 | summary = "SciPy: Scientific Library for Python" 58 | dependencies = [ 59 | "numpy<1.25.0,>=1.18.5", 60 | ] 61 | 62 | [[package]] 63 | name = "six" 64 | version = "1.16.0" 65 | requires_python = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*" 66 | summary = "Python 2 and 3 compatibility utilities" 67 | 68 | [metadata] 69 | lock_version = "4.0" 70 | content_hash = "sha256:ad039f1bc50d88d34e4a5e8e1698d95e9dee83b6678fd874bbd8e3d6f7e90760" 71 | 72 | [metadata.files] 73 | "dtcwt 0.12.0" = [ 74 | {url = "https://files.pythonhosted.org/packages/2a/dd/cb52e5b1aebf6b59227f5acc0256426c730a81cfcac4c9ff2fd9f4a45b16/dtcwt-0.12.0.tar.gz", hash = "sha256:57213e75d882cd94c8f95aeda985f7afe40dc783fb9e094da8dfda1c581c9956"}, 75 | ] 76 | "ffmpeg-python 0.2.0" = [ 77 | {url = "https://files.pythonhosted.org/packages/d7/0c/56be52741f75bad4dc6555991fabd2e07b432d333da82c11ad701123888a/ffmpeg_python-0.2.0-py3-none-any.whl", hash = "sha256:ac441a0404e053f8b6a1113a77c0f452f1cfc62f6344a769475ffdc0f56c23c5"}, 78 | {url = "https://files.pythonhosted.org/packages/dd/5e/d5f9105d59c1325759d838af4e973695081fbbc97182baf73afc78dec266/ffmpeg-python-0.2.0.tar.gz", hash = "sha256:65225db34627c578ef0e11c8b1eb528bb35e024752f6f10b78c011f6f64c4127"}, 79 | ] 80 | "future 0.18.2" = [ 81 | {url = "https://files.pythonhosted.org/packages/45/0b/38b06fd9b92dc2b68d58b75f900e97884c45bedd2ff83203d933cf5851c9/future-0.18.2.tar.gz", hash = "sha256:b1bead90b70cf6ec3f0710ae53a525360fa360d306a86583adc6bf83a4db537d"}, 82 | ] 83 | "numpy 1.23.3" = [ 84 | {url = "https://files.pythonhosted.org/packages/07/ef/282bcb710c7e5a6f56a77529e5f8d42ad05ed44f87e65f2771937d5b84aa/numpy-1.23.3-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:e5d5420053bbb3dd64c30e58f9363d7a9c27444c3648e61460c1237f9ec3fa14"}, 85 | {url = "https://files.pythonhosted.org/packages/0a/88/f4f0c7a982efdf7bf22f283acf6009b29a9cc5835b684a49f8d3a4adb22f/numpy-1.23.3.tar.gz", hash = "sha256:51bf49c0cd1d52be0a240aa66f3458afc4b95d8993d2d04f0d91fa60c10af6cd"}, 86 | {url = "https://files.pythonhosted.org/packages/23/a4/0c900aa23c934018f714f1c168e6f615bc70fc26a9a996b06185e6d33665/numpy-1.23.3-cp39-cp39-win_amd64.whl", hash = "sha256:78a63d2df1d947bd9d1b11d35564c2f9e4b57898aae4626638056ec1a231c40c"}, 87 | {url = "https://files.pythonhosted.org/packages/24/3d/b06a0b15aad299c8e53c752b8deaf2431b4f1a4d281bb536019ce4ec2659/numpy-1.23.3-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:8c79d7cf86d049d0c5089231a5bcd31edb03555bd93d81a16870aa98c6cfb79d"}, 88 | {url = "https://files.pythonhosted.org/packages/24/3e/fbbef2c3a04ed1d237fe4711146f111631d02f5155c1dbeb713005787cf5/numpy-1.23.3-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:a64403f634e5ffdcd85e0b12c08f04b3080d3e840aef118721021f9b48fc1460"}, 89 | {url = "https://files.pythonhosted.org/packages/2e/bd/286dacf2655c4db1a5076390337c746452a08def20daa53b4903722545d2/numpy-1.23.3-cp311-cp311-win_amd64.whl", hash = "sha256:8355fc10fd33a5a70981a5b8a0de51d10af3688d7a9e4a34fcc8fa0d7467bb7f"}, 90 | {url = "https://files.pythonhosted.org/packages/3a/0c/8d8fe64dcfbeac1dabeb8ae74c8b697a18cf48adfced980291abcc266984/numpy-1.23.3-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:c9f707b5bb73bf277d812ded9896f9512a43edff72712f31667d0a8c2f8e71ee"}, 91 | {url = "https://files.pythonhosted.org/packages/3b/93/e613ce34c908f3228fa181241ae9505c42a72ffc630af9e5173c2f26f406/numpy-1.23.3-cp38-cp38-macosx_11_0_arm64.whl", hash = "sha256:22d43376ee0acd547f3149b9ec12eec2f0ca4a6ab2f61753c5b29bb3e795ac4d"}, 92 | {url = "https://files.pythonhosted.org/packages/48/71/4a3866a3eedf80a2ca3cd5f56e1f7afdd041b2f0e6339b6a0d8a631f6b28/numpy-1.23.3-cp38-cp38-win32.whl", hash = "sha256:f8c02ec3c4c4fcb718fdf89a6c6f709b14949408e8cf2a2be5bfa9c49548fd85"}, 93 | {url = "https://files.pythonhosted.org/packages/51/b6/861f5e9d59c1bb6c05467f5ddcba965cb2c4b1fd62f6bf7b4c4632492625/numpy-1.23.3-cp310-cp310-win_amd64.whl", hash = "sha256:39a664e3d26ea854211867d20ebcc8023257c1800ae89773cbba9f9e97bae036"}, 94 | {url = "https://files.pythonhosted.org/packages/58/4f/55b0ea97b18e885b67aa41a9929d6a6414da7ddad5ebbd10a9c4ad086640/numpy-1.23.3-cp311-cp311-win32.whl", hash = "sha256:7cd1328e5bdf0dee621912f5833648e2daca72e3839ec1d6695e91089625f0b4"}, 95 | {url = "https://files.pythonhosted.org/packages/59/ec/57f87fe9dc2f8390edd1341d2ee9caa90c251f09524286476f536555ffc1/numpy-1.23.3-cp38-cp38-win_amd64.whl", hash = "sha256:e868b0389c5ccfc092031a861d4e158ea164d8b7fdbb10e3b5689b4fc6498df6"}, 96 | {url = "https://files.pythonhosted.org/packages/75/25/196d280e3570e19bc9c553af6941b13289ff520069bffa047a80b47e549a/numpy-1.23.3-cp310-cp310-win32.whl", hash = "sha256:98dcbc02e39b1658dc4b4508442a560fe3ca5ca0d989f0df062534e5ca3a5c1a"}, 97 | {url = "https://files.pythonhosted.org/packages/80/d9/29eb382c47203f3ee7a758eb1e2620daad08c3f74372a752b2965d8d8c7a/numpy-1.23.3-pp38-pypy38_pp73-macosx_10_9_x86_64.whl", hash = "sha256:17c0e467ade9bda685d5ac7f5fa729d8d3e76b23195471adae2d6a6941bd2c18"}, 98 | {url = "https://files.pythonhosted.org/packages/81/03/6b9e924e39d90a67a7c01952b5768818ed24f888b0da9f333ad2246a3514/numpy-1.23.3-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:09f6b7bdffe57fc61d869a22f506049825d707b288039d30f26a0d0d8ea05164"}, 99 | {url = "https://files.pythonhosted.org/packages/89/68/0400fdd510bc2e22aa658873110a525452de90bd1058eb55183438d8527b/numpy-1.23.3-cp39-cp39-win32.whl", hash = "sha256:c1ba66c48b19cc9c2975c0d354f24058888cdc674bebadceb3cdc9ec403fb5d1"}, 100 | {url = "https://files.pythonhosted.org/packages/8d/51/bfaf6dc8279c58fcd8234e9b0f0b86146e3a8814159cd30875154f743061/numpy-1.23.3-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:1f27b5322ac4067e67c8f9378b41c746d8feac8bdd0e0ffede5324667b8a075c"}, 101 | {url = "https://files.pythonhosted.org/packages/98/e0/481ed31801a69089aac50fe57229f8396b1d9cf4c85054275f9c909b13d9/numpy-1.23.3-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:0ea3f98a0ffce3f8f57675eb9119f3f4edb81888b6874bc1953f91e0b1d4f440"}, 102 | {url = "https://files.pythonhosted.org/packages/aa/a7/92b7d2698d7deabd0b846c3ad487495ef3af1dceac8a1f2b358cd20caecf/numpy-1.23.3-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:91b8d6768a75247026e951dce3b2aac79dc7e78622fc148329135ba189813584"}, 103 | {url = "https://files.pythonhosted.org/packages/b1/84/0af94541d21dd2d403377209f462b6b463dc4ba15158776285f1af2132ac/numpy-1.23.3-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:2ad3ec9a748a8943e6eb4358201f7e1c12ede35f510b1a2221b70af4bb64295c"}, 104 | {url = "https://files.pythonhosted.org/packages/c6/fd/9c3a4e030858115bfee2f81351c4614c475883cb97a3297d8de039dac046/numpy-1.23.3-pp38-pypy38_pp73-win_amd64.whl", hash = "sha256:94c15ca4e52671a59219146ff584488907b1f9b3fc232622b47e2cf832e94fb8"}, 105 | {url = "https://files.pythonhosted.org/packages/c7/31/0298a8f62a8c82b8c542f78f3761e67cb8bf0450b3e61bbe66c5c54c1a81/numpy-1.23.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:004f0efcb2fe1c0bd6ae1fcfc69cc8b6bf2407e0f18be308612007a0762b4089"}, 106 | {url = "https://files.pythonhosted.org/packages/d2/09/9ab4e760206c10081d253db9a3d4db8fd040ffcec9d7cdf5376d99531d54/numpy-1.23.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:301c00cf5e60e08e04d842fc47df641d4a181e651c7135c50dc2762ffe293dbd"}, 107 | {url = "https://files.pythonhosted.org/packages/d6/e2/bed33bdbf513cd6d3fcb4377792ef1b8aad941da542a191e1e2a98c6621f/numpy-1.23.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:efd9d3abe5774404becdb0748178b48a218f1d8c44e0375475732211ea47c67e"}, 108 | {url = "https://files.pythonhosted.org/packages/d7/da/417e298fd3998ed1df4b492b400757ec331c310d4d6cbf5a2f4aa93717e8/numpy-1.23.3-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:ffcf105ecdd9396e05a8e58e81faaaf34d3f9875f137c7372450baa5d77c9a54"}, 109 | {url = "https://files.pythonhosted.org/packages/df/5b/7f03caa84950cf02f841c910a249ae526a858de9c87ef4357cfb723b02b1/numpy-1.23.3-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:bdc9febce3e68b697d931941b263c59e0c74e8f18861f4064c1f712562903411"}, 110 | {url = "https://files.pythonhosted.org/packages/e5/0e/c76c8cd19fa0477742e553471373e69f4aeb228b3b18aaac305a66cd5f5b/numpy-1.23.3-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:bc6e8da415f359b578b00bcfb1d08411c96e9a97f9e6c7adada554a0812a6cc6"}, 111 | {url = "https://files.pythonhosted.org/packages/fe/8c/1dfc141202fb2b6b75f9ca4a6681eb5cb7f328d6d2d9d186e5675c468e6a/numpy-1.23.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:d5422d6a1ea9b15577a9432e26608c73a78faf0b9039437b075cf322c92e98e7"}, 112 | ] 113 | "opencv-python 4.6.0.66" = [ 114 | {url = "https://files.pythonhosted.org/packages/12/5d/1527327b9f7ea13bef31377f8bf399f03dc5f4f1c9f1fb69bc56b6e24cd4/opencv_python-4.6.0.66-cp36-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:5af8ba35a4fcb8913ffb86e92403e9a656a4bff4a645d196987468f0f8947875"}, 115 | {url = "https://files.pythonhosted.org/packages/7a/1c/e57fe138615c29b8d1442009057dd420c58d4773cb16112e914c9e7b47d8/opencv-python-4.6.0.66.tar.gz", hash = "sha256:c5bfae41ad4031e66bb10ec4a0a2ffd3e514d092652781e8b1ac98d1b59f1158"}, 116 | {url = "https://files.pythonhosted.org/packages/af/bf/8d189a5c43460f6b5c8eb81ead8732e94b9f73ef8d9abba9e8f5a61a6531/opencv_python-4.6.0.66-cp36-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:dbdc84a9b4ea2cbae33861652d25093944b9959279200b7ae0badd32439f74de"}, 117 | {url = "https://files.pythonhosted.org/packages/bc/71/4575227302db0b95bbf635dd87f2c58339f84c6e63ade1afc7d332414da2/opencv_python-4.6.0.66-cp36-abi3-macosx_10_15_x86_64.whl", hash = "sha256:e6e448b62afc95c5b58f97e87ef84699e6607fe5c58730a03301c52496005cae"}, 118 | {url = "https://files.pythonhosted.org/packages/c6/00/858e894a2127b3a1b5479812b1bd3669bca64175dff4fc7c778e0a6ee565/opencv_python-4.6.0.66-cp37-abi3-macosx_11_0_arm64.whl", hash = "sha256:6e32af22e3202748bd233ed8f538741876191863882eba44e332d1a34993165b"}, 119 | {url = "https://files.pythonhosted.org/packages/cf/09/b24c266cd61ddeed101b90c92a26f54d060b06f4a1b102eb891576d6e9e2/opencv_python-4.6.0.66-cp36-abi3-win_amd64.whl", hash = "sha256:0dc82a3d8630c099d2f3ac1b1aabee164e8188db54a786abb7a4e27eba309440"}, 120 | {url = "https://files.pythonhosted.org/packages/fb/45/60eaff54776b73a4c69bca7669fac5c5fe61b03b80170cf0e01c2ffd846d/opencv_python-4.6.0.66-cp36-abi3-win32.whl", hash = "sha256:f482e78de6e7b0b060ff994ffd859bddc3f7f382bb2019ef157b0ea8ca8712f5"}, 121 | ] 122 | "pywavelets 1.4.1" = [ 123 | {url = "https://files.pythonhosted.org/packages/02/15/89951f559601fb6755f2231558c33c1b9cbba9e8526906cbc258e27eb53d/PyWavelets-1.4.1-cp39-cp39-win_amd64.whl", hash = "sha256:88aa5449e109d8f5e7f0adef85f7f73b1ab086102865be64421a3a3d02d277f4"}, 124 | {url = "https://files.pythonhosted.org/packages/07/fe/90ab3b98dfeb2177e1b8c8ccdd4e777e35dfe0aa98723308bd8f1a97fd47/PyWavelets-1.4.1-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:754fa5085768227c4f4a26c1e0c78bc509a266d9ebd0eb69a278be7e3ece943c"}, 125 | {url = "https://files.pythonhosted.org/packages/0d/72/db0ef5ca311627f86de89a7af6055301c67490f4160e725cdbd32eea7700/PyWavelets-1.4.1-cp39-cp39-win32.whl", hash = "sha256:47cac4fa25bed76a45bc781a293c26ac63e8eaae9eb8f9be961758d22b58649c"}, 126 | {url = "https://files.pythonhosted.org/packages/13/e4/86bb218c7926e1da7a52e0696cab120a17c995933f08d8228d9aa83b44c5/PyWavelets-1.4.1-cp311-cp311-macosx_10_13_x86_64.whl", hash = "sha256:64c6bac6204327321db30b775060fbe8e8642316e6bff17f06b9f34936f88875"}, 127 | {url = "https://files.pythonhosted.org/packages/1d/5e/97ff80a20fb22f723f0c3f6f5f407b12579a560abf7c3a8087d052993dd9/PyWavelets-1.4.1-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:ad987748f60418d5f4138db89d82ba0cb49b086e0cbb8fd5c3ed4a814cfb705e"}, 128 | {url = "https://files.pythonhosted.org/packages/1d/a1/0f9356779440aaaa35ff82479c40a094419f19ab94a3d5f49e090398959b/PyWavelets-1.4.1-cp311-cp311-win32.whl", hash = "sha256:7231461d7a8eb3bdc7aa2d97d9f67ea5a9f8902522818e7e2ead9c2b3408eeb1"}, 129 | {url = "https://files.pythonhosted.org/packages/2f/52/080267790e23a5186185f2c26d7b774cee754387d1bcb116c7a45f3546f6/PyWavelets-1.4.1-cp38-cp38-macosx_10_13_x86_64.whl", hash = "sha256:ab7da0a17822cd2f6545626946d3b82d1a8e106afc4b50e3387719ba01c7b966"}, 130 | {url = "https://files.pythonhosted.org/packages/34/c0/a121306b618af45ff7d769e1bd45ed3d6c798dc7f0094e0b56735388d96e/PyWavelets-1.4.1-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:030670a213ee8fefa56f6387b0c8e7d970c7f7ad6850dc048bd7c89364771b9b"}, 131 | {url = "https://files.pythonhosted.org/packages/35/12/f1a4f72b5d71497e4200e71e253cc747077d8570b55693faaa7b81fb6dff/PyWavelets-1.4.1-cp310-cp310-win_amd64.whl", hash = "sha256:91d3d393cffa634f0e550d88c0e3f217c96cfb9e32781f2960876f1808d9b45b"}, 132 | {url = "https://files.pythonhosted.org/packages/3e/fc/651024e8b6e69bef6def2cbe27d520309f4ffc56b8d4885ab7046e1edc6c/PyWavelets-1.4.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:da7b9c006171be1f9ddb12cc6e0d3d703b95f7f43cb5e2c6f5f15d3233fcf202"}, 133 | {url = "https://files.pythonhosted.org/packages/50/92/a78bf0c3d84afd9b17727cce122c3fdb3860a27bd67b32448c7e64301e7b/PyWavelets-1.4.1-cp310-cp310-macosx_10_13_x86_64.whl", hash = "sha256:d854411eb5ee9cb4bc5d0e66e3634aeb8f594210f6a1bed96dbed57ec70f181c"}, 134 | {url = "https://files.pythonhosted.org/packages/51/af/53bcfea50c24cedb202b0c072193af94a1a611b26ab360082791e455b43f/PyWavelets-1.4.1-cp310-cp310-win32.whl", hash = "sha256:67a0d28a08909f21400cb09ff62ba94c064882ffd9e3a6b27880a111211d59bd"}, 135 | {url = "https://files.pythonhosted.org/packages/5a/98/4549479a32972bdfdd5e75e168219e97f4dfaee535a8308efef7291e8398/PyWavelets-1.4.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:71ab30f51ee4470741bb55fc6b197b4a2b612232e30f6ac069106f0156342356"}, 136 | {url = "https://files.pythonhosted.org/packages/6c/92/7e900e574575358a5af6ad9f8378d889b1a21e2ba835bae9d0eb7efd505b/PyWavelets-1.4.1-cp38-cp38-win32.whl", hash = "sha256:de7cd61a88a982edfec01ea755b0740e94766e00a1ceceeafef3ed4c85c605cd"}, 137 | {url = "https://files.pythonhosted.org/packages/6e/d4/008dceeb95fafcf141f39393bdfc10921d0b62a325c2794ac533195a1eb3/PyWavelets-1.4.1.tar.gz", hash = "sha256:6437af3ddf083118c26d8f97ab43b0724b956c9f958e9ea788659f6a2834ba93"}, 138 | {url = "https://files.pythonhosted.org/packages/73/8c/6d50b8e2ee4d12373a63791ad742df1e30ddd5f0f8d1c000c5b6b3afb2c9/PyWavelets-1.4.1-cp38-cp38-macosx_11_0_arm64.whl", hash = "sha256:578af438a02a86b70f1975b546f68aaaf38f28fb082a61ceb799816049ed18aa"}, 139 | {url = "https://files.pythonhosted.org/packages/88/4b/b2b2a6f51e47c091c221bfde976a01a7e5f20e7e5e6341b2b9c4db73d2ed/PyWavelets-1.4.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:058b46434eac4c04dd89aeef6fa39e4b6496a951d78c500b6641fd5b2cc2f9f4"}, 140 | {url = "https://files.pythonhosted.org/packages/94/73/4df43d2e18e68c7ea88177c1fa14a25b5813a51b4953dc94c21f2de039d5/PyWavelets-1.4.1-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:3f19327f2129fb7977bc59b966b4974dfd72879c093e44a7287500a7032695de"}, 141 | {url = "https://files.pythonhosted.org/packages/9f/67/33b37d53da9d225301e30894db5083569aa670b446253b3906fc0e96119e/PyWavelets-1.4.1-cp39-cp39-macosx_10_13_x86_64.whl", hash = "sha256:23bafd60350b2b868076d976bdd92f950b3944f119b4754b1d7ff22b7acbf6c6"}, 142 | {url = "https://files.pythonhosted.org/packages/a0/32/eeeaa4de640a84e2cc35c25aea289367059abce0cac84a9987b139a2a25f/PyWavelets-1.4.1-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:d0e56cd7a53aed3cceca91a04d62feb3a0aca6725b1912d29546c26f6ea90426"}, 143 | {url = "https://files.pythonhosted.org/packages/a9/8f/f80ff31e73385b886c35fb9fb1377849f9c43a3c1195ed8dc8ed8dc1bd88/PyWavelets-1.4.1-cp38-cp38-win_amd64.whl", hash = "sha256:7ab8d9db0fe549ab2ee0bea61f614e658dd2df419d5b75fba47baa761e95f8f2"}, 144 | {url = "https://files.pythonhosted.org/packages/cd/c1/132756d0033b37f4013299ac048bf34d5094673712984edb9e90e8d8a179/PyWavelets-1.4.1-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:9cb5ca8d11d3f98e89e65796a2125be98424d22e5ada360a0dbabff659fca0fc"}, 145 | {url = "https://files.pythonhosted.org/packages/de/a1/cd8a30e061f858f219364554b19d4318276c677a51d956c55fb0b134e8b2/PyWavelets-1.4.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:875d4d620eee655346e3589a16a73790cf9f8917abba062234439b594e706784"}, 146 | {url = "https://files.pythonhosted.org/packages/e4/13/9a1632347677e1be27900d9dc922f19bc01440eb8b0c663cea63b35275fc/PyWavelets-1.4.1-cp311-cp311-win_amd64.whl", hash = "sha256:daf0aa79842b571308d7c31a9c43bc99a30b6328e6aea3f50388cd8f69ba7dbc"}, 147 | {url = "https://files.pythonhosted.org/packages/f3/66/2bbcad043383d7be3bca2155972adba1d06be3bc5536afbfa22f1cd99688/PyWavelets-1.4.1-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:231b0e0b1cdc1112f4af3c24eea7bf181c418d37922a67670e9bf6cfa2d544d4"}, 148 | ] 149 | "scipy 1.9.1" = [ 150 | {url = "https://files.pythonhosted.org/packages/03/80/710038ffb14b8d59add3447eaeaffd31177d38a4869806c1e70473773a83/scipy-1.9.1-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:47d1a95bd9d37302afcfe1b84c8011377c4f81e33649c5a5785db9ab827a6ade"}, 151 | {url = "https://files.pythonhosted.org/packages/25/ca/92ab7808944ccfb5847fa51d892948229bf1d4f2a2ef47821c99e5f76b06/scipy-1.9.1-cp38-cp38-macosx_12_0_arm64.whl", hash = "sha256:eb954f5aca4d26f468bbebcdc5448348eb287f7bea536c6306f62ea062f63d9a"}, 152 | {url = "https://files.pythonhosted.org/packages/26/a0/f2b56f2404a47c225d5ad51a2c46f8ca673524800d41b14473c421d0924a/scipy-1.9.1-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:39ab9240cd215a9349c85ab908dda6d732f7d3b4b192fa05780812495536acc4"}, 153 | {url = "https://files.pythonhosted.org/packages/2e/e5/0e7e044d2ce9c7ebb18c3ea3c5774626780920dd42b48d0292017f7b6eff/scipy-1.9.1-cp38-cp38-win_amd64.whl", hash = "sha256:e8fe305d9d67a81255e06203454729405706907dccbdfcc330b7b3482a6c371d"}, 154 | {url = "https://files.pythonhosted.org/packages/31/ed/88f65e7007146c79d8fc04cc86112c6a449578a01cea3f1d98fbbf8cac71/scipy-1.9.1-cp39-cp39-win_amd64.whl", hash = "sha256:90c805f30c46cf60f1e76e947574f02954d25e3bb1e97aa8a07bc53aa31cf7d1"}, 155 | {url = "https://files.pythonhosted.org/packages/35/9b/9a817e981d0faa5b066f08b531e435e87ccc11663e907938218100ad3fdd/scipy-1.9.1-cp39-cp39-manylinux_2_12_i686.manylinux2010_i686.whl", hash = "sha256:f7c39f7dbb57cce00c108d06d731f3b0e2a4d3a95c66d96bce697684876ce4d4"}, 156 | {url = "https://files.pythonhosted.org/packages/45/a7/68b046344399f7e64b2fdaf57cd62af6db01cbce69300f6f6abb6878e6e0/scipy-1.9.1-cp39-cp39-macosx_12_0_universal2.macosx_10_9_x86_64.whl", hash = "sha256:3bc1ab68b9a096f368ba06c3a5e1d1d50957a86665fc929c4332d21355e7e8f4"}, 157 | {url = "https://files.pythonhosted.org/packages/59/0b/ad6e48b327d475da8ca567b9af13693a9d671818bc36d4c54481966f7435/scipy-1.9.1-cp38-cp38-macosx_12_0_universal2.macosx_10_9_x86_64.whl", hash = "sha256:3c6f5d1d4b9a5e4fe5e14f26ffc9444fc59473bbf8d45dc4a9a15283b7063a72"}, 158 | {url = "https://files.pythonhosted.org/packages/61/7b/3b169b59780e4e4e5e68be849f655c10c91b207a9467ed65767d4544e043/scipy-1.9.1-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:0419485dbcd0ed78c0d5bf234c5dd63e86065b39b4d669e45810d42199d49521"}, 159 | {url = "https://files.pythonhosted.org/packages/6f/7c/aa0abf51b2f68b97135e5a25938c4040a718a181b8d4602d0e2df3915e5a/scipy-1.9.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:34441dfbee5b002f9e15285014fd56e5e3372493c3e64ae297bae2c4b9659f5a"}, 160 | {url = "https://files.pythonhosted.org/packages/86/37/503d622ac36588f8474a634d83a14d3898e1c1ff19a42db809956e4e8e3e/scipy-1.9.1-cp39-cp39-win32.whl", hash = "sha256:09412eb7fb60b8f00b328037fd814d25d261066ebc43a1e339cdce4f7502877e"}, 161 | {url = "https://files.pythonhosted.org/packages/8b/1a/2f0481c9b54c1f48dcad24f4ab4840518edbc33a6a19a8321dda1447b9dd/scipy-1.9.1-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:7a412c476a91b080e456229e413792bbb5d6202865dae963d1e6e28c2bb58691"}, 162 | {url = "https://files.pythonhosted.org/packages/8e/fe/d4e35d6166ba578f7af91b6d40bc9f388689f9e773378c7ea7f4f8cf962d/scipy-1.9.1-cp310-cp310-macosx_12_0_universal2.macosx_10_9_x86_64.whl", hash = "sha256:825951b88f56765aeb6e5e38ac9d7d47407cfaaeb008d40aa1b45a2d7ea2731e"}, 163 | {url = "https://files.pythonhosted.org/packages/91/03/fa8613db8fa211e486fbf18c64103f53e98bddcfe3678701f42a95887690/scipy-1.9.1-cp310-cp310-macosx_12_0_arm64.whl", hash = "sha256:d79da472015d0120ba9b357b28a99146cd6c17b9609403164b1a8ed149b4dfc8"}, 164 | {url = "https://files.pythonhosted.org/packages/a9/17/67861cb65190a28e726e5f99f8938756385e8b2257cbae2b13e58594ae27/scipy-1.9.1-cp310-cp310-win_amd64.whl", hash = "sha256:8d3faa40ac16c6357aaf7ea50394ea6f1e8e99d75e927a51102b1943b311b4d9"}, 165 | {url = "https://files.pythonhosted.org/packages/bc/6a/b2f14bf7e1f9db84a5a5c692b9883ae19968feee532036534850088006a9/scipy-1.9.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:96d7cf7b25c9f23c59a766385f6370dab0659741699ecc7a451f9b94604938ce"}, 166 | {url = "https://files.pythonhosted.org/packages/c0/d7/5d6a7a36fc84f2d87e436e63f0406bb93d999b37a8c68be5e0587e95d80e/scipy-1.9.1-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:f950a04b33e17b38ff561d5a0951caf3f5b47caa841edd772ffb7959f20a6af0"}, 167 | {url = "https://files.pythonhosted.org/packages/c2/89/37b6e11bfe24e96a375fc39e6ffb6c2f27ff795cfb735ae83130e0bf78b5/scipy-1.9.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:8cc81ac25659fec73599ccc52c989670e5ccd8974cf34bacd7b54a8d809aff1a"}, 168 | {url = "https://files.pythonhosted.org/packages/cc/76/7ee1ae8709033402de63871cae0b04537ab7577b7b9ad3e367f4dd4b3796/scipy-1.9.1-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:c61b4a91a702e8e04aeb0bfc40460e1f17a640977c04dda8757efb0199c75332"}, 169 | {url = "https://files.pythonhosted.org/packages/cd/d8/ee6a26bb696499d0150fceac75b9588575258e05f03069a41744929a99b8/scipy-1.9.1-cp38-cp38-manylinux_2_12_i686.manylinux2010_i686.whl", hash = "sha256:bc4e2c77d4cd015d739e75e74ebbafed59ba8497a7ed0fd400231ed7683497c4"}, 170 | {url = "https://files.pythonhosted.org/packages/db/af/16906139f52bc6866c43401869ce247662739ad71afa11c6f18505eb0546/scipy-1.9.1.tar.gz", hash = "sha256:26d28c468900e6d5fdb37d2812ab46db0ccd22c63baa095057871faa3a498bc9"}, 171 | {url = "https://files.pythonhosted.org/packages/f1/e0/85e231371444e7765a194c24ccecc40431b651846949beaa8feaaeb6d270/scipy-1.9.1-cp38-cp38-win32.whl", hash = "sha256:b97b479f39c7e4aaf807efd0424dec74bbb379108f7d22cf09323086afcd312c"}, 172 | {url = "https://files.pythonhosted.org/packages/fe/de/caec3ae06f5380b8c91518f119f9b113a2da0619f7d8d8937b8ee517a29e/scipy-1.9.1-cp39-cp39-macosx_12_0_arm64.whl", hash = "sha256:71487c503e036740635f18324f62a11f283a632ace9d35933b2b0a04fd898c98"}, 173 | ] 174 | "six 1.16.0" = [ 175 | {url = "https://files.pythonhosted.org/packages/71/39/171f1c67cd00715f190ba0b100d606d440a28c93c7714febeca8b79af85e/six-1.16.0.tar.gz", hash = "sha256:1e61c37477a1626458e36f7b1d82aa5c9b094fa4802892072e49de9c60c4c926"}, 176 | {url = "https://files.pythonhosted.org/packages/d9/5a/e7c31adbe875f2abbb91bd84cf2dc52d792b5a01506781dbcf25c91daf11/six-1.16.0-py2.py3-none-any.whl", hash = "sha256:8abb2f1d86890a2dfb989f9a77cfcfd3e47c2a354b01111771326f8aa26e0254"}, 177 | ] 178 | -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | [project] 2 | name = "offmark-py" 3 | version = "0.1.0" 4 | description = "The Open Framework for Forensic Watermarking" 5 | authors = [ 6 | { name = "Qingyuan Liu", email = "106697538+elv-lqy@users.noreply.github.com" }, 7 | { name = "Peter Tseng", email = "42591958+elv-peter@users.noreply.github.com" }, 8 | ] 9 | dependencies = [ 10 | "numpy>=1.23.3", 11 | "opencv-python>=4.6.0.66", 12 | "pywavelets>=1.4.1", 13 | "dtcwt>=0.12.0", 14 | "scipy>=1.9.1", 15 | "ffmpeg-python>=0.2.0", 16 | ] 17 | requires-python = ">=3.10,<3.12" 18 | readme = "README.md" 19 | license = { text = "MIT" } 20 | [project.optional-dependencies] 21 | 22 | [build-system] 23 | requires = ["pdm-pep517>=1.0.0"] 24 | build-backend = "pdm.pep517.api" 25 | 26 | 27 | [tool] 28 | [tool.pdm] 29 | [[tool.pdm.source]] 30 | url = "https://pypi.org/simple" 31 | verify_ssl = true 32 | name = "pypi" 33 | 34 | [tool.pdm.dev-dependencies] 35 | dev = [] 36 | -------------------------------------------------------------------------------- /src/offmark/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eluv-io/offmark-py/b8bc2ff652fec1b96cd5c56ca31863d1620d2518/src/offmark/__init__.py -------------------------------------------------------------------------------- /src/offmark/common/__logging.py: -------------------------------------------------------------------------------- 1 | import logging 2 | 3 | logger = logging.getLogger(__name__) 4 | 5 | 6 | def trace(module_logger): 7 | def decorator(fn): 8 | def inner(*args, **kwargs): 9 | module_logger.debug(f'Entering {fn.__name__}()') # print args? 10 | result = fn(*args, **kwargs) 11 | # Exiting 12 | return result 13 | 14 | return inner 15 | 16 | return decorator 17 | -------------------------------------------------------------------------------- /src/offmark/common/__video.py: -------------------------------------------------------------------------------- 1 | import logging 2 | import pprint 3 | 4 | import ffmpeg 5 | 6 | from .__logging import trace 7 | 8 | logger = logging.getLogger(__name__) 9 | 10 | 11 | @trace(logger) 12 | def probe(video_file): 13 | info = ffmpeg.probe(video_file) 14 | logger.debug(pprint.pformat(info)) 15 | 16 | video_info = next(s for s in info['streams'] if s['codec_type'] == 'video') 17 | d = { 18 | 'width': int(video_info['width']), 19 | 'height': int(video_info['height']) 20 | } 21 | logger.info(f'Probed video: {d}') 22 | 23 | return d 24 | -------------------------------------------------------------------------------- /src/offmark/degenerator/de_block_shuffler.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import cv2 3 | 4 | class DeBlockShuffler: 5 | 6 | def __init__(self, key=None, blk_shape=(35, 30)): 7 | self.key = key 8 | self.blk_shape = blk_shape 9 | 10 | def set_shape(self, payload_shape): 11 | self.payload_shape = payload_shape 12 | return self 13 | 14 | def degenerate(self, wm, shape=(135, 240)): 15 | wm_shape = wm.shape 16 | wm = wm.astype(np.float32) 17 | wm = cv2.resize(wm, (shape[1], shape[0])) 18 | wm = self.derandomize_channel(wm, self.key, blk_shape=self.blk_shape) 19 | wm = cv2.resize(wm, (self.payload_shape[1], self.payload_shape[0])) 20 | return wm 21 | 22 | def derandomize_channel(self, channel, key, blk_shape=(8, 8)): 23 | rows = channel.shape[0] // blk_shape[0] * blk_shape[0] 24 | cols = channel.shape[1] // blk_shape[1] * blk_shape[1] 25 | blks = np.array([[ 26 | channel[i:i + blk_shape[0], j:j + blk_shape[1]] 27 | for j in range(0, cols, blk_shape[1]) 28 | ] for i in range(0, rows, blk_shape[0])]) 29 | shape = blks.shape 30 | blks = blks.reshape(-1, blk_shape[0], blk_shape[1]) 31 | blk_num = blks.shape[0] 32 | indices = np.arange(blk_num) 33 | np.random.RandomState(key).shuffle(indices) 34 | res = np.zeros(blks.shape) 35 | res[indices] = blks 36 | res = np.concatenate(np.concatenate(res.reshape(shape), 1), 1) 37 | full_res = np.copy(channel) 38 | full_res[:rows, :cols] = res 39 | return full_res 40 | -------------------------------------------------------------------------------- /src/offmark/degenerator/de_corr_shuffler.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import cv2 3 | from scipy.signal import correlate2d 4 | 5 | class DeCorrShuffler: 6 | 7 | def __init__(self, key=None): 8 | self.key = key 9 | 10 | def set_shape(self, payload_shape): 11 | return self 12 | 13 | def degenerate(self, wm, mode="fast", shape=(1080, 1920)): 14 | wmk = np.random.RandomState(self.key).randint(0, 2, shape).astype(np.float32) 15 | wmk[wmk == 0] = -1 16 | wmk = cv2.resize(wmk, (wm.shape[1], wm.shape[0])) 17 | shape = wm.shape[0] * wm.shape[1] 18 | if mode == "fast": 19 | nwm = (wm - np.mean(wm)) / np.std(wm) 20 | nwmk = (wmk - np.mean(wmk)) / np.std(wmk) 21 | corr = np.sum(nwm * nwmk) / shape 22 | elif mode == "slow": 23 | c = correlate2d(wm, wmk) / shape 24 | idx = np.unravel_index(c.argmax(), c.shape) 25 | corr = c[idx] 26 | print("Correlation: ", corr) 27 | if corr > 0.1: 28 | return True 29 | else: 30 | return False 31 | -------------------------------------------------------------------------------- /src/offmark/degenerator/de_grayscale.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | class DeGrayScale: 4 | 5 | def __init__(self, key=None): 6 | self.key = key 7 | 8 | def set_shape(self, payload_shape): 9 | self.payload_shape = payload_shape 10 | self.payload_len = np.array(payload_shape).prod() 11 | self.payload_idx = np.arange(self.payload_len) 12 | np.random.RandomState(self.key).shuffle(self.payload_idx) 13 | return self 14 | 15 | def degenerate(self, wm_bits): 16 | wm = wm_bits.flatten() 17 | payload = np.zeros(shape=self.payload_len) 18 | for i in range(self.payload_len): 19 | payload[i] = wm[i::self.payload_len].mean() 20 | payload[self.payload_idx] = payload.copy() 21 | threshold = 0.5 * (np.max(payload) + np.min(payload)) 22 | res = (payload > threshold).astype(np.uint8) * 255 23 | return res.reshape(self.payload_shape) 24 | -------------------------------------------------------------------------------- /src/offmark/degenerator/de_shuffler.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | class DeShuffler: 4 | 5 | def __init__(self, key=None): 6 | self.key = key 7 | 8 | def set_shape(self, payload_shape): 9 | self.payload_len = np.array(payload_shape).prod() 10 | self.payload_idx = np.arange(self.payload_len) 11 | np.random.RandomState(self.key).shuffle(self.payload_idx) 12 | return self 13 | 14 | def degenerate(self, wm): 15 | wm_bits = wm.flatten() 16 | payload = np.zeros(shape=self.payload_len) 17 | for i in range(self.payload_len): 18 | payload[i] = wm_bits[i::self.payload_len].mean() 19 | payload[self.payload_idx] = payload.copy() 20 | threshold = 0.5 * (np.max(payload) + np.min(payload)) 21 | res = (payload > threshold).astype(np.uint8) 22 | return res 23 | -------------------------------------------------------------------------------- /src/offmark/embed/dct_encoder.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | class DctEncoder: 5 | 6 | def __init__(self, key=None, alpha=20): 7 | self.key = key 8 | self.alpha = alpha 9 | 10 | def read_wm(self, wm): 11 | self.wm = wm[0] 12 | 13 | def wm_capacity(self, frame_shape): 14 | row, col, channels = frame_shape 15 | block_num = row * col // 64 16 | return (1, block_num) 17 | 18 | def encode(self, yuv): 19 | blk_shape = (8, 8) 20 | channel = yuv[:,:,1] 21 | lum_mask = self.luminance_mask(yuv[:,:,0]) 22 | tex_mask = self.texture_mask(yuv[:,:,0]) 23 | mask = tex_mask * lum_mask 24 | c = 0 25 | for i in range(channel.shape[0] // blk_shape[0]): 26 | for j in range(channel.shape[1] // blk_shape[1]): 27 | blk = channel[i * blk_shape[0] : i * blk_shape[0] + blk_shape[0], 28 | j * blk_shape[1] : j * blk_shape[1] + blk_shape[1]] 29 | coeffs = cv2.dct(blk) 30 | step = self.alpha * mask[i][j] 31 | step2 = step + step 32 | if self.wm[c] == 0: 33 | coeffs[2][1] = np.sign(coeffs[2][1]) * np.floor(abs(coeffs[2][1]) / step2) * step2 34 | else: 35 | coeffs[2][1] = np.sign(coeffs[2][1]) * (np.floor(abs(coeffs[2][1]) / step2) * step2 + step) 36 | channel[i * blk_shape[0] : i * blk_shape[0] + blk_shape[0], 37 | j * blk_shape[1] : j * blk_shape[1] + blk_shape[1]] = cv2.idct(coeffs) 38 | c += 1 39 | return yuv 40 | 41 | def luminance_mask(self, lum): 42 | blk_shape = (8, 8) 43 | rows = lum.shape[0] // blk_shape[0] 44 | cols = lum.shape[1] // blk_shape[1] 45 | mask = np.zeros((rows, cols)) 46 | for i in range(rows): 47 | for j in range(cols): 48 | blk = lum[i * blk_shape[0]:i * blk_shape[0] + blk_shape[0], 49 | j * blk_shape[1]:j * blk_shape[1] + blk_shape[1]] 50 | coeffs = cv2.dct(blk) 51 | mask[i][j] = coeffs[0][0] 52 | l_min, l_max = 90, 255 53 | f_max = 2 54 | mask /= 8 55 | mean = max(l_min, np.mean(mask)) 56 | f_ref = 1 + (mean - l_min) * (f_max - 1) / (l_max - l_min) 57 | for i in range(mask.shape[0]): 58 | for j in range(mask.shape[1]): 59 | if mask[i][j] > mean: 60 | mask[i][j] = 1 + (mask[i][j] - mean) / (l_max - mean) * (f_max - f_ref) 61 | elif mask[i][j] < 15: 62 | mask[i][j] = 1.25 63 | elif mask[i][j] < 25: 64 | mask[i][j] = 1.125 65 | else: 66 | mask[i][j] = 1 67 | return mask 68 | 69 | 70 | def texture_mask(self, lum): 71 | blk_shape = (8, 8) 72 | rows = lum.shape[0] // blk_shape[0] 73 | cols = lum.shape[1] // blk_shape[1] 74 | mask = np.full((rows, cols), 1.0) 75 | for i in range(rows): 76 | for j in range(cols): 77 | blk = lum[i * blk_shape[0]:i * blk_shape[0] + blk_shape[0], 78 | j * blk_shape[1]:j * blk_shape[1] + blk_shape[1]] 79 | coeffs = cv2.dct(blk) 80 | coeffs = np.abs(coeffs) 81 | dcl = coeffs[0][0] + coeffs[0][1] + coeffs[0][2] + coeffs[1][0] + coeffs[1][1] + coeffs[2][0] 82 | eh = np.sum(coeffs) - dcl 83 | if eh > 125: 84 | e = coeffs[3][0] + coeffs[4][0] + coeffs[5][0] + coeffs[6][0] + \ 85 | coeffs[0][3] + coeffs[0][4] + coeffs[0][5] + coeffs[0][6] + \ 86 | coeffs[2][1] + coeffs[1][2] + coeffs[2][2] + coeffs[3][3] 87 | h = eh - e 88 | l = dcl - coeffs[0][0] 89 | a1, b1 = 2.3, 1.6 90 | a2, b2 = 1.4, 1.1 91 | l_e, le_h = l / e, (l + e) / h 92 | if eh > 900: 93 | if (l_e >= a2 and le_h >= b2) or (l_e >= b2 and le_h >= a2) or le_h > 4: 94 | mask[i][j] = 1.125 if l + e <= 400 else 1.25 95 | else: 96 | mask[i][j] = 1 + 1.25 * (eh - 290) / (1800 - 290) 97 | else: 98 | if (l_e >= a1 and le_h >= b1) or (l_e >= b1 and le_h >= a1) or le_h > 4: 99 | mask[i][j] = 1.125 if l + e <= 400 else 1.25 100 | elif e + h > 290: 101 | mask[i][j] = 1 + 1.25 * (eh - 290) / (1800 - 290) 102 | return mask 103 | -------------------------------------------------------------------------------- /src/offmark/embed/dtcwt_img_encoder.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import cv2 3 | import dtcwt 4 | 5 | class DtcwtImgEncoder: 6 | 7 | def __init__(self, key=None, str=1.0, step=5.0): 8 | self.key = key 9 | default_scale = 1.5 10 | self.alpha = default_scale * str 11 | self.step = step 12 | 13 | def read_wm(self, wm): 14 | wm_transform = dtcwt.Transform2d() 15 | wm_coeffs = wm_transform.forward(wm, nlevels=1) 16 | self.wm_coeffs = wm_coeffs 17 | 18 | def wm_capacity(self, frame_shape): 19 | (h, w) = self.__infer_wm_shape(frame_shape) 20 | return (h, w) 21 | 22 | def encode(self, yuv): 23 | yuv_transform = dtcwt.Transform2d() 24 | yuv_coeffs = yuv_transform.forward(yuv[:, :, 1], nlevels=3) 25 | y_transform = dtcwt.Transform2d() 26 | y_coeffs = y_transform.forward(yuv[:, :, 0], nlevels=3) 27 | 28 | # Masks for the level 3 subbands 29 | masks3 = [0 for i in range(6)] 30 | shape3 = y_coeffs.highpasses[2][:, :, 0].shape 31 | for i in range(6): 32 | masks3[i] = cv2.filter2D(np.abs(y_coeffs.highpasses[1][:,:,i]), -1, np.array([[1/4, 1/4], [1/4, 1/4]])) 33 | masks3[i] = np.ceil(self.rebin(masks3[i], shape3) * (1 / self.step)) 34 | masks3[i] *= 1.0 / max(12.0, np.amax(masks3[i])) 35 | for i in range(6): 36 | coeff = self.wm_coeffs.highpasses[0][:, :, i] 37 | h, w = coeff.shape 38 | coeffs = np.zeros(masks3[i].shape, dtype='complex_') 39 | coeffs[:h, :w] = coeff 40 | coeffs[-h:, :w] = coeff 41 | coeffs[:h, -w:] = coeff 42 | coeffs[-h:, -w:] = coeff 43 | yuv_coeffs.highpasses[2][:, :, i] += self.alpha * (masks3[i] * coeffs) 44 | yuv[:, :, 1] = yuv_transform.inverse(yuv_coeffs) 45 | return yuv 46 | 47 | def __infer_wm_shape(self, img_shape): 48 | h = (((img_shape[0] + 1) // 2 + 1) // 2 + 1) // 2 49 | w = (((img_shape[1] + 1) // 2 + 1) // 2 + 1) // 2 50 | if h % 2 == 1: 51 | h += 1 52 | if w % 2 == 1: 53 | w += 1 54 | return (h, w) 55 | 56 | def rebin(self, a, shape): 57 | if a.shape[0] % 2 == 1: 58 | a = np.vstack((a, np.zeros((1, a.shape[1])))) 59 | sh = shape[0], a.shape[0] // shape[0], shape[1], a.shape[1] // shape[1] 60 | return a.reshape(sh).mean(-1).mean(1) 61 | -------------------------------------------------------------------------------- /src/offmark/embed/dtcwt_key_encoder.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import cv2 3 | import dtcwt 4 | 5 | class DtcwtKeyEncoder: 6 | 7 | def __init__(self, key=None, str=1.0, step=5.0): 8 | self.key = key 9 | default_scale = 10.0 10 | self.alpha = default_scale * str 11 | self.step = step 12 | 13 | def read_wm(self, wm): 14 | wm_transform = dtcwt.Transform2d() 15 | wm_coeffs = wm_transform.forward(wm, nlevels=1) 16 | self.wm_coeffs = wm_coeffs 17 | 18 | def wm_capacity(self, frame_shape): 19 | (h, w) = self.__infer_wm_shape(frame_shape) 20 | return (h, w) 21 | 22 | def encode(self, yuv): 23 | yuv_transform = dtcwt.Transform2d() 24 | yuv_coeffs = yuv_transform.forward(yuv[:, :, 1], nlevels=3) 25 | y_transform = dtcwt.Transform2d() 26 | y_coeffs = y_transform.forward(yuv[:, :, 0], nlevels=3) 27 | 28 | # Masks for level 3 subbands 29 | masks3 = [0 for i in range(6)] 30 | shape3 = y_coeffs.highpasses[2][:, :, 0].shape 31 | for i in range(6): 32 | masks3[i] = cv2.filter2D(np.abs(y_coeffs.highpasses[1][:,:,i]), -1, np.array([[1/4, 1/4], [1/4, 1/4]])) 33 | masks3[i] = np.ceil(self.rebin(masks3[i], shape3) / self.step) 34 | for i in range(6): 35 | coeff = self.wm_coeffs.highpasses[0][:, :, i] 36 | h, w = coeff.shape 37 | coeffs = np.zeros(masks3[i].shape, dtype='complex_') 38 | coeffs[:h, :w] = coeff 39 | coeffs[-h:, :w] = coeff 40 | coeffs[:h, -w:] = coeff 41 | coeffs[-h:, -w:] = coeff 42 | yuv_coeffs.highpasses[2][:, :, i] += self.alpha * (masks3[i] * coeffs) 43 | yuv[:, :, 1] = yuv_transform.inverse(yuv_coeffs) 44 | return yuv 45 | 46 | def __infer_wm_shape(self, img_shape): 47 | h = (((img_shape[0] + 1) // 2 + 1) // 2 + 1) // 2 48 | w = (((img_shape[1] + 1) // 2 + 1) // 2 + 1) // 2 49 | if h % 2 == 1: 50 | h += 1 51 | if w % 2 == 1: 52 | w += 1 53 | return (h, w) 54 | 55 | def rebin(self, a, shape): 56 | if a.shape[0] % 2 == 1: 57 | a = np.vstack((a, np.zeros((1, a.shape[1])))) 58 | sh = shape[0], a.shape[0] // shape[0], shape[1], a.shape[1] // shape[1] 59 | return a.reshape(sh).mean(-1).mean(1) 60 | -------------------------------------------------------------------------------- /src/offmark/embed/dwt_dct_svd_encoder.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import pywt 3 | import cv2 4 | 5 | class DwtDctSvdEncoder: 6 | def __init__(self, key=None, scales=[0,15,0], blk=4): 7 | self.key = key 8 | self.scales = scales 9 | self.blk = blk 10 | 11 | def read_wm(self, wm): 12 | self.wm = wm[0] 13 | 14 | def wm_capacity(self, frame_shape): 15 | row, col, channels = frame_shape 16 | block_num = row * col // 64 17 | return (1, block_num) 18 | 19 | def encode(self, yuv): 20 | (row, col, channels) = yuv.shape 21 | for channel in range(3): 22 | if self.scales[channel] <= 0: 23 | continue 24 | ca, hvd = pywt.dwt2(yuv[:row // 4 * 4,:col // 4 * 4, channel], 'haar') 25 | self.__encode_frame(ca, self.scales[channel]) 26 | yuv[:row // 4 * 4, :col // 4 * 4, channel] = pywt.idwt2((ca, hvd), 'haar') 27 | return yuv 28 | 29 | def __encode_frame(self, frame, scale): 30 | (row, col) = frame.shape 31 | c = 0 32 | for i in range(row // self.blk): 33 | for j in range(col // self.blk): 34 | blk = frame[i * self.blk : i * self.blk + self.blk, 35 | j * self.blk : j * self.blk + self.blk] 36 | wm_bit = self.wm[c] 37 | embedded_blk = self.__blk_embed_wm(blk, wm_bit, scale) 38 | frame[i * self.blk : i * self.blk + self.blk, 39 | j * self.blk : j * self.blk + self.blk] = embedded_blk 40 | c += 1 41 | 42 | def __blk_embed_wm(self, blk, wm_bit, scale): 43 | u, s, v = np.linalg.svd(cv2.dct(blk)) 44 | s[0] = (s[0] // scale + 0.25 + 0.5 * wm_bit) * scale 45 | return cv2.idct(np.dot(u, np.dot(np.diag(s), v))) 46 | -------------------------------------------------------------------------------- /src/offmark/extract/dct_decoder.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | class DctDecoder: 5 | 6 | def __init__(self, key=None, alpha=20): 7 | self.key = key 8 | self.alpha = alpha 9 | 10 | def decode(self, yuv): 11 | blk_shape = (8, 8) 12 | channel = yuv[:,:,1] 13 | lum_mask = self.luminance_mask(yuv[:,:,0]) 14 | tex_mask = self.texture_mask(yuv[:,:,0]) 15 | mask = tex_mask * lum_mask 16 | c = 0 17 | wm = np.zeros(yuv.shape[0] * yuv.shape[1] // blk_shape[0] // blk_shape[1]) 18 | for i in range(channel.shape[0] // blk_shape[0]): 19 | for j in range(channel.shape[1] // blk_shape[1]): 20 | blk = channel[i * blk_shape[0] : i * blk_shape[0] + blk_shape[0], 21 | j * blk_shape[1] : j * blk_shape[1] + blk_shape[1]] 22 | step = self.alpha * mask[i][j] 23 | coeffs = cv2.dct(blk) 24 | wm_bit = int(np.around(coeffs[2][1] / step) % 2 == 1) 25 | wm[c] = wm_bit 26 | c += 1 27 | return np.array(wm).reshape(1, -1) 28 | 29 | def luminance_mask(self, lum): 30 | blk_shape = (8, 8) 31 | rows = lum.shape[0] // blk_shape[0] 32 | cols = lum.shape[1] // blk_shape[1] 33 | mask = np.zeros((rows, cols)) 34 | for i in range(rows): 35 | for j in range(cols): 36 | blk = lum[i * blk_shape[0]:i * blk_shape[0] + blk_shape[0], 37 | j * blk_shape[1]:j * blk_shape[1] + blk_shape[1]] 38 | coeffs = cv2.dct(blk) 39 | mask[i][j] = coeffs[0][0] 40 | l_min, l_max = 90, 255 41 | f_max = 2 42 | mask /= 8 43 | mean = max(l_min, np.mean(mask)) 44 | f_ref = 1 + (mean - l_min) * (f_max - 1) / (l_max - l_min) 45 | for i in range(mask.shape[0]): 46 | for j in range(mask.shape[1]): 47 | if mask[i][j] > mean: 48 | mask[i][j] = 1 + (mask[i][j] - mean) / (l_max - mean) * (f_max - f_ref) 49 | elif mask[i][j] < 15: 50 | mask[i][j] = 1.25 51 | elif mask[i][j] < 25: 52 | mask[i][j] = 1.125 53 | else: 54 | mask[i][j] = 1 55 | return mask 56 | 57 | def texture_mask(self, lum): 58 | blk_shape = (8, 8) 59 | rows = lum.shape[0] // blk_shape[0] 60 | cols = lum.shape[1] // blk_shape[1] 61 | mask = np.full((rows, cols), 1.0) 62 | for i in range(rows): 63 | for j in range(cols): 64 | blk = lum[i * blk_shape[0]:i * blk_shape[0] + blk_shape[0], 65 | j * blk_shape[1]:j * blk_shape[1] + blk_shape[1]] 66 | coeffs = cv2.dct(blk) 67 | coeffs = np.abs(coeffs) 68 | dcl = coeffs[0][0] + coeffs[0][1] + coeffs[0][2] + coeffs[1][0] + coeffs[1][1] + coeffs[2][0] 69 | eh = np.sum(coeffs) - dcl 70 | if eh > 125: 71 | e = coeffs[3][0] + coeffs[4][0] + coeffs[5][0] + coeffs[6][0] + \ 72 | coeffs[0][3] + coeffs[0][4] + coeffs[0][5] + coeffs[0][6] + \ 73 | coeffs[2][1] + coeffs[1][2] + coeffs[2][2] + coeffs[3][3] 74 | h = eh - e 75 | l = dcl - coeffs[0][0] 76 | a1, b1 = 2.3, 1.6 77 | a2, b2 = 1.4, 1.1 78 | l_e, le_h = l / e, (l + e) / h 79 | if eh > 900: 80 | if (l_e >= a2 and le_h >= b2) or (l_e >= b2 and le_h >= a2) or le_h > 4: 81 | mask[i][j] = 1.125 if l + e <= 400 else 1.25 82 | else: 83 | mask[i][j] = 1 + 1.25 * (eh - 290) / (1800 - 290) 84 | else: 85 | if (l_e >= a1 and le_h >= b1) or (l_e >= b1 and le_h >= a1) or le_h > 4: 86 | mask[i][j] = 1.125 if l + e <= 400 else 1.25 87 | elif e + h > 290: 88 | mask[i][j] = 1 + 1.25 * (eh - 290) / (1800 - 290) 89 | return mask 90 | -------------------------------------------------------------------------------- /src/offmark/extract/dtcwt_img_decoder.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import cv2 3 | import dtcwt 4 | 5 | class DtcwtImgDecoder: 6 | 7 | def __init__(self, key=None, str=1.0, step=5.0): 8 | self.key = key 9 | default_scale = 1.5 10 | self.alpha = default_scale * str 11 | self.step = step 12 | 13 | def decode(self, yuv): 14 | wmed_transform = dtcwt.Transform2d() 15 | wmed_coeffs = wmed_transform.forward(yuv[:, :, 1], nlevels=3) 16 | y_transform = dtcwt.Transform2d() 17 | y_coeffs = y_transform.forward(yuv[:, :, 0], nlevels=3) 18 | 19 | masks3 = [0 for i in range(6)] 20 | inv_masks3 = [0 for i in range(6)] 21 | shape3 = y_coeffs.highpasses[2][:, :, 0].shape 22 | for i in range(6): 23 | masks3[i] = cv2.filter2D(np.abs(y_coeffs.highpasses[1][:,:,i]), -1, np.array([[1/4, 1/4], [1/4, 1/4]])) 24 | masks3[i] = np.ceil(self.rebin(masks3[i], shape3) * (1.0 / self.step)) 25 | masks3[i][masks3[i] == 0] = 0.01 26 | masks3[i] *= 1.0 / max(12.0, np.amax(masks3[i])) 27 | inv_masks3[i] = 1.0 / masks3[i] 28 | 29 | shape = wmed_coeffs.highpasses[2][:,:,i].shape 30 | h, w = (shape[0] + 1) // 2, (shape[1] + 1) // 2 31 | coeffs = np.zeros((h, w, 6), dtype='complex_') 32 | for i in range(6): 33 | coeff = (wmed_coeffs.highpasses[2][:,:,i]) * inv_masks3[i] * 1 / self.alpha 34 | coeffs[:,:,i] = coeff[:h, :w] + coeff[:h, -w:] + coeff[-h:, :w] + coeff[-h:, -w:] 35 | highpasses = tuple([coeffs]) 36 | lowpass = np.zeros((h * 2, w * 2)) 37 | t = dtcwt.Transform2d() 38 | wm = t.inverse(dtcwt.Pyramid(lowpass, highpasses)) 39 | 40 | return wm 41 | 42 | def rebin(self, a, shape): 43 | if a.shape[0] % 2 == 1: 44 | a = np.vstack((a, np.zeros((1, a.shape[1])))) 45 | sh = shape[0], a.shape[0] // shape[0], shape[1], a.shape[1] // shape[1] 46 | return a.reshape(sh).mean(-1).mean(1) 47 | -------------------------------------------------------------------------------- /src/offmark/extract/dtcwt_key_decoder.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import cv2 3 | import dtcwt 4 | 5 | class DtcwtKeyDecoder: 6 | 7 | def __init__(self, key=None, str=1.0, step=5.0): 8 | self.key = key 9 | default_scale = 10.0 10 | self.alpha = default_scale * str 11 | self.step = step 12 | 13 | def decode(self, wmed_img): 14 | wmed_transform = dtcwt.Transform2d() 15 | wmed_coeffs = wmed_transform.forward(wmed_img[:, :, 1], nlevels=3) 16 | y_transform = dtcwt.Transform2d() 17 | y_coeffs = y_transform.forward(wmed_img[:, :, 0], nlevels=3) 18 | 19 | masks3 = [0 for i in range(6)] 20 | inv_masks3 = [0 for i in range(6)] 21 | shape3 = y_coeffs.highpasses[2][:, :, 0].shape 22 | for i in range(6): 23 | masks3[i] = cv2.filter2D(np.abs(y_coeffs.highpasses[1][:,:,i]), -1, np.array([[1/4, 1/4], [1/4, 1/4]])) 24 | masks3[i] = np.ceil(self.rebin(masks3[i], shape3) / self.step) 25 | masks3[i][masks3[i] == 0] = 0.01 26 | inv_masks3[i] = 1.0 / masks3[i] 27 | 28 | shape = wmed_coeffs.highpasses[2][:,:,i].shape 29 | h, w = (shape[0] + 1) // 2, (shape[1] + 1) // 2 30 | coeffs = np.zeros((h, w, 6), dtype='complex_') 31 | for i in range(6): 32 | coeff = (wmed_coeffs.highpasses[2][:,:,i]) * inv_masks3[i] * 1 / self.alpha 33 | coeffs[:,:,i] = coeff[:h, :w] + coeff[:h, -w:] + coeff[-h:, :w] + coeff[-h:, -w:] 34 | highpasses = tuple([coeffs]) 35 | lowpass = np.zeros((h * 2, w * 2)) 36 | t = dtcwt.Transform2d() 37 | wm = t.inverse(dtcwt.Pyramid(lowpass, highpasses)) 38 | return wm 39 | 40 | def rebin(self, a, shape): 41 | if a.shape[0] % 2 == 1: 42 | a = np.vstack((a, np.zeros((1, a.shape[1])))) 43 | sh = shape[0], a.shape[0] // shape[0], shape[1], a.shape[1] // shape[1] 44 | return a.reshape(sh).mean(-1).mean(1) 45 | -------------------------------------------------------------------------------- /src/offmark/extract/dwt_dct_svd_decoder.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import pywt 3 | import cv2 4 | 5 | class DwtDctSvdDecoder: 6 | 7 | def __init__(self, key=None, scales=[0,15,0], blk=4): 8 | self.key = key 9 | self.scales = scales 10 | self.blk = blk 11 | 12 | def decode(self, yuv): 13 | (row, col, channels) = yuv.shape 14 | self.block_num = row * col // 4 // (self.blk * self.blk) 15 | wm_bits = np.zeros(shape=(3, self.block_num)) 16 | for channel in range(3): 17 | if self.scales[channel] <= 0: 18 | continue 19 | ca, hvd = pywt.dwt2(yuv[:row // 4 * 4,:col // 4 * 4, channel], 'haar') 20 | self.__decode_frame(ca, self.scales[channel], wm_bits[channel]) 21 | return np.array(wm_bits[1]).reshape(1, -1) 22 | 23 | def __decode_frame(self, frame, scale, wm_bits): 24 | (row, col) = frame.shape 25 | c = 0 26 | for i in range(row // self.blk): 27 | for j in range(col // self.blk): 28 | blk = frame[i * self.blk : i * self.blk + self.blk, 29 | j * self.blk : j * self.blk + self.blk] 30 | wm_bit = self.__blk_extract_wm(blk, scale) 31 | wm_bits[c] = wm_bit 32 | c += 1 33 | 34 | def __blk_extract_wm(self, blk, scale): 35 | u,s,v = np.linalg.svd(cv2.dct(blk)) 36 | wm = int((s[0] % scale) > scale * 0.5) 37 | return wm -------------------------------------------------------------------------------- /src/offmark/generator/block_shuffler.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | 5 | class BlockShuffler: 6 | def __init__(self, key=None, blk_shape=(35, 30)): 7 | self.key = key 8 | self.blk_shape = blk_shape 9 | 10 | @staticmethod 11 | def wm_type(): 12 | return "grayscale" 13 | 14 | def generate_wm(self, payload, capacity, shape=(135, 240)): 15 | """Scrambles the watermark image by shuffling around blocks of pixels. 16 | """ 17 | wm = cv2.resize(payload, (shape[1], shape[0])) 18 | wm = self.randomize_channel(wm, self.key, blk_shape=self.blk_shape) 19 | wm = cv2.resize(wm, (capacity[1], capacity[0])) 20 | wm = (wm > 127).astype(np.uint8) * 255 21 | wm = wm.astype(np.int32) 22 | wm[wm != 255] = -255 23 | return wm 24 | 25 | def randomize_channel(self, channel, key, blk_shape=(8, 8)): 26 | rows = channel.shape[0] // blk_shape[0] * blk_shape[0] 27 | cols = channel.shape[1] // blk_shape[1] * blk_shape[1] 28 | blks = np.array([[ 29 | channel[i:i + blk_shape[0], j:j + blk_shape[1]] 30 | for j in range(0, cols, blk_shape[1]) 31 | ] for i in range(0, rows, blk_shape[0])]) 32 | shape = blks.shape 33 | blks = blks.reshape(-1, blk_shape[0], blk_shape[1]) 34 | np.random.RandomState(key).shuffle(blks) 35 | full_res = np.copy(channel) 36 | res = np.concatenate(np.concatenate(blks.reshape(shape), 1), 1) 37 | full_res[:rows, :cols] = res 38 | return full_res 39 | -------------------------------------------------------------------------------- /src/offmark/generator/corr_shuffler.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | 5 | class CorrShuffler: 6 | 7 | def __init__(self, key=None): 8 | self.key = key 9 | 10 | @staticmethod 11 | def wm_type(): 12 | return "bits" 13 | 14 | def generate_wm(self, payload, capacity, shape=(1080, 1920)): 15 | """Generate a 2-D array with elements of either 1 or -1. Though the 16 | values may change after scaling due to resampling/interpolating 17 | 18 | >>> c = CorrShuffler(key=0) 19 | >>> c.generate_wm(None, (3, 3)) 20 | array([[ 0.5, -0.5, 1. ], 21 | [ 0.5, 0.5, 0. ], 22 | [ 0.5, 0.5, -0.5]], dtype=float32) 23 | """ 24 | # payload is unnecessary 25 | wm = np.random.RandomState(self.key).randint(0, 2, shape).astype(np.float32) 26 | wm[wm == 0] = -1 27 | wm = cv2.resize(wm, (capacity[1], capacity[0])) 28 | return wm 29 | -------------------------------------------------------------------------------- /src/offmark/generator/grayscale.py: -------------------------------------------------------------------------------- 1 | import math 2 | import warnings 3 | 4 | import numpy as np 5 | 6 | 7 | class GrayScale: 8 | 9 | def __init__(self, key=None): 10 | self.key = key 11 | 12 | @staticmethod 13 | def wm_type(): 14 | return "grayscale" 15 | 16 | def generate_wm(self, payload, capacity): 17 | """Converts the watermark image pixels to 0 or 1. 18 | """ 19 | size = np.array(capacity).prod() 20 | wm_len = np.array(payload.shape).prod() 21 | 22 | if wm_len > size: 23 | warnings.warn( 24 | "\nImage size {0} is greater than the embed's capacity: {1} pixels".format(payload.shape, size), 25 | stacklevel=3) 26 | 27 | payload = (payload > 127).astype(np.uint8).flatten() 28 | c = int(math.ceil(size / wm_len)) 29 | np.random.RandomState(self.key).shuffle(payload) 30 | wm = np.stack([payload for _ in range(c)], axis=0).flatten()[:size] 31 | return wm.reshape(capacity) 32 | -------------------------------------------------------------------------------- /src/offmark/generator/shuffler.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | import numpy as np 4 | 5 | 6 | class Shuffler: 7 | 8 | def __init__(self, key=None): 9 | self.key = key 10 | 11 | @staticmethod 12 | def wm_type(): 13 | return "bits" 14 | 15 | def generate_wm(self, payload, capacity): 16 | """Shuffles the payload and repeats it to get to capacity. 17 | """ 18 | length = np.array(capacity).prod() 19 | payload = np.copy(payload) 20 | wm_len = np.array(payload.shape).prod() 21 | c = int(math.ceil(length / wm_len)) 22 | np.random.RandomState(self.key).shuffle(payload) 23 | wm = np.stack([payload for _ in range(c)], axis=0).flatten()[:length] 24 | wm = wm.reshape(capacity) 25 | return wm 26 | -------------------------------------------------------------------------------- /src/offmark/video/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eluv-io/offmark-py/b8bc2ff652fec1b96cd5c56ca31863d1620d2518/src/offmark/video/__init__.py -------------------------------------------------------------------------------- /src/offmark/video/embedder.py: -------------------------------------------------------------------------------- 1 | import logging 2 | 3 | import cv2 4 | import numpy as np 5 | 6 | from ..common.__logging import trace 7 | 8 | logger = logging.getLogger(__name__) 9 | 10 | 11 | class Embedder: 12 | def __init__(self, frame_reader, frame_embedder, frame_writer): 13 | self.frame_reader = frame_reader 14 | self.frame_writer = frame_writer 15 | self.frame_embedder = frame_embedder 16 | 17 | @trace(logger) 18 | def start(self): 19 | while True: 20 | in_frame = self.frame_reader.read() 21 | if in_frame is None: 22 | logger.info('End of input stream') 23 | break 24 | 25 | out_frame = self.__mark_frame(in_frame) 26 | 27 | self.frame_writer.write(out_frame) 28 | 29 | self.frame_reader.close() 30 | self.frame_writer.close() 31 | logger.info('Done') 32 | 33 | def __mark_frame(self, frame_rgb): 34 | frame_yuv = cv2.cvtColor(frame_rgb.astype(np.float32), cv2.COLOR_BGR2YUV) 35 | wm_frame_yuv = self.frame_embedder.encode(frame_yuv) 36 | wm_frame_rgb = cv2.cvtColor(wm_frame_yuv, cv2.COLOR_YUV2BGR) 37 | wm_frame_rgb = np.clip(wm_frame_rgb, a_min=0, a_max=255) 38 | wm_frame_rgb = np.around(wm_frame_rgb).astype(np.uint8) 39 | return wm_frame_rgb 40 | -------------------------------------------------------------------------------- /src/offmark/video/extractor.py: -------------------------------------------------------------------------------- 1 | import logging 2 | 3 | import cv2 4 | import numpy as np 5 | 6 | from ..common.__logging import trace 7 | 8 | logger = logging.getLogger(__name__) 9 | 10 | 11 | class Extractor: 12 | def __init__(self, frame_reader, frame_extractor, degenerator): 13 | self.frame_reader = frame_reader 14 | self.frame_extractor = frame_extractor 15 | self.degenerator = degenerator 16 | 17 | @trace(logger) 18 | def start(self): 19 | while True: 20 | in_frame = self.frame_reader.read() 21 | if in_frame is None: 22 | logger.info('End of input stream') 23 | break 24 | 25 | self.__check_frame(in_frame) 26 | 27 | self.frame_reader.close() 28 | logger.info('Done') 29 | 30 | def __check_frame(self, frame_rgb): 31 | wm_frame_yuv = cv2.cvtColor(frame_rgb.astype(np.float32), cv2.COLOR_BGR2YUV) 32 | frame_yuv = self.frame_extractor.decode(wm_frame_yuv) 33 | out = self.degenerator.degenerate(frame_yuv) 34 | logger.info(out) 35 | -------------------------------------------------------------------------------- /src/offmark/video/frame_reader.py: -------------------------------------------------------------------------------- 1 | import logging 2 | import subprocess 3 | 4 | import ffmpeg 5 | import numpy as np 6 | 7 | from ..common.__logging import trace 8 | from ..common.__video import probe 9 | 10 | logger = logging.getLogger(__name__) 11 | 12 | 13 | # TODO extend ABC (abstract base class)? 14 | class FrameReader: 15 | def __init__(self): 16 | pass 17 | 18 | def read(self) -> np.ndarray: 19 | """Read one frame in RGB format.""" 20 | pass 21 | 22 | def close(self): 23 | pass 24 | 25 | 26 | # TODO FileDecoder in separate file? 27 | # TODO pix_fmt yuv420p? 28 | class FileDecoder(FrameReader): 29 | def __init__(self, file): 30 | super().__init__() 31 | self.file = file 32 | self.__start_ffmpeg() 33 | 34 | @trace(logger) 35 | def __start_ffmpeg(self): 36 | # Width and height in pixels 37 | info = probe(self.file) 38 | self.width = info['width'] 39 | self.height = info['height'] 40 | 41 | # RGB24 42 | self.frame_size_bytes = self.width * self.height * 3 43 | 44 | args = ( 45 | ffmpeg 46 | .input(self.file) 47 | .output('pipe:', format='rawvideo', pix_fmt='rgb24') 48 | .global_args('-loglevel', 'quiet') 49 | .compile() 50 | ) 51 | self.ffmpeg = subprocess.Popen(args, stdout=subprocess.PIPE) 52 | 53 | def read(self): 54 | frame_bytes = self.ffmpeg.stdout.read(self.frame_size_bytes) 55 | if len(frame_bytes) == 0: 56 | frame = None 57 | else: 58 | assert len(frame_bytes) == self.frame_size_bytes 59 | frame = ( 60 | np 61 | .frombuffer(frame_bytes, np.uint8) 62 | .reshape(self.height, self.width, 3) 63 | ) 64 | return frame 65 | 66 | @trace(logger) 67 | def close(self): 68 | logger.info('Waiting for ffmpeg decoder') 69 | self.ffmpeg.wait() 70 | -------------------------------------------------------------------------------- /src/offmark/video/frame_writer.py: -------------------------------------------------------------------------------- 1 | import logging 2 | import subprocess 3 | 4 | import ffmpeg 5 | import numpy as np 6 | 7 | from ..common.__logging import trace 8 | 9 | logger = logging.getLogger(__name__) 10 | 11 | 12 | class FrameWriter: 13 | def __init__(self): 14 | pass 15 | 16 | def write(self, frame: np.ndarray): 17 | pass 18 | 19 | def close(self): 20 | pass 21 | 22 | 23 | class FileEncoder(FrameWriter): 24 | def __init__(self, file, width, height): 25 | super().__init__() 26 | self.file = file 27 | self.__start_ffmpeg(width, height) 28 | 29 | @trace(logger) 30 | def __start_ffmpeg(self, width, height): 31 | args = ( 32 | ffmpeg 33 | .input('pipe:', format='rawvideo', pix_fmt='rgb24', s=f'{width}x{height}') 34 | .output(self.file, pix_fmt='yuv420p') 35 | .overwrite_output() 36 | .global_args('-loglevel', 'quiet') 37 | .compile() 38 | ) 39 | self.ffmpeg = subprocess.Popen(args, stdin=subprocess.PIPE) 40 | 41 | def write(self, frame): 42 | self.ffmpeg.stdin.write( 43 | frame.astype(np.uint8).tobytes() 44 | ) 45 | 46 | @trace(logger) 47 | def close(self): 48 | logger.info('Waiting for ffmpeg encoder') 49 | self.ffmpeg.stdin.close() 50 | self.ffmpeg.wait() 51 | -------------------------------------------------------------------------------- /tests/detect.py: -------------------------------------------------------------------------------- 1 | import logging 2 | import os 3 | 4 | import numpy as np 5 | 6 | from offmark.degenerator.de_shuffler import DeShuffler 7 | from offmark.extract.dwt_dct_svd_decoder import DwtDctSvdDecoder 8 | from offmark.video.extractor import Extractor 9 | from offmark.video.frame_reader import FileDecoder 10 | 11 | logger = logging.getLogger(__name__) 12 | logging.basicConfig( 13 | level=logging.DEBUG, 14 | format='%(asctime)s %(levelname)s %(name)s %(message)s') 15 | 16 | 17 | def run(): 18 | this_dir = os.path.dirname(__file__) 19 | in_file = os.path.join(this_dir, 'out', 'marked.mp4') 20 | payload = np.array([0, 1, 1, 0, 0, 1, 0, 1]) 21 | print("Payload: ", payload) 22 | 23 | r = FileDecoder(in_file) 24 | 25 | degenerator = DeShuffler(key=0) 26 | degenerator.set_shape(payload.shape) 27 | 28 | frame_extractor = DwtDctSvdDecoder() 29 | 30 | video_extractor = Extractor(r, frame_extractor, degenerator) 31 | video_extractor.start() 32 | 33 | 34 | if __name__ == '__main__': 35 | run() 36 | -------------------------------------------------------------------------------- /tests/ffmpeg_example.py: -------------------------------------------------------------------------------- 1 | import ffmpeg 2 | 3 | ( 4 | ffmpeg 5 | .input('tests/media/in.mp4') 6 | .hflip() 7 | .output('tests/out/ffmpeg_example.mp4') 8 | .run() 9 | ) 10 | -------------------------------------------------------------------------------- /tests/mark.py: -------------------------------------------------------------------------------- 1 | import logging 2 | import os 3 | 4 | import numpy as np 5 | 6 | from offmark.embed.dwt_dct_svd_encoder import DwtDctSvdEncoder 7 | from offmark.generator.shuffler import Shuffler 8 | from offmark.video.embedder import Embedder 9 | from offmark.video.frame_reader import FileDecoder 10 | from offmark.video.frame_writer import FileEncoder 11 | 12 | logger = logging.getLogger(__name__) 13 | logging.basicConfig( 14 | level=logging.DEBUG, 15 | format='%(asctime)s %(levelname)s %(name)s %(message)s') 16 | 17 | 18 | def run(): 19 | this_dir = os.path.dirname(__file__) 20 | in_file = os.path.join(this_dir, 'media', 'in.mp4') 21 | out_file = os.path.join(this_dir, 'out', 'marked.mp4') 22 | payload = np.array([0, 1, 1, 0, 0, 1, 0, 1]) 23 | print("Payload: ", payload) 24 | 25 | r = FileDecoder(in_file) 26 | w = FileEncoder(out_file, r.width, r.height) 27 | 28 | # Initialize Frame Embedder 29 | frame_embedder = DwtDctSvdEncoder() 30 | capacity = frame_embedder.wm_capacity((r.height, r.width, 3)) 31 | 32 | # Initialize Generator 33 | generator = Shuffler(key=0) 34 | wm = generator.generate_wm(payload, capacity) 35 | frame_embedder.read_wm(wm) 36 | 37 | # Start watermarking and transcoding 38 | # TODO properly preserve the original video encoding and container 39 | video_embedder = Embedder(r, frame_embedder, w) 40 | video_embedder.start() 41 | 42 | 43 | # TODO CLI flags to choose embedder 44 | if __name__ == '__main__': 45 | run() 46 | -------------------------------------------------------------------------------- /tests/media/imgs/frame63.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eluv-io/offmark-py/b8bc2ff652fec1b96cd5c56ca31863d1620d2518/tests/media/imgs/frame63.jpeg -------------------------------------------------------------------------------- /tests/media/in.mp4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eluv-io/offmark-py/b8bc2ff652fec1b96cd5c56ca31863d1620d2518/tests/media/in.mp4 -------------------------------------------------------------------------------- /tests/media/wms/numbers.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eluv-io/offmark-py/b8bc2ff652fec1b96cd5c56ca31863d1620d2518/tests/media/wms/numbers.jpeg -------------------------------------------------------------------------------- /tests/media/wms/qr.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eluv-io/offmark-py/b8bc2ff652fec1b96cd5c56ca31863d1620d2518/tests/media/wms/qr.jpeg -------------------------------------------------------------------------------- /tests/test.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | import cv2 4 | import numpy as np 5 | 6 | from offmark.degenerator.de_block_shuffler import DeBlockShuffler 7 | from offmark.degenerator.de_corr_shuffler import DeCorrShuffler 8 | from offmark.degenerator.de_grayscale import DeGrayScale 9 | from offmark.degenerator.de_shuffler import DeShuffler 10 | from offmark.embed.dct_encoder import DctEncoder 11 | from offmark.embed.dtcwt_img_encoder import DtcwtImgEncoder 12 | from offmark.embed.dtcwt_key_encoder import DtcwtKeyEncoder 13 | from offmark.embed.dwt_dct_svd_encoder import DwtDctSvdEncoder 14 | from offmark.extract.dct_decoder import DctDecoder 15 | from offmark.extract.dtcwt_img_decoder import DtcwtImgDecoder 16 | from offmark.extract.dtcwt_key_decoder import DtcwtKeyDecoder 17 | from offmark.extract.dwt_dct_svd_decoder import DwtDctSvdDecoder 18 | from offmark.generator.block_shuffler import BlockShuffler 19 | from offmark.generator.corr_shuffler import CorrShuffler 20 | from offmark.generator.grayscale import GrayScale 21 | from offmark.generator.shuffler import Shuffler 22 | 23 | this_dir = os.path.dirname(__file__) 24 | 25 | key1 = 0 26 | 27 | generators = [ 28 | Shuffler(key=key1), 29 | GrayScale(key=key1), 30 | CorrShuffler(key=key1), 31 | BlockShuffler(key=key1) 32 | ] 33 | 34 | degenerators = [ 35 | DeShuffler(key=key1), 36 | DeGrayScale(key=key1), 37 | DeCorrShuffler(key=key1), 38 | DeBlockShuffler(key=key1) 39 | ] 40 | 41 | encoders = [ 42 | DwtDctSvdEncoder(), 43 | DtcwtKeyEncoder(), 44 | DtcwtImgEncoder(), 45 | DctEncoder() 46 | ] 47 | 48 | decoders = [ 49 | DwtDctSvdDecoder(), 50 | DtcwtKeyDecoder(), 51 | DtcwtImgDecoder(), 52 | DctDecoder() 53 | ] 54 | 55 | # gen_idx:coder_idx combinations: 0:0, 0:3, 1:0, 1:3, 2:1, 3:2 56 | gen_idx = 2 57 | coder_idx = 1 58 | generator = generators[gen_idx] 59 | degenerator = degenerators[gen_idx] 60 | encoder = encoders[coder_idx] 61 | decoder = decoders[coder_idx] 62 | 63 | payload = None 64 | 65 | # payload is read in differently depending on the input type (char or image) 66 | wm_type = generator.wm_type() 67 | if wm_type == "bits": 68 | payload = np.array([0, 1, 1, 0, 0, 1, 0, 1]) 69 | print("Payload: ", payload) 70 | elif wm_type == "grayscale": 71 | wm_path = os.path.join(this_dir, 'media', 'wms', 'qr.jpeg') 72 | wm = cv2.imread(wm_path, cv2.IMREAD_GRAYSCALE) 73 | assert wm is not None, "Watermark not found in {}".format(wm_path) 74 | payload = wm 75 | print("Payload: ", wm_path) 76 | 77 | path = os.path.join(this_dir, 'media', 'imgs', 'frame63.jpeg') 78 | output_path = os.path.join(this_dir, 'out', 'output.jpeg') 79 | 80 | # Read a frame and convert to YUV pixel format 81 | img = cv2.imread(path) 82 | assert img is not None, "Image not found in {}".format(path) 83 | bgr = img.astype(np.float32) 84 | yuv = cv2.cvtColor(bgr, cv2.COLOR_BGR2YUV) 85 | 86 | # generate 87 | wm = generator.generate_wm(payload, encoder.wm_capacity(yuv.shape)) 88 | 89 | # encode 90 | encoder.read_wm(wm) 91 | yuv = encoder.encode(yuv) 92 | wmed_frame = cv2.cvtColor(yuv, cv2.COLOR_YUV2BGR) 93 | wmed_frame = np.clip(wmed_frame, a_min=0, a_max=255) 94 | wmed_frame = np.around(wmed_frame).astype(np.uint8) 95 | cv2.imwrite(output_path, wmed_frame) 96 | 97 | # diff with original (scaled for visibility) 98 | diff = img.astype(np.int32) - wmed_frame.astype(np.int32) 99 | diff = np.abs(diff) 100 | diff_max = np.max(diff) 101 | diff = np.multiply(diff, 255 * 3/diff_max) 102 | diff = diff.clip(0, 255) 103 | diff_path = os.path.join(this_dir, 'out', 'diff.jpeg') 104 | cv2.imwrite(diff_path, diff) 105 | 106 | bgr = cv2.imread(output_path).astype(np.float32) 107 | yuv = cv2.cvtColor(bgr, cv2.COLOR_BGR2YUV) 108 | 109 | # decode 110 | decoded_wm = decoder.decode(yuv) 111 | 112 | # degenerate 113 | ret_payload = degenerator.set_shape(payload.shape).degenerate(decoded_wm) 114 | print("Decoded:", ret_payload) 115 | cv2.imwrite(os.path.join(this_dir, 'out', 'degenerate.jpeg'), ret_payload) 116 | 117 | # a = (payload - np.mean(payload)) / (np.std(payload) * len(payload)) 118 | # b = (ret_payload - np.mean(ret_payload)) / (np.std(ret_payload)) 119 | # c = np.correlate(a, b, 'full') 120 | # print("Maximum correlation: ", np.amax(c)) 121 | --------------------------------------------------------------------------------