yamero999 commited on
Commit
08380dc
·
verified ·
1 Parent(s): 67c3d02

Update fen.py with HuggingFace Hub integration

Browse files
Files changed (1) hide show
  1. fen.py +54 -4
fen.py CHANGED
@@ -937,8 +937,8 @@ def run_onnx_yolo_detection_416(image, onnx_session):
937
  'black_bishop', 'black_rook', 'black_queen', 'black_king'
938
  ]
939
 
940
- # Use lower confidence threshold for ONNX
941
- conf_threshold = 0.1
942
 
943
  # Process each prediction
944
  for i, prediction in enumerate(predictions):
@@ -1010,9 +1010,59 @@ def run_onnx_yolo_detection_416(image, onnx_session):
1010
  'center': [center_x, center_y]
1011
  })
1012
 
1013
- print(f" 🔍 Detected {piece_name} at ({center_x:.1f}, {center_y:.1f}) conf: {final_conf:.3f}")
 
1014
 
1015
- return detections
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1016
 
1017
  def map_pieces_to_board(detections, board_size=512):
1018
  """Map pieces to 8x8 board with improved tolerance for pieces outside square boundaries"""
 
937
  'black_bishop', 'black_rook', 'black_queen', 'black_king'
938
  ]
939
 
940
+ # Use higher confidence threshold for better quality detections
941
+ conf_threshold = 0.4
942
 
943
  # Process each prediction
944
  for i, prediction in enumerate(predictions):
 
1010
  'center': [center_x, center_y]
1011
  })
1012
 
1013
+ # Filter to keep only highest confidence detection for each board position
1014
+ filtered_detections = filter_highest_confidence_detections(detections)
1015
 
1016
+ print(f" 📊 Filtered to {len(filtered_detections)} high-confidence detections")
1017
+ for detection in filtered_detections:
1018
+ piece_name = detection['piece']
1019
+ center_x, center_y = detection['center']
1020
+ conf = detection['confidence']
1021
+ print(f" 🔍 Final {piece_name} at ({center_x:.1f}, {center_y:.1f}) conf: {conf:.3f}")
1022
+
1023
+ return filtered_detections
1024
+
1025
+ def filter_highest_confidence_detections(detections, grid_size=64):
1026
+ """Filter detections to keep only the highest confidence detection for each board position"""
1027
+ print("🔍 Filtering detections to keep highest confidence per position...")
1028
+
1029
+ # Group detections by approximate board position
1030
+ position_groups = {}
1031
+
1032
+ for detection in detections:
1033
+ center_x, center_y = detection['center']
1034
+
1035
+ # Calculate approximate grid position (8x8 board)
1036
+ grid_x = int(center_x // grid_size)
1037
+ grid_y = int(center_y // grid_size)
1038
+
1039
+ # Clamp to valid board positions
1040
+ grid_x = max(0, min(7, grid_x))
1041
+ grid_y = max(0, min(7, grid_y))
1042
+
1043
+ position_key = f"{grid_x},{grid_y}"
1044
+
1045
+ if position_key not in position_groups:
1046
+ position_groups[position_key] = []
1047
+
1048
+ position_groups[position_key].append(detection)
1049
+
1050
+ # Keep only the highest confidence detection for each position
1051
+ filtered_detections = []
1052
+
1053
+ for position_key, group_detections in position_groups.items():
1054
+ if len(group_detections) == 1:
1055
+ # Only one detection at this position
1056
+ filtered_detections.append(group_detections[0])
1057
+ else:
1058
+ # Multiple detections - keep the one with highest confidence
1059
+ best_detection = max(group_detections, key=lambda d: d['confidence'])
1060
+ filtered_detections.append(best_detection)
1061
+
1062
+ print(f" 📍 Position {position_key}: kept best of {len(group_detections)} detections (conf: {best_detection['confidence']:.3f})")
1063
+
1064
+ print(f" ✅ Filtered from {len(detections)} to {len(filtered_detections)} detections")
1065
+ return filtered_detections
1066
 
1067
  def map_pieces_to_board(detections, board_size=512):
1068
  """Map pieces to 8x8 board with improved tolerance for pieces outside square boundaries"""