-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.py
46 lines (33 loc) · 1.18 KB
/
app.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
from flask import Flask, request, jsonify
from PIL import Image
import os
from pathlib import Path
from src.ocr_captcha.pipeline.prediction_pipeline import PredictionPipline
app = Flask(__name__)
@app.route('/')
def index():
return open(Path('templates/index2.html')).read()
@app.route('/upload', methods=['POST'])
def upload():
if 'image' not in request.files:
return jsonify({'error': 'No image uploaded'})
image_file = request.files['image']
if image_file.filename == '':
return jsonify({'error': 'No selected image'})
# Save the uploaded image to a temporary file
temp_image_path = 'temp_image.jpg'
image_file.save(temp_image_path)
# Perform OCR on the uploaded image
extracted_text = perform_ocr(temp_image_path)
# Delete the temporary image file
os.remove(temp_image_path)
return jsonify({'text': extracted_text, 'image_path': temp_image_path})
def perform_ocr(image_path):
print("In ocr")
obj = PredictionPipline(Path('artifact/model.weights.h5'))
print("loaded Model")
text = obj.predict(image_path)
print(text)
return text
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=8000)