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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -17
app.py CHANGED
@@ -438,17 +438,24 @@ def analyze_readability(pil_img):
438
  continue
439
  ratio = contrast_ratio(text_color, bg_color)
440
  required_ratio = required_contrast_ratio(size_pct)
441
- if ratio < required_ratio:
 
 
 
442
  contrast_issues += 1
443
  if len(issue_lines) < 5:
444
  severity = contrast_severity_label(ratio, required_ratio)
445
  issue_lines.append(f"- \"{word}\" {severity} against its background")
446
- issue_boxes.append({"box": (x, y, w, h), "label": "Low contrast"})
447
- if size_pct < 1.2:
448
  small_text_issues += 1
449
  if len(issue_lines) < 5:
450
  issue_lines.append(f"- \"{word}\" is a bit small to read comfortably")
451
- issue_boxes.append({"box": (x, y, w, h), "label": "Small text"})
 
 
 
 
 
452
  return {"words_checked": words_checked, "contrast_issues": contrast_issues,
453
  "small_text_issues": small_text_issues, "issue_lines": issue_lines, "issue_boxes": issue_boxes,
454
  "text_sizes": text_sizes, "all_words": all_words_loose}
@@ -486,21 +493,26 @@ def merge_issue_boxes(issue_boxes, gap=15):
486
  return merged
487
 
488
 
489
- def draw_issue_markers(overlay_bgr, issue_boxes):
490
  img = overlay_bgr.copy()
 
491
  merged_issues = merge_issue_boxes(issue_boxes)
492
  placed_label_rects = []
 
 
 
 
 
493
  for issue in merged_issues:
494
  x, y, w, h = issue["box"]
495
- pad = 4
496
- cv2.rectangle(img, (x - pad, y - pad), (x + w + pad, y + h + pad), (255, 255, 255), 3)
497
- cv2.rectangle(img, (x - pad, y - pad), (x + w + pad, y + h + pad), (0, 0, 0), 1)
498
  label = issue["label"]
499
- (tw, th), _ = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)
500
  label_x0 = x - pad
501
- label_x1 = label_x0 + tw + 8
502
- label_y1 = max(th + 6, y - pad)
503
- label_y0 = label_y1 - th - 6
504
 
505
  collision = True
506
  attempts = 0
@@ -509,15 +521,16 @@ def draw_issue_markers(overlay_bgr, issue_boxes):
509
  for (px0, py0, px1, py1) in placed_label_rects:
510
  if not (label_x1 < px0 or label_x0 > px1 or label_y1 < py0 or label_y0 > py1):
511
  collision = True
512
- label_y1 -= (th + 10)
513
- label_y0 = label_y1 - th - 6
514
  break
515
  attempts += 1
516
 
517
  placed_label_rects.append((label_x0, label_y0, label_x1, label_y1))
518
  cv2.rectangle(img, (label_x0, label_y0), (label_x1, label_y1), (255, 255, 255), -1)
519
- cv2.rectangle(img, (label_x0, label_y0), (label_x1, label_y1), (0, 0, 0), 1)
520
- cv2.putText(img, label, (label_x0 + 4, label_y1 - 4), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1, cv2.LINE_AA)
 
521
  return img
522
 
523
 
@@ -655,7 +668,7 @@ def analyze(pil_image, overlay_strength):
655
 
656
  overlay_bgr = draw_scan_path(overlay_bgr, zones, h, w)
657
  overlay_bgr = draw_zone_labels(overlay_bgr, zones, h, w)
658
- overlay_bgr = draw_issue_markers(overlay_bgr, read.get("issue_boxes", []))
659
  overlay_rgb = cv2.cvtColor(overlay_bgr, cv2.COLOR_BGR2RGB)
660
 
661
  summary = f"## Grade: {grade} — {total_score}/100\n"
 
438
  continue
439
  ratio = contrast_ratio(text_color, bg_color)
440
  required_ratio = required_contrast_ratio(size_pct)
441
+ is_contrast_issue = ratio < required_ratio
442
+ is_small_issue = size_pct < 1.2
443
+
444
+ if is_contrast_issue:
445
  contrast_issues += 1
446
  if len(issue_lines) < 5:
447
  severity = contrast_severity_label(ratio, required_ratio)
448
  issue_lines.append(f"- \"{word}\" {severity} against its background")
449
+ if is_small_issue:
 
450
  small_text_issues += 1
451
  if len(issue_lines) < 5:
452
  issue_lines.append(f"- \"{word}\" is a bit small to read comfortably")
453
+
454
+ if is_contrast_issue or is_small_issue:
455
+ combined_label = " + ".join(
456
+ l for l, flag in [("Low contrast", is_contrast_issue), ("Small text", is_small_issue)] if flag
457
+ )
458
+ issue_boxes.append({"box": (x, y, w, h), "label": combined_label})
459
  return {"words_checked": words_checked, "contrast_issues": contrast_issues,
460
  "small_text_issues": small_text_issues, "issue_lines": issue_lines, "issue_boxes": issue_boxes,
461
  "text_sizes": text_sizes, "all_words": all_words_loose}
 
493
  return merged
494
 
495
 
496
+ def draw_issue_markers(overlay_bgr, issue_boxes, img_h, img_w):
497
  img = overlay_bgr.copy()
498
+ scale = max(1.0, min(img_h, img_w) / 800.0)
499
  merged_issues = merge_issue_boxes(issue_boxes)
500
  placed_label_rects = []
501
+ pad = int(4 * scale)
502
+ box_border = max(2, int(3 * scale))
503
+ thin_border = max(1, int(scale))
504
+ font_scale = 0.5 * scale
505
+ text_thickness = max(1, int(round(scale)))
506
  for issue in merged_issues:
507
  x, y, w, h = issue["box"]
508
+ cv2.rectangle(img, (x - pad, y - pad), (x + w + pad, y + h + pad), (255, 255, 255), box_border)
509
+ cv2.rectangle(img, (x - pad, y - pad), (x + w + pad, y + h + pad), (0, 0, 0), thin_border)
 
510
  label = issue["label"]
511
+ (tw, th), _ = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, font_scale, text_thickness)
512
  label_x0 = x - pad
513
+ label_x1 = label_x0 + tw + int(8 * scale)
514
+ label_y1 = max(th + int(6 * scale), y - pad)
515
+ label_y0 = label_y1 - th - int(6 * scale)
516
 
517
  collision = True
518
  attempts = 0
 
521
  for (px0, py0, px1, py1) in placed_label_rects:
522
  if not (label_x1 < px0 or label_x0 > px1 or label_y1 < py0 or label_y0 > py1):
523
  collision = True
524
+ label_y1 -= (th + int(10 * scale))
525
+ label_y0 = label_y1 - th - int(6 * scale)
526
  break
527
  attempts += 1
528
 
529
  placed_label_rects.append((label_x0, label_y0, label_x1, label_y1))
530
  cv2.rectangle(img, (label_x0, label_y0), (label_x1, label_y1), (255, 255, 255), -1)
531
+ cv2.rectangle(img, (label_x0, label_y0), (label_x1, label_y1), (0, 0, 0), thin_border)
532
+ cv2.putText(img, label, (label_x0 + int(4 * scale), label_y1 - int(4 * scale)),
533
+ cv2.FONT_HERSHEY_SIMPLEX, font_scale, (0, 0, 0), text_thickness, cv2.LINE_AA)
534
  return img
535
 
536
 
 
668
 
669
  overlay_bgr = draw_scan_path(overlay_bgr, zones, h, w)
670
  overlay_bgr = draw_zone_labels(overlay_bgr, zones, h, w)
671
+ overlay_bgr = draw_issue_markers(overlay_bgr, read.get("issue_boxes", []), h, w)
672
  overlay_rgb = cv2.cvtColor(overlay_bgr, cv2.COLOR_BGR2RGB)
673
 
674
  summary = f"## Grade: {grade} — {total_score}/100\n"