Commit ·
347eef9
1
Parent(s): 1637d86
init project
Browse files- .env +1 -0
- api.py +73 -0
- cloudVisionAPI.json +13 -0
- dataExtractOCR.log +1 -0
- main.py +263 -0
- main_old.py +324 -0
- requirements.txt +16 -0
.env
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
API_KEY = "AIzaSyCmchquNos99yozM_hN-kBQVAY4YDPOSxA"
|
api.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
import asyncio
|
| 4 |
+
from typing import List, Union
|
| 5 |
+
import uvicorn
|
| 6 |
+
import logging
|
| 7 |
+
from datetime import datetime
|
| 8 |
+
import pytz
|
| 9 |
+
from main import main
|
| 10 |
+
|
| 11 |
+
# Logging setup
|
| 12 |
+
logging.basicConfig(filename="dataExtractOCR.log", filemode='w')
|
| 13 |
+
logger = logging.getLogger("NID")
|
| 14 |
+
logger.setLevel(logging.DEBUG)
|
| 15 |
+
file_handler = logging.FileHandler("dataExtractOCR.log")
|
| 16 |
+
logger.addHandler(file_handler)
|
| 17 |
+
|
| 18 |
+
total_done = 0
|
| 19 |
+
total_error = 0
|
| 20 |
+
|
| 21 |
+
app = FastAPI()
|
| 22 |
+
|
| 23 |
+
class Item(BaseModel):
|
| 24 |
+
url: str
|
| 25 |
+
|
| 26 |
+
def get_bd_time():
|
| 27 |
+
bd_timezone = pytz.timezone("Asia/Dhaka")
|
| 28 |
+
time_now = datetime.now(bd_timezone)
|
| 29 |
+
current_time = time_now.strftime("%I:%M:%S %p")
|
| 30 |
+
return current_time
|
| 31 |
+
|
| 32 |
+
async def process_item(item: Item):
|
| 33 |
+
try:
|
| 34 |
+
result = await main(item.url)
|
| 35 |
+
return result
|
| 36 |
+
except Exception as e:
|
| 37 |
+
raise HTTPException(status_code=500, detail=f"Error in process_item: {str(e)}")
|
| 38 |
+
|
| 39 |
+
async def process_items(items: Union[Item, List[Item]]):
|
| 40 |
+
try:
|
| 41 |
+
if isinstance(items, list):
|
| 42 |
+
coroutines = [process_item(item) for item in items]
|
| 43 |
+
results = await asyncio.gather(*coroutines)
|
| 44 |
+
else:
|
| 45 |
+
results = await process_item(items)
|
| 46 |
+
return results
|
| 47 |
+
except Exception as e:
|
| 48 |
+
raise HTTPException(status_code=500, detail=f"Error in process_items: {str(e)}")
|
| 49 |
+
|
| 50 |
+
@app.get("/status")
|
| 51 |
+
async def status():
|
| 52 |
+
try:
|
| 53 |
+
return "Server is running"
|
| 54 |
+
except Exception as e:
|
| 55 |
+
raise HTTPException(status_code=500, detail=f"Status Error: {str(e)}")
|
| 56 |
+
|
| 57 |
+
@app.post("/ocr")
|
| 58 |
+
async def create_items(items: Union[Item, List[Item]]):
|
| 59 |
+
global total_done, total_error
|
| 60 |
+
try:
|
| 61 |
+
total_done += 1
|
| 62 |
+
results = await process_items(items)
|
| 63 |
+
if results:
|
| 64 |
+
logger.info(f"Time: {get_bd_time()}, Successful Execution: {total_done}, BODY: {items}, Result: {results}")
|
| 65 |
+
return {"extractedData": results}
|
| 66 |
+
except Exception as e:
|
| 67 |
+
total_error += 1
|
| 68 |
+
logger.info(f"Time: {get_bd_time()}, Failed Execution: {total_error}, BODY: {items}")
|
| 69 |
+
logger.error(str(e))
|
| 70 |
+
raise HTTPException(status_code=500, detail=f"create_items Error: {str(e)}")
|
| 71 |
+
|
| 72 |
+
if __name__ == "__main__":
|
| 73 |
+
uvicorn.run(app, host="127.0.0.1", port=8080)
|
cloudVisionAPI.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"type": "service_account",
|
| 3 |
+
"project_id": "our-card-401407",
|
| 4 |
+
"private_key_id": "356ef6d1447b5273c1545a07a06c28a376e021cb",
|
| 5 |
+
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCk1S5ITrDC9H00\nRzBVElQZw+Vo1x0jOAyb633iCKaETxqfHHwre83sOeQ2uw7Bxqh9r6OVN+9/fVnW\nJf4thjDxinM6UjTH8CyN+HMcnmUPiPnngTB+ikVl2OGCP57tCoF4h99f4uBPK83W\n1ccwNeGKXEkI892LgGr8pyZ4mXQB8EObhVGPXYM47VTyf6rU7z0E5bUSRrDbmDgP\n+V1YUlaTLu/JNoKSlq5KWd9aNZME+c6lDmULLFLaATCUkoNJmpycGH9608Yupndu\nQ2Ai/RRgcYIns93dojBSsjGekp6ajTYhDI0Jlluv7gy5kzI+ykAwrCSqaO6qBMFS\ngKNNSwSHAgMBAAECggEAEDAQRvE7cmbL/2m/LGU/AlzDvenj80TnSbTB4x3rkVjd\n1qLXt8MfDd3NmzZmes9SVfpjoh1UYi/itiF7EE6tpdSgzMLgBIOCZkFWt+7/IjYK\nKbTKGQZ/tEKtbiFmlgVb6IUBG4vOpAsIQKeabUVzxfPN1GXZ6+ZYgdskiXmAjbDT\nPB8F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1ZqAz8\n-----END PRIVATE KEY-----\n",
|
| 6 |
+
"client_email": "md-rakibul-hasan-naym@our-card-401407.iam.gserviceaccount.com",
|
| 7 |
+
"client_id": "111450790892641364901",
|
| 8 |
+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
|
| 9 |
+
"token_uri": "https://oauth2.googleapis.com/token",
|
| 10 |
+
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
|
| 11 |
+
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/md-rakibul-hasan-naym%40our-card-401407.iam.gserviceaccount.com",
|
| 12 |
+
"universe_domain": "googleapis.com"
|
| 13 |
+
}
|
dataExtractOCR.log
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
INFO:NID:Time: 07:09:50 PM, Successful Execution: 1, BODY: url='https://bl-bucket.sgp1.digitaloceanspaces.com/mmauto/batb-ocr/2025-04-07T13:00:26/image.jpg-original', Result: মুসি ভয়ালি আল হাসান নিঝুম মদর মেহেদী মোহাম্মদ হিসেল সাইদুল আলম নাঈম বেনসন গোল্ড লিফ লাকি স্ট্রাইক রয়েল বেডল টাইগার রাকিবুল হাসান
|
main.py
ADDED
|
@@ -0,0 +1,263 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# import os
|
| 2 |
+
# import re
|
| 3 |
+
# from aiohttp import ClientSession
|
| 4 |
+
# from google.cloud import vision
|
| 5 |
+
# from io import BytesIO
|
| 6 |
+
# import google.generativeai as genai
|
| 7 |
+
# import traceback
|
| 8 |
+
# import nltk
|
| 9 |
+
# from nltk.corpus import stopwords
|
| 10 |
+
# from nltk.tokenize import word_tokenize
|
| 11 |
+
# from nltk.stem import WordNetLemmatizer
|
| 12 |
+
# from string import punctuation
|
| 13 |
+
# from PIL import Image
|
| 14 |
+
# import io
|
| 15 |
+
# from PIL import Image, ImageEnhance, ImageFilter
|
| 16 |
+
|
| 17 |
+
# # Download necessary NLTK resources
|
| 18 |
+
# nltk.download('punkt')
|
| 19 |
+
# nltk.download('stopwords')
|
| 20 |
+
# nltk.download('wordnet')
|
| 21 |
+
|
| 22 |
+
# os.environ['GOOGLE_API_KEY'] = "AIzaSyA9sqz4YKQHKXR9TU1imw0DPOghzHOMiBo"
|
| 23 |
+
# # genai.configure(api_key = os.environ['GOOGLE_API_KEY'])
|
| 24 |
+
|
| 25 |
+
# os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'cloudVisionAPI.json'
|
| 26 |
+
|
| 27 |
+
# # model = genai.GenerativeModel('gemini-pro')
|
| 28 |
+
# # model = genai.GenerativeModel('gemini-1.5-flash')
|
| 29 |
+
|
| 30 |
+
# async def remove_text_from_field(texts_to_remove, text_field):
|
| 31 |
+
# for pattern in texts_to_remove:
|
| 32 |
+
# text_field = re.sub(pattern, "", text_field)
|
| 33 |
+
# return text_field
|
| 34 |
+
|
| 35 |
+
# async def getImage(img_url):
|
| 36 |
+
# try:
|
| 37 |
+
# async with ClientSession() as session:
|
| 38 |
+
# async with session.get(img_url) as response:
|
| 39 |
+
# img_data = await response.read()
|
| 40 |
+
# return BytesIO(img_data)
|
| 41 |
+
# except Exception as e:
|
| 42 |
+
# raise ValueError(f"Error in getImage: {str(e)}")
|
| 43 |
+
|
| 44 |
+
# # async def detectText(url):
|
| 45 |
+
# # try:
|
| 46 |
+
# # client = vision.ImageAnnotatorClient()
|
| 47 |
+
# # image_bytes = await getImage(url)
|
| 48 |
+
# # image = vision.Image(content=image_bytes.getvalue())
|
| 49 |
+
|
| 50 |
+
# # image_response = client.document_text_detection(image=image)
|
| 51 |
+
# # if image_response.error.message:
|
| 52 |
+
# # raise Exception("{}\nFor more info on error messages, check: ""https://cloud.google.com/apis/design/errors".format(image_response.error.message))
|
| 53 |
+
|
| 54 |
+
# # image_texts = image_response.text_annotations
|
| 55 |
+
# # imageData = image_texts[0].description
|
| 56 |
+
# # return imageData
|
| 57 |
+
# # except Exception as e:
|
| 58 |
+
# # traceback.print_exc()
|
| 59 |
+
# # raise ValueError(f"Error in detectText: {str(e)}")
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
# async def detectText(url, threshold=0.0):
|
| 63 |
+
# try:
|
| 64 |
+
# client = vision.ImageAnnotatorClient()
|
| 65 |
+
# image_bytes = await getImage(url)
|
| 66 |
+
# image = vision.Image(content=image_bytes.getvalue())
|
| 67 |
+
# image_response = client.document_text_detection(image=image)
|
| 68 |
+
|
| 69 |
+
# if image_response.error.message:
|
| 70 |
+
# raise Exception(
|
| 71 |
+
# "{}\nFor more info on error messages, check: "
|
| 72 |
+
# "https://cloud.google.com/apis/design/errors".format(image_response.error.message)
|
| 73 |
+
# )
|
| 74 |
+
|
| 75 |
+
# image_texts = image_response.full_text_annotation
|
| 76 |
+
|
| 77 |
+
# bangla_words = []
|
| 78 |
+
# for page in image_texts.pages:
|
| 79 |
+
# for block in page.blocks:
|
| 80 |
+
# for paragraph in block.paragraphs:
|
| 81 |
+
# for word in paragraph.words:
|
| 82 |
+
# word_text = ''.join([symbol.text for symbol in word.symbols])
|
| 83 |
+
# if word.confidence >= threshold:
|
| 84 |
+
# # Check if the word contains any Bangla character
|
| 85 |
+
# if re.search(r'[\u0980-\u09FF]', word_text):
|
| 86 |
+
# bangla_words.append(word_text)
|
| 87 |
+
|
| 88 |
+
# return ' '.join(bangla_words)
|
| 89 |
+
|
| 90 |
+
# except Exception as e:
|
| 91 |
+
# traceback.print_exc()
|
| 92 |
+
# raise ValueError(f"Error in detectText: {str(e)}")
|
| 93 |
+
|
| 94 |
+
# def clean_text(text):
|
| 95 |
+
# tokens = word_tokenize(text)
|
| 96 |
+
# tokens = [word for word in tokens if word not in punctuation]
|
| 97 |
+
# lemmatizer = WordNetLemmatizer()
|
| 98 |
+
# tokens = [lemmatizer.lemmatize(word) for word in tokens]
|
| 99 |
+
# clean_tokens = [i for i in tokens if i != '·']
|
| 100 |
+
# print("Cleaned Tokens:", clean_tokens)
|
| 101 |
+
# return ' '.join(clean_tokens)
|
| 102 |
+
|
| 103 |
+
# async def main(url):
|
| 104 |
+
# try:
|
| 105 |
+
# data = await detectText(url)
|
| 106 |
+
# # 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."
|
| 107 |
+
# # response = model.generate_content(myQue)
|
| 108 |
+
# # text = response.text
|
| 109 |
+
|
| 110 |
+
# cleaned_text = clean_text(data)
|
| 111 |
+
|
| 112 |
+
# return cleaned_text
|
| 113 |
+
# except Exception as e:
|
| 114 |
+
# traceback.print_exc()
|
| 115 |
+
# raise ValueError(f"Error in main: {str(e)}")
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
import os
|
| 127 |
+
import re
|
| 128 |
+
import io
|
| 129 |
+
import traceback
|
| 130 |
+
import nltk
|
| 131 |
+
from aiohttp import ClientSession
|
| 132 |
+
from google.cloud import vision
|
| 133 |
+
from io import BytesIO
|
| 134 |
+
from string import punctuation
|
| 135 |
+
from PIL import Image, ImageEnhance, ImageFilter, ImageDraw, ImageFont
|
| 136 |
+
from nltk.corpus import stopwords
|
| 137 |
+
from nltk.tokenize import word_tokenize
|
| 138 |
+
from nltk.stem import WordNetLemmatizer
|
| 139 |
+
import google.generativeai as genai
|
| 140 |
+
|
| 141 |
+
# Environment variables
|
| 142 |
+
os.environ['GOOGLE_API_KEY'] = "AIzaSyA9sqz4YKQHKXR9TU1imw0DPOghzHOMiBo"
|
| 143 |
+
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'cloudVisionAPI.json'
|
| 144 |
+
|
| 145 |
+
genai.configure(api_key = os.environ['GOOGLE_API_KEY'])
|
| 146 |
+
|
| 147 |
+
# # model = genai.GenerativeModel('gemini-pro')
|
| 148 |
+
model = genai.GenerativeModel('gemini-1.5-flash')
|
| 149 |
+
|
| 150 |
+
# ========== Utility Functions ==========
|
| 151 |
+
|
| 152 |
+
async def getImage(img_url):
|
| 153 |
+
try:
|
| 154 |
+
async with ClientSession() as session:
|
| 155 |
+
async with session.get(img_url) as response:
|
| 156 |
+
img_data = await response.read()
|
| 157 |
+
return BytesIO(img_data)
|
| 158 |
+
except Exception as e:
|
| 159 |
+
raise ValueError(f"Error in getImage: {str(e)}")
|
| 160 |
+
|
| 161 |
+
counter = 0
|
| 162 |
+
|
| 163 |
+
async def preprocess_image_for_ocr(image_bytes):
|
| 164 |
+
global counter
|
| 165 |
+
image = Image.open(image_bytes).convert('L') # Convert to grayscale
|
| 166 |
+
|
| 167 |
+
# Save image to a bytes buffer
|
| 168 |
+
byte_arr = io.BytesIO()
|
| 169 |
+
image.save(byte_arr, format='PNG')
|
| 170 |
+
counter +=1
|
| 171 |
+
image.save(f"images/{counter}_image.jpg", format='PNG')
|
| 172 |
+
return byte_arr.getvalue()
|
| 173 |
+
|
| 174 |
+
async def detectText(url, threshold=0.0):
|
| 175 |
+
try:
|
| 176 |
+
# Initialize the Vision client
|
| 177 |
+
client = vision.ImageAnnotatorClient()
|
| 178 |
+
|
| 179 |
+
# Fetch and preprocess image
|
| 180 |
+
image_bytes = await getImage(url)
|
| 181 |
+
processed_image_bytes = await preprocess_image_for_ocr(image_bytes)
|
| 182 |
+
|
| 183 |
+
# Create Image object for Google Vision API
|
| 184 |
+
image = vision.Image(content=processed_image_bytes)
|
| 185 |
+
|
| 186 |
+
# Perform document text detection
|
| 187 |
+
response = client.document_text_detection(image=image)
|
| 188 |
+
|
| 189 |
+
# Check for errors in the API response
|
| 190 |
+
if response.error.message:
|
| 191 |
+
raise Exception(f"API Error: {response.error.message}\nCheck: https://cloud.google.com/apis/design/errors")
|
| 192 |
+
|
| 193 |
+
# Extract text from the response
|
| 194 |
+
image_texts = response.full_text_annotation
|
| 195 |
+
bangla_words = []
|
| 196 |
+
|
| 197 |
+
# Loop through the detected text and filter Bangla words
|
| 198 |
+
for page in image_texts.pages:
|
| 199 |
+
for block in page.blocks:
|
| 200 |
+
for paragraph in block.paragraphs:
|
| 201 |
+
for word in paragraph.words:
|
| 202 |
+
word_text = ''.join([symbol.text for symbol in word.symbols])
|
| 203 |
+
if word.confidence >= threshold and re.search(r'[\u0980-\u09FF]', word_text):
|
| 204 |
+
bangla_words.append(word_text)
|
| 205 |
+
|
| 206 |
+
# Return Bangla words as a space-separated string
|
| 207 |
+
return ' '.join(bangla_words)
|
| 208 |
+
|
| 209 |
+
except Exception as e:
|
| 210 |
+
traceback.print_exc()
|
| 211 |
+
raise ValueError(f"Error in detectText: {str(e)}")
|
| 212 |
+
|
| 213 |
+
|
| 214 |
+
def clean_text(text):
|
| 215 |
+
tokens = word_tokenize(text)
|
| 216 |
+
tokens = [word for word in tokens if word not in punctuation]
|
| 217 |
+
lemmatizer = WordNetLemmatizer()
|
| 218 |
+
tokens = [lemmatizer.lemmatize(word) for word in tokens]
|
| 219 |
+
clean_tokens = [i for i in tokens if i != '·']
|
| 220 |
+
print("Cleaned Tokens:", clean_tokens)
|
| 221 |
+
return ' '.join(clean_tokens)
|
| 222 |
+
|
| 223 |
+
|
| 224 |
+
|
| 225 |
+
|
| 226 |
+
|
| 227 |
+
|
| 228 |
+
async def main(url):
|
| 229 |
+
try:
|
| 230 |
+
dic = ['গোল্ডলিফ', 'লাকি স্ট্রাইক', 'বেনসন']
|
| 231 |
+
|
| 232 |
+
# Extract the data using detectText function
|
| 233 |
+
data = await detectText(url)
|
| 234 |
+
|
| 235 |
+
# Define the query (myQue) to pass to the model
|
| 236 |
+
myQue = f"""
|
| 237 |
+
Extract and correct the names of cigarette brands and Bangladeshi people's names from the following text. The text may contain spelling errors, grammatical issues, and improperly formatted names.
|
| 238 |
+
|
| 239 |
+
Your task:
|
| 240 |
+
- Extract and correct the names of cigarette brands and Bangladeshi people also bangla writings.
|
| 241 |
+
- Return only the corrected data (in Bangla), with no additional information or explanations.
|
| 242 |
+
|
| 243 |
+
Here is the provided text:
|
| 244 |
+
{data}
|
| 245 |
+
|
| 246 |
+
Only return the corrected names, no extra text.
|
| 247 |
+
"""
|
| 248 |
+
|
| 249 |
+
print("Query to Model:", myQue) # Optional: For debugging to see the query sent to the model
|
| 250 |
+
|
| 251 |
+
# Generate the response from the model
|
| 252 |
+
response = model.generate_content(myQue)
|
| 253 |
+
text = response.text.strip()
|
| 254 |
+
clean_txt = clean_text(text)
|
| 255 |
+
print("Response Text:", clean_txt)
|
| 256 |
+
|
| 257 |
+
# Return the cleaned response with only the extracted names
|
| 258 |
+
return clean_txt
|
| 259 |
+
|
| 260 |
+
except Exception as e:
|
| 261 |
+
# Print and raise a detailed error message for debugging
|
| 262 |
+
traceback.print_exc()
|
| 263 |
+
raise ValueError(f"Error in main: {str(e)}")
|
main_old.py
ADDED
|
@@ -0,0 +1,324 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# import os
|
| 2 |
+
# import re
|
| 3 |
+
# from aiohttp import ClientSession
|
| 4 |
+
# from google.cloud import vision
|
| 5 |
+
# from io import BytesIO
|
| 6 |
+
# import google.generativeai as genai
|
| 7 |
+
# import traceback
|
| 8 |
+
# import nltk
|
| 9 |
+
# from nltk.corpus import stopwords
|
| 10 |
+
# from nltk.tokenize import word_tokenize
|
| 11 |
+
# from nltk.stem import WordNetLemmatizer
|
| 12 |
+
# from string import punctuation
|
| 13 |
+
# from PIL import Image
|
| 14 |
+
# import io
|
| 15 |
+
# from PIL import Image, ImageEnhance, ImageFilter
|
| 16 |
+
|
| 17 |
+
# # Download necessary NLTK resources
|
| 18 |
+
# nltk.download('punkt')
|
| 19 |
+
# nltk.download('stopwords')
|
| 20 |
+
# nltk.download('wordnet')
|
| 21 |
+
|
| 22 |
+
# os.environ['GOOGLE_API_KEY'] = "AIzaSyA9sqz4YKQHKXR9TU1imw0DPOghzHOMiBo"
|
| 23 |
+
# # genai.configure(api_key = os.environ['GOOGLE_API_KEY'])
|
| 24 |
+
|
| 25 |
+
# os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'cloudVisionAPI.json'
|
| 26 |
+
|
| 27 |
+
# # model = genai.GenerativeModel('gemini-pro')
|
| 28 |
+
# # model = genai.GenerativeModel('gemini-1.5-flash')
|
| 29 |
+
|
| 30 |
+
# async def remove_text_from_field(texts_to_remove, text_field):
|
| 31 |
+
# for pattern in texts_to_remove:
|
| 32 |
+
# text_field = re.sub(pattern, "", text_field)
|
| 33 |
+
# return text_field
|
| 34 |
+
|
| 35 |
+
# async def getImage(img_url):
|
| 36 |
+
# try:
|
| 37 |
+
# async with ClientSession() as session:
|
| 38 |
+
# async with session.get(img_url) as response:
|
| 39 |
+
# img_data = await response.read()
|
| 40 |
+
# return BytesIO(img_data)
|
| 41 |
+
# except Exception as e:
|
| 42 |
+
# raise ValueError(f"Error in getImage: {str(e)}")
|
| 43 |
+
|
| 44 |
+
# # async def detectText(url):
|
| 45 |
+
# # try:
|
| 46 |
+
# # client = vision.ImageAnnotatorClient()
|
| 47 |
+
# # image_bytes = await getImage(url)
|
| 48 |
+
# # image = vision.Image(content=image_bytes.getvalue())
|
| 49 |
+
|
| 50 |
+
# # image_response = client.document_text_detection(image=image)
|
| 51 |
+
# # if image_response.error.message:
|
| 52 |
+
# # raise Exception("{}\nFor more info on error messages, check: ""https://cloud.google.com/apis/design/errors".format(image_response.error.message))
|
| 53 |
+
|
| 54 |
+
# # image_texts = image_response.text_annotations
|
| 55 |
+
# # imageData = image_texts[0].description
|
| 56 |
+
# # return imageData
|
| 57 |
+
# # except Exception as e:
|
| 58 |
+
# # traceback.print_exc()
|
| 59 |
+
# # raise ValueError(f"Error in detectText: {str(e)}")
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
# async def detectText(url, threshold=0.0):
|
| 63 |
+
# try:
|
| 64 |
+
# client = vision.ImageAnnotatorClient()
|
| 65 |
+
# image_bytes = await getImage(url)
|
| 66 |
+
# image = vision.Image(content=image_bytes.getvalue())
|
| 67 |
+
# image_response = client.document_text_detection(image=image)
|
| 68 |
+
|
| 69 |
+
# if image_response.error.message:
|
| 70 |
+
# raise Exception(
|
| 71 |
+
# "{}\nFor more info on error messages, check: "
|
| 72 |
+
# "https://cloud.google.com/apis/design/errors".format(image_response.error.message)
|
| 73 |
+
# )
|
| 74 |
+
|
| 75 |
+
# image_texts = image_response.full_text_annotation
|
| 76 |
+
|
| 77 |
+
# bangla_words = []
|
| 78 |
+
# for page in image_texts.pages:
|
| 79 |
+
# for block in page.blocks:
|
| 80 |
+
# for paragraph in block.paragraphs:
|
| 81 |
+
# for word in paragraph.words:
|
| 82 |
+
# word_text = ''.join([symbol.text for symbol in word.symbols])
|
| 83 |
+
# if word.confidence >= threshold:
|
| 84 |
+
# # Check if the word contains any Bangla character
|
| 85 |
+
# if re.search(r'[\u0980-\u09FF]', word_text):
|
| 86 |
+
# bangla_words.append(word_text)
|
| 87 |
+
|
| 88 |
+
# return ' '.join(bangla_words)
|
| 89 |
+
|
| 90 |
+
# except Exception as e:
|
| 91 |
+
# traceback.print_exc()
|
| 92 |
+
# raise ValueError(f"Error in detectText: {str(e)}")
|
| 93 |
+
|
| 94 |
+
# def clean_text(text):
|
| 95 |
+
# tokens = word_tokenize(text)
|
| 96 |
+
# tokens = [word for word in tokens if word not in punctuation]
|
| 97 |
+
# lemmatizer = WordNetLemmatizer()
|
| 98 |
+
# tokens = [lemmatizer.lemmatize(word) for word in tokens]
|
| 99 |
+
# clean_tokens = [i for i in tokens if i != '·']
|
| 100 |
+
# print("Cleaned Tokens:", clean_tokens)
|
| 101 |
+
# return ' '.join(clean_tokens)
|
| 102 |
+
|
| 103 |
+
# async def main(url):
|
| 104 |
+
# try:
|
| 105 |
+
# data = await detectText(url)
|
| 106 |
+
# # 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."
|
| 107 |
+
# # response = model.generate_content(myQue)
|
| 108 |
+
# # text = response.text
|
| 109 |
+
|
| 110 |
+
# cleaned_text = clean_text(data)
|
| 111 |
+
|
| 112 |
+
# return cleaned_text
|
| 113 |
+
# except Exception as e:
|
| 114 |
+
# traceback.print_exc()
|
| 115 |
+
# raise ValueError(f"Error in main: {str(e)}")
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
import os
|
| 127 |
+
import re
|
| 128 |
+
import io
|
| 129 |
+
import traceback
|
| 130 |
+
import nltk
|
| 131 |
+
from aiohttp import ClientSession
|
| 132 |
+
from google.cloud import vision
|
| 133 |
+
from io import BytesIO
|
| 134 |
+
from string import punctuation
|
| 135 |
+
from PIL import Image, ImageEnhance, ImageFilter, ImageDraw, ImageFont
|
| 136 |
+
from nltk.corpus import stopwords
|
| 137 |
+
from nltk.tokenize import word_tokenize
|
| 138 |
+
from nltk.stem import WordNetLemmatizer
|
| 139 |
+
|
| 140 |
+
# # Download necessary NLTK resources
|
| 141 |
+
# nltk.download('punkt')
|
| 142 |
+
# nltk.download('stopwords')
|
| 143 |
+
# nltk.download('wordnet')
|
| 144 |
+
|
| 145 |
+
# Environment variables
|
| 146 |
+
os.environ['GOOGLE_API_KEY'] = "AIzaSyA9sqz4YKQHKXR9TU1imw0DPOghzHOMiBo"
|
| 147 |
+
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'cloudVisionAPI.json'
|
| 148 |
+
|
| 149 |
+
# ========== Utility Functions ==========
|
| 150 |
+
|
| 151 |
+
async def getImage(img_url):
|
| 152 |
+
try:
|
| 153 |
+
async with ClientSession() as session:
|
| 154 |
+
async with session.get(img_url) as response:
|
| 155 |
+
img_data = await response.read()
|
| 156 |
+
return BytesIO(img_data)
|
| 157 |
+
except Exception as e:
|
| 158 |
+
raise ValueError(f"Error in getImage: {str(e)}")
|
| 159 |
+
|
| 160 |
+
# async def preprocess_image_for_ocr(image_bytes):
|
| 161 |
+
# image = Image.open(io.BytesIO(image_bytes)).convert('L')
|
| 162 |
+
# contrast = ImageEnhance.Contrast(image)
|
| 163 |
+
# image = contrast.enhance(2.0)
|
| 164 |
+
# image = image.point(lambda x: 0 if x < 140 else 255, '1')
|
| 165 |
+
# image = image.filter(ImageFilter.SHARPEN)
|
| 166 |
+
# image = image.filter(ImageFilter.MedianFilter())
|
| 167 |
+
|
| 168 |
+
# # Resize image (scale up)
|
| 169 |
+
# new_size = (image.size[0] * 2, image.size[1] * 2)
|
| 170 |
+
# image = image.resize(new_size)
|
| 171 |
+
|
| 172 |
+
# # Generate thumbnail (optional)
|
| 173 |
+
# thumb_size = (640, 640)
|
| 174 |
+
# image.thumbnail(thumb_size)
|
| 175 |
+
|
| 176 |
+
# # Convert PIL image to bytes for Google Vision
|
| 177 |
+
# byte_arr = io.BytesIO()
|
| 178 |
+
# image.save("preprocessed_output.png")
|
| 179 |
+
# return byte_arr.getvalue()
|
| 180 |
+
|
| 181 |
+
|
| 182 |
+
counter = 0
|
| 183 |
+
|
| 184 |
+
# async def preprocess_image_for_ocr(image_bytes):
|
| 185 |
+
# image = Image.open(io.BytesIO(image_bytes)).convert('L') # Convert to grayscale
|
| 186 |
+
|
| 187 |
+
# # Enhance contrast
|
| 188 |
+
# contrast = ImageEnhance.Contrast(image)
|
| 189 |
+
# image = contrast.enhance(2.0)
|
| 190 |
+
|
| 191 |
+
# # Binarize image
|
| 192 |
+
# image = image.point(lambda x: 0 if x < 140 else 255, '1')
|
| 193 |
+
|
| 194 |
+
# # Apply filters
|
| 195 |
+
# image = image.filter(ImageFilter.SHARPEN)
|
| 196 |
+
# image = image.filter(ImageFilter.MedianFilter())
|
| 197 |
+
|
| 198 |
+
# # Resize image (scale up)
|
| 199 |
+
# new_size = (image.size[0] * 2, image.size[1] * 2)
|
| 200 |
+
# image = image.resize(new_size)
|
| 201 |
+
|
| 202 |
+
# # # Generate thumbnail (optional)
|
| 203 |
+
# thumb_size = (640, 640)
|
| 204 |
+
# image.thumbnail(thumb_size)
|
| 205 |
+
|
| 206 |
+
# # Convert PIL image to bytes for Google Vision
|
| 207 |
+
# byte_arr = io.BytesIO(image.getvalue())
|
| 208 |
+
# # image.save(byte_arr, format='PNG') # Save to buffer instead of disk
|
| 209 |
+
# return byte_arr.getvalue()
|
| 210 |
+
|
| 211 |
+
|
| 212 |
+
async def preprocess_image_for_ocr(image_bytes):
|
| 213 |
+
global counter
|
| 214 |
+
# Since image_bytes is already a BytesIO object, we can directly use it
|
| 215 |
+
image = Image.open(image_bytes).convert('L') # Convert to grayscale
|
| 216 |
+
|
| 217 |
+
# Enhance contrast
|
| 218 |
+
# contrast = ImageEnhance.Contrast(image)
|
| 219 |
+
# image = contrast.enhance(2.0)
|
| 220 |
+
|
| 221 |
+
# # Binarize image
|
| 222 |
+
# image = image.point(lambda x: 0 if x < 140 else 255, '1')
|
| 223 |
+
|
| 224 |
+
# # Apply filters
|
| 225 |
+
# image = image.filter(ImageFilter.SHARPEN)
|
| 226 |
+
# image = image.filter(ImageFilter.MedianFilter())
|
| 227 |
+
|
| 228 |
+
# # Resize image (scale up)
|
| 229 |
+
# new_size = (image.size[0] * 2, image.size[1] * 2)
|
| 230 |
+
# image = image.resize(new_size)
|
| 231 |
+
|
| 232 |
+
# Save image to a bytes buffer
|
| 233 |
+
byte_arr = io.BytesIO()
|
| 234 |
+
image.save(byte_arr, format='PNG')
|
| 235 |
+
counter +=1
|
| 236 |
+
image.save(f"images/{counter}_image.jpg", format='PNG')
|
| 237 |
+
return byte_arr.getvalue()
|
| 238 |
+
|
| 239 |
+
async def detectText(url, threshold=0.0):
|
| 240 |
+
try:
|
| 241 |
+
# Initialize the Vision client
|
| 242 |
+
client = vision.ImageAnnotatorClient()
|
| 243 |
+
|
| 244 |
+
# Fetch and preprocess image
|
| 245 |
+
image_bytes = await getImage(url)
|
| 246 |
+
processed_image_bytes = await preprocess_image_for_ocr(image_bytes)
|
| 247 |
+
|
| 248 |
+
# Create Image object for Google Vision API
|
| 249 |
+
image = vision.Image(content=processed_image_bytes)
|
| 250 |
+
|
| 251 |
+
# Perform document text detection
|
| 252 |
+
response = client.document_text_detection(image=image)
|
| 253 |
+
|
| 254 |
+
# Check for errors in the API response
|
| 255 |
+
if response.error.message:
|
| 256 |
+
raise Exception(f"API Error: {response.error.message}\nCheck: https://cloud.google.com/apis/design/errors")
|
| 257 |
+
|
| 258 |
+
# Extract text from the response
|
| 259 |
+
image_texts = response.full_text_annotation
|
| 260 |
+
bangla_words = []
|
| 261 |
+
|
| 262 |
+
# Loop through the detected text and filter Bangla words
|
| 263 |
+
for page in image_texts.pages:
|
| 264 |
+
for block in page.blocks:
|
| 265 |
+
for paragraph in block.paragraphs:
|
| 266 |
+
for word in paragraph.words:
|
| 267 |
+
word_text = ''.join([symbol.text for symbol in word.symbols])
|
| 268 |
+
if word.confidence >= threshold and re.search(r'[\u0980-\u09FF]', word_text):
|
| 269 |
+
bangla_words.append(word_text)
|
| 270 |
+
|
| 271 |
+
# Return Bangla words as a space-separated string
|
| 272 |
+
return ' '.join(bangla_words)
|
| 273 |
+
|
| 274 |
+
except Exception as e:
|
| 275 |
+
traceback.print_exc()
|
| 276 |
+
raise ValueError(f"Error in detectText: {str(e)}")
|
| 277 |
+
|
| 278 |
+
|
| 279 |
+
# async def detectText(url, threshold=0.0):
|
| 280 |
+
# try:
|
| 281 |
+
# client = vision.ImageAnnotatorClient()
|
| 282 |
+
# image_bytes = await getImage(url)
|
| 283 |
+
# processed_image_bytes = await preprocess_image_for_ocr(image_bytes.getvalue())
|
| 284 |
+
|
| 285 |
+
# image = vision.Image(content=processed_image_bytes)
|
| 286 |
+
# response = client.document_text_detection(image=image)
|
| 287 |
+
|
| 288 |
+
# if response.error.message:
|
| 289 |
+
# raise Exception(f"{response.error.message}\nCheck: https://cloud.google.com/apis/design/errors")
|
| 290 |
+
|
| 291 |
+
# image_texts = response.full_text_annotation
|
| 292 |
+
|
| 293 |
+
# bangla_words = []
|
| 294 |
+
# for page in image_texts.pages:
|
| 295 |
+
# for block in page.blocks:
|
| 296 |
+
# for paragraph in block.paragraphs:
|
| 297 |
+
# for word in paragraph.words:
|
| 298 |
+
# word_text = ''.join([symbol.text for symbol in word.symbols])
|
| 299 |
+
# if word.confidence >= threshold and re.search(r'[\u0980-\u09FF]', word_text):
|
| 300 |
+
# bangla_words.append(word_text)
|
| 301 |
+
|
| 302 |
+
# return ' '.join(bangla_words)
|
| 303 |
+
|
| 304 |
+
# except Exception as e:
|
| 305 |
+
# traceback.print_exc()
|
| 306 |
+
# raise ValueError(f"Error in detectText: {str(e)}")
|
| 307 |
+
|
| 308 |
+
def clean_text(text):
|
| 309 |
+
tokens = word_tokenize(text)
|
| 310 |
+
tokens = [word for word in tokens if word not in punctuation]
|
| 311 |
+
lemmatizer = WordNetLemmatizer()
|
| 312 |
+
tokens = [lemmatizer.lemmatize(word) for word in tokens]
|
| 313 |
+
clean_tokens = [i for i in tokens if i != '·']
|
| 314 |
+
print("Cleaned Tokens:", clean_tokens)
|
| 315 |
+
return ' '.join(clean_tokens)
|
| 316 |
+
|
| 317 |
+
async def main(url):
|
| 318 |
+
try:
|
| 319 |
+
data = await detectText(url)
|
| 320 |
+
cleaned_text = clean_text(data)
|
| 321 |
+
return cleaned_text
|
| 322 |
+
except Exception as e:
|
| 323 |
+
traceback.print_exc()
|
| 324 |
+
raise ValueError(f"Error in main: {str(e)}")
|
requirements.txt
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pillow
|
| 2 |
+
fastapi
|
| 3 |
+
pydantic
|
| 4 |
+
uvicorn
|
| 5 |
+
pytz
|
| 6 |
+
aiohttp
|
| 7 |
+
pandas
|
| 8 |
+
google-cloud-vision
|
| 9 |
+
google-generativeai
|
| 10 |
+
requests
|
| 11 |
+
numpy
|
| 12 |
+
face_recognition
|
| 13 |
+
hypercorn
|
| 14 |
+
|
| 15 |
+
# easyocr
|
| 16 |
+
# pytesseract
|