Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,26 +1,74 @@
|
|
1 |
-
from flask import Flask, request, jsonify
|
2 |
-
from handler import EndpointHandler
|
3 |
|
4 |
-
app = Flask(__name__)
|
5 |
-
handler = EndpointHandler()
|
6 |
|
7 |
-
@app.route('/process_image', methods=['POST'])
|
8 |
-
def process_image():
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
|
23 |
-
|
24 |
|
25 |
-
if __name__ == '__main__':
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# from flask import Flask, request, jsonify
|
2 |
+
# from handler import EndpointHandler
|
3 |
|
4 |
+
# app = Flask(__name__)
|
5 |
+
# handler = EndpointHandler()
|
6 |
|
7 |
+
# @app.route('/process_image', methods=['POST'])
|
8 |
+
# def process_image():
|
9 |
+
# data = request.json
|
10 |
+
# if data is None:
|
11 |
+
# return jsonify({'error': 'No JSON data received'}), 400
|
12 |
|
13 |
+
# try:
|
14 |
+
# result_image = handler(data)
|
15 |
+
# except Exception as e:
|
16 |
+
# return jsonify({'error': str(e)}), 500
|
17 |
|
18 |
+
# # Convert PIL image to base64 string
|
19 |
+
# buffered = BytesIO()
|
20 |
+
# result_image.save(buffered, format="JPEG")
|
21 |
+
# result_image_str = base64.b64encode(buffered.getvalue()).decode()
|
22 |
|
23 |
+
# return jsonify({'result_image': result_image_str})
|
24 |
|
25 |
+
# if __name__ == '__main__':
|
26 |
+
# app.run(debug=False,port=8001)
|
27 |
+
|
28 |
+
import easyocr as ocr #OCR
|
29 |
+
import streamlit as st #Web App
|
30 |
+
from PIL import Image #Image Processing
|
31 |
+
import numpy as np #Image Processing
|
32 |
+
|
33 |
+
#title
|
34 |
+
st.title("Easy OCR - Extract Text from Images")
|
35 |
+
|
36 |
+
#subtitle
|
37 |
+
st.markdown("## Optical Character Recognition - Using `easyocr`, `streamlit` - hosted on 🤗 Spaces")
|
38 |
+
|
39 |
+
st.markdown("Link to the app - [image-to-text-app on 🤗 Spaces](https://huggingface.co/spaces/Amrrs/image-to-text-app)")
|
40 |
+
|
41 |
+
#image uploader
|
42 |
+
image = st.file_uploader(label = "Upload your image here",type=['png','jpg','jpeg'])
|
43 |
+
|
44 |
+
|
45 |
+
@st.cache
|
46 |
+
def load_model():
|
47 |
+
reader = ocr.Reader(['en'],model_storage_directory='.')
|
48 |
+
return reader
|
49 |
+
|
50 |
+
reader = load_model() #load model
|
51 |
+
|
52 |
+
if image is not None:
|
53 |
+
|
54 |
+
input_image = Image.open(image) #read image
|
55 |
+
st.image(input_image) #display image
|
56 |
+
|
57 |
+
with st.spinner("🤖 AI is at Work! "):
|
58 |
+
|
59 |
+
|
60 |
+
result = reader.readtext(np.array(input_image))
|
61 |
+
|
62 |
+
result_text = [] #empty list for results
|
63 |
+
|
64 |
+
|
65 |
+
for text in result:
|
66 |
+
result_text.append(text[1])
|
67 |
+
|
68 |
+
st.write(result_text)
|
69 |
+
#st.success("Here you go!")
|
70 |
+
st.balloons()
|
71 |
+
else:
|
72 |
+
st.write("Upload an Image")
|
73 |
+
|
74 |
+
st.caption("Made with ❤️ by @1littlecoder. Credits to 🤗 Spaces for Hosting this ")
|