forked from AOSSIE-Org/PictoPy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathface_clusters.py
More file actions
349 lines (312 loc) · 11.3 KB
/
Copy pathface_clusters.py
File metadata and controls
349 lines (312 loc) · 11.3 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import logging
from binascii import Error as Base64Error
import base64
from typing import Annotated
import uuid
import os
from fastapi import APIRouter, HTTPException, Query, status
from app.database.face_clusters import (
db_get_cluster_by_id,
db_update_cluster,
db_get_all_clusters_with_face_counts,
db_get_images_by_cluster_id,
)
from app.utils.face_clusters import cluster_util_face_clusters_sync
from app.schemas.face_clusters import (
RenameClusterRequest,
RenameClusterResponse,
RenameClusterData,
ErrorResponse,
GetClustersResponse,
GetClustersData,
GlobalReclusterResponse,
GlobalReclusterData,
ClusterMetadata,
GetClusterImagesResponse,
GetClusterImagesData,
ImageInCluster,
)
from app.schemas.images import FaceSearchRequest, InputType
from app.utils.faceSearch import perform_face_search
logger = logging.getLogger(__name__)
router = APIRouter()
@router.put(
"/{cluster_id}",
response_model=RenameClusterResponse,
responses={code: {"model": ErrorResponse} for code in [400, 404, 500]},
)
def rename_cluster(cluster_id: str, request: RenameClusterRequest):
"""Rename a face cluster by its ID."""
try:
# Step 1: Data Validation
if not cluster_id.strip():
raise ValueError("Cluster ID cannot be empty")
if not request.cluster_name.strip():
raise ValueError("Cluster name cannot be empty")
# Step 2: Check if cluster exists
existing_cluster = db_get_cluster_by_id(cluster_id)
if not existing_cluster:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=ErrorResponse(
success=False,
error="Cluster Not Found",
message=f"Cluster with ID '{cluster_id}' does not exist.",
).model_dump(),
)
# Step 3: Update cluster name
updated = db_update_cluster(
cluster_id=cluster_id,
cluster_name=request.cluster_name.strip(),
)
if not updated:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=ErrorResponse(
success=False,
error="Update Failed",
message=f"Failed to update cluster '{cluster_id}'.",
).model_dump(),
)
return RenameClusterResponse(
success=True,
message=f"Successfully renamed cluster to '{request.cluster_name}'",
data=RenameClusterData(
cluster_id=cluster_id,
cluster_name=request.cluster_name.strip(),
),
)
except ValueError as e:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=ErrorResponse(
success=False,
error="Validation Error",
message=str(e),
).model_dump(),
)
except HTTPException as e:
# Re-raise HTTPExceptions to preserve the status code and detail
raise e
except Exception as e:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=ErrorResponse(
success=False,
error="Internal server error",
message=f"Unable to rename cluster: {str(e)}",
).model_dump(),
)
@router.get(
"/",
response_model=GetClustersResponse,
responses={code: {"model": ErrorResponse} for code in [500]},
)
def get_all_clusters():
"""Get metadata for all face clusters including face counts."""
try:
clusters_data = db_get_all_clusters_with_face_counts()
clusters = [
ClusterMetadata(
cluster_id=cluster["cluster_id"],
cluster_name=cluster["cluster_name"],
face_count=cluster["face_count"],
face_image_base64=cluster["face_image_base64"],
)
for cluster in clusters_data
]
return GetClustersResponse(
success=True,
message=f"Successfully retrieved {len(clusters)} cluster(s)",
data=GetClustersData(clusters=clusters),
)
except Exception as e:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=ErrorResponse(
success=False,
error="Internal server error",
message=f"Unable to retrieve clusters: {str(e)}",
).model_dump(),
)
@router.get(
"/{cluster_id}/images",
response_model=GetClusterImagesResponse,
responses={code: {"model": ErrorResponse} for code in [404, 500]},
)
def get_cluster_images(cluster_id: str):
"""Get all images that contain faces belonging to a specific cluster."""
try:
# Step 1: Validate cluster exists
cluster = db_get_cluster_by_id(cluster_id)
if not cluster:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=ErrorResponse(
success=False,
error="Cluster Not Found",
message=f"Cluster with ID '{cluster_id}' does not exist.",
).model_dump(),
)
# Step 2: Get images for this cluster
images_data = db_get_images_by_cluster_id(cluster_id)
# Step 3: Convert to response models
images = [
ImageInCluster(
id=img["image_id"],
path=img["image_path"],
thumbnailPath=img["thumbnail_path"],
metadata=img["metadata"],
face_id=img["face_id"],
confidence=img["confidence"],
bbox=img["bbox"],
)
for img in images_data
]
return GetClusterImagesResponse(
success=True,
message=f"Successfully retrieved {len(images)} image(s) for cluster '{cluster_id}'",
data=GetClusterImagesData(
cluster_id=cluster_id,
cluster_name=cluster["cluster_name"],
images=images,
total_images=len(images),
),
)
except HTTPException as e:
# Re-raise HTTPExceptions to preserve the status code and detail
raise e
except Exception as e:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=ErrorResponse(
success=False,
error="Internal server error",
message=f"Unable to retrieve images for cluster: {str(e)}",
).model_dump(),
)
@router.post(
"/face-search",
responses={code: {"model": ErrorResponse} for code in [400, 500]},
)
def face_tagging(
payload: FaceSearchRequest,
input_type: Annotated[
InputType, Query(description="Choose input type: 'path' or 'base64'")
] = InputType.path,
):
image_path = None
if input_type == InputType.path:
local_file_path = payload.path
if not local_file_path:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=ErrorResponse(
success=False,
error="No Image path provided ",
message="image path is required.",
).model_dump(),
)
if not os.path.isfile(local_file_path):
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=ErrorResponse(
success=False,
error="Invalid file path",
message="The provided path is not a valid file",
).model_dump(),
)
image_path = payload.path
elif input_type == InputType.base64:
base64_data = payload.base64_data
if not base64_data:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=ErrorResponse(
success=False,
error="No base64 data",
message="Base64 image data is required.",
).model_dump(),
)
MAX_B64_LEN = 14_000_000 # 10MB
if len(base64_data) > MAX_B64_LEN:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=ErrorResponse(
success=False,
error="Payload too large",
message="Base64 image exceeds maximum allowed size.",
).model_dump(),
)
try:
image_bytes = base64.b64decode(base64_data.split(",")[-1])
except (Base64Error, ValueError):
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=ErrorResponse(
success=False,
error="Invalid base64 data",
message="The provided base64 image data is malformed or invalid.",
).model_dump(),
)
format_match = (
base64_data.split(";")[0].split("/")[-1] if ";" in base64_data else "jpeg"
)
extension = (
format_match
if format_match in ["jpeg", "jpg", "png", "gif", "webp"]
else "jpeg"
)
image_id = str(uuid.uuid4())[:8]
temp_dir = "temp_uploads"
os.makedirs(temp_dir, exist_ok=True)
local_image_path = os.path.join(temp_dir, f"{image_id}.{extension}")
with open(local_image_path, "wb") as f:
f.write(image_bytes)
image_path = local_image_path
try:
return perform_face_search(image_path)
finally:
if input_type == InputType.base64 and image_path and os.path.exists(image_path):
os.remove(image_path)
@router.post(
"/global-recluster",
response_model=GlobalReclusterResponse,
responses={code: {"model": ErrorResponse} for code in [500]},
)
def trigger_global_reclustering():
"""
Manually trigger global face reclustering.
This forces full reclustering regardless of the 24-hour rule.
"""
try:
logger.info("Starting manual global face reclustering...")
result, total_faces_skipped = cluster_util_face_clusters_sync(
force_full_reclustering=True
)
if result == 0:
return GlobalReclusterResponse(
success=True,
message="No faces found to cluster",
data=GlobalReclusterData(
clusters_created=0, faces_skipped=total_faces_skipped
),
)
logger.info("Global reclustering completed successfully")
return GlobalReclusterResponse(
success=True,
message="Global reclustering completed successfully.",
data=GlobalReclusterData(
clusters_created=result, faces_skipped=total_faces_skipped
),
)
except Exception as e:
logger.error(f"Global reclustering failed: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=ErrorResponse(
success=False,
error="Internal server error",
message=f"Global reclustering failed: {str(e)}",
).model_dump(),
)