-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
102 lines (78 loc) · 2.89 KB
/
app.py
File metadata and controls
102 lines (78 loc) · 2.89 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
99
100
101
102
import os
import uuid
from flask import Flask, request, jsonify
from werkzeug.utils import secure_filename
from utils.text_recognition import TextRecognizer
from utils.barcode_detection import BarcodeDetector
import json
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'uploads'
app.config['ALLOWED_EXTENSIONS'] = {'png', 'jpg', 'jpeg'}
if not os.path.exists(app.config['UPLOAD_FOLDER']):
os.makedirs(app.config['UPLOAD_FOLDER'])
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in app.config['ALLOWED_EXTENSIONS']
def process_file(file, processor_func):
if file.filename == '':
return jsonify({"error": "No selected file"}), 400
if file and allowed_file(file.filename):
log_id = str(uuid.uuid4().int)
filename = secure_filename(f"{log_id}.png")
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(file_path)
results = processor_func(file_path)
response = {
"log_id": log_id,
"result_num": len(results),
"results": results
}
json_path = os.path.join(app.config['UPLOAD_FOLDER'], f"{log_id}.json")
with open(json_path, 'w', encoding='utf-8') as json_file:
json.dump(response, json_file, ensure_ascii=False, indent=4)
return jsonify(response)
return jsonify({"error": "File type not allowed"}), 400
@app.route('/ocr', methods=['POST'])
def ocr():
if 'file' not in request.files:
return jsonify({"error": "No file part"}), 400
file = request.files['file']
def process_ocr(file_path):
recognizer = TextRecognizer()
ocr_results = recognizer.recognize_text_from_image(file_path)
return [
{
"words": text,
"location": {
"top": bbox[1],
"left": bbox[0],
"width": bbox[2],
"height": bbox[3]
}
}
for text, bbox in ocr_results
]
return process_file(file, process_ocr)
@app.route('/barcode', methods=['POST'])
def barcode():
if 'file' not in request.files:
return jsonify({"error": "No file part"}), 400
file = request.files['file']
def process_barcode(file_path):
detector = BarcodeDetector()
barcode_results = detector.detect_barcodes_from_image(file_path)
return [
{
"value": value,
"symbology": symbology,
"location": {
"top": bbox[1],
"left": bbox[0],
"width": bbox[2],
"height": bbox[3]
}
}
for value, symbology, bbox in barcode_results
]
return process_file(file, process_barcode)
if __name__ == '__main__':
app.run(host="0.0.0.0", port=5001, debug=False)