Nighty3912 commited on
Commit
08c3b1b
·
verified ·
1 Parent(s): 7137ab0

Update scalingtestupdated.py

Browse files
Files changed (1) hide show
  1. scalingtestupdated.py +22 -24
scalingtestupdated.py CHANGED
@@ -30,7 +30,7 @@ class ScalingSquareDetector:
30
  raise ValueError("Invalid feature detector. Choose 'ORB' or 'SIFT'.")
31
 
32
  def find_scaling_square(
33
- self, reference_image_path, target_image, known_size_mm, roi_margin=30
34
  ):
35
  """
36
  Detect the scaling square in the target image based on the reference image.
@@ -50,22 +50,23 @@ class ScalingSquareDetector:
50
 
51
  # # Select the largest square-like contour
52
  largest_square = None
53
- largest_square_area = 0
54
- for contour in contours:
55
- x_c, y_c, w_c, h_c = cv2.boundingRect(contour)
56
- aspect_ratio = w_c / float(h_c)
57
- if 0.9 <= aspect_ratio <= 1.1:
58
- peri = cv2.arcLength(contour, True)
59
- approx = cv2.approxPolyDP(contour, 0.02 * peri, True)
60
- if len(approx) == 4:
61
- area = cv2.contourArea(contour)
62
- if area > largest_square_area:
63
- largest_square = contour
64
- largest_square_area = area
65
 
66
  # if largest_square is None:
67
  # raise ValueError("No square-like contour found in the ROI.")
68
-
 
69
  # Draw the largest contour on the original image
70
  target_image_color = cv2.cvtColor(target_image, cv2.COLOR_GRAY2BGR)
71
  cv2.drawContours(
@@ -82,7 +83,7 @@ class ScalingSquareDetector:
82
 
83
  # Calculate the scaling factor
84
  avg_square_size_px = (square_width_px + square_height_px) / 2
85
- scaling_factor = 0.5 / avg_square_size_px # mm per pixel
86
 
87
  return scaling_factor #, square_height_px, square_width_px, roi_binary
88
 
@@ -101,9 +102,8 @@ class ScalingSquareDetector:
101
 
102
 
103
  def calculate_scaling_factor(
104
- reference_image_path,
105
  target_image,
106
- known_square_size_mm=12.7,
107
  feature_detector="ORB",
108
  debug=False,
109
  roi_margin=30,
@@ -113,9 +113,8 @@ def calculate_scaling_factor(
113
 
114
  # Find scaling square and calculate scaling factor
115
  scaling_factor = detector.find_scaling_square(
116
- reference_image_path=reference_image_path,
117
  target_image=target_image,
118
- known_size_mm=known_square_size_mm,
119
  roi_margin=roi_margin,
120
  )
121
 
@@ -137,7 +136,7 @@ if __name__ == "__main__":
137
  for idx, file in enumerate(os.listdir("./sample_images")):
138
  img = np.array(Image.open(os.path.join("./sample_images", file)))
139
  img = yolo_detect(img, ['box'])
140
- model = YOLO("./last.pt")
141
  res = model.predict(img, conf=0.6)
142
 
143
  box_img = save_one_box(res[0].cpu().boxes.xyxy, im=res[0].orig_img, save=False)
@@ -148,9 +147,8 @@ if __name__ == "__main__":
148
  try:
149
 
150
  scaling_factor = calculate_scaling_factor(
151
- reference_image_path="./Reference_ScalingBox.jpg",
152
  target_image=box_img,
153
- known_square_size_mm=12.7,
154
  feature_detector="ORB",
155
  debug=False,
156
  roi_margin=90,
@@ -158,7 +156,7 @@ if __name__ == "__main__":
158
  # cv2.imwrite(f"./outputs/{idx}_binary_{file}", roi_binary)
159
 
160
  # Square size in mm
161
- # square_size_mm = 12.7
162
 
163
  # # Compute the calculated scaling factors and compare
164
  # calculated_scaling_factor = square_size_mm / height_px
@@ -177,4 +175,4 @@ if __name__ == "__main__":
177
  except Exception as e:
178
  from traceback import print_exc
179
  print(print_exc())
180
- print(f"Error: {e}")
 
30
  raise ValueError("Invalid feature detector. Choose 'ORB' or 'SIFT'.")
31
 
32
  def find_scaling_square(
33
+ self, target_image, known_size_mm, roi_margin=30
34
  ):
35
  """
36
  Detect the scaling square in the target image based on the reference image.
 
50
 
51
  # # Select the largest square-like contour
52
  largest_square = None
53
+ # largest_square_area = 0
54
+ # for contour in contours:
55
+ # x_c, y_c, w_c, h_c = cv2.boundingRect(contour)
56
+ # aspect_ratio = w_c / float(h_c)
57
+ # if 0.9 <= aspect_ratio <= 1.1:
58
+ # peri = cv2.arcLength(contour, True)
59
+ # approx = cv2.approxPolyDP(contour, 0.02 * peri, True)
60
+ # if len(approx) == 4:
61
+ # area = cv2.contourArea(contour)
62
+ # if area > largest_square_area:
63
+ # largest_square = contour
64
+ # largest_square_area = area
65
 
66
  # if largest_square is None:
67
  # raise ValueError("No square-like contour found in the ROI.")
68
+ for contour in contours:
69
+ largest_square=contour
70
  # Draw the largest contour on the original image
71
  target_image_color = cv2.cvtColor(target_image, cv2.COLOR_GRAY2BGR)
72
  cv2.drawContours(
 
83
 
84
  # Calculate the scaling factor
85
  avg_square_size_px = (square_width_px + square_height_px) / 2
86
+ scaling_factor = known_size_mm / avg_square_size_px # mm per pixel
87
 
88
  return scaling_factor #, square_height_px, square_width_px, roi_binary
89
 
 
102
 
103
 
104
  def calculate_scaling_factor(
 
105
  target_image,
106
+ reference_obj_size_mm=0.955,
107
  feature_detector="ORB",
108
  debug=False,
109
  roi_margin=30,
 
113
 
114
  # Find scaling square and calculate scaling factor
115
  scaling_factor = detector.find_scaling_square(
 
116
  target_image=target_image,
117
+ known_size_mm=reference_obj_size_mm,
118
  roi_margin=roi_margin,
119
  )
120
 
 
136
  for idx, file in enumerate(os.listdir("./sample_images")):
137
  img = np.array(Image.open(os.path.join("./sample_images", file)))
138
  img = yolo_detect(img, ['box'])
139
+ model = YOLO("./coin_det.pt")
140
  res = model.predict(img, conf=0.6)
141
 
142
  box_img = save_one_box(res[0].cpu().boxes.xyxy, im=res[0].orig_img, save=False)
 
147
  try:
148
 
149
  scaling_factor = calculate_scaling_factor(
 
150
  target_image=box_img,
151
+ known_square_size_mm=0.955,
152
  feature_detector="ORB",
153
  debug=False,
154
  roi_margin=90,
 
156
  # cv2.imwrite(f"./outputs/{idx}_binary_{file}", roi_binary)
157
 
158
  # Square size in mm
159
+ # square_size_mm = 0.955
160
 
161
  # # Compute the calculated scaling factors and compare
162
  # calculated_scaling_factor = square_size_mm / height_px
 
175
  except Exception as e:
176
  from traceback import print_exc
177
  print(print_exc())
178
+ print(f"Error: {e}")