def func(cls, args, container, watermark, params, default_metrics, callback): th = (args[0], args[1], args[2]) max_b = 2**int(args[3]) min_b = 2**int(args[4]) if min_b > max_b: max_b = min_b qp = args[5] sc = args[6] params = copy(params) params['homogeneity_threshold'] = th params['min_block_size'] = min_b params['max_block_size'] = max_b params['quant_power'] = qp params['ch_scale'] = sc qtar = QtarStego(**params) maxbpp = len(container.getbands()) * 8 try: embed_result = qtar.embed(container, watermark) container_image = embed_result.img_container stego_image = embed_result.img_stego watermark = embed_result.img_watermark extracted_wm = qtar.extract(stego_image, embed_result.key) cls.eval_callback(callback) psnr_ = psnr(container_image, stego_image) bcr_ = bcr(watermark, extracted_wm) bpp_ = embed_result.bpp except: return 1 return sqrt((-1 / psnr_)**2 + (1 - bcr_)**2 + (1 - bpp_ / maxbpp)**2)
def func(cls, args, container, watermark, params, default_metrics, callback): def_psnr = default_metrics['container psnr'] def_bpp = default_metrics['container bpp'] th = (args[0], args[1], args[2]) x = int(args[3]) y = int(args[4]) params = copy(params) params['homogeneity_threshold'] = th params['offset'] = (x, y) qtar = QtarStego(**params) embed_result = qtar.embed(container, watermark) cls.eval_callback(callback) container_image = embed_result.img_container stego_image = embed_result.img_stego psnr_ = psnr(container_image, stego_image) bpp_ = embed_result.bpp func = -((psnr_ - def_psnr) / def_psnr + (bpp_ - def_bpp) / def_bpp) if psnr_ < def_psnr or bpp_ < def_bpp: return func + 1000 return func
def func(cls, offset, container, watermark, params, default_metrics, callback): params = copy(params) params['offset'] = array(offset).astype(int) qtar = QtarStego(**params) embed_result = qtar.embed(container, watermark) cls.eval_callback(callback) watermark = embed_result.img_watermark extracted_wm = qtar.extract(embed_result.img_stego, embed_result.key) return -bcr(watermark, extracted_wm)
def func(cls, offset, container, watermark, params, default_metrics, callback): params = copy(params) params['offset'] = array(offset).astype(int) qtar = QtarStego(**params) embed_result = qtar.embed(container, watermark) cls.eval_callback(callback) container_image = embed_result.img_container stego_image = embed_result.img_stego return -psnr(container_image, stego_image)
def func(cls, threshold, container, watermark, params, default_metrics, callback): params = copy(params) params['homogeneity_threshold'] = threshold qtar = QtarStego(**params) embed_result = qtar.embed(container, watermark) cls.eval_callback(callback) watermark = embed_result.img_watermark extracted_wm = qtar.extract(embed_result.img_stego, embed_result.key) return -bcr(watermark, extracted_wm)
def func(cls, args, container, watermark, params, default_metrics, callback): def_psnr_c = default_metrics['container psnr'] def_psnr_w = default_metrics['watermark psnr'] def_bpp = default_metrics['container bpp'] new_params = { 'homogeneity_threshold': (args[0], args[1], args[2]), 'min_block_size': 2**int(args[3]), 'cf_grid_size': int(args[4]), 'wmdct_block_size': 2**int(args[5]), 'offset': (int(args[6]), int(args[7])) } params = copy(params) params.update(new_params) qtar = QtarStego(**params) try: embed_result = qtar.embed(container, watermark) wm_extracted = qtar.extract(embed_result.img_stego, embed_result.key) except Exception as e: cls.eval_callback(callback, error=e, new_params=new_params, f=cls.best_one) return 0 psnr_c = psnr(embed_result.img_container, embed_result.img_stego) psnr_w = psnr(embed_result.img_watermark, wm_extracted) bpp_ = embed_result.bpp func = -((psnr_c - def_psnr_c) / def_psnr_c + (bpp_ - def_bpp) / def_bpp + (psnr_w - def_psnr_w) / def_psnr_w) if psnr_c < def_psnr_c or bpp_ < def_bpp or psnr_w < def_psnr_w: func = 0 if func < cls.best_one: cls.best_one = func cls.eval_callback(callback, f=cls.best_one) return func
def func(cls, args, container, watermark, params, default_metrics, callback): max_b = 2**int(args[0]) qp = args[1] sc = args[2] params = copy(params) params['max_block_size'] = max_b params['quant_power'] = qp params['ch_scale'] = sc qtar = QtarStego(**params) embed_result = qtar.embed(container, watermark) cls.eval_callback(callback) watermark = embed_result.img_watermark extracted_wm = qtar.extract(embed_result.img_stego, embed_result.key) return -bcr(watermark, extracted_wm)
def func(cls, args, container, watermark, params, default_metrics, callback): max_b = 2**int(args[0]) qp = args[1] sc = args[2] params = copy(params) params['max_block_size'] = max_b params['quant_power'] = qp params['ch_scale'] = sc qtar = QtarStego(**params) embed_result = qtar.embed(container, watermark) cls.eval_callback(callback) container_image = embed_result.img_container stego_image = embed_result.img_stego return -psnr(container_image, stego_image)
def embed(params, filter_=None): container = Image.open(params['container']) if 'container_size' in params and params['container_size']: container = container.resize(params['container_size'], Image.BILINEAR) watermark = Image.open(params['watermark']) if 'watermark_size' in params and params['watermark_size']: watermark = watermark.resize(params['watermark_size'], Image.BILINEAR) qtar = QtarStego(**params) embed_result = qtar.embed(container, watermark) container = embed_result.img_container stego = embed_result.img_stego wm = embed_result.img_watermark key = embed_result.key bpp_ = embed_result.bpp psnr_container = psnr(container, stego) ssim_container = ssim(container, stego) if filter_ is not None: stego = filter_(stego) extracted_wm = qtar.extract(stego, key) bcr_wm = bcr(wm, extracted_wm) psnr_wm = psnr(wm, extracted_wm) ssim_wm = ssim(wm, extracted_wm) return { "stego img": stego, "extracted wm": extracted_wm, "container psnr": psnr_container, "container ssim": ssim_container, "watermark psnr": psnr_wm, "watermark ssim": ssim_wm, "watermark bcr": bcr_wm, "container bpp": bpp_, "key size": key.size }
def embed(params): container = Image.open(params['container']) if params['container_size']: container = container.resize(params['container_size'], Image.BILINEAR) watermark = Image.open(params['watermark']) if params['watermark_size']: watermark = watermark.resize(params['watermark_size'], Image.BILINEAR) qtar = QtarStego(**params) try: with benchmark("Embedded in "): embed_result = qtar.embed(container, watermark, stages=True) except NoSpaceError as e: print(e) return stego = embed_result.img_stego key = embed_result.key if params['stego'] is None: stego_path = 'stego_%s.png' % extract_filename(params['container']) else: stego_path = params['stego'] save_file(stego, stego_path) save_file(key, params['key']) bpp_ = embed_result.bpp psnr_container = psnr(container, stego) ssim_container = ssim(container, stego) metrics_info = METRICS_INFO_TEMPLATE.format(psnr_container=psnr_container, ssim_container=ssim_container, bpp=bpp_, key=key) print(metrics_info)
def test_save_open(self): embed_result = QtarStego().embed(self.container, self.watermark) key = embed_result.key key.save(TESTING_KEY_PATH) opened_key = Key.open(TESTING_KEY_PATH) self.assertAlmostEqual(opened_key.ch_scale, key.ch_scale, 3, "ch_scale") self.assertEqual(opened_key.offset, key.offset, "offset") self.assertEqual(opened_key.wm_shape, key.wm_shape, "wm_shape") for ch in range(len(key.chs_qt_key)): self.assertEqual(opened_key.chs_qt_key[ch], key.chs_qt_key[ch], "qt_key")
def extract(params): key = Key.open(params['key']) stego_image = Image.open(params['stego']) with benchmark("extracted in"): secret_image = QtarStego.extract(stego_image, key) if params['wm_path']: si_path = params['wm_path'] else: stego_image_file_name = extract_filename(params['stego']) si_path = 'extracted_from_%s.png' % stego_image_file_name save_file(secret_image, si_path)
def func(cls, args, container, watermark, params, default_metrics, callback): th = (args[0], args[1], args[2]) x = int(args[3]) y = int(args[4]) qp = args[5] sc = args[6] params = copy(params) params['homogeneity_threshold'] = th params['offset'] = (x, y) params['quant_power'] = qp params['ch_scale'] = sc qtar = QtarStego(**params) maxbpp = len(container.getbands()) * 8 psnr_w = 0.999 bcr_w = 0.0005 bpp_w = 0.0005 try: embed_result = qtar.embed(container, watermark) container_image = embed_result.img_container stego_image = embed_result.img_stego watermark = embed_result.img_watermark extracted_wm = qtar.extract(stego_image, embed_result.key) cls.eval_callback(callback) psnr_ = psnr(container_image, stego_image) bcr_ = bcr(watermark, extracted_wm) bpp_ = embed_result.bpp except: return 1 return sqrt(psnr_w * (-1 / psnr_)**2 + bcr_w * (1 - bcr_)**2 + bpp_w * (1 - bpp_ / maxbpp)**2)
def test(params): container = Image.open(params['container']) if params['container_size']: container = container.resize(params['container_size'], Image.BILINEAR) watermark = Image.open(params['watermark']) if params['watermark_size']: watermark = watermark.resize(params['watermark_size'], Image.BILINEAR) embedding_info = EMBEDDING_INFO_TEMPLATE.format(**params) print(embedding_info) qtar = QtarStego(**params) try: with benchmark("embedded in "): embed_result = qtar.embed(container, watermark, stages=True) except NoSpaceError as e: print(e) return { "container psnr": 0, "container ssim": 0, "watermark psnr": 0, "watermark ssim": 0, "watermark bcr": 0, "container bpp": 0, "key size": 0 } container = embed_result.img_container stego = embed_result.img_stego wm = embed_result.img_watermark key = embed_result.key bpp_ = embed_result.bpp psnr_container = psnr(container, stego) ssim_container = ssim(container, stego) if 'ss' in params and params['ss']: save_stages( embed_result.stages_imgs, STAMP_TEMPLATE.format(psnr=psnr_container, ssim=ssim_container, bpp=bpp_) if params['st'] else None) if 'key' in params and params['key']: save_file(key, params['key']) key = Key.open(params['key']) key_info = KEY_INFO_TEMPLATE.format(key=key) print(key_info) with benchmark("extracted in"): extract_stages_imgs = qtar.extract(stego, key, stages=True) extracted_wm = extract_stages_imgs['9-extracted_watermark'] if 'ss' in params and params['ss']: save_stages(extract_stages_imgs) bcr_wm = bcr(wm, extracted_wm) psnr_wm = psnr(wm, extracted_wm) ssim_wm = ssim(wm, extracted_wm) metrics_info = METRICS_INFO_TEMPLATE.format(psnr_container=psnr_container, ssim_container=ssim_container, psnr_wm=psnr_wm, ssim_wm=ssim_wm, bpp=bpp_, bcr=bcr_wm, width=wm.size[0], height=wm.size[1]) print(metrics_info) print("_" * 40 + '\n') return { "container psnr": psnr_container, "container ssim": ssim_container, "watermark psnr": psnr_wm, "watermark ssim": ssim_wm, "watermark bcr": bcr_wm, "container bpp": bpp_, "key size": key.size }