-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyzeImageAPI.py
More file actions
40 lines (31 loc) · 1.28 KB
/
analyzeImageAPI.py
File metadata and controls
40 lines (31 loc) · 1.28 KB
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
from fastapi import FastAPI, File, UploadFile
from fastapi.responses import JSONResponse
import io
from Interrogator import Interrogator, SWINV2_MODEL_DSV3_REPO
app = FastAPI()
# Initialize the Interrogator
interrogator = Interrogator()
interrogator.load_model(SWINV2_MODEL_DSV3_REPO)
@app.post("/analyze_image/")
async def analyze_image(file: UploadFile = File(...)):
try:
# Read the uploaded file
contents = await file.read()
image = io.BytesIO(contents)
# Set thresholds
general_thresh = 0.35
character_thresh = 0.85
# Predict using the Interrogator
ratings, general_tags, character_tags = interrogator.predict(image, general_thresh, character_thresh)
# Prepare the response
response = {
"ratings": [{"name": name, "score": float(score)} for name, score in ratings],
"general_tags": [{"name": name, "score": float(score)} for name, score in general_tags],
"character_tags": [{"name": name, "score": float(score)} for name, score in character_tags]
}
return JSONResponse(content=response)
except Exception as e:
return JSONResponse(content={"error": str(e)}, status_code=500)
if __name__ == "__main__":
import uvicorn
uvicorn.run(app,port=8000)