{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install stegano","metadata":{"execution":{"iopub.status.busy":"2021-12-04T07:08:24.109164Z","iopub.execute_input":"2021-12-04T07:08:24.109540Z","iopub.status.idle":"2021-12-04T07:08:34.538117Z","shell.execute_reply.started":"2021-12-04T07:08:24.109497Z","shell.execute_reply":"2021-12-04T07:08:34.537030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nfrom stegano import lsb #USED FOR PNG IMAGE\nimport skimage.io as sk\nimport matplotlib.pyplot as plt\nfrom scipy import spatial\nfrom tqdm import tqdm\n\nfrom PIL import Image\nfrom random import shuffle\n\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# /kaggle/input/alaska2-image-steganalysis/sample_submission.csv\n# /kaggle/input/alaska2-image-steganalysis/Cover/64601.jpg","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-12-04T07:08:34.540374Z","iopub.execute_input":"2021-12-04T07:08:34.540606Z","iopub.status.idle":"2021-12-04T07:08:35.386907Z","shell.execute_reply.started":"2021-12-04T07:08:34.540579Z","shell.execute_reply":"2021-12-04T07:08:35.386063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = sk.imread(\"/kaggle/input/alaska2-image-steganalysis/Cover/64601.jpg\")\n\nsecret = lsb.hide(\"/kaggle/input/alaska2-image-steganalysis/Cover/64601.jpg\", \"duoduo\")\nsecret.save(\"encoded.png\")","metadata":{"execution":{"iopub.status.busy":"2021-12-04T07:08:35.388203Z","iopub.execute_input":"2021-12-04T07:08:35.388507Z","iopub.status.idle":"2021-12-04T07:08:35.632189Z","shell.execute_reply.started":"2021-12-04T07:08:35.388469Z","shell.execute_reply":"2021-12-04T07:08:35.631335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img1 = sk.imread(\"/kaggle/input/alaska2-image-steganalysis/Cover/64601.jpg\")\nimg2 = sk.imread(\"/kaggle/working/encoded.png\")\n\nfig,ax = plt.subplots(1,2,figsize=(18,8))\n    \nax[0].imshow(img1)\nax[1].imshow(img2)","metadata":{"execution":{"iopub.status.busy":"2021-12-04T07:08:35.633521Z","iopub.execute_input":"2021-12-04T07:08:35.633911Z","iopub.status.idle":"2021-12-04T07:08:36.368950Z","shell.execute_reply.started":"2021-12-04T07:08:35.633869Z","shell.execute_reply":"2021-12-04T07:08:36.368060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(lsb.reveal(\"/kaggle/working/encoded.png\"))\n\n(img1 - img2).sum()","metadata":{"execution":{"iopub.status.busy":"2021-12-04T07:08:36.370722Z","iopub.execute_input":"2021-12-04T07:08:36.370949Z","iopub.status.idle":"2021-12-04T07:08:36.394053Z","shell.execute_reply.started":"2021-12-04T07:08:36.370923Z","shell.execute_reply":"2021-12-04T07:08:36.393014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Knaive\nvec1 = np.reshape(img1,(512*512*3))\nvec2 = np.reshape(img2,(512*512*3))\n\n# cosine similarity\nprint(spatial.distance.cosine(vec1,vec2))\nprint(1 - spatial.distance.cosine(vec1,vec2))","metadata":{"execution":{"iopub.status.busy":"2021-12-04T07:08:36.395230Z","iopub.execute_input":"2021-12-04T07:08:36.395458Z","iopub.status.idle":"2021-12-04T07:08:36.408755Z","shell.execute_reply.started":"2021-12-04T07:08:36.395430Z","shell.execute_reply":"2021-12-04T07:08:36.407892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Data exploration","metadata":{}},{"cell_type":"code","source":"BASE_PATH = \"/kaggle/input/alaska2-image-steganalysis\"\ntrain_imageids = pd.Series(os.listdir(BASE_PATH + '/Cover')).sort_values(ascending=True).reset_index(drop=True)\ntest_imageids = pd.Series(os.listdir(BASE_PATH + '/Test')).sort_values(ascending=True).reset_index(drop=True)\n\ntrain_imageids","metadata":{"execution":{"iopub.status.busy":"2021-12-04T07:08:36.410479Z","iopub.execute_input":"2021-12-04T07:08:36.411039Z","iopub.status.idle":"2021-12-04T07:08:38.205296Z","shell.execute_reply.started":"2021-12-04T07:08:36.410995Z","shell.execute_reply":"2021-12-04T07:08:38.204520Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cover_images_path = pd.Series(BASE_PATH + '/Cover/' + train_imageids ).sort_values(ascending=True)\nJMIPOD_images_path = pd.Series(BASE_PATH + '/JMiPOD/'+train_imageids).sort_values(ascending=True)\nJUNIWARD_images_path = pd.Series(BASE_PATH + '/JUNIWARD/'+train_imageids).sort_values(ascending=True)\nUERD_images_path = pd.Series(BASE_PATH + '/UERD/'+train_imageids).sort_values(ascending=True)\ntest_images_path = pd.Series(BASE_PATH + '/Test/'+test_imageids).sort_values(ascending=True)\nss = pd.read_csv(f'{BASE_PATH}/sample_submission.csv')\n\nss","metadata":{"execution":{"iopub.status.busy":"2021-12-04T07:08:38.206483Z","iopub.execute_input":"2021-12-04T07:08:38.206707Z","iopub.status.idle":"2021-12-04T07:08:38.460373Z","shell.execute_reply.started":"2021-12-04T07:08:38.206682Z","shell.execute_reply":"2021-12-04T07:08:38.459442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(nrows=2, ncols=2, figsize=(30, 25))\nk=0\nfor i, row in enumerate(ax):\n    for j, col in enumerate(row):\n        img = sk.imread(cover_images_path[k])\n        col.imshow(img)\n        col.set_title(cover_images_path[k])\n        k=k+1\nplt.suptitle('Cover 图', fontsize=14)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-04T07:28:31.796153Z","iopub.execute_input":"2021-12-04T07:28:31.796577Z","iopub.status.idle":"2021-12-04T07:28:34.122392Z","shell.execute_reply.started":"2021-12-04T07:28:31.796530Z","shell.execute_reply":"2021-12-04T07:28:34.121397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 展示cover 加 三种编码的 stego\nfig, ax = plt.subplots(nrows=3, ncols=4, figsize=(30, 15))\nfor i in range(3):\n    cvimg = sk.imread(cover_images_path[i])\n    uniimg = sk.imread(JUNIWARD_images_path[i])\n    jpodimg = sk.imread(JMIPOD_images_path[i])\n    uerdimg = sk.imread(UERD_images_path[i])\n    \n    ax[i,0].imshow(cvimg)\n    ax[i,0].set_title('Cover_IMG'+train_imageids[i])\n    ax[i,1].imshow(uniimg)\n    ax[i,1].set_title('JNIWARD_IMG'+train_imageids[i])\n    ax[i,2].imshow(jpodimg)\n    ax[i,2].set_title('JMiPOD_IMG'+train_imageids[i])\n    ax[i,3].imshow(uerdimg)\n    ax[i,3].set_title('UERD_IMG'+train_imageids[i])","metadata":{"execution":{"iopub.status.busy":"2021-12-04T07:31:36.532376Z","iopub.execute_input":"2021-12-04T07:31:36.532870Z","iopub.status.idle":"2021-12-04T07:31:39.302824Z","shell.execute_reply.started":"2021-12-04T07:31:36.532812Z","shell.execute_reply":"2021-12-04T07:31:39.301809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 查看隐写区域\nimg_cover = sk.imread(cover_images_path[0])\nimg_jmipod = sk.imread(JMIPOD_images_path[0])\nimg_juniward = sk.imread(JUNIWARD_images_path[0])\nimg_uerd = sk.imread(UERD_images_path[0])\n\nfig, ax = plt.subplots(nrows=3, ncols=4, figsize=(16, 12))\n\nax[0,1].set_title(f'{train_imageids[k]} Channel 0')\nax[0,2].set_title(f'{train_imageids[k]} Channel 1')\nax[0,3].set_title(f'{train_imageids[k]} Channel 2')\n\nax[0,0].imshow(img_jmipod)\nax[0,1].imshow((img_cover == img_jmipod).astype(int)[:,:,0])\nax[0,2].imshow((img_cover == img_jmipod).astype(int)[:,:,1])\nax[0,3].imshow((img_cover == img_jmipod).astype(int)[:,:,2])\nax[0,0].set_ylabel('JMiPOD', rotation=90, size='large')\n\nax[1,0].imshow(img_juniward)\nax[1,1].imshow((img_cover == img_juniward).astype(int)[:,:,0])\nax[1,2].imshow((img_cover == img_juniward).astype(int)[:,:,1])\nax[1,3].imshow((img_cover == img_juniward).astype(int)[:,:,2])\nax[1,0].set_ylabel('JUNIWARD', rotation=90, size='large')\n\nax[2,0].imshow(img_uerd)\nax[2,1].imshow((img_cover == img_uerd).astype(int)[:,:,0])\nax[2,2].imshow((img_cover == img_uerd).astype(int)[:,:,1])\nax[2,3].imshow((img_cover == img_uerd).astype(int)[:,:,2])\nax[2,0].set_ylabel('UERD', rotation=90, size='large')\n\nplt.suptitle('Pixel Deviation from Cover Image')\nplt.show()\n\n(img_cover == img_juniward).shape","metadata":{"execution":{"iopub.status.busy":"2021-12-04T07:43:40.726191Z","iopub.execute_input":"2021-12-04T07:43:40.726473Z","iopub.status.idle":"2021-12-04T07:43:42.395432Z","shell.execute_reply.started":"2021-12-04T07:43:40.726442Z","shell.execute_reply":"2021-12-04T07:43:42.394903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 在YCbCr颜色空间上查看图像的三个通道 Cover\nfig,ax = plt.subplots(3,4,figsize=(20,16))\n\nfor i,paths in enumerate(cover_images_path[:3]):\n    image = Image.open(paths)\n    ycbcr = image.convert('YCbCr')\n    (y, cb, cr) = ycbcr.split()\n\n    ax[i,0].imshow(image)\n    ax[i,0].set_title('Cover'+train_imageids[i])\n    ax[i,1].imshow(y)\n    ax[i,1].set_title('Y : Luminance')\n    ax[i,2].imshow(cb)\n    ax[i,2].set_title('Cb : Chroma Blue')\n    ax[i,3].imshow(cr)\n    ax[i,3].set_title('Cr : Chroma Red')","metadata":{"execution":{"iopub.status.busy":"2021-12-04T08:18:17.730564Z","iopub.execute_input":"2021-12-04T08:18:17.730895Z","iopub.status.idle":"2021-12-04T08:18:20.443535Z","shell.execute_reply.started":"2021-12-04T08:18:17.730857Z","shell.execute_reply":"2021-12-04T08:18:20.442477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 查看三种隐写的 YCbCr 通道\nfig,ax = plt.subplots(4,4,figsize=(20,16))\nplt.tight_layout()\n\n\nim1 = Image.open(cover_images_path[0])\nim2 = Image.open(JUNIWARD_images_path[0])\nim3 = Image.open(JMIPOD_images_path[0])\nim4 = Image.open(UERD_images_path[0])\n\nfor i,image in enumerate([im1,im2,im3,im4]):\n    ycbcr = image.convert('YCbCr')\n    (y, cb, cr) = ycbcr.split()\n\n    ax[i,0].imshow(image)\n    ax[i,0].set_title('Image')\n    ax[i,1].imshow(y)\n    ax[i,1].set_title('Luminance')\n    ax[i,2].imshow(cb)\n    ax[i,2].set_title('Cb:Chroma Blue')\n    ax[i,3].imshow(cr)\n    ax[i,3].set_title('Cr:Chroma Red')","metadata":{"execution":{"iopub.status.busy":"2021-12-04T08:17:55.539403Z","iopub.execute_input":"2021-12-04T08:17:55.539691Z","iopub.status.idle":"2021-12-04T08:17:59.146617Z","shell.execute_reply.started":"2021-12-04T08:17:55.539664Z","shell.execute_reply":"2021-12-04T08:17:59.145502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! git clone https://github.com/dwgoon/jpegio\n!pip install jpegio/.\nimport jpegio as jio","metadata":{"execution":{"iopub.status.busy":"2021-12-04T08:20:31.766557Z","iopub.execute_input":"2021-12-04T08:20:31.766884Z","iopub.status.idle":"2021-12-04T08:20:43.372336Z","shell.execute_reply.started":"2021-12-04T08:20:31.766835Z","shell.execute_reply":"2021-12-04T08:20:43.371608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 查看三个通道的DCT系数\nfig,ax = plt.subplots(4,4,figsize=(20,16))\nplt.tight_layout()\n\nfor i,path in enumerate([cover_images_path[0],JUNIWARD_images_path[0],JMIPOD_images_path[0],UERD_images_path[0]]):\n    \n    image = Image.open(path)\n    jpeg = jio.read(path)\n    DCT_Y = jpeg.coef_arrays[0]\n    DCT_Cr = jpeg.coef_arrays[1]\n    DCT_Cb = jpeg.coef_arrays[2]\n    \n    \n    ax[i,0].imshow(image)\n    ax[i,0].set_title('Image')\n    ax[i,1].imshow(DCT_Y)\n    ax[i,1].set_title('Luminance')\n    ax[i,2].imshow(DCT_Cb)\n    ax[i,2].set_title('Cb:Chroma Blue')\n    ax[i,3].imshow(DCT_Cr)\n    ax[i,3].set_title('Cr:Chroma Red')","metadata":{"execution":{"iopub.status.busy":"2021-12-04T08:23:24.065546Z","iopub.execute_input":"2021-12-04T08:23:24.065898Z","iopub.status.idle":"2021-12-04T08:23:27.305280Z","shell.execute_reply.started":"2021-12-04T08:23:24.065854Z","shell.execute_reply":"2021-12-04T08:23:27.304135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 查看DCT系数区别\ncoverDCT = np.zeros([512,512,3])\nstegoDCT = np.zeros([512,512,3])\njpeg = jio.read(cover_images_path[0])\nstego_juni = jio.read(JUNIWARD_images_path[0])\n\ncoverDCT[:,:,0] = jpeg.coef_arrays[0]\ncoverDCT[:,:,1] = jpeg.coef_arrays[1]\ncoverDCT[:,:,2] = jpeg.coef_arrays[2]\nstegoDCT[:,:,0] = stego_juni.coef_arrays[0]\nstegoDCT[:,:,1] = stego_juni.coef_arrays[1]\nstegoDCT[:,:,2] = stego_juni.coef_arrays[2]\n\nDCT_diff = coverDCT - stegoDCT\nprint(\"DCT系数相异数目：\",len(DCT_diff[np.where(DCT_diff!=0)]))\nprint(\"DCT差异量：\",np.unique(DCT_diff))\nplt.figure(figsize=(16,10))\nplt.imshow(abs(DCT_diff) )\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-04T08:36:11.956390Z","iopub.execute_input":"2021-12-04T08:36:11.956671Z","iopub.status.idle":"2021-12-04T08:36:12.354407Z","shell.execute_reply.started":"2021-12-04T08:36:11.956643Z","shell.execute_reply":"2021-12-04T08:36:12.353581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 从像素入手查看差别\ncoverPixels = np.array(Image.open(cover_images_path[0])).astype('float')\nstegoPixels = np.array(Image.open(JUNIWARD_images_path[0])).astype('float')\n\npixelsDiff = coverPixels - stegoPixels\n\nprint(len(pixelsDiff[np.where(pixelsDiff!=0)]))\nprint(np.unique(pixelsDiff))\nplt.figure(figsize=(16,10))\nplt.imshow(abs(pixelsDiff))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-04T08:47:58.102284Z","iopub.execute_input":"2021-12-04T08:47:58.103147Z","iopub.status.idle":"2021-12-04T08:47:58.492894Z","shell.execute_reply.started":"2021-12-04T08:47:58.103112Z","shell.execute_reply":"2021-12-04T08:47:58.492247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 对比两个 diff 图\nfig,ax = plt.subplots(1,2,figsize=(16,12))\nax[0].imshow(abs(DCT_diff))\nax[1].imshow(abs(pixelsDiff))","metadata":{"execution":{"iopub.status.busy":"2021-12-04T08:48:15.122020Z","iopub.execute_input":"2021-12-04T08:48:15.122896Z","iopub.status.idle":"2021-12-04T08:48:15.663364Z","shell.execute_reply.started":"2021-12-04T08:48:15.122839Z","shell.execute_reply":"2021-12-04T08:48:15.662577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}