From 6153e2cf38df7aa8ec3d4ff3ce4455a4bea4d81d Mon Sep 17 00:00:00 2001 From: guofei9987 Date: Sun, 31 Dec 2023 20:39:00 +0800 Subject: [PATCH] tidy codes --- README.md | 19 ++++++++++--------- example/example_echo_watermark.py | 11 ++++++----- example/example_hide_in_music.py | 10 +++++----- example/ori_file/.keep | 0 example/{ => ori_file}/sounds.wav | Bin 5 files changed, 21 insertions(+), 19 deletions(-) create mode 100644 example/ori_file/.keep rename example/{ => ori_file}/sounds.wav (100%) diff --git a/README.md b/README.md index feeab56..ae429df 100644 --- a/README.md +++ b/README.md @@ -185,28 +185,29 @@ hide_as_music.file_decode(filename='化物为音-解出来的文件.zip', wav_fi 回声水印技术对于音质的影响相对较小,同时具有较好的鲁棒性,能够在一定程度上抵抗压缩、转换等处理过程。这使得它适用于版权保护、内容认证、隐秘通讯等领域。 ```python -from hide_info.echo_watermark import EchoWatermark, get_error_rate -from hide_info import utils +from hide_info.echo_watermark import EchoWatermark +from hide_info import utils, evaluate +from scipy.io import wavfile -ori_file = "sounds.wav" # 载体 -embedded_file = "sounds_with_watermark.wav" # 嵌入水印后的文件名 +ori_file = "./ori_file/sounds.wav" # 载体 +embedded_file = "./output/sounds_with_watermark.wav" # 嵌入水印后的文件名 wm_str = "回声水印算法,欢迎 star!" # 水印 wm_bits = utils.bytes2bin(wm_str.encode('utf-8')) len_wm_bits = len(wm_bits) -# 嵌入水印 +# embed: echo_wm = EchoWatermark(pwd=111001) echo_wm.embed(origin_filename=ori_file, wm_bits=wm_bits, embed_filename=embedded_file) -# 提取水印 +# extract: echo_wm = EchoWatermark(pwd=111001) wm_extract = echo_wm.extract(embed_filename=embedded_file, len_wm_bits=len_wm_bits) wm_str_extract = utils.bin2bytes(wm_extract).decode('utf-8', errors='replace') -print("解出水印:", wm_str_extract) -# 错误率: -get_error_rate(wm_extract, wm_bits) +print("extract watermark: ", wm_str_extract) +# error rate: +evaluate.get_error_rate(wm_extract, wm_bits) ``` diff --git a/example/example_echo_watermark.py b/example/example_echo_watermark.py index 831f214..57b8e9a 100644 --- a/example/example_echo_watermark.py +++ b/example/example_echo_watermark.py @@ -1,10 +1,9 @@ -import numpy as np from hide_info.echo_watermark import EchoWatermark from hide_info import utils, evaluate from scipy.io import wavfile -ori_file = "sounds.wav" # 载体 -embedded_file = "sounds_with_watermark.wav" # 嵌入水印后的文件名 +ori_file = "./ori_file/sounds.wav" # 载体 +embedded_file = "./output/sounds_with_watermark.wav" # 嵌入水印后的文件名 wm_str = "回声水印算法,欢迎 star!" # 水印 wm_bits = utils.bytes2bin(wm_str.encode('utf-8')) @@ -19,11 +18,13 @@ wm_extract = echo_wm.extract(embed_filename=embedded_file, len_wm_bits=len_wm_bits) wm_str_extract = utils.bin2bytes(wm_extract).decode('utf-8', errors='replace') -print("解出水印:", wm_str_extract) -# 错误率: +print("extract watermark: ", wm_str_extract) +# error rate: evaluate.get_error_rate(wm_extract, wm_bits) # %% There are 3 algorithms: +import numpy as np + wm_bits = np.random.randint(2, size=200).tolist() len_wm_bits = len(wm_bits) diff --git a/example/example_hide_in_music.py b/example/example_hide_in_music.py index 7047d82..280b2b0 100644 --- a/example/example_hide_in_music.py +++ b/example/example_hide_in_music.py @@ -2,20 +2,20 @@ text = "待嵌入到音乐文件的文本,下面的代码中,会把这段文本以二进制形式隐藏到一个音乐文件中" -hide_in_music.encode(text.encode('utf-8'), music_filename="sounds.wav", music_filename_new="藏文于音.wav") +hide_in_music.encode(text.encode('utf-8'), music_filename="./ori_file/sounds.wav", music_filename_new="./output/藏文于音.wav") -text_encode = hide_in_music.decode(music_filename="藏文于音.wav") +text_encode = hide_in_music.decode(music_filename="./output/藏文于音.wav") print(text_encode.decode('utf-8')) # %% # 把文件隐藏到某个音乐中 -hide_in_music.file_encode(filename='要隐藏的文件.zip', music_filename="sounds.wav", music_filename_new="藏物于音.wav") +hide_in_music.file_encode(filename='要隐藏的文件.zip', music_filename="./ori_file/sounds.wav", music_filename_new="./output/藏物于音.wav") # 从音乐中提取文件 -hide_in_music.file_decode(filename="藏物于音-解出的文件.zip", music_filename="藏物于音.wav") +hide_in_music.file_decode(filename="./output/藏物于音-解出的文件.zip", music_filename="./output/藏物于音.wav") # %% 要隐藏的文件和解出的文件一模一样 import hashlib -with open('要隐藏的文件.zip', 'rb') as f1, open("藏物于音-解出的文件.zip", 'rb') as f2: +with open('要隐藏的文件.zip', 'rb') as f1, open("./output/藏物于音-解出的文件.zip", 'rb') as f2: assert hashlib.md5(f1.read()).hexdigest() == hashlib.md5(f2.read()).hexdigest() diff --git a/example/ori_file/.keep b/example/ori_file/.keep new file mode 100644 index 0000000..e69de29 diff --git a/example/sounds.wav b/example/ori_file/sounds.wav similarity index 100% rename from example/sounds.wav rename to example/ori_file/sounds.wav