tjw commited on
Commit
2045e5a
1 Parent(s): 01179bf
Files changed (1) hide show
  1. aimodel.py +15 -1
aimodel.py CHANGED
@@ -79,6 +79,7 @@ def get_meter_bbox(fl:Florence, img:Image):
79
  return bbox
80
 
81
  def get_circles(fl:Florence, img2:Image, polygons:list):
 
82
  img3 = Image.new('L', img2.size, color = 'black')
83
  draw = ImageDraw.Draw(img3)
84
  for polygon in polygons:
@@ -102,12 +103,14 @@ def get_needle_polygons(fl:Florence, img2:Image):
102
  return needle_polygons
103
 
104
  def get_ocr(fl:Florence, img2:Image):
 
105
  parsed_answer = fl.run(img2, TASK_OCR)
106
  assert len(parsed_answer)==1
107
  k,v = parsed_answer.popitem()
108
  return v
109
 
110
  def get_ai_model_result(img:Image.Image|Path|str, fl:Florence, fl_ft:Florence):
 
111
  if isinstance(img, Path):
112
  key = img.parts[-1]
113
  elif isinstance(img, str):
@@ -119,13 +122,15 @@ def get_ai_model_result(img:Image.Image|Path|str, fl:Florence, fl_ft:Florence):
119
  if isinstance(img, (Path, str)):
120
  img = Image.open(img)
121
  meter_bbox = get_meter_bbox(fl, img)
 
122
  img2 = img.crop(meter_bbox)
123
  needle_polygons = get_needle_polygons(fl, img2)
 
124
  result = AIModelResult(img, img2, meter_bbox, needle_polygons,
125
  get_circles(fl, img2, needle_polygons),
126
  get_ocr(fl, img2),
127
  get_ocr(fl_ft, img2)
128
- )
129
  if key is not None:
130
  cached_results[key] = result
131
  return result
@@ -134,6 +139,7 @@ from skimage.measure import regionprops
134
  from skimage.measure import EllipseModel
135
  from skimage.draw import ellipse_perimeter
136
  def get_regionprops(polygons:list) -> regionprops:
 
137
  coords = np.concatenate(polygons).reshape(-1, 2)
138
  size = tuple( (coords.max(axis=0)+2).astype('int') )
139
  img = Image.new('L', size, color = 'black')
