iukhan commited on
Commit
9c7c4bb
·
verified ·
1 Parent(s): 445550f

Create python app.py

Browse files
Files changed (1) hide show
  1. python app.py +40 -0
python app.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request
2
+ import gradio as gr
3
+ from transformers import TrOCRProcessor, VisionEncoderDecoderModel
4
+ from PIL import Image
5
+ import requests
6
+ import io
7
+
8
+ # Initialize Flask app
9
+ app = Flask(__name__)
10
+
11
+ # Load the processor and model
12
+ processor = TrOCRProcessor.from_pretrained('microsoft/trocr-large-handwritten')
13
+ model = VisionEncoderDecoderModel.from_pretrained('microsoft/trocr-large-handwritten')
14
+
15
+ def recognize_handwritten_text(image):
16
+ # Preprocess the image
17
+ pixel_values = processor(images=image, return_tensors="pt").pixel_values
18
+
19
+ # Generate text
20
+ generated_ids = model.generate(pixel_values)
21
+ generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
22
+
23
+ return generated_text
24
+
25
+ def capture_image(image):
26
+ # Convert the image to PIL format
27
+ image = Image.open(io.BytesIO(image))
28
+ # Recognize handwritten text
29
+ text = recognize_handwritten_text(image)
30
+ return text
31
+
32
+ # Create Gradio interface
33
+ iface = gr.Interface(fn=capture_image, inputs="image", outputs="text", live=True)
34
+
35
+ @app.route('/capture', methods=['GET'])
36
+ def capture():
37
+ return iface.launch(share=True)
38
+
39
+ if __name__ == '__main__':
40
+ app.run(debug=True)