File size: 11,344 Bytes
347eef9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# import os
# import re
# from aiohttp import ClientSession
# from google.cloud import vision
# from io import BytesIO
# import google.generativeai as genai
# import traceback
# import nltk
# from nltk.corpus import stopwords
# from nltk.tokenize import word_tokenize
# from nltk.stem import WordNetLemmatizer
# from string import punctuation
# from PIL import Image
# import io
# from PIL import Image, ImageEnhance, ImageFilter

# # Download necessary NLTK resources
# nltk.download('punkt')
# nltk.download('stopwords')
# nltk.download('wordnet')

# os.environ['GOOGLE_API_KEY'] = "AIzaSyA9sqz4YKQHKXR9TU1imw0DPOghzHOMiBo"
# # genai.configure(api_key = os.environ['GOOGLE_API_KEY'])

# os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'cloudVisionAPI.json'

# # model = genai.GenerativeModel('gemini-pro')
# # model = genai.GenerativeModel('gemini-1.5-flash')

# async def remove_text_from_field(texts_to_remove, text_field):
#     for pattern in texts_to_remove:
#         text_field = re.sub(pattern, "", text_field)
#     return text_field

# async def getImage(img_url):
#     try:
#         async with ClientSession() as session:
#             async with session.get(img_url) as response:
#                 img_data = await response.read()
#                 return BytesIO(img_data)
#     except Exception as e:
#         raise ValueError(f"Error in getImage: {str(e)}")

# # async def detectText(url):
# #     try:
# #         client = vision.ImageAnnotatorClient()
# #         image_bytes = await getImage(url)
# #         image = vision.Image(content=image_bytes.getvalue())
        
# #         image_response = client.document_text_detection(image=image)
# #         if image_response.error.message:
# #             raise Exception("{}\nFor more info on error messages, check: ""https://cloud.google.com/apis/design/errors".format(image_response.error.message))
        
# #         image_texts = image_response.text_annotations
# #         imageData = image_texts[0].description
# #         return imageData
# #     except Exception as e:
# #         traceback.print_exc()
# #         raise ValueError(f"Error in detectText: {str(e)}")


# async def detectText(url, threshold=0.0):
#     try:
#         client = vision.ImageAnnotatorClient()
#         image_bytes = await getImage(url)
#         image = vision.Image(content=image_bytes.getvalue())
#         image_response = client.document_text_detection(image=image)

#         if image_response.error.message:
#             raise Exception(
#                 "{}\nFor more info on error messages, check: "
#                 "https://cloud.google.com/apis/design/errors".format(image_response.error.message)
#             )

#         image_texts = image_response.full_text_annotation

#         bangla_words = []
#         for page in image_texts.pages:
#             for block in page.blocks:
#                 for paragraph in block.paragraphs:
#                     for word in paragraph.words:
#                         word_text = ''.join([symbol.text for symbol in word.symbols])
#                         if word.confidence >= threshold:
#                             # Check if the word contains any Bangla character
#                             if re.search(r'[\u0980-\u09FF]', word_text):
#                                 bangla_words.append(word_text)

#         return ' '.join(bangla_words)

#     except Exception as e:
#         traceback.print_exc()
#         raise ValueError(f"Error in detectText: {str(e)}")

# def clean_text(text):
#     tokens = word_tokenize(text)
#     tokens = [word for word in tokens if word not in punctuation]
#     lemmatizer = WordNetLemmatizer()
#     tokens = [lemmatizer.lemmatize(word) for word in tokens]
#     clean_tokens = [i for i in tokens if i != '·']
#     print("Cleaned Tokens:", clean_tokens)         
#     return ' '.join(clean_tokens)

# async def main(url):
#     try:
#         data = await detectText(url)
#         # myQue = f"Extract only name from {data} also correct the name of Cigarettes and person name if the name is wrong. Dont give any other information except those name."
#         # response = model.generate_content(myQue)
#         # text = response.text
        
#         cleaned_text = clean_text(data)

#         return cleaned_text
#     except Exception as e:
#         traceback.print_exc()
#         raise ValueError(f"Error in main: {str(e)}")










import os
import re
import io
import traceback
import nltk
from aiohttp import ClientSession
from google.cloud import vision
from io import BytesIO
from string import punctuation
from PIL import Image, ImageEnhance, ImageFilter, ImageDraw, ImageFont
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk.stem import WordNetLemmatizer

# # Download necessary NLTK resources
# nltk.download('punkt')
# nltk.download('stopwords')
# nltk.download('wordnet')

# Environment variables
os.environ['GOOGLE_API_KEY'] = "AIzaSyA9sqz4YKQHKXR9TU1imw0DPOghzHOMiBo"
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'cloudVisionAPI.json'

# ========== Utility Functions ==========

async def getImage(img_url):
    try:
        async with ClientSession() as session:
            async with session.get(img_url) as response:
                img_data = await response.read()
                return BytesIO(img_data)
    except Exception as e:
        raise ValueError(f"Error in getImage: {str(e)}")

# async def preprocess_image_for_ocr(image_bytes):
#     image = Image.open(io.BytesIO(image_bytes)).convert('L')
#     contrast = ImageEnhance.Contrast(image)
#     image = contrast.enhance(2.0)
#     image = image.point(lambda x: 0 if x < 140 else 255, '1')
#     image = image.filter(ImageFilter.SHARPEN)
#     image = image.filter(ImageFilter.MedianFilter())
    
