YassineYousfi
commited on
Commit
·
79a2092
1
Parent(s):
f78d55d
fix
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import stc
|
| 2 |
-
import cv2
|
| 3 |
import random
|
| 4 |
import numpy as np
|
| 5 |
import gradio as gr
|
|
@@ -22,40 +22,40 @@ description = '''Explore hiding messages in images using content adaptive stega
|
|
| 22 |
def HILL(input_image, operation, message, key):
|
| 23 |
tmp_name = str(random.randint(100,500))
|
| 24 |
try:
|
| 25 |
-
input_image
|
| 26 |
-
buffer = input_image.read()
|
| 27 |
I = cv2.imdecode(np.frombuffer(buffer, np.uint8), 1)
|
| 28 |
I = cv2.cvtColor(I,cv2.COLOR_BGR2GRAY)
|
| 29 |
cv2.imwrite('tmp/'+tmp_name+'.png',I)
|
| 30 |
-
except:
|
|
|
|
| 31 |
raise ValueError('Unable to read image')
|
| 32 |
|
| 33 |
-
if operation == 'decode':
|
| 34 |
try:
|
| 35 |
stc.extract('tmp/'+tmp_name+'.png', key, 'tmp/'+tmp_name+'.txt')
|
| 36 |
return 'tmp/'+tmp_name+'.txt'
|
| 37 |
except:
|
| 38 |
raise ValueError('Unable to decode')
|
| 39 |
-
|
| 40 |
-
else:
|
| 41 |
H = np.array(
|
| 42 |
[[-1, 2, -1],
|
| 43 |
[ 2, -4, 2],
|
| 44 |
[-1, 2, -1]])
|
| 45 |
L1 = np.ones((3, 3)).astype('float32')/(3**2)
|
| 46 |
L2 = np.ones((15, 15)).astype('float32')/(15**2)
|
| 47 |
-
costs = signal.convolve2d(I, H, mode='same', boundary='symm')
|
| 48 |
costs = abs(costs)
|
| 49 |
-
costs = signal.convolve2d(costs, L1, mode='same', boundary='symm')
|
| 50 |
costs = 1/costs
|
| 51 |
-
costs = signal.convolve2d(costs, L2, mode='same', boundary='symm')
|
| 52 |
costs[costs == np.inf] = 1
|
| 53 |
stc.embed('tmp/'+tmp_name+'.png', costs, message, key, 'tmp/'+tmp_name+'.png')
|
| 54 |
return 'tmp/'+tmp_name+'.png'
|
| 55 |
|
| 56 |
-
iface = gr.Interface(HILL,
|
| 57 |
-
['
|
| 58 |
-
|
| 59 |
examples=[['tmp/8825.png', 'encode', 'This is a secret message', 'secret-key'],
|
| 60 |
['tmp/9390.png', 'encode', 'This is another secret message', 'secret-key-2']],
|
| 61 |
title=title,
|
|
|
|
| 1 |
import stc
|
| 2 |
+
import cv2
|
| 3 |
import random
|
| 4 |
import numpy as np
|
| 5 |
import gradio as gr
|
|
|
|
| 22 |
def HILL(input_image, operation, message, key):
|
| 23 |
tmp_name = str(random.randint(100,500))
|
| 24 |
try:
|
| 25 |
+
buffer = input_image
|
|
|
|
| 26 |
I = cv2.imdecode(np.frombuffer(buffer, np.uint8), 1)
|
| 27 |
I = cv2.cvtColor(I,cv2.COLOR_BGR2GRAY)
|
| 28 |
cv2.imwrite('tmp/'+tmp_name+'.png',I)
|
| 29 |
+
except Exception as e:
|
| 30 |
+
print(e)
|
| 31 |
raise ValueError('Unable to read image')
|
| 32 |
|
| 33 |
+
if operation == 'decode':
|
| 34 |
try:
|
| 35 |
stc.extract('tmp/'+tmp_name+'.png', key, 'tmp/'+tmp_name+'.txt')
|
| 36 |
return 'tmp/'+tmp_name+'.txt'
|
| 37 |
except:
|
| 38 |
raise ValueError('Unable to decode')
|
| 39 |
+
|
| 40 |
+
else:
|
| 41 |
H = np.array(
|
| 42 |
[[-1, 2, -1],
|
| 43 |
[ 2, -4, 2],
|
| 44 |
[-1, 2, -1]])
|
| 45 |
L1 = np.ones((3, 3)).astype('float32')/(3**2)
|
| 46 |
L2 = np.ones((15, 15)).astype('float32')/(15**2)
|
| 47 |
+
costs = signal.convolve2d(I, H, mode='same', boundary='symm')
|
| 48 |
costs = abs(costs)
|
| 49 |
+
costs = signal.convolve2d(costs, L1, mode='same', boundary='symm')
|
| 50 |
costs = 1/costs
|
| 51 |
+
costs = signal.convolve2d(costs, L2, mode='same', boundary='symm')
|
| 52 |
costs[costs == np.inf] = 1
|
| 53 |
stc.embed('tmp/'+tmp_name+'.png', costs, message, key, 'tmp/'+tmp_name+'.png')
|
| 54 |
return 'tmp/'+tmp_name+'.png'
|
| 55 |
|
| 56 |
+
iface = gr.Interface(fn=HILL,
|
| 57 |
+
inputs=[gr.components.File(type='binary'), gr.components.Radio(['encode', 'decode']), gr.components.Textbox(), gr.components.Textbox()],
|
| 58 |
+
outputs=gr.components.File(),
|
| 59 |
examples=[['tmp/8825.png', 'encode', 'This is a secret message', 'secret-key'],
|
| 60 |
['tmp/9390.png', 'encode', 'This is another secret message', 'secret-key-2']],
|
| 61 |
title=title,
|