-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
59 lines (45 loc) · 2 KB
/
Copy pathapp.py
File metadata and controls
59 lines (45 loc) · 2 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
from flask import Flask, request, jsonify
import os
import uuid
import json
from werkzeug.utils import secure_filename
from scripts.read_fields import extract_nid_data
app = Flask(__name__)
# Configure upload folder
UPLOAD_FOLDER = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'uploads')
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
# Configure allowed extensions
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'}
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/health', methods=['GET'])
def health_check():
return jsonify({'status': 'ok', 'message': 'NID Extraction Server is running'})
@app.route('/extract', methods=['POST'])
def extract_nid():
# Check if the post request has the file part
if 'image' not in request.files:
return jsonify({'error': 'No image part in the request'}), 400
file = request.files['image']
# If user does not select file, browser also submits an empty part without filename
if file.filename == '':
return jsonify({'error': 'No selected file'}), 400
if file and allowed_file(file.filename):
# Generate a unique filename to avoid collisions
filename = str(uuid.uuid4()) + '_' + secure_filename(file.filename)
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(filepath)
# Extract data from the NID image
try:
save_output = request.form.get('save_output', 'false').lower() == 'true'
result = extract_nid_data(filepath, save_output)
# Add the file path to the result
result['file_path'] = filepath
return jsonify(result)
except Exception as e:
return jsonify({'error': str(e)}), 500
return jsonify({'error': 'File type not allowed'}), 400
if __name__ == '__main__':
port = int(5001)
app.run(host='0.0.0.0', port=port, debug=True)