File size: 1,211 Bytes
4d7fcdf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import pandas as pd
from PIL import Image
from aiohttp import ClientSession
from io import BytesIO
import asyncio
from data.model import testModel

async def getImage(img_url, session):
    async with session.get(img_url) as response:
        img_data = await response.read()
        return BytesIO(img_data)
    

async def detection(model,img_content,confidence):
    img = Image.open(img_content)
    # result = model(img)
    result = model(source=img,device=0,conf=confidence)
    detection = {}
    data = json.loads(result[0].tojson())
    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

async def mainDet(url):
    async with ClientSession() as session:
        image = await asyncio.create_task(getImage(url, session))
        nbrtuDict = await asyncio.create_task(detection(testModel, image,0.6))

        nagad_result = json.dumps({"sticker count test":nbrtuDict})

        
        return nagad_result