Spaces:
Runtime error
Runtime error
Ahsen Khaliq
commited on
Commit
•
c5554e4
1
Parent(s):
66f9a76
Update app.py
Browse files
app.py
CHANGED
@@ -35,7 +35,7 @@ seed1 = np.frombuffer(seed1, dtype=np.float32)
|
|
35 |
seed1 = seed1.reshape([96, 128])
|
36 |
|
37 |
# Preprocess image
|
38 |
-
def inference(img):
|
39 |
image = Image.open(img.name).convert('RGB')
|
40 |
image = image.resize([360, 360])
|
41 |
arr = np.array(image).astype(np.float32) / 255.0
|
@@ -51,7 +51,23 @@ def inference(img):
|
|
51 |
hash_bits = ''.join(['1' if it >= 0 else '0' for it in hash_output])
|
52 |
hash_hex = '{:0{}x}'.format(int(hash_bits, 2), len(hash_bits) // 4)
|
53 |
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
title = "AppleNeuralHash"
|
57 |
description = "Gradio demo for Apple NeuralHash, a perceptual hashing method for images based on neural networks. It can tolerate image resize and compression. To use it, simply upload your image, or click one of the examples to load them. Read more at the links below."
|
@@ -60,8 +76,8 @@ examples = [['sunset.jpg']]
|
|
60 |
|
61 |
gr.Interface(
|
62 |
inference,
|
63 |
-
gr.inputs.Image(type="file", label="Input"),
|
64 |
-
gr.outputs.Textbox(label="Output"),
|
65 |
title=title,
|
66 |
description=description,
|
67 |
article=article,
|
|
|
35 |
seed1 = seed1.reshape([96, 128])
|
36 |
|
37 |
# Preprocess image
|
38 |
+
def inference(img, img2):
|
39 |
image = Image.open(img.name).convert('RGB')
|
40 |
image = image.resize([360, 360])
|
41 |
arr = np.array(image).astype(np.float32) / 255.0
|
|
|
51 |
hash_bits = ''.join(['1' if it >= 0 else '0' for it in hash_output])
|
52 |
hash_hex = '{:0{}x}'.format(int(hash_bits, 2), len(hash_bits) // 4)
|
53 |
|
54 |
+
|
55 |
+
image2 = Image.open(img2.name).convert('RGB')
|
56 |
+
image2 = image2.resize([360, 360])
|
57 |
+
arr2 = np.array(image2).astype(np.float32) / 255.0
|
58 |
+
arr2 = arr2 * 2.0 - 1.0
|
59 |
+
arr2 = arr2.transpose(2, 0, 1).reshape([1, 3, 360, 360])
|
60 |
+
|
61 |
+
# Run model
|
62 |
+
inputs2 = {session.get_inputs()[0].name: arr2}
|
63 |
+
outs2 = session.run(None, inputs2)
|
64 |
+
|
65 |
+
# Convert model output to hex hash
|
66 |
+
hash_output2 = seed1.dot(outs2[0].flatten())
|
67 |
+
hash_bits2 = ''.join(['1' if it >= 0 else '0' for it in hash_output2])
|
68 |
+
hash_hex2 = '{:0{}x}'.format(int(hash_bits2, 2), len(hash_bits2) // 4)
|
69 |
+
|
70 |
+
return hash_hex, hash_hex2
|
71 |
|
72 |
title = "AppleNeuralHash"
|
73 |
description = "Gradio demo for Apple NeuralHash, a perceptual hashing method for images based on neural networks. It can tolerate image resize and compression. To use it, simply upload your image, or click one of the examples to load them. Read more at the links below."
|
|
|
76 |
|
77 |
gr.Interface(
|
78 |
inference,
|
79 |
+
[gr.inputs.Image(type="file", label="Input Image"),gr.inputs.Image(type="file", label="Input Image")],
|
80 |
+
[gr.outputs.Textbox(label="Output"),gr.outputs.Textbox(label="Output")] ,
|
81 |
title=title,
|
82 |
description=description,
|
83 |
article=article,
|