2023-05-17 13:07:17 -04:00
|
|
|
import os
|
|
|
|
|
import numpy as np
|
|
|
|
|
import cv2 as cv
|
|
|
|
|
import uvicorn
|
|
|
|
|
|
|
|
|
|
from insightface.app import FaceAnalysis
|
2023-02-18 09:13:37 -06:00
|
|
|
from transformers import pipeline
|
2023-05-19 22:37:01 -05:00
|
|
|
from sentence_transformers import SentenceTransformer
|
2023-03-18 08:44:42 -05:00
|
|
|
from PIL import Image
|
2023-04-26 05:39:24 -05:00
|
|
|
from fastapi import FastAPI
|
|
|
|
|
from pydantic import BaseModel
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MlRequestBody(BaseModel):
|
|
|
|
|
thumbnailPath: str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ClipRequestBody(BaseModel):
|
|
|
|
|
text: str
|
|
|
|
|
|
2023-02-18 09:13:37 -06:00
|
|
|
|
2023-04-26 05:39:24 -05:00
|
|
|
classification_model = os.getenv(
|
2023-05-19 22:37:01 -05:00
|
|
|
"MACHINE_LEARNING_CLASSIFICATION_MODEL", "microsoft/resnet-50"
|
|
|
|
|
)
|
|
|
|
|
clip_image_model = os.getenv("MACHINE_LEARNING_CLIP_IMAGE_MODEL", "clip-ViT-B-32")
|
|
|
|
|
clip_text_model = os.getenv("MACHINE_LEARNING_CLIP_TEXT_MODEL", "clip-ViT-B-32")
|
2023-05-17 13:07:17 -04:00
|
|
|
facial_recognition_model = os.getenv(
|
2023-05-19 22:37:01 -05:00
|
|
|
"MACHINE_LEARNING_FACIAL_RECOGNITION_MODEL", "buffalo_l"
|
|
|
|
|
)
|
2023-05-17 13:07:17 -04:00
|
|
|
|
2023-06-02 22:42:47 -04:00
|
|
|
min_face_score = float(os.getenv("MACHINE_LEARNING_MIN_FACE_SCORE", 0.7))
|
|
|
|
|
min_tag_score = float(os.getenv("MACHINE_LEARNING_MIN_TAG_SCORE", 0.9))
|
|
|
|
|
eager_startup = (
|
|
|
|
|
os.getenv("MACHINE_LEARNING_EAGER_STARTUP", "true") == "true"
|
|
|
|
|
) # loads all models at startup
|
|
|
|
|
|
2023-05-19 22:37:01 -05:00
|
|
|
cache_folder = os.getenv("MACHINE_LEARNING_CACHE_FOLDER", "/cache")
|
2023-03-18 08:44:42 -05:00
|
|
|
|
|
|
|
|
_model_cache = {}
|
2023-04-26 05:39:24 -05:00
|
|
|
|
2023-05-17 13:07:17 -04:00
|
|
|
app = FastAPI()
|
|
|
|
|
|
2023-04-26 05:39:24 -05:00
|
|
|
|
2023-05-19 22:37:01 -05:00
|
|
|
@app.on_event("startup")
|
|
|
|
|
async def startup_event():
|
2023-06-02 22:42:47 -04:00
|
|
|
models = [
|
|
|
|
|
(classification_model, "image-classification"),
|
|
|
|
|
(clip_image_model, "clip"),
|
|
|
|
|
(clip_text_model, "clip"),
|
|
|
|
|
(facial_recognition_model, "facial-recognition"),
|
|
|
|
|
]
|
|
|
|
|
|
2023-05-19 22:37:01 -05:00
|
|
|
# Get all models
|
2023-06-02 22:42:47 -04:00
|
|
|
for model_name, model_type in models:
|
|
|
|
|
if eager_startup:
|
|
|
|
|
get_cached_model(model_name, model_type)
|
|
|
|
|
else:
|
|
|
|
|
_get_model(model_name, model_type)
|
2023-05-19 22:37:01 -05:00
|
|
|
|
|
|
|
|
|
2023-04-26 05:39:24 -05:00
|
|
|
@app.get("/")
|
|
|
|
|
async def root():
|
|
|
|
|
return {"message": "Immich ML"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.get("/ping")
|
2023-02-18 09:13:37 -06:00
|
|
|
def ping():
|
|
|
|
|
return "pong"
|
|
|
|
|
|
2023-06-02 22:42:47 -04:00
|
|
|
|
2023-04-26 05:39:24 -05:00
|
|
|
@app.post("/image-classifier/tag-image", status_code=200)
|
|
|
|
|
def image_classification(payload: MlRequestBody):
|
2023-06-02 22:42:47 -04:00
|
|
|
model = get_cached_model(classification_model, "image-classification")
|
2023-04-26 05:39:24 -05:00
|
|
|
assetPath = payload.thumbnailPath
|
|
|
|
|
return run_engine(model, assetPath)
|
|
|
|
|
|
2023-03-18 08:44:42 -05:00
|
|
|
|
2023-04-26 05:39:24 -05:00
|
|
|
@app.post("/sentence-transformer/encode-image", status_code=200)
|
|
|
|
|
def clip_encode_image(payload: MlRequestBody):
|
2023-06-02 22:42:47 -04:00
|
|
|
model = get_cached_model(clip_image_model, "clip")
|
2023-04-26 05:39:24 -05:00
|
|
|
assetPath = payload.thumbnailPath
|
|
|
|
|
return model.encode(Image.open(assetPath)).tolist()
|
2023-02-18 09:13:37 -06:00
|
|
|
|
2023-04-26 05:39:24 -05:00
|
|
|
|
|
|
|
|
@app.post("/sentence-transformer/encode-text", status_code=200)
|
|
|
|
|
def clip_encode_text(payload: ClipRequestBody):
|
2023-06-02 22:42:47 -04:00
|
|
|
model = get_cached_model(clip_text_model, "clip")
|
2023-04-26 05:39:24 -05:00
|
|
|
text = payload.text
|
|
|
|
|
return model.encode(text).tolist()
|
|
|
|
|
|
2023-02-18 09:13:37 -06:00
|
|
|
|
2023-05-17 13:07:17 -04:00
|
|
|
@app.post("/facial-recognition/detect-faces", status_code=200)
|
|
|
|
|
def facial_recognition(payload: MlRequestBody):
|
2023-06-02 22:42:47 -04:00
|
|
|
model = get_cached_model(facial_recognition_model, "facial-recognition")
|
2023-05-17 13:07:17 -04:00
|
|
|
assetPath = payload.thumbnailPath
|
|
|
|
|
img = cv.imread(assetPath)
|
|
|
|
|
height, width, _ = img.shape
|
|
|
|
|
results = []
|
|
|
|
|
faces = model.get(img)
|
2023-05-19 22:37:01 -05:00
|
|
|
|
2023-05-17 13:07:17 -04:00
|
|
|
for face in faces:
|
2023-06-02 22:42:47 -04:00
|
|
|
if face.det_score < min_face_score:
|
2023-05-17 13:07:17 -04:00
|
|
|
continue
|
|
|
|
|
x1, y1, x2, y2 = face.bbox
|
2023-05-19 22:37:01 -05:00
|
|
|
|
|
|
|
|
results.append(
|
|
|
|
|
{
|
|
|
|
|
"imageWidth": width,
|
|
|
|
|
"imageHeight": height,
|
|
|
|
|
"boundingBox": {
|
|
|
|
|
"x1": round(x1),
|
|
|
|
|
"y1": round(y1),
|
|
|
|
|
"x2": round(x2),
|
|
|
|
|
"y2": round(y2),
|
|
|
|
|
},
|
|
|
|
|
"score": face.det_score.item(),
|
|
|
|
|
"embedding": face.normed_embedding.tolist(),
|
|
|
|
|
}
|
|
|
|
|
)
|
2023-05-17 13:07:17 -04:00
|
|
|
return results
|
|
|
|
|
|
|
|
|
|
|
2023-02-18 09:13:37 -06:00
|
|
|
def run_engine(engine, path):
|
|
|
|
|
result = []
|
|
|
|
|
predictions = engine(path)
|
|
|
|
|
|
|
|
|
|
for index, pred in enumerate(predictions):
|
2023-05-19 22:37:01 -05:00
|
|
|
tags = pred["label"].split(", ")
|
2023-06-02 22:42:47 -04:00
|
|
|
if pred["score"] > min_tag_score:
|
2023-02-26 04:02:35 +00:00
|
|
|
result = [*result, *tags]
|
2023-02-18 09:13:37 -06:00
|
|
|
|
2023-05-19 22:37:01 -05:00
|
|
|
if len(result) > 1:
|
2023-02-18 09:13:37 -06:00
|
|
|
result = list(set(result))
|
|
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
2023-06-02 22:42:47 -04:00
|
|
|
def get_cached_model(model, task):
|
2023-04-26 05:39:24 -05:00
|
|
|
global _model_cache
|
2023-05-19 22:37:01 -05:00
|
|
|
key = "|".join([model, str(task)])
|
2023-04-26 05:39:24 -05:00
|
|
|
if key not in _model_cache:
|
2023-06-02 22:42:47 -04:00
|
|
|
model = _get_model(model, task)
|
|
|
|
|
_model_cache[key] = model
|
|
|
|
|
|
2023-04-26 05:39:24 -05:00
|
|
|
return _model_cache[key]
|
|
|
|
|
|
|
|
|
|
|
2023-06-02 22:42:47 -04:00
|
|
|
def _get_model(model, task):
|
|
|
|
|
match task:
|
|
|
|
|
case "facial-recognition":
|
|
|
|
|
model = FaceAnalysis(
|
|
|
|
|
name=model,
|
|
|
|
|
root=cache_folder,
|
|
|
|
|
allowed_modules=["detection", "recognition"],
|
|
|
|
|
)
|
|
|
|
|
model.prepare(ctx_id=0, det_size=(640, 640))
|
|
|
|
|
case "clip":
|
|
|
|
|
model = SentenceTransformer(model, cache_folder=cache_folder)
|
|
|
|
|
case _:
|
|
|
|
|
model = pipeline(model=model, task=task)
|
|
|
|
|
return model
|
|
|
|
|
|
|
|
|
|
|
2023-02-18 09:13:37 -06:00
|
|
|
if __name__ == "__main__":
|
2023-05-19 22:37:01 -05:00
|
|
|
host = os.getenv("MACHINE_LEARNING_HOST", "0.0.0.0")
|
|
|
|
|
port = int(os.getenv("MACHINE_LEARNING_PORT", 3003))
|
|
|
|
|
is_dev = os.getenv("NODE_ENV") == "development"
|
2023-05-17 13:07:17 -04:00
|
|
|
|
|
|
|
|
uvicorn.run("main:app", host=host, port=port, reload=is_dev, workers=1)
|