Spaces:
Sleeping
Sleeping
Update app.py
Browse files437 unmodified lines43843944044144244344444544644744844945045145245345431 unmodified lines4864874884894904914924934944954964974984995005015025035045055062 unmodified lines509510511512513514515516517518519520521522523131 unmodified lines655656657658659660661437 unmodified linescontinueratio = contrast_ratio(text_color, bg_color)required_ratio = required_contrast_ratio(size_pct)if ratio < required_ratio:contrast_issues += 1if len(issue_lines) < 5:severity = contrast_severity_label(ratio, required_ratio)issue_lines.append(f"- \"{word}\" {severity} against its background")issue_boxes.append({"box": (x, y, w, h), "label": "Low contrast"})if size_pct < 1.2:small_text_issues += 1if len(issue_lines) < 5:issue_lines.append(f"- \"{word}\" is a bit small to read comfortably")issue_boxes.append({"box": (x, y, w, h), "label": "Small text"})return {"words_checked": words_checked, "contrast_issues": contrast_issues,"small_text_issues": small_text_issues, "issue_lines": issue_lines, "issue_boxes": issue_boxes,"text_sizes": text_sizes, "all_words": all_words_loose}31 unmodified linesreturn mergeddef draw_issue_markers(overlay_bgr, issue_boxes):img = overlay_bgr.copy()merged_issues = merge_issue_boxes(issue_boxes)placed_label_rects = []for issue in merged_issues:x, y, w, h = issue["box"]pad = 4cv2.rectangle(img, (x - pad, y - pad), (x + w + pad, y + h + pad), (255, 255, 255), 3)cv2.rectangle(img, (x - pad, y - pad), (x + w + pad, y + h + pad), (0, 0, 0), 1)label = issue["label"](tw, th), _ = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)label_x0 = x - padlabel_x1 = label_x0 + tw + 8label_y1 = max(th + 6, y - pad)label_y0 = label_y1 - th - 6collision = Trueattempts = 02 unmodified linesfor (px0, py0, px1, py1) in placed_label_rects:if not (label_x1 < px0 or label_x0 > px1 or label_y1 < py0 or label_y0 > py1):collision = Truelabel_y1 -= (th + 10)label_y0 = label_y1 - th - 6breakattempts += 1placed_label_rects.append((label_x0, label_y0, label_x1, label_y1))cv2.rectangle(img, (label_x0, label_y0), (label_x1, label_y1), (255, 255, 255), -1)cv2.rectangle(img, (label_x0, label_y0), (label_x1, label_y1), (0, 0, 0), 1)cv2.putText(img, label, (label_x0 + 4, label_y1 - 4), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1, cv2.LINE_AA)return img131 unmodified linesoverlay_bgr = draw_scan_path(overlay_bgr, zones, h, w)overlay_bgr = draw_zone_labels(overlay_bgr, zones, h, w)overlay_bgr = draw_issue_markers(overlay_bgr, read.get("issue_boxes", []))overlay_rgb = cv2.cvtColor(overlay_bgr, cv2.COLOR_BGR2RGB)summary = f"## Grade: {grade} — {total_score}/100\n"437 unmodified lines43843944044144244344444544644744844945045145245345445545645745845946046131 unmodified lines4934944954964974984995005015025035045055065075085095105115125135145155165175182 unmodified lines521522523524525526527528529530531532533534535536131 unmodified lines668669670671672673674437 unmodified linescontinueratio = contrast_ratio(text_color, bg_color)required_ratio = required_contrast_ratio(size_pct)is_contrast_issue = ratio < required_ratiois_small_issue = size_pct < 1.2if is_contrast_issue:contrast_issues += 1if len(issue_lines) < 5:severity = contrast_severity_label(ratio, required_ratio)issue_lines.append(f"- \"{word}\" {severity} against its background")if is_small_issue:small_text_issues += 1if len(issue_lines) < 5:issue_lines.append(f"- \"{word}\" is a bit small to read comfortably")if is_contrast_issue or is_small_issue:combined_label = " + ".join(l for l, flag in [("Low contrast", is_contrast_issue), ("Small text", is_small_issue)] if flag)issue_boxes.append({"box": (x, y, w, h), "label": combined_label})return {"words_checked": words_checked, "contrast_issues": contrast_issues,"small_text_issues": small_text_issues, "issue_lines": issue_lines, "issue_boxes": issue_boxes,"text_sizes": text_sizes, "all_words": all_words_loose}31 unmodified linesreturn mergeddef draw_issue_markers(overlay_bgr, issue_boxes, img_h, img_w):img = overlay_bgr.copy()scale = max(1.0, min(img_h, img_w) / 800.0)merged_issues = merge_issue_boxes(issue_boxes)placed_label_rects = []pad = int(4 * scale)box_border = max(2, int(3 * scale))thin_border = max(1, int(scale))font_scale = 0.5 * scaletext_thickness = max(1, int(round(scale)))for issue in merged_issues:x, y, w, h = issue["box"]cv2.rectangle(img, (x - pad, y - pad), (x + w + pad, y + h + pad), (255, 255, 255), box_border)cv2.rectangle(img, (x - pad, y - pad), (x + w + pad, y + h + pad), (0, 0, 0), thin_border)label = issue["label"](tw, th), _ = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, font_scale, text_thickness)label_x0 = x - padlabel_x1 = label_x0 + tw + int(8 * scale)label_y1 = max(th + int(6 * scale), y - pad)label_y0 = label_y1 - th - int(6 * scale)collision = Trueattempts = 02 unmodified linesfor (px0, py0, px1, py1) in placed_label_rects:if not (label_x1 < px0 or label_x0 > px1 or label_y1 < py0 or label_y0 > py1):collision = Truelabel_y1 -= (th + int(10 * scale))label_y0 = label_y1 - th - int(6 * scale)breakattempts += 1placed_label_rects.append((label_x0, label_y0, label_x1, label_y1))cv2.rectangle(img, (label_x0, label_y0), (label_x1, label_y1), (255, 255, 255), -1)cv2.rectangle(img, (label_x0, label_y0), (label_x1, label_y1), (0, 0, 0), thin_border)cv2.putText(img, label, (label_x0 + int(4 * scale), label_y1 - int(4 * scale)),cv2.FONT_HERSHEY_SIMPLEX, font_scale, (0, 0, 0), text_thickness, cv2.LINE_AA)return img131 unmodified linesoverlay_bgr = draw_scan_path(overlay_bgr, zones, h, w)overlay_bgr = draw_zone_labels(overlay_bgr, zones, h, w)overlay_bgr = draw_issue_markers(overlay_bgr, read.get("issue_boxes", []), h, w)overlay_rgb = cv2.cvtColor(overlay_bgr, cv2.COLOR_BGR2RGB)summary = f"## Grade: {grade} — {total_score}/100\n"
app.py+30-17