#     # Resize image (scale up)
#     new_size = (image.size[0] * 2, image.size[1] * 2)
#     image = image.resize(new_size)
    
#     # Generate thumbnail (optional)
#     thumb_size = (640, 640)
#     image.thumbnail(thumb_size)

#     # Convert PIL image to bytes for Google Vision
#     byte_arr = io.BytesIO()
#     image.save("preprocessed_output.png")
#     return byte_arr.getvalue()


counter = 0

# async def preprocess_image_for_ocr(image_bytes):
#     image = Image.open(io.BytesIO(image_bytes)).convert('L')  # Convert to grayscale

#     # Enhance contrast
#     contrast = ImageEnhance.Contrast(image)
#     image = contrast.enhance(2.0)

#     # Binarize image
#     image = image.point(lambda x: 0 if x < 140 else 255, '1')

#     # Apply filters
#     image = image.filter(ImageFilter.SHARPEN)
#     image = image.filter(ImageFilter.MedianFilter())

#     # Resize image (scale up)
#     new_size = (image.size[0] * 2, image.size[1] * 2)
#     image = image.resize(new_size)

#     # # Generate thumbnail (optional)
#     thumb_size = (640, 640)
#     image.thumbnail(thumb_size)

#     # Convert PIL image to bytes for Google Vision
#     byte_arr = io.BytesIO(image.getvalue())
#     # image.save(byte_arr, format='PNG')  # Save to buffer instead of disk
#     return byte_arr.getvalue()


async def preprocess_image_for_ocr(image_bytes):
    global counter
    # Since image_bytes is already a BytesIO object, we can directly use it
    image = Image.open(image_bytes).convert('L')  # Convert to grayscale

    # Enhance contrast
    # contrast = ImageEnhance.Contrast(image)
    # image = contrast.enhance(2.0)

    # # Binarize image
    # image = image.point(lambda x: 0 if x < 140 else 255, '1')

    # # Apply filters
    # image = image.filter(ImageFilter.SHARPEN)
    # image = image.filter(ImageFilter.MedianFilter())

    # # Resize image (scale up)
    # new_size = (image.size[0] * 2, image.size[1] * 2)
    # image = image.resize(new_size)

    # Save image to a bytes buffer
    byte_arr = io.BytesIO()
    image.save(byte_arr, format='PNG')
    counter +=1
    image.save(f"images/{counter}_image.jpg", format='PNG')
    return byte_arr.getvalue()

async def detectText(url, threshold=0.0):
    try:
        # Initialize the Vision client
        client = vision.ImageAnnotatorClient()
        
        # Fetch and preprocess image
        image_bytes = await getImage(url)
        processed_image_bytes = await preprocess_image_for_ocr(image_bytes)

        # Create Image object for Google Vision API
        image = vision.Image(content=processed_image_bytes)
        
        # Perform document text detection
        response = client.document_text_detection(image=image)

        # Check for errors in the API response
        if response.error.message:
            raise Exception(f"API Error: {response.error.message}\nCheck: https://cloud.google.com/apis/design/errors")

        # Extract text from the response
        image_texts = response.full_text_annotation
        bangla_words = []

        # Loop through the detected text and filter Bangla words
        for page in image_texts.pages:
            for block in page.blocks:
                for paragraph in block.paragraphs:
                    for word in paragraph.words:
                        word_text = ''.join([symbol.text for symbol in word.symbols])
                        if word.confidence >= threshold and re.search(r'[\u0980-\u09FF]', word_text):
                            bangla_words.append(word_text)

        # Return Bangla words as a space-separated string
        return ' '.join(bangla_words)

    except Exception as e:
        traceback.print_exc()
        raise ValueError(f"Error in detectText: {str(e)}")    
    
    
# async def detectText(url, threshold=0.0):
#     try:
#         client = vision.ImageAnnotatorClient()
#         image_bytes = await getImage(url)
#         processed_image_bytes = await preprocess_image_for_ocr(image_bytes.getvalue())

#         image = vision.Image(content=processed_image_bytes)
#         response = client.document_text_detection(image=image)

#         if response.error.message:
#             raise Exception(f"{response.error.message}\nCheck: https://cloud.google.com/apis/design/errors")

#         image_texts = response.full_text_annotation

#         bangla_words = []
#         for page in image_texts.pages:
#             for block in page.blocks:
#                 for paragraph in block.paragraphs:
#                     for word in paragraph.words:
#                         word_text = ''.join([symbol.text for symbol in word.symbols])
#                         if word.confidence >= threshold and re.search(r'[\u0980-\u09FF]', word_text):
#                             bangla_words.append(word_text)

#         return ' '.join(bangla_words)

#     except Exception as e:
#         traceback.print_exc()
#         raise ValueError(f"Error in detectText: {str(e)}")

def clean_text(text):
    tokens = word_tokenize(text)
    tokens = [word for word in tokens if word not in punctuation]
    lemmatizer = WordNetLemmatizer()
    tokens = [lemmatizer.lemmatize(word) for word in tokens]
    clean_tokens = [i for i in tokens if i != '·']
    print("Cleaned Tokens:", clean_tokens)
    return ' '.join(clean_tokens)

async def main(url):
    try:
        data = await detectText(url)
        cleaned_text = clean_text(data)
        return cleaned_text
    except Exception as e:
        traceback.print_exc()
        raise ValueError(f"Error in main: {str(e)}")