-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
54 lines (43 loc) · 1.45 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import io
import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import transforms
from PIL import Image
from flask import Flask, request, jsonify
from captcha_cnn import CaptchaCNN
app = Flask(__name__)
transform = transforms.Compose([
transforms.Resize((38, 112)),
transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,))
])
model = CaptchaCNN()
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
device = torch.device("cpu")
model.load_state_dict(torch.load("model/captcha_model_1000_ry.pth", map_location=torch.device('cpu')))
model.to(device) # 将模型移到GPU
model.eval()
def predict_image(file):
image = Image.open(io.BytesIO(file.read())).convert("RGB")
image = transform(image).unsqueeze(0)
image = image.to(device)
output = model(image)
_, predicted = torch.max(output, 2)
return ''.join(str(digit.item()) for digit in predicted[0])
@app.route("/predict", methods=['POST'])
def predict():
if 'file' not in request.files:
return jsonify({'error': 'No file part'}), 400
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No selected file'}), 400
if file:
predict_result = predict_image(file)
json_result = {
"predict": str(predict_result)
}
return jsonify(json_result), 200
if __name__ == "__main__":
app.run(host='0.0.0.0', port=15556, debug=False)