Spaces:
Runtime error
Runtime error
Ahsen Khaliq
commited on
Commit
•
53db93c
1
Parent(s):
8aa85ca
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright 2021 Asuhariet Ygvar
|
2 |
+
#
|
3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
+
# you may not use this file except in compliance with the License.
|
5 |
+
# You may obtain a copy of the License at
|
6 |
+
#
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
#
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
|
12 |
+
# or implied. See the License for the specific language governing
|
13 |
+
# permissions and limitations under the License.
|
14 |
+
|
15 |
+
import sys
|
16 |
+
import onnxruntime
|
17 |
+
import numpy as np
|
18 |
+
from PIL import Image
|
19 |
+
import torchtext
|
20 |
+
|
21 |
+
|
22 |
+
torchtext.utils.download_from_url("https://drive.google.com/uc?id=146kojNCMfswRbWNE4NKRyOiRnGs12rZN", root=".")
|
23 |
+
torchtext.utils.download_from_url("https://drive.google.com/uc?id=1a3omUl96fKkvFgUHsR3RD9W3kjSAgJFK", root=".")
|
24 |
+
|
25 |
+
|
26 |
+
|
27 |
+
# Load ONNX model
|
28 |
+
session = onnxruntime.InferenceSession('model.onnx')
|
29 |
+
|
30 |
+
# Load output hash matrix
|
31 |
+
seed1 = open('neuralhash_128x96_seed1.dat', 'rb').read()[128:]
|
32 |
+
seed1 = np.frombuffer(seed1, dtype=np.float32)
|
33 |
+
seed1 = seed1.reshape([96, 128])
|
34 |
+
|
35 |
+
# Preprocess image
|
36 |
+
def inference(img):
|
37 |
+
image = Image.open(img.name).convert('RGB')
|
38 |
+
image = image.resize([360, 360])
|
39 |
+
arr = np.array(image).astype(np.float32) / 255.0
|
40 |
+
arr = arr * 2.0 - 1.0
|
41 |
+
arr = arr.transpose(2, 0, 1).reshape([1, 3, 360, 360])
|
42 |
+
|
43 |
+
# Run model
|
44 |
+
inputs = {session.get_inputs()[0].name: arr}
|
45 |
+
outs = session.run(None, inputs)
|
46 |
+
|
47 |
+
# Convert model output to hex hash
|
48 |
+
hash_output = seed1.dot(outs[0].flatten())
|
49 |
+
hash_bits = ''.join(['1' if it >= 0 else '0' for it in hash_output])
|
50 |
+
hash_hex = '{:0{}x}'.format(int(hash_bits, 2), len(hash_bits) // 4)
|
51 |
+
|
52 |
+
return hash_hex
|
53 |
+
|
54 |
+
title = "Anime2Sketch"
|
55 |
+
description = "demo for Anime2Sketch. To use it, simply upload your image, or click one of the examples to load them. Read more at the links below."
|
56 |
+
article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2104.05703'>Adversarial Open Domain Adaption for Sketch-to-Photo Synthesis</a> | <a href='https://github.com/Mukosame/Anime2Sketch'>Github Repo</a></p>"
|
57 |
+
|
58 |
+
gr.Interface(
|
59 |
+
inference,
|
60 |
+
gr.inputs.Image(type="file", label="Input"),
|
61 |
+
gr.outputs.Textbox(label="Output Text"),
|
62 |
+
title=title,
|
63 |
+
description=description,
|
64 |
+
article=article
|
65 |
+
).launch(debug=True)
|