Add dockerized detector and UI cleanup
This commit is contained in:
53
app/main.py
53
app/main.py
@@ -2,7 +2,7 @@ from fastapi import FastAPI, File, Form, HTTPException, UploadFile
|
||||
from fastapi.responses import HTMLResponse, StreamingResponse
|
||||
from app.config import settings
|
||||
|
||||
app = FastAPI(title="face-lock", version="0.1.0")
|
||||
app = FastAPI(title="face-lock", version="0.2.0")
|
||||
|
||||
|
||||
@app.get("/health")
|
||||
@@ -25,16 +25,30 @@ def index():
|
||||
<title>face-lock</title>
|
||||
</head>
|
||||
<body class="bg-slate-950 text-slate-100 min-h-screen">
|
||||
<main class="mx-auto max-w-5xl p-6">
|
||||
<main class="mx-auto max-w-6xl p-6">
|
||||
<div class="mb-6">
|
||||
<h1 class="text-3xl font-bold">face-lock</h1>
|
||||
<p class="text-slate-400">Drop an image, get the primary subject squared and cropped.</p>
|
||||
<p class="text-slate-400">Auto-detect the subject, square it up, and crop with buffer.</p>
|
||||
</div>
|
||||
<div class="grid gap-6 md:grid-cols-2">
|
||||
<section class="rounded-2xl border border-slate-800 bg-slate-900 p-4">
|
||||
<input id="file" type="file" accept="image/*" class="block w-full rounded-lg border border-slate-700 bg-slate-950 p-3" />
|
||||
<label class="mt-4 block text-sm text-slate-400">Buffer ratio</label>
|
||||
<input id="buffer_ratio" type="number" step="0.05" min="0" max="0.5" value="0.15" class="block w-full rounded-lg border border-slate-700 bg-slate-950 p-3" />
|
||||
<label class="block text-sm text-slate-400">Image</label>
|
||||
<input id="file" type="file" accept="image/*" class="mt-2 block w-full rounded-lg border border-slate-700 bg-slate-950 p-3" />
|
||||
<div class="mt-4 grid gap-4 sm:grid-cols-2">
|
||||
<div>
|
||||
<label class="block text-sm text-slate-400">Detector</label>
|
||||
<select id="detector" class="mt-2 block w-full rounded-lg border border-slate-700 bg-slate-950 p-3">
|
||||
<option value="auto">Auto</option>
|
||||
<option value="face">Face</option>
|
||||
<option value="person">Person</option>
|
||||
<option value="salient">Subject</option>
|
||||
</select>
|
||||
</div>
|
||||
<div>
|
||||
<label class="block text-sm text-slate-400">Buffer ratio</label>
|
||||
<input id="buffer_ratio" type="number" step="0.05" min="0" max="0.6" value="0.20" class="mt-2 block w-full rounded-lg border border-slate-700 bg-slate-950 p-3" />
|
||||
</div>
|
||||
</div>
|
||||
<button id="go" class="mt-4 rounded-lg bg-cyan-500 px-4 py-2 font-semibold text-slate-950">Process</button>
|
||||
<pre id="meta" class="mt-4 whitespace-pre-wrap rounded-lg bg-slate-950 p-3 text-xs text-slate-300"></pre>
|
||||
</section>
|
||||
@@ -63,11 +77,20 @@ def index():
|
||||
if (!file.files.length) return;
|
||||
const form = new FormData();
|
||||
form.append('file', file.files[0]);
|
||||
form.append('detector', document.getElementById('detector').value);
|
||||
form.append('buffer_ratio', document.getElementById('buffer_ratio').value);
|
||||
meta.textContent = 'Working...';
|
||||
const resp = await fetch('/api/focus', { method: 'POST', body: form });
|
||||
const data = await resp.json();
|
||||
meta.textContent = JSON.stringify(data, null, 2);
|
||||
meta.textContent = JSON.stringify({
|
||||
filename: data.filename,
|
||||
detector: data.detector,
|
||||
method: data.method,
|
||||
buffer_ratio: data.buffer_ratio,
|
||||
detected_bbox: data.detected_bbox,
|
||||
square_bbox: data.square_bbox,
|
||||
source_size: data.source_size,
|
||||
}, null, 2);
|
||||
crop.src = data.crop_data_url;
|
||||
annotated.src = data.annotated_data_url;
|
||||
crop.classList.remove('hidden');
|
||||
@@ -81,23 +104,31 @@ def index():
|
||||
|
||||
|
||||
@app.post("/api/focus")
|
||||
async def focus(file: UploadFile = File(...), buffer_ratio: float = Form(0.15)):
|
||||
async def focus(
|
||||
file: UploadFile = File(...),
|
||||
buffer_ratio: float = Form(0.15),
|
||||
detector: str = Form("auto"),
|
||||
):
|
||||
from app.vision import process_image
|
||||
|
||||
try:
|
||||
payload = await file.read()
|
||||
return process_image(payload, file.filename or "upload", buffer_ratio=buffer_ratio)
|
||||
return process_image(payload, file.filename or "upload", buffer_ratio=buffer_ratio, detector=detector)
|
||||
except ValueError as exc:
|
||||
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
||||
|
||||
|
||||
@app.post("/api/focus/image")
|
||||
async def focus_image(file: UploadFile = File(...), buffer_ratio: float = Form(0.15)):
|
||||
async def focus_image(
|
||||
file: UploadFile = File(...),
|
||||
buffer_ratio: float = Form(0.15),
|
||||
detector: str = Form("auto"),
|
||||
):
|
||||
from app.vision import process_image
|
||||
|
||||
try:
|
||||
payload = await file.read()
|
||||
result = process_image(payload, file.filename or "upload", buffer_ratio=buffer_ratio)
|
||||
result = process_image(payload, file.filename or "upload", buffer_ratio=buffer_ratio, detector=detector)
|
||||
return StreamingResponse(result["crop_bytes_io"], media_type=result["mime_type"])
|
||||
except ValueError as exc:
|
||||
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
||||
|
||||
Reference in New Issue
Block a user