mustafa2ak commited on
Commit
3a5fb08
·
verified ·
1 Parent(s): 6e605a5

Update database.py

Browse files
Files changed (1) hide show
  1. database.py +58 -19
database.py CHANGED
@@ -326,25 +326,64 @@ class DogDatabase:
326
  frame_number: int, video_source: str,
327
  bbox: List[float], confidence: float,
328
  pose_keypoints: Optional[List] = None):
329
- """Save dog image to database"""
330
- _, buffer = cv2.imencode('.jpg', image)
331
- image_data = base64.b64encode(buffer).decode('utf-8')
332
-
333
- thumbnail = cv2.resize(image, (128, 128))
334
- _, thumb_buffer = cv2.imencode('.jpg', thumbnail, [cv2.IMWRITE_JPEG_QUALITY, 70])
335
- thumb_data = base64.b64encode(thumb_buffer).decode('utf-8')
336
-
337
- h, w = image.shape[:2]
338
- self.cursor.execute("""
339
- INSERT INTO dog_images
340
- (dog_id, image_data, thumbnail, width, height,
341
- frame_number, video_source, bbox, confidence, pose_keypoints)
342
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
343
- """, (dog_id, image_data, thumb_data, w, h,
344
- frame_number, video_source, json.dumps(bbox),
345
- confidence, json.dumps(pose_keypoints) if pose_keypoints else None))
346
- self.conn.commit()
347
- return self.cursor.lastrowid
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
348
 
349
  def get_dog_images(self, dog_id: int, validated_only: bool = False,
350
  include_discarded: bool = False) -> List[Dict]:
 
326
  frame_number: int, video_source: str,
327
  bbox: List[float], confidence: float,
328
  pose_keypoints: Optional[List] = None):
329
+ """Save dog image to database"""
330
+ _, buffer = cv2.imencode('.jpg', image)
331
+ image_data = base64.b64encode(buffer).decode('utf-8')
332
+
333
+ thumbnail = cv2.resize(image, (128, 128))
334
+ _, thumb_buffer = cv2.imencode('.jpg', thumbnail, [cv2.IMWRITE_JPEG_QUALITY, 70])
335
+ thumb_data = base64.b64encode(thumb_buffer).decode('utf-8')
336
+
337
+ h, w = image.shape[:2]
338
+ self.cursor.execute("""
339
+ INSERT INTO dog_images
340
+ (dog_id, image_data, thumbnail, width, height,
341
+ frame_number, video_source, bbox, confidence, pose_keypoints)
342
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
343
+ """, (dog_id, image_data, thumb_data, w, h,
344
+ frame_number, video_source, json.dumps(bbox),
345
+ confidence, json.dumps(pose_keypoints) if pose_keypoints else None))
346
+ self.conn.commit()
347
+ return self.cursor.lastrowid
348
+ def add_dog_image(self, dog_id: int, image: np.ndarray,
349
+ timestamp: float, confidence: float, bbox: tuple):
350
+ """Add a dog image to database (simplified for dataset collection)"""
351
+ try:
352
+ # Convert image to base64 for storage
353
+ _, buffer = cv2.imencode('.jpg', image)
354
+ image_data = base64.b64encode(buffer).decode('utf-8')
355
+
356
+ # Create thumbnail
357
+ thumbnail = cv2.resize(image, (128, 128))
358
+ _, thumb_buffer = cv2.imencode('.jpg', thumbnail, [cv2.IMWRITE_JPEG_QUALITY, 70])
359
+ thumb_data = base64.b64encode(thumb_buffer).decode('utf-8')
360
+
361
+ h, w = image.shape[:2]
362
+
363
+ # Insert into database
364
+ self.cursor.execute("""
365
+ INSERT INTO dog_images
366
+ (dog_id, image_data, thumbnail, width, height,
367
+ timestamp, bbox, confidence)
368
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?)
369
+ """, (dog_id, image_data, thumb_data, w, h,
370
+ timestamp, json.dumps(bbox), confidence))
371
+
372
+ # Update dog's last_seen and sighting count
373
+ self.cursor.execute("""
374
+ UPDATE dogs
375
+ SET last_seen = datetime('now'),
376
+ total_sightings = total_sightings + 1
377
+ WHERE dog_id = ?
378
+ """, (dog_id,))
379
+
380
+ self.conn.commit()
381
+ return self.cursor.lastrowid
382
+
383
+ except Exception as e:
384
+ self.conn.rollback()
385
+ print(f"Error adding dog image: {e}")
386
+ raise
387
 
388
  def get_dog_images(self, dog_id: int, validated_only: bool = False,
389
  include_discarded: bool = False) -> List[Dict]: