def test_add(self): text1 = "11" scores1 = [0.9, 0.8] ss1 = ScoreString(text1, scores1) text2 = "22" scores2 = [0.7, 0.6] ss2 = ScoreString(text2, scores2) ss = ss1 + ss2 self.assertEqual(ss.scores, scores1 + scores2) self.assertEqual(len(ss.scores), len(ss.data)) self.assertEqual(ss.data, text1 + text2)
def test_long_scores(self): text = "2" scores = [0.9, 0.8] ss = ScoreString(text, scores) self.assertEqual(len(ss.scores), len(text)) self.assertEqual(ss.scores[0], 0.9)
def test_short_scores(self): text = "2019" scores = [0.9, 0.8] ss = ScoreString(text, scores) self.assertEqual(len(ss.scores), len(text)) self.assertAlmostEqual(ss.scores[2], 0.85) self.assertAlmostEqual(ss.scores[3], 0.85)
def test_replace_single_char_with_single_char(self): text = "1.12.1" scores = [0.9, 0.8, 0.7, 0.6, 0.5, 0.4] ss = ScoreString(text, scores) replaced_ss = ss.replace(".", "a") self.assertEqual(len(replaced_ss.scores), len(replaced_ss.data)) self.assertEqual(replaced_ss.scores, [0.9, 0.8, 0.7, 0.6, 0.5, 0.4]) self.assertEqual(replaced_ss.data, "1a12a1")
def test_replace_multi_char_with_multi_char_thresh_filter(self): text = "1.12..1" scores = [0.9, 0.8, 0.7, 0.6, 0.5, 0.1, 0.4] ss = ScoreString(text, scores) replaced_ss = ss.replace("..", "aaa", thresh=0.6) self.assertEqual(len(replaced_ss.scores), len(replaced_ss.data)) self.assertEqual(replaced_ss.data, "1.12aaa1") self.assertEqual(replaced_ss.scores, [0.9, 0.8, 0.7, 0.6, 0.5, 0.1, 0.3, 0.4])
def __init__(self, raw_node, ltrb=True): """ :param raw_data: ltrb==False [text, x1, y1, x2, y2, x3, y3, x4, y4, angle, label, probability] ltrb==True [text, left, top, right, bottom, label, probability] """ if ltrb: # 使用 cv2.boundingRect 获得 BBox 会导致 right, bottom + 1,所以这里要减去 1 raw_node = [ raw_node[0], raw_node[1], raw_node[2], raw_node[3], raw_node[2], raw_node[3], raw_node[4], raw_node[1], raw_node[4], 0, *raw_node[5:], ] self.raw_node = raw_node self.uid = uuid.uuid1().hex self.text: str = raw_node[0] self.ltrb = ltrb x, y, w, h = cv2.boundingRect( np.array(raw_node[1:9]).reshape(4, 2).astype(np.int)) if ltrb: self.bbox = BBox([x, y, x + w - 1, y + h - 1]) else: self.bbox = BBox([x, y, x + w, y + h]) self.rbox = RBox(raw_node[1:10]) self.text_label = raw_node[10] self.scores = raw_node[11:] self._ss = ScoreString(self.text, self.scores) self.is_cut = False
def test_length(self): text = "1.1.1" scores = [0.9, 0.8, 0.7, 0.6, 0.5] ss = ScoreString(text, scores) self.assertEqual(len(ss), 5)
def test_empty_scores(self): text = "2019-3-1.1" scores = [] ss = ScoreString(text, scores) self.assertEqual(ss.scores, [1] * len(text))
def test_all_empty(self): text = "" scores = [] ss = ScoreString(text, scores) self.assertEqual(len(ss.scores), len(text))
def test_empty_text(self): text = "" scores = [0.9, 0.8] ss = ScoreString(text, scores) self.assertEqual(len(ss.scores), len(text))
def test_slice(self): text = "2019-3-1.1" scores = [] ss = ScoreString(text, scores) self.assertEqual(text[:4], "2019")