skytnt's picture
init
1bdd3b2
raw history blame
No virus
1.04 kB
import cv2
import numpy as np
import gradio as gr
import onnxruntime as rt
from huggingface_hub import hf_hub_download
def predict(img):
img = img.astype(np.float32) / 255
s = 640
h, w = img.shape[:-1]
h, w = (s, int(s * w / h)) if h > w else (int(s * h / w), s)
ph, pw = s - h, s - w
img_input = np.zeros([s, s, 3], dtype=np.float32)
img_input[ph // 2:ph // 2 + h, pw // 2:pw // 2 + w] = cv2.resize(img, (w, h))
img_input = np.transpose(img_input, (2, 0, 1))
img_input = img_input[np.newaxis, :]
pred = model.run(None, {"img": img_input})[0][0]
return {"not best": pred[0].item(), "best": pred[1].item()}
if __name__ == "__main__":
model_path = hf_hub_download(repo_id="skytnt/anime_quality", filename="classifier.onnx")
model = rt.InferenceSession(model_path, providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])
app = gr.Interface(predict, gr.Image(label="input image"), gr.Label(label="result"),title="Best Anime or Not", allow_flagging="never")
app.launch()