mlbench123 commited on
Commit
da39403
·
verified ·
1 Parent(s): edb0795

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -58
app.py CHANGED
@@ -974,62 +974,47 @@ def predict_with_paper(image, paper_size, offset, offset_unit, finger_clearance=
974
  raise gr.Error(f"Error processing image: {str(e)}")
975
 
976
  try:
977
- # Get paper bounds with expansion
978
- rect = cv2.boundingRect(paper_contour)
979
- expansion = max(20, int(min(rect[2], rect[3]) * 0.05)) # 5% expansion
980
-
981
- x, y, w, h = rect
982
- x_min = max(0, x - expansion)
983
- y_min = max(0, y - expansion)
984
- x_max = min(image.shape[1], x + w + expansion)
985
- y_max = min(image.shape[0], y + h + expansion)
986
-
987
- # Process the expanded paper area
988
- cropped_image = image[y_min:y_max, x_min:x_max]
989
- crop_offset = (x_min, y_min)
990
-
991
- # Remove background
992
- objects_mask = remove_bg(cropped_image)
993
-
994
- # Resize mask back to cropped image size
995
- target_height = y_max - y_min
996
- target_width = x_max - x_min
997
- objects_mask_resized = cv2.resize(objects_mask, (target_width, target_height))
998
-
999
- # Place back in full image space
1000
- full_mask = np.zeros((image.shape[0], image.shape[1]), dtype=np.uint8)
1001
- full_mask[y_min:y_max, x_min:x_max] = objects_mask_resized
1002
-
1003
- # Light filtering only - don't exclude paper area aggressively
1004
- # Just remove small noise
1005
- kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
1006
- objects_mask = cv2.morphologyEx(full_mask, cv2.MORPH_OPEN, kernel)
1007
-
1008
- # Resize mask to match cropped region and place back in original image space
1009
- full_mask = np.zeros((image.shape[0], image.shape[1]), dtype=np.uint8)
1010
- x_min, y_min = crop_offset # Use crop_offset to ensure defined variables
1011
- if cropped_image.shape[:2] != processed_size:
1012
- resized_mask = cv2.resize(objects_mask, (cropped_image.shape[1], cropped_image.shape[0]))
1013
- else:
1014
- resized_mask = objects_mask
1015
- if x_min == 0 and y_min == 0 and cropped_image.shape[:2] == image.shape[:2]:
1016
- full_mask = resized_mask # No cropping occurred, use mask directly
1017
- else:
1018
- full_mask[y_min:y_min+resized_mask.shape[0], x_min:x_min+resized_mask.shape[1]] = resized_mask
1019
-
1020
- # Remove paper area from mask to focus only on objects
1021
- objects_mask = exclude_paper_area(full_mask, paper_contour)
1022
-
1023
- # Debug: Save intermediate masks
1024
- cv2.imwrite("./debug/objects_mask_after_yolo.jpg", objects_mask)
1025
-
1026
- # Check if we actually have object pixels after paper exclusion
1027
- object_pixels = np.count_nonzero(objects_mask)
1028
- if object_pixels < 300: # Minimum threshold
1029
- raise NoObjectDetectedError("No significant object detected after excluding paper area")
1030
-
1031
- # Validate single object
1032
- validate_single_object(objects_mask, paper_contour)
1033
 
1034
  except (MultipleObjectsError, NoObjectDetectedError) as e:
1035
  return (
@@ -1065,7 +1050,7 @@ def predict_with_paper(image, paper_size, offset, offset_unit, finger_clearance=
1065
  dxf, finger_polygons, original_polygons = save_dxf_spline(
1066
  contours,
1067
  scaling_factor, # This should be mm/px
1068
- orig_size[0], # Use original image height
1069
  finger_clearance=(finger_clearance == "On")
1070
  )
1071
  except FingerCutOverlapError as e:
@@ -1079,7 +1064,7 @@ def predict_with_paper(image, paper_size, offset, offset_unit, finger_clearance=
1079
  for poly in finger_polygons:
1080
  try:
1081
  coords = np.array([
1082
- (int(x / scaling_factor), int(orig_size[0] - y / scaling_factor))
1083
  for x, y in poly.exterior.coords
1084
  ], np.int32).reshape((-1, 1, 2))
1085
 
 
974
  raise gr.Error(f"Error processing image: {str(e)}")
975
 
976
  try:
977
+ # Get paper bounds with expansion
978
+ rect = cv2.boundingRect(paper_contour)
979
+ expansion = max(20, int(min(rect[2], rect[3]) * 0.05)) # 5% expansion
980
+
981
+ x, y, w, h = rect
982
+ x_min = max(0, x - expansion)
983
+ y_min = max(0, y - expansion)
984
+ x_max = min(image.shape[1], x + w + expansion)
985
+ y_max = min(image.shape[0], y + h + expansion)
986
+
987
+ # Process the expanded paper area
988
+ cropped_image = image[y_min:y_max, x_min:x_max]
989
+ crop_offset = (x_min, y_min)
990
+
991
+ # Remove background
992
+ objects_mask = remove_bg(cropped_image)
993
+
994
+ # Resize mask back to cropped image size
995
+ target_height = y_max - y_min
996
+ target_width = x_max - x_min
997
+ objects_mask_resized = cv2.resize(objects_mask, (target_width, target_height))
998
+
999
+ # Place back in full image space
1000
+ full_mask = np.zeros((image.shape[0], image.shape[1]), dtype=np.uint8)
1001
+ full_mask[y_min:y_max, x_min:x_max] = objects_mask_resized
1002
+
1003
+ # Light filtering only - don't exclude paper area aggressively
1004
+ # Just remove small noise
1005
+ kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
1006
+ objects_mask = cv2.morphologyEx(full_mask, cv2.MORPH_OPEN, kernel)
1007
+
1008
+ # Debug: Save intermediate masks
1009
+ cv2.imwrite("./debug/objects_mask_after_processing.jpg", objects_mask)
1010
+
1011
+ # Check if we actually have object pixels
1012
+ object_pixels = np.count_nonzero(objects_mask)
1013
+ if object_pixels < 300: # Minimum threshold
1014
+ raise NoObjectDetectedError("No significant object detected")
1015
+
1016
+ # Validate single object
1017
+ validate_single_object(objects_mask, paper_contour)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1018
 
1019
  except (MultipleObjectsError, NoObjectDetectedError) as e:
1020
  return (
 
1050
  dxf, finger_polygons, original_polygons = save_dxf_spline(
1051
  contours,
1052
  scaling_factor, # This should be mm/px
1053
+ image.shape[0], # Use original image height
1054
  finger_clearance=(finger_clearance == "On")
1055
  )
1056
  except FingerCutOverlapError as e:
 
1064
  for poly in finger_polygons:
1065
  try:
1066
  coords = np.array([
1067
+ (int(x / scaling_factor), int(image.shape[0] - y / scaling_factor))
1068
  for x, y in poly.exterior.coords
1069
  ], np.int32).reshape((-1, 1, 2))
1070