Skip to content

Commit

Permalink
tidy codes
Browse files Browse the repository at this point in the history
  • Loading branch information
guofei9987 committed Dec 31, 2023
1 parent 7758033 commit 6153e2c
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 19 deletions.
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```


Expand Down
11 changes: 6 additions & 5 deletions example/example_echo_watermark.py
Original file line number Diff line number Diff line change
@@ -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'))
Expand All @@ -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)

Expand Down
10 changes: 5 additions & 5 deletions example/example_hide_in_music.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Empty file added example/ori_file/.keep
Empty file.
File renamed without changes.

0 comments on commit 6153e2c

Please sign in to comment.