import unittest import cv2 import numpy as np from copienator.ink_detection import detect_bounds class EdgeMarksTests(unittest.TestCase): def page(self, last_answer=1000): rgb = np.full((2340,1654,3),255,np.uint8) cv2.putText(rgb,'answer',(500,600),cv2.FONT_HERSHEY_SIMPLEX,1.5,(20,60,180),2) cv2.putText(rgb,'more',(500,last_answer),cv2.FONT_HERSHEY_SIMPLEX,1.5,(20,60,180),2) return rgb def test_isolated_footer_fraction_is_removed(self): rgb=self.page() cv2.putText(rgb,'9/9',(1400,2220),cv2.FONT_HERSHEY_SIMPLEX,1.2,(20,20,20),2) debug={} result=detect_bounds(rgb,diagnostics=debug) self.assertLess(result['bottom_px'],1100) self.assertTrue(debug['masks']['isolated_edge_marks'].any()) self.assertFalse(debug['masks']['foreground'][2150:].any()) self.assertFalse(debug['masks']['strong_seeds'][2150:].any()) self.assertEqual(result,detect_bounds(rgb)) def test_close_footer_stays_with_answer(self): rgb=self.page(last_answer=2170) cv2.putText(rgb,'9/9',(1400,2220),cv2.FONT_HERSHEY_SIMPLEX,1.2,(20,20,20),2) self.assertGreater(detect_bounds(rgb)['bottom_px'],2220) def test_central_sparse_ratio_stays(self): rgb=self.page() cv2.putText(rgb,'9/9',(760,2220),cv2.FONT_HERSHEY_SIMPLEX,1.2,(20,20,20),2) self.assertGreater(detect_bounds(rgb)['bottom_px'],2220) def test_isolated_bare_border_digit_needs_no_recognition(self): rgb=self.page() cv2.putText(rgb,'7',(1400,2240),cv2.FONT_HERSHEY_SIMPLEX,1.2,(20,20,20),2) self.assertLess(detect_bounds(rgb)['bottom_px'],1100) def test_underlined_question_at_upper_edge_stays(self): rgb=self.page() cv2.putText(rgb,'16',(310,85),cv2.FONT_HERSHEY_SIMPLEX,1.2,(20,20,20),2) cv2.line(rgb,(310,90),(355,90),(20,20,20),2) self.assertLess(detect_bounds(rgb)['top_px'],60) def test_name_in_other_margin_blocks_header_removal(self): rgb=self.page() cv2.circle(rgb,(90,55),22,(20,20,20),2) cv2.putText(rgb,'1',(81,64),cv2.FONT_HERSHEY_SIMPLEX,.7,(20,20,20),1) cv2.putText(rgb,'Name',(1350,120),cv2.FONT_HERSHEY_SIMPLEX,1,(20,20,20),2) result=detect_bounds(rgb) self.assertEqual(result['top_px'],0) if __name__=='__main__': unittest.main()