rakib72642 commited on
Commit
75d9abf
·
1 Parent(s): e92bd0b

build v8 structure

Browse files
AI_Model/ArabicOCR.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:dd3844fc58d898dec0f19ffc33ee09f19701c96c40e334c8ecca29aff9bc9a19
3
+ size 87671593
a.jpg → OLD/a.jpg RENAMED
File without changes
a.py → OLD/a.py RENAMED
File without changes
api.py → OLD/api.py RENAMED
File without changes
main.py → OLD/main.py RENAMED
File without changes
new.jpg → OLD/new.jpg RENAMED
File without changes
README.md ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ HuggingFace: https://huggingface.co/rakib72642/Arabic_OCR
2
+
3
+ sudo apt install iproute2 && sudo apt install wget && sudo apt install unzip && sudo apt install nvtop && sudo apt-get install git-lfs && sudo apt-get update && sudo apt-get install libgl1 && curl -s https://ngrok-agent.s3.amazonaws.com/ngrok.asc | sudo tee /etc/apt/trusted.gpg.d/ngrok.asc >/dev/null && echo "deb https://ngrok-agent.s3.amazonaws.com buster main" | sudo tee /etc/apt/sources.list.d/ngrok.list && sudo apt update && sudo apt install ngrok && ngrok config add-authtoken 2Qm8hS1zPhVXiLjEdlI4738tLzF_2QJwGJMK5oTbQD33QSVXS && sudo apt update && sudo apt upgrade && ngrok http --domain=hawkeyes.ngrok.app 8000
4
+
5
+
6
+ git clone https://huggingface.co/rakib72642/Arabic_OCR && cd Arabic_OCR && pip install -r requirements.txt && sudo apt update && sudo apt upgrade && python ocr_api.py
7
+
8
+ cd Arabic_OCR && python ocr_api.py
9
+
10
+ hypercorn ocr_api:app --bind 127.0.0.1:8000 --workers 4
11
+
12
+
13
+ OLD OCR :
14
+ # ************************************************************
15
+ ngrok config add-authtoken 2Q8xOjna6gvwQRiMTZayN1uEgWy_6uRD8M1b6rZtYMz4yLzAw
16
+
17
+ ngrok http --domain=dominant-eagerly-deer.ngrok-free.app 8000
data/data.py ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+
2
+
3
+ name_distribute = {
4
+ "anti-dandruff":"Anti-Dandruff",
5
+ "clear":"Clear",
6
+ "confidence":"Confidence",
7
+ "revival":"Revival",
8
+ }
data/model.py ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ from ultralytics import YOLO
2
+
3
+ OCR_model = YOLO("AI_Model/ArabicOCR.pt").cuda()
4
+
5
+ OCR_model.to(device=0)
ocr_api.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from pydantic import BaseModel
3
+ import asyncio
4
+ from typing import List, Union
5
+ from ocr_main import *
6
+ import uvicorn
7
+ import logging
8
+ from datetime import datetime
9
+ import pytz
10
+ import torch
11
+ import json
12
+
13
+ logging.basicConfig(filename="ArabicOCR.log",
14
+ filemode='w')
15
+ logger = logging.getLogger("OCR")
16
+ logger.setLevel(logging.DEBUG)
17
+ file_handler = logging.FileHandler("ArabicOCR.log")
18
+ logger.addHandler(file_handler)
19
+ total_done = 0
20
+ total_error = 0
21
+
22
+ app = FastAPI()
23
+
24
+ class Item(BaseModel):
25
+ url: str
26
+
27
+ def get_bd_time():
28
+ bd_timezone = pytz.timezone("Asia/Dhaka")
29
+ time_now = datetime.now(bd_timezone)
30
+ current_time = time_now.strftime("%I:%M:%S %p")
31
+ return current_time
32
+
33
+
34
+ async def process_item(item: Item):
35
+ try:
36
+ result = await mainDet(item.url)
37
+ result = json.loads(result)
38
+ return result
39
+ finally:
40
+ torch.cuda.empty_cache()
41
+ pass
42
+
43
+ async def process_items(items: Union[Item, List[Item]]):
44
+ print(type(items))
45
+ if type(items)==list:
46
+ coroutines = [process_item(item) for item in items]
47
+ results = await asyncio.gather(*coroutines)
48
+ print("multi : ",results)
49
+ else:
50
+ results = await process_item(items)
51
+ print("single : ", results)
52
+ return results
53
+
54
+
55
+
56
+ @app.get("/status")
57
+ async def status():
58
+ return "AI Server in running"
59
+
60
+ @app.post("/arabicOCR")
61
+ async def create_items(items: Union[Item, List[Item]]):
62
+ try:
63
+ # global total_done
64
+ # total_done +=1
65
+ results = await process_items(items)
66
+ print("Result Sent to User:", results)
67
+ print("###################################################################################################")
68
+ print(items)
69
+ print("Last Execution Time : ", get_bd_time())
70
+ # logger.info(f"Time:{get_bd_time()}, Execution Done and Total Successfull Execution : {total_done}")
71
+ return results
72
+ except Exception as e:
73
+ global total_error
74
+ total_error += 1
75
+ logger.info(f"Time:{get_bd_time()}, Execution Failed and Total Failed Execution : {total_error}, Payload:{items}")
76
+ logger.error(str(e))
77
+ return {"AI": f"Error: {str(e)}"}
78
+ finally:
79
+ global total_done
80
+ total_done +=1
81
+ logger.info(f"Time:{get_bd_time()}, Execution Done and Total Successfull Execution : {total_done}, Payload:{items}")
82
+ torch.cuda.empty_cache()
83
+ pass
84
+
85
+ if __name__ == "__main__":
86
+ try:
87
+ del OCR_model
88
+ uvicorn.run(app, host="127.0.0.1", port=4444)
89
+ finally:
90
+ torch.cuda.empty_cache()
ocr_main.py ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import pandas as pd
3
+ import asyncio
4
+ import base64
5
+ from PIL import Image, ImageDraw
6
+ from aiohttp import ClientSession
7
+ from io import BytesIO
8
+ from data.model import OCR_model
9
+ from data.data import name_distribute
10
+
11
+
12
+ async def getImage(img_url):
13
+ async with ClientSession() as session:
14
+ try:
15
+ async with session.get(img_url) as response:
16
+ img_data = await response.read()
17
+ return BytesIO(img_data)
18
+ except Exception as e:
19
+ print({"Error in getImage":str(e)})
20
+
21
+
22
+
23
+
24
+ async def detection(model,img_content):
25
+ try:
26
+ img = Image.open(img_content)
27
+ # result = model(img)
28
+ result = model(img,device=0)
29
+
30
+ detection = {}
31
+ data = json.loads(result[0].tojson())
32
+ rect_data = []
33
+ rec_rect = []
34
+ for items in data:
35
+ rect_data.append(items["box"])
36
+ await create_rectangle(img_content,rect_data)
37
+ if len(data) == 0:
38
+ res = {"AI": "No Detection"}
39
+ detection.update(res)
40
+ else:
41
+ df = pd.DataFrame(data)
42
+ name_counts = df['name'].value_counts().sort_index()
43
+
44
+ for name, count in name_counts.items():
45
+ res = {name: count}
46
+ detection.update(res)
47
+ return detection
48
+ except Exception as e:
49
+ print({"Error in detection":str(e)})
50
+
51
+
52
+ async def share_iamge(img):
53
+ try:
54
+ buffer = BytesIO()
55
+ img.save(buffer, format="JPEG")
56
+ base64_image = base64.b64encode(buffer.getvalue()).decode("utf-8")
57
+ return base64_image
58
+ except Exception as e:
59
+ print({"Error in share_iamge":str(e)})
60
+
61
+ async def create_rectangle(image_data,cords):
62
+ try:
63
+ drawing = ImageDraw.Draw(image_data)
64
+
65
+ for item in cords:
66
+ x1 = item["x1"]
67
+ x2 = item["x2"]
68
+ y1 = item["y1"]
69
+ y2 = item["y2"]
70
+ drawing.rectangle([x1,y1,x2,y2],outline="red",width=5)
71
+ return drawing
72
+ except Exception as e:
73
+ print({"Error in create_rectangle":str(e)})
74
+
75
+
76
+
77
+
78
+
79
+
80
+ async def format_result(res,conv):
81
+ try:
82
+ result = {}
83
+ for key,value in conv.items():
84
+ if key in res:
85
+ data = {value:res[key]}
86
+ result.update(data)
87
+ return result
88
+ except Exception as e:
89
+ print({"Error in format_result":str(e)})
90
+
91
+
92
+
93
+
94
+
95
+
96
+ async def mainDet(url):
97
+ try:
98
+ image = await asyncio.create_task(getImage(url))
99
+ detect_data = await asyncio.create_task(detection(OCR_model, image))
100
+ tab_data = await asyncio.create_task(format_result(detect_data,name_distribute))
101
+ img_data = await asyncio.create_task(share_iamge(image))
102
+
103
+ result = {
104
+ "tabularData":tab_data,
105
+ "imageData":img_data,
106
+ }
107
+ return json.dumps(result)
108
+
109
+ except Exception as e:
110
+ print({"Error in mainDet":str(e)})
111
+
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ fastapi
2
+ pydantic
3
+ uvicorn
4
+ ultralytics
5
+ aiohttp
6
+ pytz
7
+ regex
8
+ hypercorn