Adding annotation list
This commit is contained in:
@@ -658,6 +658,75 @@ class DatabaseManager:
|
||||
|
||||
# ==================== Annotation Operations ====================
|
||||
|
||||
def get_annotated_images_summary(
|
||||
self,
|
||||
name_filter: Optional[str] = None,
|
||||
order_by: str = "filename",
|
||||
order_dir: str = "ASC",
|
||||
limit: Optional[int] = None,
|
||||
offset: int = 0,
|
||||
) -> List[Dict]:
|
||||
"""Return images that have at least one manual annotation.
|
||||
|
||||
Args:
|
||||
name_filter: Optional substring filter applied to filename/relative_path.
|
||||
order_by: One of: 'filename', 'relative_path', 'annotation_count', 'added_at'.
|
||||
order_dir: 'ASC' or 'DESC'.
|
||||
limit: Optional max number of rows.
|
||||
offset: Pagination offset.
|
||||
|
||||
Returns:
|
||||
List of dicts: {id, relative_path, filename, added_at, annotation_count}
|
||||
"""
|
||||
|
||||
allowed_order_by = {
|
||||
"filename": "i.filename",
|
||||
"relative_path": "i.relative_path",
|
||||
"annotation_count": "annotation_count",
|
||||
"added_at": "i.added_at",
|
||||
}
|
||||
order_expr = allowed_order_by.get(order_by, "i.filename")
|
||||
dir_norm = str(order_dir).upper().strip()
|
||||
if dir_norm not in {"ASC", "DESC"}:
|
||||
dir_norm = "ASC"
|
||||
|
||||
conn = self.get_connection()
|
||||
try:
|
||||
params: List[Any] = []
|
||||
where_sql = ""
|
||||
if name_filter:
|
||||
# Case-insensitive substring search.
|
||||
token = f"%{name_filter}%"
|
||||
where_sql = "WHERE (i.filename LIKE ? OR i.relative_path LIKE ?)"
|
||||
params.extend([token, token])
|
||||
|
||||
limit_sql = ""
|
||||
if limit is not None:
|
||||
limit_sql = " LIMIT ? OFFSET ?"
|
||||
params.extend([int(limit), int(offset)])
|
||||
|
||||
query = f"""
|
||||
SELECT
|
||||
i.id,
|
||||
i.relative_path,
|
||||
i.filename,
|
||||
i.added_at,
|
||||
COUNT(a.id) AS annotation_count
|
||||
FROM images i
|
||||
JOIN annotations a ON a.image_id = i.id
|
||||
{where_sql}
|
||||
GROUP BY i.id
|
||||
HAVING annotation_count > 0
|
||||
ORDER BY {order_expr} {dir_norm}
|
||||
{limit_sql}
|
||||
"""
|
||||
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(query, params)
|
||||
return [dict(row) for row in cursor.fetchall()]
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def add_annotation(
|
||||
self,
|
||||
image_id: int,
|
||||
|
||||
Reference in New Issue
Block a user