Spaces:
Paused
Paused
Update database.py
Browse files- database.py +31 -29
database.py
CHANGED
|
@@ -321,31 +321,33 @@ class DogDatabase:
|
|
| 321 |
})
|
| 322 |
return features
|
| 323 |
|
|
|
|
| 324 |
# ========== Images Management ==========
|
| 325 |
def save_image(self, dog_id: int, image: np.ndarray,
|
| 326 |
frame_number: int, video_source: str,
|
| 327 |
bbox: List[float], confidence: float,
|
| 328 |
pose_keypoints: Optional[List] = None):
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
|
|
|
| 349 |
timestamp: float, confidence: float, bbox: tuple):
|
| 350 |
"""Add a dog image to database (simplified for dataset collection)"""
|
| 351 |
try:
|
|
@@ -380,13 +382,13 @@ class DogDatabase:
|
|
| 380 |
self.conn.commit()
|
| 381 |
return self.cursor.lastrowid
|
| 382 |
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
|
| 388 |
def get_dog_images(self, dog_id: int, validated_only: bool = False,
|
| 389 |
-
|
| 390 |
"""Get all images for a dog"""
|
| 391 |
query = "SELECT * FROM dog_images WHERE dog_id = ?"
|
| 392 |
params = [dog_id]
|
|
@@ -408,13 +410,13 @@ class DogDatabase:
|
|
| 408 |
'image_id': row['image_id'],
|
| 409 |
'image': image,
|
| 410 |
'thumbnail': row['thumbnail'],
|
| 411 |
-
'bbox': json.loads(row['bbox']),
|
| 412 |
'confidence': row['confidence'],
|
| 413 |
-
'frame_number': row
|
| 414 |
-
'video_source': row
|
| 415 |
'is_validated': row['is_validated'],
|
| 416 |
'is_discarded': row['is_discarded'],
|
| 417 |
-
'pose_keypoints': json.loads(row['pose_keypoints']) if row
|
| 418 |
})
|
| 419 |
return images
|
| 420 |
|
|
|
|
| 321 |
})
|
| 322 |
return features
|
| 323 |
|
| 324 |
+
# ========== Images Management ==========
|
| 325 |
# ========== Images Management ==========
|
| 326 |
def save_image(self, dog_id: int, image: np.ndarray,
|
| 327 |
frame_number: int, video_source: str,
|
| 328 |
bbox: List[float], confidence: float,
|
| 329 |
pose_keypoints: Optional[List] = None):
|
| 330 |
+
"""Save dog image to database"""
|
| 331 |
+
_, buffer = cv2.imencode('.jpg', image)
|
| 332 |
+
image_data = base64.b64encode(buffer).decode('utf-8')
|
| 333 |
+
|
| 334 |
+
thumbnail = cv2.resize(image, (128, 128))
|
| 335 |
+
_, thumb_buffer = cv2.imencode('.jpg', thumbnail, [cv2.IMWRITE_JPEG_QUALITY, 70])
|
| 336 |
+
thumb_data = base64.b64encode(thumb_buffer).decode('utf-8')
|
| 337 |
+
|
| 338 |
+
h, w = image.shape[:2]
|
| 339 |
+
self.cursor.execute("""
|
| 340 |
+
INSERT INTO dog_images
|
| 341 |
+
(dog_id, image_data, thumbnail, width, height,
|
| 342 |
+
frame_number, video_source, bbox, confidence, pose_keypoints)
|
| 343 |
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
| 344 |
+
""", (dog_id, image_data, thumb_data, w, h,
|
| 345 |
+
frame_number, video_source, json.dumps(bbox),
|
| 346 |
+
confidence, json.dumps(pose_keypoints) if pose_keypoints else None))
|
| 347 |
+
self.conn.commit()
|
| 348 |
+
return self.cursor.lastrowid
|
| 349 |
+
|
| 350 |
+
def add_dog_image(self, dog_id: int, image: np.ndarray,
|
| 351 |
timestamp: float, confidence: float, bbox: tuple):
|
| 352 |
"""Add a dog image to database (simplified for dataset collection)"""
|
| 353 |
try:
|
|
|
|
| 382 |
self.conn.commit()
|
| 383 |
return self.cursor.lastrowid
|
| 384 |
|
| 385 |
+
except Exception as e:
|
| 386 |
+
self.conn.rollback()
|
| 387 |
+
print(f"Error adding dog image: {e}")
|
| 388 |
+
raise
|
| 389 |
|
| 390 |
def get_dog_images(self, dog_id: int, validated_only: bool = False,
|
| 391 |
+
include_discarded: bool = False) -> List[Dict]:
|
| 392 |
"""Get all images for a dog"""
|
| 393 |
query = "SELECT * FROM dog_images WHERE dog_id = ?"
|
| 394 |
params = [dog_id]
|
|
|
|
| 410 |
'image_id': row['image_id'],
|
| 411 |
'image': image,
|
| 412 |
'thumbnail': row['thumbnail'],
|
| 413 |
+
'bbox': json.loads(row['bbox']) if row['bbox'] else [],
|
| 414 |
'confidence': row['confidence'],
|
| 415 |
+
'frame_number': row.get('frame_number'),
|
| 416 |
+
'video_source': row.get('video_source'),
|
| 417 |
'is_validated': row['is_validated'],
|
| 418 |
'is_discarded': row['is_discarded'],
|
| 419 |
+
'pose_keypoints': json.loads(row['pose_keypoints']) if row.get('pose_keypoints') else None
|
| 420 |
})
|
| 421 |
return images
|
| 422 |
|