HaanIlango commited on
Commit
f66be25
·
verified ·
1 Parent(s): e82e34e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -5
app.py CHANGED
@@ -186,6 +186,47 @@ def explain_grade(focus_score, readability_score, read):
186
  return "a mix of small readability and focus factors - see details below"
187
 
188
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
189
  def coverage_description(spread_score):
190
  if spread_score < 15:
191
  return "concentrated on one clear focal point - that's usually a good sign, not a problem"
@@ -195,12 +236,18 @@ def coverage_description(spread_score):
195
  return "spread fairly widely across the design"
196
 
197
 
198
- def create_score_chart(focus_score, spread_score, readability_score):
199
  labels = ["Clear Focal\nPoint", "Attention\nCoverage"]
200
  values = [focus_score, spread_score]
201
  if readability_score is not None:
202
  labels.append("Readability")
203
  values.append(readability_score)
 
 
 
 
 
 
204
  colors = []
205
  for v in values:
206
  if v >= 75:
@@ -350,6 +397,7 @@ def analyze_readability(pil_img):
350
  small_text_issues = 0
351
  issue_lines = []
352
  issue_boxes = []
 
353
  for i in range(len(ocr_data['text'])):
354
  word = ocr_data['text'][i].strip()
355
  conf = int(ocr_data['conf'][i])
@@ -359,11 +407,12 @@ def analyze_readability(pil_img):
359
  if w < 3 or h < 3:
360
  continue
361
  words_checked += 1
 
 
362
  text_color, bg_color = sample_text_and_bg_color(img_arr, x, y, w, h)
363
  if text_color is None:
364
  continue
365
  ratio = contrast_ratio(text_color, bg_color)
366
- size_pct = (h / img_h) * 100
367
  required_ratio = required_contrast_ratio(size_pct)
368
  if ratio < required_ratio:
369
  contrast_issues += 1
@@ -377,7 +426,8 @@ def analyze_readability(pil_img):
377
  issue_lines.append(f"- \"{word}\" is a bit small to read comfortably")
378
  issue_boxes.append({"box": (x, y, w, h), "label": "Small text"})
379
  return {"words_checked": words_checked, "contrast_issues": contrast_issues,
380
- "small_text_issues": small_text_issues, "issue_lines": issue_lines, "issue_boxes": issue_boxes}
 
381
 
382
 
383
  def merge_issue_boxes(issue_boxes, gap=15):
@@ -573,7 +623,11 @@ def analyze(pil_image, overlay_strength):
573
  total_score, grade = compute_overall_grade(scores['focus_score'], scores['spread_score'], readability_score)
574
  grade_reason = explain_grade(scores['focus_score'], readability_score, read)
575
  tips = generate_recommendations(saliency_map, scores, read, h, w)
576
- chart_image = create_score_chart(scores['focus_score'], scores['spread_score'], readability_score)
 
 
 
 
577
 
578
  overlay_bgr = draw_scan_path(overlay_bgr, zones, h, w)
579
  overlay_bgr = draw_zone_labels(overlay_bgr, zones, h, w)
@@ -616,6 +670,13 @@ def analyze(pil_image, overlay_strength):
616
  for tip in tips:
617
  summary += f"- {tip}\n"
618
 
 
 
 
 
 
 
 
619
  summary += (
620
  "\n---\n"
621
  "**Methodology:** Attention prediction combines MSI-Net, a peer-reviewed saliency model "
@@ -660,7 +721,9 @@ with gr.Blocks(title="Design Analyzer") as demo:
660
  "- **No inflated claims.** A cited, transparent process instead "
661
  "of a marketing number.\n"
662
  "- **Scan-path arrows** show the predicted order attention flows, "
663
- "not just isolated hotspots.\n\n"
 
 
664
  "Coming soon: before/after comparisons."
665
  )
666
  with gr.Row():
 
186
  return "a mix of small readability and focus factors - see details below"
187
 
188
 