@@ -193,11 +199,15 @@ MeterResult = namedtuple('MeterResult', [
193
 
194
  def read_meter(img:Image.Image|str|Path, fl, fl_ft):
195
  # ai model results
 
196
  result = get_ai_model_result(img, fl, fl_ft)
 
197
 
198
  # needle direction
199
  coords = np.concatenate(result.needle_polygons).reshape(-1, 2)
200
  orign, direction = estimate_line(coords)
 
 
201
 
202
  # calculate the meter center
203
  circle_props = get_regionprops(result.circle_polygons)
@@ -248,6 +258,7 @@ def read_meter(img:Image.Image|str|Path, fl, fl_ft):
248
  # use the model to calculated the needle psi and kg/cm2
249
  needle_psi = lm.predict(needle_theta)[1]
250
  needle_kg_cm2 = needle_psi / 14.223
 
251
 
252
  return MeterResult(result=result,
253
  needle_psi=needle_psi,
@@ -270,6 +281,7 @@ def read_meter(img:Image.Image|str|Path, fl, fl_ft):
270
 
271
 
272
  def more_visualization_data(meter_result:MeterResult):
 
273
  result = meter_result.result
274
  center = meter_result.center
275
  # following calculations are for visualization and debugging
@@ -290,6 +302,7 @@ def more_visualization_data(meter_result:MeterResult):
290
  return inlier_theta, inlier_psi, predict_theta, predict_psi, needle_head, direction
291
 
292
  def visualization(meter_result:MeterResult):
 
293
  result = meter_result.result
294
  center = meter_result.center
295
  needle_psi, needle_kg_cm2 = meter_result.needle_psi, meter_result.needle_kg_cm2
@@ -358,6 +371,7 @@ def visualization(meter_result:MeterResult):
358
  f'psi={needle_psi:.1f} kg_cm2={needle_kg_cm2:.2f}', color='yellow')
359
  plt.xlim(0, 360)
360
  plt.ylim(0, 160)
 
361
  return draw._image, plt.gcf()
362
 
363
  def clear_cache():
 
79
  return bbox
80
 
81
  def get_circles(fl:Florence, img2:Image, polygons:list):
82
+ logger.info('get_circle')
83
  img3 = Image.new('L', img2.size, color = 'black')
84
  draw = ImageDraw.Draw(img3)
85
  for polygon in polygons:
 
103
  return needle_polygons
104
 
105
  def get_ocr(fl:Florence, img2:Image):
106
+ logger.info('get_ocr')
107
  parsed_answer = fl.run(img2, TASK_OCR)
108
  assert len(parsed_answer)==1
109
  k,v = parsed_answer.popitem()
110
  return v
111
 
112
  def get_ai_model_result(img:Image.Image|Path|str, fl:Florence, fl_ft:Florence):
113
+ logger.info("get_ai_model_result")
114
  if isinstance(img, Path):
115
  key = img.parts[-1]
116
  elif isinstance(img, str):
 
122
  if isinstance(img, (Path, str)):
123
  img = Image.open(img)
124
  meter_bbox = get_meter_bbox(fl, img)
125
+ logger.info("get meter_bbox")
126
  img2 = img.crop(meter_bbox)
127
  needle_polygons = get_needle_polygons(fl, img2)
128
+ logger.info("get needle_polygons")
129
  result = AIModelResult(img, img2, meter_bbox, needle_polygons,
130
  get_circles(fl, img2, needle_polygons),
131
  get_ocr(fl, img2),
132
  get_ocr(fl_ft, img2)
133
+ )
134
  if key is not None:
135
  cached_results[key] = result
136
  return result
 
139
  from skimage.measure import EllipseModel
140
  from skimage.draw import ellipse_perimeter
141
  def get_regionprops(polygons:list) -> regionprops:
142
+ logger.info('get_regionprops')
143
  coords = np.concatenate(polygons).reshape(-1, 2)
144
  size = tuple( (coords.max(axis=0)+2).astype('int') )
145
  img = Image.new('L', size, color = 'black')
 
199
 
200
  def read_meter(img:Image.Image|str|Path, fl, fl_ft):
201
  # ai model results
202
+ logger.info("read_meter")
203
  result = get_ai_model_result(img, fl, fl_ft)
204
+ logger.info('ai model done')
205
 
206
  # needle direction
207
  coords = np.concatenate(result.needle_polygons).reshape(-1, 2)
208
  orign, direction = estimate_line(coords)
209
+ logger.info('needle direction done')
210
+
211
 
212
  # calculate the meter center
213
  circle_props = get_regionprops(result.circle_polygons)
 
258
  # use the model to calculated the needle psi and kg/cm2
259
  needle_psi = lm.predict(needle_theta)[1]
260
  needle_kg_cm2 = needle_psi / 14.223
261
+ logger.info('meter result done')
262
 
263
  return MeterResult(result=result,
264
  needle_psi=needle_psi,
 
281
 
282
 
283
  def more_visualization_data(meter_result:MeterResult):
284
+ logger.info('more visualization')
285
  result = meter_result.result
286
  center = meter_result.center
287
  # following calculations are for visualization and debugging
 
302
  return inlier_theta, inlier_psi, predict_theta, predict_psi, needle_head, direction
303
 
304
  def visualization(meter_result:MeterResult):
305
+ logger.info('visualization')
306
  result = meter_result.result
307
  center = meter_result.center
308
  needle_psi, needle_kg_cm2 = meter_result.needle_psi, meter_result.needle_kg_cm2
 
371
  f'psi={needle_psi:.1f} kg_cm2={needle_kg_cm2:.2f}', color='yellow')
372
  plt.xlim(0, 360)
373
  plt.ylim(0, 160)
374
+ logger.info('visualization done')
375
  return draw._image, plt.gcf()
376
 
377
  def clear_cache():