-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval.py
132 lines (118 loc) · 3.41 KB
/
eval.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import argparse
import json
# from google.colab.patches import cv2_imshow
import math
import os
import time
from collections import Counter
import cv2
import matplotlib.pyplot as plt
import numpy as np
# from mmocr.utils.ocr import MMOCR
from PIL import Image, ImageDraw, ImageFont
from tools.minimum_hull import minimum_bounding_rectangle
from tqdm import tqdm
from vietocr.tool.config import Cfg
from vietocr.tool.predictor import Predictor
mapper = [
["hoangsa"],
["truongsa"],
["hoangsa", "truongsa"],
["hoang", "sa"],
["truong", "sa"],
["hoang", "sa", "truong", "sa"],
["hoàng", "sa", "trường", "sa"],
["hoang", "sa", "trương", "sa"],
["hoàng", "sa", "trương", "sa"],
["hoàng", "sa"],
["trường", "sa"],
["trương", "sa"],
["quan", "dao", "truong"],
["quan", "dao", "hoang"],
["paracel"],
["spratly"],
]
def check(word_bag):
global mapper
for cond in mapper:
count_dict = Counter(cond)
for word in word_bag:
if word in count_dict:
count_dict[word] -= 1
f = 1
for element, count in count_dict.items():
if count > 0:
f = 0
if f == 1:
return 1
return 0
eval = False
if eval == True:
### POSITIVE GT
path = "../Dataset/Vietnam_map/prediction/positive/vietnamese/predicted"
files = os.listdir(path)
pos = 0
neg = 0
for file in files:
word_bag = []
with open(path + "/" + file, "r") as file:
lines = file.readlines()
for i in range(len(lines)):
lines[i] = lines[i].strip()
word_bag.append(lines[i].split(",")[-1].lower())
if check(word_bag) == 1:
neg += 1
else:
pos += 1
path = "../Dataset/Vietnam_map/prediction/positive/english/predicted"
files = os.listdir(path)
for file in files:
word_bag = []
with open(path + "/" + file, "r") as file:
lines = file.readlines()
for i in range(len(lines)):
lines[i] = lines[i].strip()
word_bag.append(lines[i].split(",")[-1].lower())
if check(word_bag) == 1:
neg += 1
else:
pos += 1
TP = pos
FN = neg
### NEGATIVE GT
path = "../Dataset/Vietnam_map/prediction/negative/vietnamese/predicted"
files = os.listdir(path)
pos = 0
neg = 0
for file in files:
word_bag = []
with open(path + "/" + file, "r") as file:
lines = file.readlines()
for i in range(len(lines)):
lines[i] = lines[i].strip()
word_bag.append(lines[i].split(",")[-1].lower())
if check(word_bag) == 1:
neg += 1
else:
pos += 1
path = "../Dataset/Vietnam_map/prediction/negative/english/predicted"
files = os.listdir(path)
for file in files:
word_bag = []
with open(path + "/" + file, "r") as file:
lines = file.readlines()
for i in range(len(lines)):
lines[i] = lines[i].strip()
word_bag.append(lines[i].split(",")[-1].lower())
if check(word_bag) == 1:
neg += 1
else:
pos += 1
FP = pos
TN = neg
Precision = TP / (TP + FP)
Recall = TP / (TP + FN)
F1 = 2 * (Precision * Recall) / (Precision + Recall)
print("Precision:", Precision)
print("Recall:", Recall)
print("F1:", F1)