189
+ def compute_hierarchy(text_sizes):
190
+ if len(text_sizes) < 2:
191
+ return None
192
+ largest = max(text_sizes)
193
+ smallest = min(text_sizes)
194
+ ratio = largest / max(smallest, 0.1)
195
+ score = round(min(100, ratio * 20), 1)
196
+ return score, ratio
197
+
198
+
199
+ def hierarchy_description(ratio):
200
+ if ratio < 1.5:
201
+ return "text sizes are very similar - there's no single element that clearly leads the eye"
202
+ elif ratio < 2.5:
203
+ return "there's some size variation, but the hierarchy could be stronger"
204
+ else:
205
+ return "clear size hierarchy - one element leads, the rest support it"
206
+
207
+
208
+ def compute_balance(saliency_map):
209
+ h, w = saliency_map.shape
210
+ left = saliency_map[:, :w // 2].mean()
211
+ right = saliency_map[:, w // 2:].mean()
212
+ top = saliency_map[:h // 2, :].mean()
213
+ bottom = saliency_map[h // 2:, :].mean()
214
+ horiz_diff = abs(left - right) / max(left + right, 1e-6)
215
+ vert_diff = abs(top - bottom) / max(top + bottom, 1e-6)
216
+ imbalance = (horiz_diff + vert_diff) / 2
217
+ score = round(max(0, 100 - imbalance * 200), 1)
218
+ return score
219
+
220
+
221
+ def balance_description(score):
222
+ if score >= 75:
223
+ return "well balanced - visual weight is evenly distributed"
224
+ elif score >= 50:
225
+ return "somewhat uneven - one side carries noticeably more visual weight"
226
+ else:
227
+ return "heavily lopsided - attention is pulled hard toward one side or corner"
228
+
229
+
230
  def coverage_description(spread_score):
231
  if spread_score < 15:
232
  return "concentrated on one clear focal point - that's usually a good sign, not a problem"
 
236
  return "spread fairly widely across the design"
237
 
238
 
239
+ def create_score_chart(focus_score, spread_score, readability_score, hierarchy_score=None, balance_score=None):
240
  labels = ["Clear Focal\nPoint", "Attention\nCoverage"]
241
  values = [focus_score, spread_score]
242
  if readability_score is not None:
243
  labels.append("Readability")
244
  values.append(readability_score)
245
+ if hierarchy_score is not None:
246
+ labels.append("Visual\nHierarchy")
247
+ values.append(hierarchy_score)
248
+ if balance_score is not None:
249
+ labels.append("Balance")
250
+ values.append(balance_score)
251
  colors = []
252
  for v in values:
253
  if v >= 75:
 
397
  small_text_issues = 0
398
  issue_lines = []
399
  issue_boxes = []
400
+ text_sizes = []
401
  for i in range(len(ocr_data['text'])):
402
  word = ocr_data['text'][i].strip()
403
  conf = int(ocr_data['conf'][i])
 
407
  if w < 3 or h < 3:
408
  continue
409
  words_checked += 1
410
+ size_pct = (h / img_h) * 100
411
+ text_sizes.append(size_pct)
412
  text_color, bg_color = sample_text_and_bg_color(img_arr, x, y, w, h)
413
  if text_color is None:
414
  continue
415
  ratio = contrast_ratio(text_color, bg_color)
 
416
  required_ratio = required_contrast_ratio(size_pct)
417
  if ratio < required_ratio:
418
  contrast_issues += 1
 
426
  issue_lines.append(f"- \"{word}\" is a bit small to read comfortably")
427
  issue_boxes.append({"box": (x, y, w, h), "label": "Small text"})
428
  return {"words_checked": words_checked, "contrast_issues": contrast_issues,
429
+ "small_text_issues": small_text_issues, "issue_lines": issue_lines, "issue_boxes": issue_boxes,
430
+ "text_sizes": text_sizes}
431
 
432
 
433
  def merge_issue_boxes(issue_boxes, gap=15):
 
623
  total_score, grade = compute_overall_grade(scores['focus_score'], scores['spread_score'], readability_score)
624
  grade_reason = explain_grade(scores['focus_score'], readability_score, read)
625
  tips = generate_recommendations(saliency_map, scores, read, h, w)
626
+ hierarchy_result = compute_hierarchy(read.get('text_sizes', []))
627
+ balance_score = compute_balance(saliency_map)
628
+ hierarchy_score = hierarchy_result[0] if hierarchy_result else None
629
+ chart_image = create_score_chart(scores['focus_score'], scores['spread_score'], readability_score,
630
+ hierarchy_score, balance_score)
631
 
632
  overlay_bgr = draw_scan_path(overlay_bgr, zones, h, w)
633
  overlay_bgr = draw_zone_labels(overlay_bgr, zones, h, w)
 
670
  for tip in tips:
671
  summary += f"- {tip}\n"
672
 
673
+ summary += "\n**Layout & Composition:** *(based on classic layout principles - hierarchy and balance)*\n"
674
+ if hierarchy_result:
675
+ summary += f"- Visual Hierarchy: {hierarchy_description(hierarchy_result[1])}\n"
676
+ else:
677
+ summary += "- Visual Hierarchy: not enough distinct text elements to judge\n"
678
+ summary += f"- Balance: {balance_description(balance_score)}\n"
679
+
680
  summary += (
681
  "\n---\n"
682
  "**Methodology:** Attention prediction combines MSI-Net, a peer-reviewed saliency model "
 
721
  "- **No inflated claims.** A cited, transparent process instead "
722
  "of a marketing number.\n"
723
  "- **Scan-path arrows** show the predicted order attention flows, "
724
+ "not just isolated hotspots.\n"
725
+ "- **Grounded in classic layout theory** - checks visual hierarchy "
726
+ "and balance, principles used by professional designers for decades.\n\n"
727
  "Coming soon: before/after comparisons."
728
  )
729
  with gr.Row():