Download app.py from wjlin/IP_DEMO: direct link, hf CLI and curl.
- Browser
- Download file 14.4 kB
-
https://huggingface.co/spaces/wjlin/IP_DEMO/resolve/main/app.py
- Command line
-
hf download hf://spaces/wjlin/IP_DEMO/app.py
-
curl -L -o app.py https://huggingface.co/spaces/wjlin/IP_DEMO/resolve/main/app.py
14.4 kB
| #!/usr/bin/env python | |
| # coding: utf-8 | |
| # In[1]: | |
| #get_ipython().system('pip3 install gradio') | |
| #get_ipython().system('pip3 install pyqrcode') | |
| #get_ipython().system('pip3 install pypng') | |
| # In[2]: | |
| import numpy as np | |
| import gradio as gr | |
| from matplotlib import pyplot as plt | |
| from skimage.color import rgb2lab, lab2rgb, rgb2hsv, hsv2rgb, rgb2gray | |
| from skimage.util import invert | |
| import cv2 | |
| from PIL import Image | |
| from steganography import Steganography | |
| from sklearn.cluster import KMeans | |
| from sklearn.metrics import pairwise_distances_argmin | |
| from sklearn.datasets import load_sample_image | |
| from sklearn.utils import shuffle | |
| def bp_slicing(x, ch): | |
| grayImg = rgb2gray(x) | |
| grayImg = np.uint8(grayImg*255) | |
| r,g,b = x[...,0],x[...,1],x[...,2] | |
| y = grayImg.copy() | |
| if ch == 0: | |
| y = grayImg.copy() | |
| elif ch == 1: | |
| y = r.copy() | |
| elif ch == 2: | |
| y = g.copy() | |
| elif ch == 3: | |
| y = b.copy() | |
| out = np.full((y.shape[0], y.shape[1], 8), 0, np.uint8) | |
| for k in range(0, 8): | |
| # create an image for each k bit plane | |
| plane = np.full((y.shape[0], y.shape[1]), 2 ** k, np.uint8) | |
| # execute bitwise and operation | |
| res = cv2.bitwise_and(plane, y) | |
| # multiply ones (bit plane sliced) with 255 just for better visualization | |
| out[...,k] = res * 255 | |
| return grayImg,r,g,b,out[...,7],out[...,6],out[...,5],out[...,4],out[...,3],out[...,2],out[...,1],out[...,0] | |
| def merge_img(img1, img2): | |
| Steganography().merge(img1, img2).save("merged.png") | |
| return Steganography().merge(img1, img2) | |
| def unmerge_img(img): | |
| image = Image.open("merged.png") | |
| Steganography().unmerge(image).save("unmerged.png") | |
| return Steganography().unmerge(image) | |
| cho = ['gray image', 'R channel', 'G channel', 'B channel'] | |
| def color_decompose_RGB(x): | |
| im7 = x.copy() | |
| im7[...,0] = 0 | |
| im8 = x.copy() | |
| im8[...,1] = 0 | |
| im9 = x.copy() | |
| im9[...,2] = 0 | |
| return im7, im8, im9 | |
| def color_decompose_Lab(x): | |
| im1 = rgb2lab(x) | |
| im1[...,0] = 0 | |
| im1 = lab2rgb(im1) | |
| im2 = rgb2lab(x) | |
| im2[...,1] = 0 | |
| im2 = lab2rgb(im2) | |
| im3 = rgb2lab(x) | |
| im3[...,2] = 0 | |
| im3 = lab2rgb(im3) | |
| return im1, im2, im3 | |
| def color_decompose_HSV(x): | |
| im4 = rgb2hsv(x) | |
| im4[...,0] = 0 | |
| im4 = hsv2rgb(im4) | |
| im5 = rgb2hsv(x) | |
| im5[...,1] = 0 | |
| im5 = hsv2rgb(im5) | |
| im6 = rgb2hsv(x) | |
| im6[...,2] = 0 | |
| im6 = hsv2rgb(im6) | |
| return im4, im5, im6 | |
| def color_inverted(x): | |
| im10 = invert(x) # RGB inverted | |
| im11 = rgb2lab(x) # Lab inverted | |
| im11[...,0] = np.max(im11[...,0]) - im11[...,0] | |
| im11 = lab2rgb(im11) | |
| im12 = rgb2hsv(x) # HSV inverted | |
| im12[...,0] = np.max(im12[...,0]) - im12[...,0] | |
| im12 = hsv2rgb(im12) | |
| return im10, im11, im12 | |
| def brighter_darker_image(x, value): | |
| im1 = rgb2lab(x) | |
| im1[...,0] = im1[...,0] + value | |
| im1 = lab2rgb(im1) | |
| im2 = rgb2lab(x) | |
| im2[...,0] = im2[...,0] - value | |
| im2 = lab2rgb(im2) | |
| return im1, im2 | |
| def recreate_image(codebook, labels, w, h): | |
| """Recreate the (compressed) image from the code book & labels""" | |
| return codebook[labels].reshape(w, h, -1) | |
| def color_quqntization(img, qtz_level): | |
| z = img.reshape((-1,3)) | |
| # convert to np.float32 | |
| z = np.float32(z) | |
| # define criteria, number of clusters(K) and apply kmeans() | |
| criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0) | |
| K = qtz_level | |
| ret,label,center=cv2.kmeans(z,K,None,criteria,10,cv2.KMEANS_RANDOM_CENTERS) | |
| # Convert back into uint8, and make original image | |
| center = np.uint8(center) | |
| res = center[label.flatten()] | |
| res2 = res.reshape((img.shape)) | |
| return res2 | |
| def image_quantization(img, h, m, l): | |
| h_qtz = color_quqntization(img, int(h)) | |
| m_qtz = color_quqntization(img, int(m)) | |
| l_qtz = color_quqntization(img, int(l)) | |
| return h_qtz, m_qtz, l_qtz | |
| def color_planes_RGB(x): | |
| return x[...,0], x[...,1], x[...,2] | |
| def color_planes_Lab(x): | |
| y_L = rgb2lab(x) | |
| y_a = y_L.copy() | |
| y_b = y_L.copy() | |
| y_L[...,1] = y_L[...,2] = 0 | |
| y_a[...,0] = y_a[...,2] = 0 | |
| y_b[...,0] = y_b[...,1] = 0 | |
| y_L = lab2rgb(y_L) | |
| y_a = lab2rgb(y_a) | |
| y_b = lab2rgb(y_b) | |
| return y_L, y_a, y_b | |
| def color_planes_HSV(x): | |
| z = rgb2hsv(x) | |
| return z[...,0], z[...,1], z[...,2] | |
| def img_hist(x): | |
| f = plt.figure() | |
| pl = x.histogram() | |
| plt.subplot(3,1,1); plt.bar(range(256), pl[:256], color='r', alpha=0.5) | |
| plt.subplot(3,1,2); plt.bar(range(256), pl[256:2*256], color='g', alpha=0.4) | |
| plt.subplot(3,1,3); plt.bar(range(256), pl[2*256:], color='b', alpha=0.3) | |
| plt.show() | |
| return f | |
| with gr.Blocks() as demo: | |
| gr.Markdown("Chapter 1: Color manipulation.") | |
| with gr.Tab("bit-plane slicing"): | |
| with gr.Row(): | |
| img_in = gr.Image(label="Color image") | |
| with gr.Row(): | |
| ex = gr.Examples(examples=["./lena_color.gif", "./Ishihara.png", "./image1.jpg", | |
| "./IMG_1955.jpg","./IMG_1958.jpg","./cars.jpg","./cycle.jpg", | |
| "./fish.jpg","./4.1.01.jpg","./4.1.04.jpg", | |
| "./4.2.03.jpg","./4.2.05.jpg","./4.2.07.jpg"], | |
| examples_per_page=15, inputs=img_in) | |
| with gr.Row(): | |
| ch = gr.components.Dropdown(label="image to be sliced", choices=cho) | |
| ok_btn = gr.Button("generate gray/R/G/B . and bit planes") | |
| with gr.Row(): | |
| with gr.Column(): | |
| img_gray = gr.Image(label="Gray image") | |
| img_r = gr.Image(label="R plane") | |
| with gr.Column(): | |
| img_g = gr.Image(label="G plane") | |
| img_b = gr.Image(label="B plane") | |
| with gr.Row(): | |
| with gr.Column(): | |
| imgb7 = gr.Image(label="the 7th bit plane") | |
| imgb6 = gr.Image(label="the 6th bit plane") | |
| with gr.Column(): | |
| imgb5 = gr.Image(label="the 5th bit plane") | |
| imgb4 = gr.Image(label="the 4th bit plane") | |
| with gr.Row(): | |
| with gr.Column(): | |
| imgb3 = gr.Image(label="the 3th bit plane") | |
| imgb2 = gr.Image(label="the 2th bit plane") | |
| with gr.Column(): | |
| imgb1 = gr.Image(label="the 1th bit plane") | |
| imgb0 = gr.Image(label="the 0th bit plane") | |
| ok_btn.click(bp_slicing, inputs=[img_in, ch],outputs=[img_gray,img_r,img_g,img_b,imgb7,imgb6,imgb5,imgb4,imgb3,imgb2,imgb1,imgb0]) | |
| with gr.Tab("merge/unmerge ."): | |
| with gr.Row(): | |
| img_in1 = gr.Image(label="Cover Image", type="pil") | |
| img_in2 = gr.Image(label="Image to be hidden", type="pil") | |
| with gr.Row(): | |
| ex1 = gr.Examples(examples=["./lena_color.gif", "./Ishihara.png", "./image1.jpg", | |
| "./IMG_1955.jpg","./IMG_1958.jpg","./cars.jpg","./cycle.jpg", | |
| "./fish.jpg","./4.1.01.jpg","./4.1.04.jpg", | |
| "./4.2.03.jpg","./4.2.05.jpg", | |
| "./4.2.07.jpg"],examples_per_page=5,inputs=[img_in1]) | |
| ex2 = gr.Examples(examples=["./lena_color.gif", "./Ishihara.png", "./image1.jpg", | |
| "./IMG_1955.jpg","./IMG_1958.jpg","./cars.jpg","./cycle.jpg", | |
| "./fish.jpg","./4.1.01.jpg","./4.1.04.jpg", | |
| "./4.2.03.jpg","./4.2.05.jpg", | |
| "./4.2.07.jpg"],examples_per_page=5,inputs=[img_in2]) | |
| with gr.Row(): | |
| mg_btn = gr.Button("merge these two .") | |
| with gr.Row(): | |
| img_out = gr.Image(label="merged image", type="pil") | |
| with gr.Row(): | |
| umg_btn = gr.Button("unmerge the image") | |
| with gr.Row(): | |
| img_umg = gr.Image(label="UNmerged", type="pil") | |
| mg_btn.click(merge_img, inputs=[img_in1, img_in2], outputs=img_out) | |
| umg_btn.click(unmerge_img, inputs=[img_out], outputs=img_umg) | |
| with gr.Tab("color space"): | |
| with gr.Row(): | |
| img_color_input = gr.Image() | |
| with gr.Row(): | |
| ex3 = gr.Examples(examples=["./lena_color.gif", "./Ishihara.png", "./image1.jpg", | |
| "./IMG_1955.jpg","./IMG_1958.jpg","./cars.jpg","./cycle.jpg", | |
| "./fish.jpg","./4.1.01.jpg","./4.1.04.jpg", | |
| "./4.2.03.jpg","./4.2.05.jpg", | |
| "./4.2.07.jpg"],examples_per_page=15,inputs=[img_color_input]) | |
| with gr.Row(): | |
| imgP1 = gr.Image() | |
| imgP2 = gr.Image() | |
| imgP3 = gr.Image() | |
| with gr.Row(): | |
| img_RGB_button = gr.Button("R/G/B planes") | |
| img_Lab_button = gr.Button("L/a/b planes") | |
| img_HSV_button = gr.Button("H/S/V planes") | |
| img_RGB_button.click(color_planes_RGB, inputs=img_color_input, outputs=[imgP1, imgP2, imgP3]) | |
| img_Lab_button.click(color_planes_Lab, inputs=img_color_input, outputs=[imgP1, imgP2, imgP3]) | |
| img_HSV_button.click(color_planes_HSV, inputs=img_color_input, outputs=[imgP1, imgP2, imgP3]) | |
| with gr.Row(): | |
| img_input = gr.Image() | |
| with gr.Row(): | |
| ex4 = gr.Examples(examples=["./lena_color.gif", "./Ishihara.png", "./image1.jpg", | |
| "./IMG_1955.jpg","./IMG_1958.jpg","./cars.jpg","./cycle.jpg", | |
| "./fish.jpg","./4.1.01.jpg","./4.1.04.jpg", | |
| "./4.2.03.jpg","./4.2.05.jpg", | |
| "./4.2.07.jpg"],examples_per_page=15,inputs=[img_input]) | |
| with gr.Row(): | |
| img1 = gr.Image() | |
| img2 = gr.Image() | |
| img3 = gr.Image() | |
| with gr.Row(): | |
| img_button1 = gr.Button("RGB to GB/RB/RG") | |
| img_button2 = gr.Button("Lab to ab/Lb/La") | |
| img_button3 = gr.Button("HSV to SV/HV/HS") | |
| img_button4 = gr.Button("Inverted RGB/Lab/HSV") | |
| img_button1.click(color_decompose_RGB, inputs=img_input, outputs=[img1, img2, img3]) | |
| img_button2.click(color_decompose_Lab, inputs=img_input, outputs=[img1, img2, img3]) | |
| img_button3.click(color_decompose_HSV, inputs=img_input, outputs=[img1, img2, img3]) | |
| img_button4.click(color_inverted, inputs=img_input, outputs=[img1, img2, img3]) | |
| with gr.Tab("color quantization"): | |
| with gr.Row(): | |
| img_org = gr.Image() | |
| h_in = gr.Textbox(label="High", placeholder="# colors") | |
| m_in = gr.Textbox(label="Medium", placeholder="# colors") | |
| l_in = gr.Textbox(label="Low", placeholder="# colors") | |
| with gr.Row(): | |
| ex5 = gr.Examples(examples=["./lena_color.gif", "./Ishihara.png", "./image1.jpg", | |
| "./IMG_1955.jpg","./IMG_1958.jpg","./cars.jpg","./cycle.jpg", | |
| "./fish.jpg","./4.1.01.jpg","./4.1.04.jpg", | |
| "./4.2.03.jpg","./4.2.05.jpg", | |
| "./4.2.07.jpg"],examples_per_page=15,inputs=[img_org]) | |
| color_qtz = gr.Button("Quantize the image") | |
| with gr.Row(): | |
| h_img = gr.Image(label="high # colors") | |
| m_img = gr.Image(label="medium # colors") | |
| l_img = gr.Image(label="low # colors") | |
| color_qtz.click(image_quantization, inputs=[img_org, h_in, m_in, l_in], outputs=[h_img, m_img, l_img]) | |
| with gr.Tab("brighter/darker"): | |
| with gr.Row(): | |
| image_input = gr.Image(label="source image") | |
| image_output1 = gr.Image(label="brighter image") | |
| image_output2 = gr.Image(label="darker image") | |
| with gr.Row(): | |
| ex6 = gr.Examples(examples=["./lena_color.gif", "./Ishihara.png", "./image1.jpg", | |
| "./IMG_1955.jpg","./IMG_1958.jpg","./cars.jpg","./cycle.jpg", | |
| "./fish.jpg","./4.1.01.jpg","./4.1.04.jpg", | |
| "./4.2.03.jpg","./4.2.05.jpg", | |
| "./4.2.07.jpg"],examples_per_page=15,inputs=[image_input]) | |
| slider_bar = gr.Slider(30, 80, step=10) | |
| image_button = gr.Button("Make image Brighter and Darker") | |
| image_button.click(brighter_darker_image, inputs=[image_input, slider_bar], outputs=[image_output1, image_output2]) | |
| with gr.Tab("Histogram"): | |
| with gr.Row(): | |
| image_hist_input = gr.Image(label="source image", type="pil") | |
| with gr.Row(): | |
| ex6 = gr.Examples(examples=["./lena_color.gif", "./Ishihara.png", "./image1.jpg", | |
| "./IMG_1955.jpg","./IMG_1958.jpg","./cars.jpg","./cycle.jpg", | |
| "./fish.jpg","./4.1.01.jpg","./4.1.04.jpg", | |
| "./4.2.03.jpg","./4.2.05.jpg", | |
| "./4.2.07.jpg"],examples_per_page=15,inputs=[image_hist_input]) | |
| image_hist_button = gr.Button("Histogram of R/G/B") | |
| with gr.Row(): | |
| hist_plot = gr.Plot(label="Histograms of R/G/B planes") | |
| image_hist_button.click(img_hist, inputs=[image_hist_input], outputs=[hist_plot]) | |
| demo.launch(share='true') | |
| # In[4]: | |
| #!pip3 install pyqrcode | |
| #!pip3 install pypng | |
| import os # for working with files | |
| import numpy as np # for numerical computationss | |
| import pandas as pd # for working with dataframes | |
| import matplotlib.pyplot as plt # for plotting informations on graph and . using tensors | |
| from matplotlib import image as mpimg | |
| from PIL import Image # for checking . | |
| import gradio as gr | |
| import pyqrcode | |
| from pyqrcode import QRCode | |
| qrstring = "https://4cf87b4c4f8c0d31d2.gradio.live" | |
| url = pyqrcode.create(qrstring) | |
| url.png("qrcode") | |
| plt.figure() | |
| plt.title("Chapter 1 Color manipulation") | |
| image = mpimg.imread("./qrcode") | |
| plt.imshow(image) | |
| plt.show() | |
| # In[ ]: | |