server updated
Browse files- .gitattributes +1 -0
- .gitignore +6 -0
- AI_Model/Refrigerator.pt +3 -0
- AI_Model/softDrinks.pt +3 -0
- Data/__init__.py +0 -0
- Data/data.py +13 -0
- Data/model.py +6 -0
- api.py +50 -0
- main.py +103 -0
- requirements.txt +0 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
secret_config.py
|
.gitignore
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Ignore a specific file
|
| 2 |
+
secret_config.py
|
| 3 |
+
|
| 4 |
+
# Ignore all .log files
|
| 5 |
+
*.log
|
| 6 |
+
__pycache__
|
AI_Model/Refrigerator.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e3cfeb15ec7c5e9b62eccf185e0399f3595403f4022b418789532a0c33d06fa7
|
| 3 |
+
size 87709289
|
AI_Model/softDrinks.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1c13864b73d78065b32a3a4128dd07d7f33bc3cbeae9feb227d3dca9ea202a10
|
| 3 |
+
size 195455953
|
Data/__init__.py
ADDED
|
File without changes
|
Data/data.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pepsi_items = [
|
| 2 |
+
"7up"
|
| 3 |
+
]
|
| 4 |
+
|
| 5 |
+
competitor_items = [
|
| 6 |
+
"coca_cola",
|
| 7 |
+
"speed",
|
| 8 |
+
"sprite"
|
| 9 |
+
]
|
| 10 |
+
|
| 11 |
+
water_items = [
|
| 12 |
+
"fresh_water"
|
| 13 |
+
]
|
Data/model.py
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from ultralytics import YOLO
|
| 2 |
+
fridgeModel = YOLO('AI_Model/Refrigerator.pt').cuda()
|
| 3 |
+
drinksModel = YOLO('AI_Model/softDrinks.pt').cuda()
|
| 4 |
+
|
| 5 |
+
fridgeModel.to(device=0)
|
| 6 |
+
drinksModel.to(device=0)
|
api.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from typing import List
|
| 4 |
+
import uvicorn
|
| 5 |
+
import logging
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
import pytz
|
| 8 |
+
import torch
|
| 9 |
+
|
| 10 |
+
from main import ImageProcessor
|
| 11 |
+
|
| 12 |
+
logging.basicConfig(filename="drinksLog.log", filemode='w')
|
| 13 |
+
logger = logging.getLogger("drinks")
|
| 14 |
+
logger.setLevel(logging.DEBUG)
|
| 15 |
+
file_handler = logging.FileHandler("drinksLog.log")
|
| 16 |
+
logger.addHandler(file_handler)
|
| 17 |
+
|
| 18 |
+
app = FastAPI()
|
| 19 |
+
|
| 20 |
+
class RequestBody(BaseModel):
|
| 21 |
+
fdz: List[str]
|
| 22 |
+
cItem: List[str]
|
| 23 |
+
|
| 24 |
+
class RequestData(BaseModel):
|
| 25 |
+
body: RequestBody
|
| 26 |
+
|
| 27 |
+
@app.get("/status")
|
| 28 |
+
async def status():
|
| 29 |
+
return {"status": "AI Server is running"}
|
| 30 |
+
|
| 31 |
+
@app.post("/drinks")
|
| 32 |
+
async def detect_items(request_data: RequestData):
|
| 33 |
+
try:
|
| 34 |
+
image_processor = ImageProcessor() # Initialize the image processor
|
| 35 |
+
fdz_urls = request_data.body.fdz
|
| 36 |
+
citem_urls = request_data.body.cItem
|
| 37 |
+
|
| 38 |
+
result = await image_processor.process_images(fdz_urls, citem_urls)
|
| 39 |
+
|
| 40 |
+
return {"response": result}
|
| 41 |
+
|
| 42 |
+
except Exception as e:
|
| 43 |
+
logger.error(f"Error during detection: {str(e)}")
|
| 44 |
+
return {"error": "An error occurred during detection"}
|
| 45 |
+
|
| 46 |
+
if __name__ == "__main__":
|
| 47 |
+
try:
|
| 48 |
+
uvicorn.run(app, host="127.0.0.1", port=4444)
|
| 49 |
+
finally:
|
| 50 |
+
torch.cuda.empty_cache()
|
main.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from aiohttp import ClientSession
|
| 5 |
+
from io import BytesIO
|
| 6 |
+
import asyncio
|
| 7 |
+
from Data.model import fridgeModel, drinksModel
|
| 8 |
+
from Data.data import pepsi_items, competitor_items, water_items
|
| 9 |
+
|
| 10 |
+
class ImageFetcher:
|
| 11 |
+
async def fetch_image(self, url, session):
|
| 12 |
+
try:
|
| 13 |
+
async with session.get(url) as response:
|
| 14 |
+
if response.status == 200:
|
| 15 |
+
img_data = await response.read()
|
| 16 |
+
return Image.open(BytesIO(img_data))
|
| 17 |
+
else:
|
| 18 |
+
print(f"Failed to fetch image from {url}, status code: {response.status}")
|
| 19 |
+
return None
|
| 20 |
+
except Exception as e:
|
| 21 |
+
print(f"Exception during image fetching from {url}: {e}")
|
| 22 |
+
return None
|
| 23 |
+
|
| 24 |
+
class DetectionFilter:
|
| 25 |
+
@staticmethod
|
| 26 |
+
def filter_detection(detection_dict, category_list):
|
| 27 |
+
filtered = {}
|
| 28 |
+
for name, count in detection_dict.items():
|
| 29 |
+
if name in category_list:
|
| 30 |
+
filtered[name] = count
|
| 31 |
+
return filtered
|
| 32 |
+
|
| 33 |
+
class ImageDetector:
|
| 34 |
+
def __init__(self, model, thresh):
|
| 35 |
+
self.model = model
|
| 36 |
+
self.thresh = thresh
|
| 37 |
+
|
| 38 |
+
async def detect_items(self, urls, session):
|
| 39 |
+
detection = {}
|
| 40 |
+
fetcher = ImageFetcher()
|
| 41 |
+
try:
|
| 42 |
+
for url in urls:
|
| 43 |
+
image = await fetcher.fetch_image(url, session)
|
| 44 |
+
if image:
|
| 45 |
+
results = self.model(image, conf=self.thresh)
|
| 46 |
+
if len(results) > 0:
|
| 47 |
+
data = json.loads(results[0].tojson())
|
| 48 |
+
df = pd.DataFrame(data)
|
| 49 |
+
#print("Dataframe:", df)
|
| 50 |
+
if 'name' in df.columns:
|
| 51 |
+
name_counts = df['name'].value_counts().sort_index()
|
| 52 |
+
for name, count in name_counts.items():
|
| 53 |
+
if name in detection:
|
| 54 |
+
detection[name] += count
|
| 55 |
+
else:
|
| 56 |
+
detection[name] = count
|
| 57 |
+
else:
|
| 58 |
+
print(f"No 'name' column found in the DataFrame for URL: {url}")
|
| 59 |
+
else:
|
| 60 |
+
print(f"No results found for image from URL: {url}")
|
| 61 |
+
else:
|
| 62 |
+
print(f"No image fetched for URL: {url}")
|
| 63 |
+
except Exception as e:
|
| 64 |
+
print(f"Error during detection: {e}")
|
| 65 |
+
return detection
|
| 66 |
+
|
| 67 |
+
class ImageProcessor:
|
| 68 |
+
def __init__(self):
|
| 69 |
+
# Initialize models (Category lists are now imported directly)
|
| 70 |
+
self.fridge_model = fridgeModel
|
| 71 |
+
self.drinks_model = drinksModel
|
| 72 |
+
|
| 73 |
+
async def process_images(self, fdz_urls, citem_urls):
|
| 74 |
+
async with ClientSession() as session:
|
| 75 |
+
# Run detection tasks concurrently for both models
|
| 76 |
+
fridge_detector = ImageDetector(self.fridge_model, thresh=0.8)
|
| 77 |
+
drinks_detector = ImageDetector(self.drinks_model, thresh=0.6)
|
| 78 |
+
|
| 79 |
+
fdz_detection = await fridge_detector.detect_items(fdz_urls, session)
|
| 80 |
+
citem_detection = await drinks_detector.detect_items(citem_urls, session)
|
| 81 |
+
|
| 82 |
+
# Filter citem_detection into categories
|
| 83 |
+
filter_tool = DetectionFilter()
|
| 84 |
+
pepsi = filter_tool.filter_detection(citem_detection, pepsi_items)
|
| 85 |
+
competitor = filter_tool.filter_detection(citem_detection, competitor_items)
|
| 86 |
+
water = filter_tool.filter_detection(citem_detection, water_items)
|
| 87 |
+
|
| 88 |
+
# Construct skuDetection dictionary only if it has items
|
| 89 |
+
sku_detection = {}
|
| 90 |
+
if pepsi:
|
| 91 |
+
sku_detection["pepsico"] = pepsi
|
| 92 |
+
if competitor:
|
| 93 |
+
sku_detection["competitor"] = competitor
|
| 94 |
+
if water:
|
| 95 |
+
sku_detection["water"] = water
|
| 96 |
+
|
| 97 |
+
# Prepare response
|
| 98 |
+
response = {
|
| 99 |
+
"fdzDetection": fdz_detection,
|
| 100 |
+
"skuDetection": sku_detection
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
return response
|
requirements.txt
ADDED
|
Binary file (148 Bytes). View file
|
|
|