Spaces:
Sleeping
Sleeping
| from os.path import join, dirname, basename, exists, normpath | |
| from os import makedirs | |
| from glob import glob | |
| import cv2 | |
| import numpy as np | |
| # Use normpath to ensure Windows/Linux compatibility | |
| dirname_val = normpath(dirname(__file__)) | |
| db_path = join(dirname_val, 'static', 'db_fingerprints') | |
| def getAllImagesFromDatabase(): | |
| fingerprint_db = [] | |
| # Search for all common image formats | |
| for ext in ('*.png', '*.jpg', '*.jpeg', '*.bmp'): | |
| fingerprint_db.extend(glob(join(db_path, ext))) | |
| db_json = [] | |
| # Use enumerate for cleaner ID generation | |
| for i, file_path in enumerate(fingerprint_db, 1): | |
| # 1. Extract the label (filename without extension) | |
| name = basename(file_path).rsplit('.', 1)[0] | |
| # 2. Fix the URL pathing | |
| # We ensure it starts with a forward slash and uses forward slashes | |
| # for Flask template compatibility | |
| relative_url = file_path.replace(dirname_val, '').replace('\\', '/') | |
| db_json.append({ | |
| "id": i, | |
| "label": name, | |
| "url": relative_url | |
| }) | |
| # Sort by label so the database view looks organized | |
| return sorted(db_json, key=lambda k: k['label']) | |
| def saveImageToDatabase(label, img): | |
| """Saves the enhanced fingerprint to the database folder.""" | |
| if not exists(db_path): | |
| makedirs(db_path) | |
| # Ensure filename is safe (remove spaces/special chars if needed) | |
| safe_label = "".join([c for c in label if c.isalnum() or c in (' ', '-', '_')]).strip() | |
| save_path = join(db_path, f"{safe_label}.jpg") | |
| cv2.imwrite(save_path, img) | |
| return save_path |