Spaces:
Sleeping
Sleeping
Synced repo using 'sync_with_huggingface' Github Action
Browse files- gradio_app.py +113 -0
- requirements.txt +4 -0
gradio_app.py
ADDED
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import gradio as gr
|
3 |
+
|
4 |
+
from torchvision import transforms
|
5 |
+
from torchvision.transforms.functional import InterpolationMode
|
6 |
+
|
7 |
+
import os
|
8 |
+
import onnxruntime
|
9 |
+
import numpy as np
|
10 |
+
|
11 |
+
def predict_fault(image, model):
|
12 |
+
image = image.detach().cpu().numpy()
|
13 |
+
input = {model.get_inputs()[0].name: image}
|
14 |
+
output = model.run(None, input)
|
15 |
+
preds = np.argmax(output[0], 1)
|
16 |
+
return preds.item()
|
17 |
+
|
18 |
+
def detect(image, writing_type, post_it, corner, empty):
|
19 |
+
writing_type_model, post_it_model, corner_model, empty_model = models
|
20 |
+
|
21 |
+
res_dict = {}
|
22 |
+
|
23 |
+
if writing_type:
|
24 |
+
input_image = writing_type_transforms(image).unsqueeze(0)
|
25 |
+
label = predict_fault(input_image, writing_type_model)
|
26 |
+
res_dict['writing_type'] = label
|
27 |
+
|
28 |
+
if post_it:
|
29 |
+
input_image = data_transforms(image).unsqueeze(0)
|
30 |
+
label = predict_fault(input_image, post_it_model)
|
31 |
+
res_dict['post_it'] = label
|
32 |
+
|
33 |
+
if corner:
|
34 |
+
input_image = data_transforms(image).unsqueeze(0)
|
35 |
+
label = predict_fault(input_image, corner_model)
|
36 |
+
res_dict['corner'] = label
|
37 |
+
|
38 |
+
if empty:
|
39 |
+
input_image = empty_transforms(image).unsqueeze(0)
|
40 |
+
label = predict_fault(input_image, empty_model)
|
41 |
+
res_dict['empty'] = 1 - label
|
42 |
+
|
43 |
+
return res_dict
|
44 |
+
|
45 |
+
def load_models():
|
46 |
+
try:
|
47 |
+
MODEL_PATH = os.environ.get("MODEL_PATH", './models/')
|
48 |
+
POST_IT_MODEL = os.environ.get("POST_IT_MODEL", 'post_it_model.onnx')
|
49 |
+
CORNER_MODEL = os.environ.get("CORNER_MODEL", 'corner_model.onnx')
|
50 |
+
EMPTY_MODEL = os.environ.get("EMPTY_MODEL", 'empty_v5_24_08_23.onnx')
|
51 |
+
WRITING_TYPE_MODEL = os.environ.get("WRITING_TYPE_MODEL", 'writing_type_v1.onnx')
|
52 |
+
|
53 |
+
print(f"ORT device: {onnxruntime.get_device()}")
|
54 |
+
|
55 |
+
# Load the models and the trained weights
|
56 |
+
writing_type_model = onnxruntime.InferenceSession(os.path.join(MODEL_PATH, WRITING_TYPE_MODEL))
|
57 |
+
post_it_model = onnxruntime.InferenceSession(os.path.join(MODEL_PATH, POST_IT_MODEL))
|
58 |
+
corner_model = onnxruntime.InferenceSession(os.path.join(MODEL_PATH, CORNER_MODEL))
|
59 |
+
empty_model = onnxruntime.InferenceSession(os.path.join(MODEL_PATH, EMPTY_MODEL))
|
60 |
+
|
61 |
+
return writing_type_model, post_it_model, corner_model, empty_model
|
62 |
+
except Exception as e:
|
63 |
+
print("Failed to load pretrained models: {}".format(e))
|
64 |
+
|
65 |
+
# Load the models
|
66 |
+
models = load_models()
|
67 |
+
|
68 |
+
# Transform methods for corner & post-it model inputs
|
69 |
+
data_transforms = transforms.Compose([
|
70 |
+
transforms.Resize((224, 224)),
|
71 |
+
transforms.ToTensor()
|
72 |
+
])
|
73 |
+
|
74 |
+
# Transform methods for empty model inputs
|
75 |
+
empty_transforms = transforms.Compose([
|
76 |
+
transforms.Resize((224, 224), interpolation=InterpolationMode.BICUBIC),
|
77 |
+
transforms.ToTensor(),
|
78 |
+
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
|
79 |
+
])
|
80 |
+
|
81 |
+
# Transform methods for writing-type model inputs
|
82 |
+
writing_type_transforms = transforms.Compose([
|
83 |
+
transforms.Resize((224, 224), interpolation=InterpolationMode.BICUBIC),
|
84 |
+
transforms.ToTensor(),
|
85 |
+
transforms.Normalize([0.882, 0.883, 0.899], [0.088, 0.089, 0.094])
|
86 |
+
])
|
87 |
+
|
88 |
+
|
89 |
+
with gr.Blocks(title="Image Faulty Demo") as demo:
|
90 |
+
gr.Markdown("""
|
91 |
+
# Image Faulty
|
92 |
+
Find the project [here](https://github.com/xiaoyao9184/image-faulty).
|
93 |
+
""")
|
94 |
+
|
95 |
+
with gr.Row():
|
96 |
+
with gr.Column():
|
97 |
+
detecting_img = gr.Image(label="Input Image", type="pil", height=512)
|
98 |
+
with gr.Column():
|
99 |
+
writing_ckb = gr.Checkbox(label="Writing type", value=True)
|
100 |
+
postit_ckb = gr.Checkbox(label="Post it", value=True)
|
101 |
+
corner_ckb = gr.Checkbox(label="Folded corner", value=True)
|
102 |
+
empty_ckb = gr.Checkbox(label="Parper empty", value=True)
|
103 |
+
detecting_btn = gr.Button("Detect")
|
104 |
+
predicted_messages = gr.JSON(label="Detected Messages")
|
105 |
+
|
106 |
+
detecting_btn.click(
|
107 |
+
fn=detect,
|
108 |
+
inputs=[detecting_img, writing_ckb, postit_ckb, corner_ckb, empty_ckb],
|
109 |
+
outputs=[predicted_messages]
|
110 |
+
)
|
111 |
+
|
112 |
+
if __name__ == '__main__':
|
113 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio==5.8.0
|
2 |
+
onnxruntime==1.20.1
|
3 |
+
torchvision==0.13.0
|
4 |
+
numpy==1.21.6
|