File size: 3,135 Bytes
75d9abf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80b06cc
75d9abf
 
 
 
 
 
0816a09
 
75d9abf
 
 
 
 
 
 
 
 
 
0816a09
75d9abf
 
 
 
 
 
0816a09
75d9abf
 
 
0816a09
 
75d9abf
 
 
0816a09
 
75d9abf
 
 
 
 
 
 
 
 
0816a09
 
75d9abf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0816a09
75d9abf
0816a09
75d9abf
0816a09
80b06cc
75d9abf
 
 
 
 
 
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
import json
import pandas as pd
import asyncio
import base64
from PIL import Image, ImageDraw
from aiohttp import ClientSession
from io import BytesIO
from data.model import OCR_model
from data.data import name_distribute

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


async def detection(model,img_content):
    try:
        img = Image.open(img_content)
        # result = model(img)
        result = model(img,device=0,conf=0.6)
        
        detection = {}
        data = json.loads(result[0].tojson())
        rect_data = []
        for items in data:
            rect_data.append(items["box"])
            print(items["box"])
        img = await create_rectangle(Image.open(img_content),rect_data)
        if len(data) == 0:
            res = {"AI": "No Detection"}
            detection.update(res)
        else:
            df = pd.DataFrame(data)
            name_counts = df['name'].value_counts().sort_index()
            
            for name, count in name_counts.items():
                res = {name: count}
                detection.update(res)
        return detection, img
    except Exception as e:
        print({"Error in detection":str(e)})


async def share_iamge(img):
    try:
        # img = Image.open(imgs)
        buffer = BytesIO()
        img.save(buffer, format="JPEG")
        base64_image = base64.b64encode(buffer.getvalue()).decode("utf-8")
        data = {"Arabic Detected": base64_image}
        return data
    except Exception as e:
        print({"Error in share_iamge":str(e)})



async def create_rectangle(image_data,cords):
    try:
        drawing = ImageDraw.Draw(image_data)

        for item in cords:
            x1 = item["x1"]
            x2 = item["x2"]
            y1 = item["y1"]
            y2 = item["y2"]
            drawing.rectangle([x1,y1,x2,y2],outline="red",width=1)
        return image_data
    except Exception as e:
        print({"Error in create_rectangle":str(e)})






async def format_result(res,conv):
    try:
        result = {}
        for key,value in conv.items():
            if key in res:
                data = {value:res[key]}
                result.update(data)
        return result
    except Exception as e:
        print({"Error in format_result":str(e)})






async def mainDet(url):
    try:
        image = await asyncio.create_task(getImage(url))
        detect_data,img = await asyncio.create_task(detection(OCR_model, image))
        tab_data = await asyncio.create_task(format_result(detect_data,name_distribute))
        img_data = await asyncio.create_task(share_iamge(img))
        result = {
                    "tabularData":{} if len(tab_data)==0 else tab_data,
                    "imageData":{} # if len(tab_data)==0  else img_data,
                    }
        return json.dumps(result)

    except Exception as e:
        print({"Error in mainDet":str(e)})