Skip to content

Commit

Permalink
version 0.1.7
Browse files Browse the repository at this point in the history
  • Loading branch information
guofei9987 committed Dec 31, 2023
1 parent 3fd6ca9 commit 7758033
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 29 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# HideInfo

Info Hiding Library
一些原理简洁的信息隐藏方法
一些小而美的信息隐藏方法



Expand Down
16 changes: 9 additions & 7 deletions example/example_echo_watermark.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import numpy as np
from hide_info.echo_watermark import EchoWatermark, get_error_rate, get_snr
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" # 嵌入水印后的文件名
wm_str = "回声水印算法,欢迎 star!" # 水印


wm_bits = utils.bytes2bin(wm_str.encode('utf-8'))
len_wm_bits = len(wm_bits)

Expand All @@ -22,7 +21,7 @@
wm_str_extract = utils.bin2bytes(wm_extract).decode('utf-8', errors='replace')
print("解出水印:", wm_str_extract)
# 错误率:
get_error_rate(wm_extract, wm_bits)
evaluate.get_error_rate(wm_extract, wm_bits)

# %% There are 3 algorithms:
wm_bits = np.random.randint(2, size=200).tolist()
Expand All @@ -32,6 +31,9 @@
echo_wm = EchoWatermark(pwd=111001, algo_type=algo_type)
echo_wm.embed(origin_filename=ori_file, wm_bits=wm_bits, embed_filename=embedded_file)
wm_extract = echo_wm.extract(embed_filename=embedded_file, len_wm_bits=len_wm_bits)
error_rate = get_error_rate(wm_extract, wm_bits)
get_snr(embedded_file, ori_file)
error_rate = evaluate.get_error_rate(wm_extract, wm_bits)
assert error_rate <= 0.03

_, ori_signal = wavfile.read(ori_file)
_, wm_signal = wavfile.read(embedded_file)
evaluate.get_snr(ori_signal, wm_signal)
2 changes: 1 addition & 1 deletion hide_info/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.1.6'
__version__ = '0.1.7'
20 changes: 0 additions & 20 deletions hide_info/echo_watermark.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,23 +167,3 @@ def extract(self, embed_filename, len_wm_bits):

return wm_extract


def get_error_rate(wm_extract, wm_bits):
# 计算错误率
len_wm_bits = len(wm_bits)
error_num = np.sum(np.abs(wm_extract - wm_bits))
error_rate = error_num / len_wm_bits
print(f'bit error rate = {error_rate:.2%} ({error_num} / {len_wm_bits})')
return error_rate


def get_snr(file_with_wm, orig_file):
sr, ori_signal = wavfile.read(orig_file)
_, wm_signal = wavfile.read(file_with_wm)

SNR = 10 * np.log10(
np.sum(np.square(ori_signal.astype(np.float32)))
/ np.sum(np.square(ori_signal.astype(np.float32)
- wm_signal.astype(np.float32))))
print(f'SNR = {SNR:.2f} dB')
return SNR
18 changes: 18 additions & 0 deletions hide_info/evaluate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import numpy as np


def get_error_rate(wm_extract, wm_bits):
len_wm_bits = len(wm_bits)
error_num = np.sum(np.abs(wm_extract - wm_bits))
error_rate = error_num / len_wm_bits
print(f'bit error rate = {error_rate:.2%} ({error_num} / {len_wm_bits})')
return error_rate


def get_snr(ori_signal, wm_signal):
snr = 10 * np.log10(
np.sum(np.square(ori_signal.astype(np.float32)))
/ np.sum(np.square(ori_signal.astype(np.float32)
- wm_signal.astype(np.float32))))
print(f'SNR = {snr:.2f} dB')
return snr

0 comments on commit 7758033

Please sign in to comment.