Spaces:
Configuration error
Configuration error
Upload 6 files
Browse files- Dockerfile +25 -0
- README.md +3 -0
- labels.txt +2 -0
- main.py +48 -0
- predict.py +53 -0
- requirements.txt +6 -0
Dockerfile
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ---- build stage ----
|
| 2 |
+
FROM python:3.11-slim AS build
|
| 3 |
+
|
| 4 |
+
WORKDIR /app
|
| 5 |
+
COPY requirements.txt .
|
| 6 |
+
RUN pip install --upgrade pip && \
|
| 7 |
+
pip wheel --no-deps -r requirements.txt -w /wheels
|
| 8 |
+
|
| 9 |
+
# ---- runtime stage ----
|
| 10 |
+
FROM python:3.11-slim
|
| 11 |
+
|
| 12 |
+
ENV PYTHONUNBUFFERED=1
|
| 13 |
+
WORKDIR /app
|
| 14 |
+
|
| 15 |
+
# add wheels then install
|
| 16 |
+
COPY --from=build /wheels /wheels
|
| 17 |
+
RUN pip install --no-index --find-links=/wheels /wheels/*
|
| 18 |
+
|
| 19 |
+
# copy source
|
| 20 |
+
COPY app ./app
|
| 21 |
+
COPY logs ./logs # create if empty
|
| 22 |
+
RUN mkdir -p temp_uploads
|
| 23 |
+
|
| 24 |
+
EXPOSE 8000
|
| 25 |
+
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
|
README.md
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 🛡️ Fake-Logo Detector API
|
| 2 |
+
|
| 3 |
+
Edge-friendly backend (FastAPI + TFLite) that spots *Real vs Fake* brand logos from a single photo and streams analytics.
|
labels.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Genuine
|
| 2 |
+
Fake
|
main.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile, Form
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
+
from app.utils.predict import predict_logo
|
| 5 |
+
import shutil
|
| 6 |
+
import os
|
| 7 |
+
import uuid
|
| 8 |
+
|
| 9 |
+
app = FastAPI(title="Fake Logo Detector API")
|
| 10 |
+
|
| 11 |
+
# Allow frontend calls (camera/gallery)
|
| 12 |
+
app.add_middleware(
|
| 13 |
+
CORSMiddleware,
|
| 14 |
+
allow_origins=["*"],
|
| 15 |
+
allow_credentials=True,
|
| 16 |
+
allow_methods=["*"],
|
| 17 |
+
allow_headers=["*"],
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
UPLOAD_DIR = "temp_uploads"
|
| 21 |
+
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 22 |
+
|
| 23 |
+
@app.post("/scan-logo/")
|
| 24 |
+
async def scan_logo(image: UploadFile = File(...), brand: str = Form("Unknown")):
|
| 25 |
+
try:
|
| 26 |
+
# Save the uploaded image temporarily
|
| 27 |
+
temp_filename = os.path.join(UPLOAD_DIR, f"{uuid.uuid4()}.jpg")
|
| 28 |
+
with open(temp_filename, "wb") as buffer:
|
| 29 |
+
shutil.copyfileobj(image.file, buffer)
|
| 30 |
+
|
| 31 |
+
# Call prediction logic
|
| 32 |
+
verdict, confidence = predict_logo(temp_filename)
|
| 33 |
+
|
| 34 |
+
# Delete temp file
|
| 35 |
+
os.remove(temp_filename)
|
| 36 |
+
|
| 37 |
+
return JSONResponse(content={
|
| 38 |
+
"brand": brand,
|
| 39 |
+
"verdict": verdict,
|
| 40 |
+
"confidence": round(confidence * 100, 2)
|
| 41 |
+
})
|
| 42 |
+
|
| 43 |
+
except Exception as e:
|
| 44 |
+
return JSONResponse(status_code=500, content={"error": str(e)})
|
| 45 |
+
|
| 46 |
+
@app.get("/")
|
| 47 |
+
async def root():
|
| 48 |
+
return {"message": "Fake Logo Detector API is running!"}
|
predict.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import csv, os, datetime
|
| 4 |
+
from tflite_runtime.interpreter import Interpreter # use TensorFlow Lite runtime (tiny!)
|
| 5 |
+
|
| 6 |
+
MODEL_PATH = os.path.join(os.path.dirname(_file_), "..", "model", "best_float32.tflite")
|
| 7 |
+
LABELS_PATH = os.path.join(os.path.dirname(_file_), "..", "model", "labels.txt")
|
| 8 |
+
LOG_PATH = os.path.join(os.path.dirname(_file_), "..", "..", "logs")
|
| 9 |
+
os.makedirs(LOG_PATH, exist_ok=True)
|
| 10 |
+
LOG_FILE = os.path.join(LOG_PATH, "detections.csv")
|
| 11 |
+
|
| 12 |
+
# --- load model once ---------------------------------------------------------
|
| 13 |
+
interpreter = Interpreter(model_path=MODEL_PATH)
|
| 14 |
+
interpreter.allocate_tensors()
|
| 15 |
+
|
| 16 |
+
input_details = interpreter.get_input_details()
|
| 17 |
+
output_details = interpreter.get_output_details()
|
| 18 |
+
input_height, input_width = input_details[0]["shape"][1:3]
|
| 19 |
+
|
| 20 |
+
# binary labels: idx 0 = Fake, idx 1 = Real
|
| 21 |
+
with open(LABELS_PATH, "r") as f:
|
| 22 |
+
labels = [l.strip() for l in f.readlines()]
|
| 23 |
+
|
| 24 |
+
# -----------------------------------------------------------------------------
|
| 25 |
+
def _preprocess(image_path: str) -> np.ndarray:
|
| 26 |
+
img = Image.open(image_path).convert("RGB").resize((input_width, input_height))
|
| 27 |
+
arr = np.asarray(img, dtype=np.float32) / 255.0 # normalize 0-1
|
| 28 |
+
arr = np.expand_dims(arr, axis=0) # add batch dim
|
| 29 |
+
return arr
|
| 30 |
+
|
| 31 |
+
def _log(brand: str, verdict: str, conf: float):
|
| 32 |
+
is_new = not os.path.exists(LOG_FILE)
|
| 33 |
+
with open(LOG_FILE, "a", newline="") as f:
|
| 34 |
+
w = csv.writer(f)
|
| 35 |
+
if is_new:
|
| 36 |
+
w.writerow(["timestamp", "brand", "verdict", "confidence"])
|
| 37 |
+
w.writerow([datetime.datetime.now().isoformat(timespec="seconds"),
|
| 38 |
+
brand, verdict, f"{conf:.4f}"])
|
| 39 |
+
|
| 40 |
+
# -----------------------------------------------------------------------------
|
| 41 |
+
def predict_logo(image_path: str, brand: str = "Unknown"):
|
| 42 |
+
"""Returns verdict ('Real'/'Fake') & confidence (0-1 float)."""
|
| 43 |
+
inp = _preprocess(image_path)
|
| 44 |
+
interpreter.set_tensor(input_details[0]["index"], inp)
|
| 45 |
+
interpreter.invoke()
|
| 46 |
+
output = interpreter.get_tensor(output_details[0]["index"])[0] # shape (2,)
|
| 47 |
+
|
| 48 |
+
conf_real = float(output[1]) # confidence for 'Real'
|
| 49 |
+
verdict = "Real" if conf_real >= 0.5 else "Fake"
|
| 50 |
+
confidence = conf_real if verdict == "Real" else 1.0 - conf_real
|
| 51 |
+
|
| 52 |
+
_log(brand, verdict, confidence)
|
| 53 |
+
return verdict, confidences
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi==0.111.0
|
| 2 |
+
uvicorn[standard]==0.29.0 # production ASGI server
|
| 3 |
+
tflite-runtime==2.14.0 # 1-MB wheel — loads your .tflite model
|
| 4 |
+
pillow==10.3.0 # image handling
|
| 5 |
+
python-multipart==0.0.9 # enables UploadFile in FastAPI
|
| 6 |
+
numpy==1.26.4
|