git_Josh commited on
Commit
6b99da0
·
1 Parent(s): b087f41

add application file

Browse files
Files changed (1) hide show
  1. app.py +69 -0
app.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pathlib import Path
2
+ import torch
3
+ import gradio as gr
4
+ from torch import nn
5
+ import cv2
6
+ import numpy as np
7
+
8
+ BASE_DIR = Path(__file__).parent
9
+ LABELS = (BASE_DIR / "class_names.txt").read_text().splitlines()
10
+
11
+ model = nn.Sequential(
12
+ nn.Conv2d(1, 32, 3, padding="same"),
13
+ nn.ReLU(),
14
+ nn.MaxPool2d(2),
15
+ nn.Conv2d(32, 64, 3, padding="same"),
16
+ nn.ReLU(),
17
+ nn.MaxPool2d(2),
18
+ nn.Conv2d(64, 128, 3, padding="same"),
19
+ nn.ReLU(),
20
+ nn.MaxPool2d(2),
21
+ nn.Flatten(),
22
+ nn.Linear(1152, 256),
23
+ nn.ReLU(),
24
+ nn.Linear(256, len(LABELS)),
25
+ )
26
+ state_dict = torch.load(BASE_DIR /"pytorch_model.bin", map_location="cpu")
27
+ model.load_state_dict(state_dict, strict=False)
28
+ model.eval()
29
+
30
+
31
+ def predict(im):
32
+ # 检查是否为空画布
33
+ if isinstance(im, dict):
34
+ # 空画布
35
+ if "layers" in im and (not im["layers"]):
36
+ return {"请先在画板上画图": 1.0}
37
+ # 优先用composite
38
+ if "composite" in im and im["composite"] is not None:
39
+ im = im["composite"]
40
+ # 否则用layers[0]
41
+ elif "layers" in im and isinstance(im["layers"], list) and len(im["layers"]) > 0:
42
+ im = im["layers"][0]
43
+ else:
44
+ return {"无法识别输入": 1.0}
45
+ # 转为numpy数组
46
+ im = np.array(im)
47
+ # 只取第一个通道(灰度)
48
+ if im.ndim == 3:
49
+ im = im[..., 0]
50
+ # resize 到模型训练时的尺寸(假设24x24)
51
+ im = cv2.resize(im, (24, 24), interpolation=cv2.INTER_AREA)
52
+ x = torch.tensor(im, dtype=torch.float32).unsqueeze(0).unsqueeze(0) / 255.0
53
+ with torch.no_grad():
54
+ out = model(x)
55
+ probabilities = torch.nn.functional.softmax(out[0], dim=0)
56
+ values, indices = torch.topk(probabilities, 5)
57
+ return {LABELS[i]: v.item() for i, v in zip(indices, values)}
58
+
59
+ interface = gr.Interface(
60
+ predict,
61
+ inputs="sketchpad",
62
+ outputs="label",
63
+ # theme="huggingface",
64
+ title="Sketch Recognition",
65
+ description="Who wants to play Pictionary? Draw a common object like a shovel or a laptop, and the algorithm will guess in real time!",
66
+ article="<p style='text-align: center'>Sketch Recognition | Demo Model</p>",
67
+ # live=True,
68
+ )
69
+ interface.launch(share=True)