File size: 2,140 Bytes
ac598ad
 
 
 
f67d0b5
ac598ad
 
 
 
 
 
 
 
 
 
 
 
 
 
f67d0b5
 
 
ac598ad
 
 
 
 
 
15e20a0
ac598ad
 
 
 
 
 
 
 
 
 
 
 
 
 
f67d0b5
 
 
 
ac598ad
 
 
 
 
 
 
 
 
 
 
 
f67d0b5
 
 
ac598ad
 
 
 
 
 
 
 
 
f67d0b5
 
 
ac598ad
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
import json
import pandas as pd
import asyncio
import base64
import torch
from PIL import Image, ImageDraw
from aiohttp import ClientSession
from io import BytesIO
from data.teamData import member_details
from model.model import faceModel


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:
            raise ValueError(f"getImage ERROR : {str(e)}")
        finally:
            torch.cuda.empty_cache()


async def detection(model,img_content):
    try:
        img = Image.open(img_content)
        # result = model(img)
        result = model(img,device=0,conf=0.8)
        detection = {}
        data = json.loads(result[0].tojson())
        if len(data) == 0:
            res = {"AI": "Not Found"}
            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
    except Exception as e:
        raise ValueError(f"detection ERROR : {str(e)}")
    finally:
        torch.cuda.empty_cache()





async def format_result(ai_result,convert_data):
    try:
        result = {}
        for i,j in ai_result.items():
            if i in member_details:
                result.update({i:member_details[i]})
        return result
    except Exception as e:
        raise ValueError(f"format_result ERROR : {str(e)}")
    finally:
        torch.cuda.empty_cache()


async def mainDet(url):
    try:
        image = await asyncio.create_task(getImage(url))
        detect_data = await asyncio.create_task(detection(faceModel, image))
        result = await asyncio.create_task(format_result(detect_data,member_details))
        return json.dumps(result)
    except Exception as e:
        raise ValueError(f"mainDet ERROR : {str(e)}")
    finally:
        torch.cuda.empty_cache()