| import json |
| import pandas as pd |
| from PIL import Image |
| from .model import model |
| from .data import pepsodent_qpds_data |
|
|
|
|
|
|
|
|
|
|
|
|
| async def detection(model, img_content, confidence): |
| try: |
| img = Image.open(img_content) |
| result = model(source=img, device=0, conf=confidence) |
| detection = {} |
| data = json.loads(result[0].tojson()) |
| if not result or not data: |
| return {} |
| else: |
| name_counts = {} |
| for item in data: |
| name = item['name'] |
| if name in name_counts: |
| name_counts[name] += 1 |
| else: |
| name_counts[name] = 1 |
| detection.update(name_counts) |
|
|
| return detection |
| except Exception as e: |
| return {} |
|
|
|
|
| async def format_result(convertData,resData): |
| try: |
| result = {} |
| for aiName,sysName in convertData.items(): |
| if aiName in resData: |
| result.update({sysName:resData[aiName]}) |
| return result |
| except Exception as e: |
| pass |
|
|
|
|
|
|
|
|
| async def main(img): |
| try: |
| result = await detection(model,img,0.3) |
| formattedResult = await format_result(pepsodent_qpds_data,result) |
|
|
| return formattedResult |
| except Exception as e: |
| pass |
|
|