Spaces:
Sleeping
Sleeping
Update scalingtestupdated.py
Browse files- scalingtestupdated.py +34 -2
scalingtestupdated.py
CHANGED
|
@@ -198,6 +198,7 @@ PAPER_SIZES = {
|
|
| 198 |
}
|
| 199 |
|
| 200 |
|
|
|
|
| 201 |
def calculate_paper_scaling_factor(
|
| 202 |
paper_contour: np.ndarray,
|
| 203 |
paper_size: str,
|
|
@@ -205,12 +206,17 @@ def calculate_paper_scaling_factor(
|
|
| 205 |
) -> Tuple[float, str]:
|
| 206 |
"""
|
| 207 |
Calculate scaling factor based on detected paper dimensions with proper unit handling.
|
|
|
|
| 208 |
|
| 209 |
:param paper_contour: Detected paper contour
|
| 210 |
:param paper_size: Paper size identifier ("A4", "A3", "US Letter")
|
| 211 |
:param output_unit: Desired unit for scaling factor ("mm" or "inches")
|
| 212 |
:return: Tuple of (scaling_factor, unit_string)
|
| 213 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
# Get paper dimensions in the desired unit
|
| 215 |
if output_unit == "inches":
|
| 216 |
expected_width = PAPER_SIZES[paper_size]["width_inches"]
|
|
@@ -230,12 +236,38 @@ def calculate_paper_scaling_factor(
|
|
| 230 |
scale_x = expected_width / detected_width_px
|
| 231 |
scale_y = expected_height / detected_height_px
|
| 232 |
|
| 233 |
-
#
|
| 234 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
|
| 236 |
print(f"Paper detection: {detected_width_px}x{detected_height_px} px")
|
| 237 |
print(f"Expected paper size: {expected_width}x{expected_height} {output_unit}")
|
| 238 |
print(f"Scale X: {scale_x:.6f}, Scale Y: {scale_y:.6f}")
|
|
|
|
|
|
|
| 239 |
print(f"Final scaling factor: {scaling_factor:.6f} {unit_string}")
|
| 240 |
|
| 241 |
return scaling_factor, unit_string
|
|
|
|
| 198 |
}
|
| 199 |
|
| 200 |
|
| 201 |
+
|
| 202 |
def calculate_paper_scaling_factor(
|
| 203 |
paper_contour: np.ndarray,
|
| 204 |
paper_size: str,
|
|
|
|
| 206 |
) -> Tuple[float, str]:
|
| 207 |
"""
|
| 208 |
Calculate scaling factor based on detected paper dimensions with proper unit handling.
|
| 209 |
+
Includes empirical correction factor for improved accuracy.
|
| 210 |
|
| 211 |
:param paper_contour: Detected paper contour
|
| 212 |
:param paper_size: Paper size identifier ("A4", "A3", "US Letter")
|
| 213 |
:param output_unit: Desired unit for scaling factor ("mm" or "inches")
|
| 214 |
:return: Tuple of (scaling_factor, unit_string)
|
| 215 |
"""
|
| 216 |
+
# Empirical correction factor based on measurement analysis
|
| 217 |
+
# Your measurements show DXF is ~79% of original size, so we need to reduce scaling by this factor
|
| 218 |
+
CORRECTION_FACTOR = 0.79 # Adjust this value if needed based on more measurements
|
| 219 |
+
|
| 220 |
# Get paper dimensions in the desired unit
|
| 221 |
if output_unit == "inches":
|
| 222 |
expected_width = PAPER_SIZES[paper_size]["width_inches"]
|
|
|
|
| 236 |
scale_x = expected_width / detected_width_px
|
| 237 |
scale_y = expected_height / detected_height_px
|
| 238 |
|
| 239 |
+
# Try different approaches to find the best scaling factor
|
| 240 |
+
|
| 241 |
+
# Method 1: Use minimum scale (your current approach)
|
| 242 |
+
scaling_factor_min = min(scale_x, scale_y)
|
| 243 |
+
|
| 244 |
+
# Method 2: Use average scale (often more accurate for real-world images)
|
| 245 |
+
scaling_factor_avg = (scale_x + scale_y) / 2
|
| 246 |
+
|
| 247 |
+
# Method 3: Use aspect ratio to determine orientation and pick appropriate scale
|
| 248 |
+
detected_aspect_ratio = detected_width_px / detected_height_px
|
| 249 |
+
expected_aspect_ratio = expected_width / expected_height
|
| 250 |
+
|
| 251 |
+
if abs(detected_aspect_ratio - expected_aspect_ratio) < abs(detected_aspect_ratio - (1/expected_aspect_ratio)):
|
| 252 |
+
# Same orientation
|
| 253 |
+
scaling_factor_oriented = (scale_x + scale_y) / 2
|
| 254 |
+
else:
|
| 255 |
+
# Rotated 90 degrees
|
| 256 |
+
scale_x_rot = expected_height / detected_width_px
|
| 257 |
+
scale_y_rot = expected_width / detected_height_px
|
| 258 |
+
scaling_factor_oriented = (scale_x_rot + scale_y_rot) / 2
|
| 259 |
+
|
| 260 |
+
# Choose the best method (you can experiment with different methods)
|
| 261 |
+
base_scaling_factor = scaling_factor_avg # Changed from min to avg
|
| 262 |
+
|
| 263 |
+
# Apply correction factor
|
| 264 |
+
scaling_factor = base_scaling_factor * CORRECTION_FACTOR
|
| 265 |
|
| 266 |
print(f"Paper detection: {detected_width_px}x{detected_height_px} px")
|
| 267 |
print(f"Expected paper size: {expected_width}x{expected_height} {output_unit}")
|
| 268 |
print(f"Scale X: {scale_x:.6f}, Scale Y: {scale_y:.6f}")
|
| 269 |
+
print(f"Base scaling (avg): {base_scaling_factor:.6f}")
|
| 270 |
+
print(f"Correction factor: {CORRECTION_FACTOR}")
|
| 271 |
print(f"Final scaling factor: {scaling_factor:.6f} {unit_string}")
|
| 272 |
|
| 273 |
return scaling_factor, unit_string
|