Skip to content

Commit

Permalink
demonstrate multiprocessing and multithreading
Browse files Browse the repository at this point in the history
  • Loading branch information
guofei9987 committed Apr 9, 2023
1 parent b4e3812 commit 1e494d8
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ jobs:
python examples/example_img.py
python examples/example_no_writing.py
python examples/example_str.py
python examples/example_str_multi.py
# pytest --cov .
# - name: Upload coverage reports to Codecov with GitHub Action
# uses: codecov/codecov-action@v3
52 changes: 52 additions & 0 deletions examples/example_str_multi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
demonstrate multiprocessing and multithreading
'''
# embed string
from blind_watermark import WaterMark
import cv2
import os

os.chdir(os.path.dirname(__file__))

mode = 'multiprocessing'
bwm = WaterMark(password_img=1, password_wm=1, processes=mode)
bwm.read_img('pic/ori_img.jpeg')
wm = '@guofei9987 开源万岁!'
bwm.read_wm(wm, mode='str')
bwm.embed('output/embedded.png')

len_wm = len(bwm.wm_bit) # 解水印需要用到长度
print('Put down the length of wm_bit {len_wm}'.format(len_wm=len_wm))

ori_img_shape = cv2.imread('pic/ori_img.jpeg').shape[:2] # 抗攻击有时需要知道原图的shape
h, w = ori_img_shape

# %% 解水印
bwm1 = WaterMark(password_img=1, password_wm=1, processes=mode)
wm_extract = bwm1.extract('output/embedded.png', wm_shape=len_wm, mode='str')
print("不攻击的提取结果:", wm_extract)

assert wm == wm_extract, '提取水印和原水印不一致'

mode = 'multithreading'

bwm = WaterMark(password_img=1, password_wm=1, processes=mode)
bwm.read_img('pic/ori_img.jpeg')
wm = '@guofei9987 开源万岁!'
bwm.read_wm(wm, mode='str')
bwm.embed('output/embedded.png')

len_wm = len(bwm.wm_bit) # 解水印需要用到长度
print('Put down the length of wm_bit {len_wm}'.format(len_wm=len_wm))

ori_img_shape = cv2.imread('pic/ori_img.jpeg').shape[:2] # 抗攻击有时需要知道原图的shape
h, w = ori_img_shape

# %% 解水印
bwm1 = WaterMark(password_img=1, password_wm=1, processes=mode)
wm_extract = bwm1.extract('output/embedded.png', wm_shape=len_wm, mode='str')
print("不攻击的提取结果:", wm_extract)

assert wm == wm_extract, '提取水印和原水印不一致'

0 comments on commit 1e494d8

Please sign in to comment.