-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
executable file
·98 lines (91 loc) · 2.96 KB
/
server.py
File metadata and controls
executable file
·98 lines (91 loc) · 2.96 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/local/lib/python3.7/
# import barreader
import cv2
from flask import Flask
from flask import request
import json
import numpy as np
import PaddleOCR.tools.infer.utility as utility
import PaddleOCR.tools.infer.predict_system as pocr
import deskew
import subprocess
import os
import io
import pyheif
from PIL import Image
app = Flask(__name__)
@app.route('/bar', methods=['POST'])
def scanBar():
# Gets the image sent by the client
img_file = request.files['img'].read()
# Processes said image
img = preprocess(img_file)
barreader.read(img)
# Repackages results
dictobj = {'numBlocks': 3, 'texts': [], 'scores': [], 'blocks': []}
jsonobj = json.dumps(dictobj)
print('******')
return jsonobj
@app.route('/', methods=['POST'])
def scan():
# Gets the image sent by the client
print('Received request')
filein = request.files['img']
# Processes said image
args = utility.parse_args()
# args.det_model_dir = "./PaddleOCR/inference/det_sast_tt/"
# args.det_sast_polygon = True
# args.det_algorithm = "SAST"
img = convertcv2(filein)
img = preprocess(img)
print("processing")
dt_boxes, rec_res = pocr.process(args, img, filein.filename)
# Repackages results
print("packaging")
dictobj = {'numBlocks': len(rec_res), 'texts': [], 'scores': [], 'blocks': []}
# Reformats the texts and scores into separate lists
for text, score in rec_res:
dictobj['texts'].append(text)
dictobj['scores'].append(float(score))
# Reformats the texts' coordinates
for box in dt_boxes:
corners = []
for pair in box:
xy = []
for val in pair:
xy.append(int(val))
corners.append(xy)
dictobj['blocks'].append(corners)
jsonobj = json.dumps(dictobj)
print('******')
return jsonobj
def convertcv2(filein):
img_name = filein.filename
img_file = filein.read()
print("Got image: " + img_name)
# Converts filein to a cv2 image
if 'heic' in img_name:
heifimg = pyheif.read_heif(img_file)
pilimg = Image.frombytes(heifimg.mode, heifimg.size, heifimg.data, "raw",
heifimg.mode, heifimg.stride)
img = cv2.cvtColor(np.asarray(pilimg), cv2.COLOR_RGB2BGR)
filein.filename = filein.filename +'.jpg'
else:
# Converts string data from client to numpy array
npimg = np.fromstring(img_file, np.uint8)
# Converts numpy array to image
img = cv2.imdecode(npimg, cv2.IMREAD_UNCHANGED)
return img
def preprocess(img):
print("preprocessing")
# Deskews image
img = deskew.correct(img)
# blurs image
# filter = 1/9 * np.array([[1,1,1],[1,1,1],[1,1,1]])
# blurimg = cv2.filter2D(rotateimg,-1, filter)
# cv2.imshow("img", img)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
return img
if __name__ == '__main__':
app.run()