Spaces:
Running
Running
Update scalingtestupdated.py
Browse files- scalingtestupdated.py +74 -1
scalingtestupdated.py
CHANGED
|
@@ -271,7 +271,80 @@ def calculate_paper_scaling_factor(
|
|
| 271 |
print(f"Final scaling factor: {scaling_factor:.6f} {unit_string}")
|
| 272 |
|
| 273 |
return scaling_factor, unit_string
|
| 274 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 275 |
|
| 276 |
# Example usage:
|
| 277 |
if __name__ == "__main__":
|
|
|
|
| 271 |
print(f"Final scaling factor: {scaling_factor:.6f} {unit_string}")
|
| 272 |
|
| 273 |
return scaling_factor, unit_string
|
| 274 |
+
|
| 275 |
+
def calculate_paper_scaling_factor_corrected(
|
| 276 |
+
paper_contour: np.ndarray,
|
| 277 |
+
paper_size: str,
|
| 278 |
+
output_unit: str = "mm",
|
| 279 |
+
correction_factor: float = 0.79,
|
| 280 |
+
method: str = "average"
|
| 281 |
+
) -> Tuple[float, str]:
|
| 282 |
+
"""
|
| 283 |
+
Calculate scaling factor with configurable correction and method.
|
| 284 |
+
|
| 285 |
+
:param paper_contour: Detected paper contour
|
| 286 |
+
:param paper_size: Paper size identifier ("A4", "A3", "US Letter")
|
| 287 |
+
:param output_unit: Desired unit for scaling factor ("mm" or "inches")
|
| 288 |
+
:param correction_factor: Empirical correction factor (default 0.79)
|
| 289 |
+
:param method: Calculation method ("min", "max", "average", "auto")
|
| 290 |
+
:return: Tuple of (scaling_factor, unit_string)
|
| 291 |
+
"""
|
| 292 |
+
# Get paper dimensions in the desired unit
|
| 293 |
+
if output_unit == "inches":
|
| 294 |
+
expected_width = PAPER_SIZES[paper_size]["width_inches"]
|
| 295 |
+
expected_height = PAPER_SIZES[paper_size]["height_inches"]
|
| 296 |
+
unit_string = "inches per pixel"
|
| 297 |
+
else:
|
| 298 |
+
expected_width = PAPER_SIZES[paper_size]["width_mm"]
|
| 299 |
+
expected_height = PAPER_SIZES[paper_size]["height_mm"]
|
| 300 |
+
unit_string = "mm per pixel"
|
| 301 |
+
|
| 302 |
+
# Calculate bounding rectangle of paper contour
|
| 303 |
+
rect = cv2.boundingRect(paper_contour)
|
| 304 |
+
detected_width_px = rect[2]
|
| 305 |
+
detected_height_px = rect[3]
|
| 306 |
+
|
| 307 |
+
# Calculate scaling factors for both dimensions
|
| 308 |
+
scale_x = expected_width / detected_width_px
|
| 309 |
+
scale_y = expected_height / detected_height_px
|
| 310 |
+
|
| 311 |
+
# Choose scaling method
|
| 312 |
+
if method == "min":
|
| 313 |
+
base_scaling_factor = min(scale_x, scale_y)
|
| 314 |
+
elif method == "max":
|
| 315 |
+
base_scaling_factor = max(scale_x, scale_y)
|
| 316 |
+
elif method == "average":
|
| 317 |
+
base_scaling_factor = (scale_x + scale_y) / 2
|
| 318 |
+
elif method == "auto":
|
| 319 |
+
# Auto-select based on aspect ratio similarity
|
| 320 |
+
detected_aspect = detected_width_px / detected_height_px
|
| 321 |
+
expected_aspect = expected_width / expected_height
|
| 322 |
+
|
| 323 |
+
# If aspect ratios are similar, use average; otherwise use method that matches better
|
| 324 |
+
aspect_diff = abs(detected_aspect - expected_aspect)
|
| 325 |
+
aspect_diff_inv = abs(detected_aspect - (1/expected_aspect))
|
| 326 |
+
|
| 327 |
+
if aspect_diff < aspect_diff_inv:
|
| 328 |
+
base_scaling_factor = (scale_x + scale_y) / 2 # Same orientation
|
| 329 |
+
else:
|
| 330 |
+
# Different orientation - swap and recalculate
|
| 331 |
+
scale_x_swap = expected_height / detected_width_px
|
| 332 |
+
scale_y_swap = expected_width / detected_height_px
|
| 333 |
+
base_scaling_factor = (scale_x_swap + scale_y_swap) / 2
|
| 334 |
+
else:
|
| 335 |
+
raise ValueError(f"Unknown method: {method}")
|
| 336 |
+
|
| 337 |
+
# Apply correction factor
|
| 338 |
+
scaling_factor = base_scaling_factor * correction_factor
|
| 339 |
+
|
| 340 |
+
print(f"Paper detection: {detected_width_px}x{detected_height_px} px")
|
| 341 |
+
print(f"Expected paper size: {expected_width}x{expected_height} {output_unit}")
|
| 342 |
+
print(f"Scale X: {scale_x:.6f}, Scale Y: {scale_y:.6f}")
|
| 343 |
+
print(f"Method: {method}, Base scaling: {base_scaling_factor:.6f}")
|
| 344 |
+
print(f"Correction factor: {correction_factor}")
|
| 345 |
+
print(f"Final scaling factor: {scaling_factor:.6f} {unit_string}")
|
| 346 |
+
|
| 347 |
+
return scaling_factor, unit_string
|
| 348 |
|
| 349 |
# Example usage:
|
| 350 |
if __name__ == "__main__":
|