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