Ajay98 commited on
Commit
4a8cf98
·
verified ·
1 Parent(s): 2142f33

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +110 -64
app.py CHANGED
@@ -1,74 +1,120 @@
1
- from flask import Flask, request, jsonify, render_template
2
- from google.cloud import vision
3
- import io
4
- import pandas as pd
5
  import requests
6
- from salesforce_api import Salesforce
7
- from datasets import load_dataset
8
  import os
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
- app = Flask(__name__)
11
-
12
- # Initialize Google Cloud Vision client
13
- vision_client = vision.ImageAnnotatorClient()
14
-
15
- # Initialize Salesforce client
16
- sf = Salesforce(
17
- username='your_salesforce_username',
18
- password='your_salesforce_password',
19
- security_token='your_salesforce_security_token'
20
- )
21
-
22
- # Load dataset from Hugging Face
23
- dataset = load_dataset('Ajay98/setajsondataset')
24
- json_data = pd.DataFrame(dataset['train'])
25
-
26
- @app.route('/')
27
- def index():
28
- return render_template('index.html')
29
-
30
- @app.route('/upload', methods=['POST'])
31
- def upload_image():
32
- if 'file' not in request.files:
33
- return "No file part"
34
-
35
- file = request.files['file']
36
 
37
- if file.filename == '':
38
- return "No selected file"
 
 
 
 
39
 
40
- if file:
41
- # Extract text from the image
42
- extracted_text = extract_text_from_image(file)
 
 
 
 
 
 
 
 
43
 
44
- # Compare extracted text with JSON dataset
45
- matched_data = compare_data(extracted_text, json_data)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
 
47
- # Update Salesforce with structured data
48
- update_salesforce(matched_data)
 
49
 
50
- return jsonify({"extracted_text": extracted_text, "matched_data": matched_data.to_dict(orient='records')})
 
 
 
51
 
52
- def extract_text_from_image(image_file):
53
- content = image_file.read()
54
- image = vision.Image(content=content)
55
- response = vision_client.text_detection(image=image)
56
- texts = response.text_annotations
57
- extracted_text = texts[0].description if texts else ''
58
- return extracted_text
 
 
 
59
 
60
- def compare_data(extracted_text, json_data):
61
- matches = json_data[json_data['column_name'].str.contains(extracted_text, case=False, na=False)]
62
- return matches
63
-
64
- def update_salesforce(data):
65
- for _, row in data.iterrows():
66
- sf.Contact.create({
67
- 'LastName': row['LastName'],
68
- 'FirstName': row['FirstName'],
69
- 'Email': row['Email'],
70
- 'Phone': row['Phone']
71
- })
72
-
73
- if __name__ == '__main__':
74
- app.run(debug=True)
 
1
+ import gradio as gr
2
+ import tensorflow as tf
3
+ import keras_ocr
 
4
  import requests
5
+ import cv2
 
6
  import os
7
+ import csv
8
+ import numpy as np
9
+ import pandas as pd
10
+ import huggingface_hub
11
+ from huggingface_hub import Repository
12
+ from datetime import datetime
13
+ import scipy.ndimage.interpolation as inter
14
+ import easyocr
15
+ import datasets
16
+ from datasets import load_dataset, Image
17
+ from PIL import Image
18
+ from paddleocr import PaddleOCR
19
+ from save_data import flag
20
+
21
+ """
22
+ Paddle OCR
23
+ """
24
+ def ocr_with_paddle(img):
25
+ finaltext = ''
26
+ ocr = PaddleOCR(lang='en', use_angle_cls=True)
27
+ # img_path = 'exp.jpeg'
28
+ result = ocr.ocr(img)
29
+
30
+ for i in range(len(result[0])):
31
+ text = result[0][i][1][0]
32
+ finaltext += ' '+ text
33
+ return finaltext
34
 
35
+ """
36
+ Keras OCR
37
+ """
38
+ def ocr_with_keras(img):
39
+ output_text = ''
40
+ pipeline=keras_ocr.pipeline.Pipeline()
41
+ images=[keras_ocr.tools.read(img)]
42
+ predictions=pipeline.recognize(images)
43
+ first=predictions[0]
44
+ for text,box in first:
45
+ output_text += ' '+ text
46
+ return output_text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
+ """
49
+ easy OCR
50
+ """
51
+ # gray scale image
52
+ def get_grayscale(image):
53
+ return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
54
 
55
+ # Thresholding or Binarization
56
+ def thresholding(src):
57
+ return cv2.threshold(src,127,255, cv2.THRESH_TOZERO)[1]
58
+ def ocr_with_easy(img):
59
+ gray_scale_image=get_grayscale(img)
60
+ thresholding(gray_scale_image)
61
+ cv2.imwrite('image.png',gray_scale_image)
62
+ reader = easyocr.Reader(['th','en'])
63
+ bounds = reader.readtext('image.png',paragraph="False",detail = 0)
64
+ bounds = ''.join(bounds)
65
+ return bounds
66
 
67
+ """
68
+ Generate OCR
69
+ """
70
+ def generate_ocr(Method,img):
71
+
72
+ text_output = ''
73
+ if (img).any():
74
+ add_csv = []
75
+ image_id = 1
76
+ print("Method___________________",Method)
77
+ if Method == 'EasyOCR':
78
+ text_output = ocr_with_easy(img)
79
+ if Method == 'KerasOCR':
80
+ text_output = ocr_with_keras(img)
81
+ if Method == 'PaddleOCR':
82
+ text_output = ocr_with_paddle(img)
83
+
84
+ try:
85
+ flag(Method,text_output,img)
86
+ except Exception as e:
87
+ print(e)
88
+ return text_output
89
+ else:
90
+ raise gr.Error("Please upload an image!!!!")
91
+
92
+ # except Exception as e:
93
+ # print("Error in ocr generation ==>",e)
94
+ # text_output = "Something went wrong"
95
+ # return text_output
96
+
97
 
98
+ """
99
+ Create user interface for OCR demo
100
+ """
101
 
102
+ # image = gr.Image(shape=(300, 300))
103
+ image = gr.Image()
104
+ method = gr.Radio(["PaddleOCR","EasyOCR", "KerasOCR"],value="PaddleOCR")
105
+ output = gr.Textbox(label="Output")
106
 
107
+ demo = gr.Interface(
108
+ generate_ocr,
109
+ [method,image],
110
+ output,
111
+ title="Optical Character Recognition",
112
+ css=".gradio-container {background-color: lightgray} #radio_div {background-color: #FFD8B4; font-size: 40px;}",
113
+ article = """<p style='text-align: center;'>Feel free to give us your thoughts on this demo and please contact us at
114
+ <a href="mailto:ajayaklaram@gmail.com" target="_blank">aajayk999@gmail.com</a>
115
+ <p style='text-align: center;'>Developed by: <a href="https://www.sathkrutha.com" target="_blank">sathkrutha tech solutions</a></p>"""
116
+
117
 
118
+ )
119
+ # demo.launch(enable_queue = False)
120
+ demo.launch()