HaanIlango commited on
Commit
cb66ff1
·
verified ·
1 Parent(s): be41de9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -5
app.py CHANGED
@@ -171,9 +171,13 @@ def compute_overall_grade(focus_score, spread_score, readability_score):
171
  return total, grade
172
 
173
 
174
- def explain_grade(focus_score, readability_score, read):
 
 
175
  if readability_score is None:
176
- if focus_score < 50:
 
 
177
  return "based on attention alone (this design's text couldn't be reliably read, so readability wasn't scored) - mainly because attention is spread out without one clear focal point"
178
  elif focus_score > 90:
179
  return "based on attention alone (this design's text couldn't be reliably read, so readability wasn't scored) - attention is very tightly focused on one spot"
@@ -183,7 +187,9 @@ def explain_grade(focus_score, readability_score, read):
183
  weighted_focus_deficit = 0.55 * max(0, 100 - focus_score)
184
  weighted_read_deficit = 0.25 * max(0, 100 - readability_score)
185
 
186
- if weighted_read_deficit >= weighted_focus_deficit and total_issues > 0:
 
 
187
  return "mainly due to text readability - some text is harder to read than ideal"
188
  elif focus_score < 50:
189
  return "mainly because attention is spread out, without one clear focal point"
@@ -643,10 +649,19 @@ def draw_zone_labels(overlay_bgr, zones, img_h, img_w):
643
  return img
644
 
645
 
 
 
 
 
 
 
 
 
 
646
  def analyze(pil_image, overlay_strength):
647
  if pil_image is None:
648
  return None, None, "Upload an image first."
649
- pil_rgb = pil_image.convert("RGB")
650
  img_bgr = cv2.cvtColor(np.array(pil_rgb), cv2.COLOR_RGB2BGR)
651
  h, w = img_bgr.shape[:2]
652
 
@@ -658,7 +673,7 @@ def analyze(pil_image, overlay_strength):
658
  read = analyze_readability(pil_rgb)
659
  readability_score = compute_readability_score(read)
660
  total_score, grade = compute_overall_grade(scores['focus_score'], scores['spread_score'], readability_score)
661
- grade_reason = explain_grade(scores['focus_score'], readability_score, read)
662
  tips = generate_recommendations(saliency_map, scores, read, h, w)
663
  hierarchy_result = compute_hierarchy(read.get('text_sizes', []))
664
  balance_score = compute_balance(saliency_map)
 
171
  return total, grade
172
 
173
 
174
+ def explain_grade(focus_score, spread_score, readability_score, read):
175
+ coverage_score = coverage_grade_score(spread_score)
176
+ weighted_coverage_deficit = 0.20 * max(0, 100 - coverage_score)
177
  if readability_score is None:
178
+ if weighted_coverage_deficit > 0.55 * max(0, 100 - focus_score):
179
+ return "based on attention alone (this design's text couldn't be reliably read, so readability wasn't scored) - mainly because attention is spread too widely without settling anywhere"
180
+ elif focus_score < 50:
181
  return "based on attention alone (this design's text couldn't be reliably read, so readability wasn't scored) - mainly because attention is spread out without one clear focal point"
182
  elif focus_score > 90:
183
  return "based on attention alone (this design's text couldn't be reliably read, so readability wasn't scored) - attention is very tightly focused on one spot"
 
187
  weighted_focus_deficit = 0.55 * max(0, 100 - focus_score)
188
  weighted_read_deficit = 0.25 * max(0, 100 - readability_score)
189
 
190
+ if weighted_coverage_deficit > weighted_focus_deficit and weighted_coverage_deficit > weighted_read_deficit:
191
+ return "mainly because attention is spread too widely across the design without settling anywhere"
192
+ elif weighted_read_deficit >= weighted_focus_deficit and total_issues > 0:
193
  return "mainly due to text readability - some text is harder to read than ideal"
194
  elif focus_score < 50:
195
  return "mainly because attention is spread out, without one clear focal point"
 
649
  return img
650
 
651
 
652
+ def flatten_transparency(pil_image):
653
+ if pil_image.mode in ("RGBA", "LA") or (pil_image.mode == "P" and "transparency" in pil_image.info):
654
+ rgba = pil_image.convert("RGBA")
655
+ background = Image.new("RGB", rgba.size, (255, 255, 255))
656
+ background.paste(rgba, mask=rgba.split()[-1])
657
+ return background
658
+ return pil_image.convert("RGB")
659
+
660
+
661
  def analyze(pil_image, overlay_strength):
662
  if pil_image is None:
663
  return None, None, "Upload an image first."
664
+ pil_rgb = flatten_transparency(pil_image)
665
  img_bgr = cv2.cvtColor(np.array(pil_rgb), cv2.COLOR_RGB2BGR)
666
  h, w = img_bgr.shape[:2]
667
 
 
673
  read = analyze_readability(pil_rgb)
674
  readability_score = compute_readability_score(read)
675
  total_score, grade = compute_overall_grade(scores['focus_score'], scores['spread_score'], readability_score)
676
+ grade_reason = explain_grade(scores['focus_score'], scores['spread_score'], readability_score, read)
677
  tips = generate_recommendations(saliency_map, scores, read, h, w)
678
  hierarchy_result = compute_hierarchy(read.get('text_sizes', []))
679
  balance_score = compute_balance(saliency_map)