m2zm commited on
Commit
c2b394f
·
verified ·
1 Parent(s): 9b5e39d

Update Test Solver

Browse files
Files changed (1) hide show
  1. app.py +21 -14
app.py CHANGED
@@ -22,21 +22,28 @@ model = VisionEncoderDecoderModel.from_pretrained(
22
  model.eval()
23
  model = torch.compile(model, mode="reduce-overhead")
24
 
25
- def advanced_preprocess(cv_image):
26
  gray = cv2.cvtColor(cv_image, cv2.COLOR_BGR2GRAY)
27
  gray = cv2.bilateralFilter(gray, 9, 75, 75)
28
  kernel = np.ones((3,3), np.uint8)
29
  gray = cv2.morphologyEx(gray, cv2.MORPH_OPEN, kernel, iterations=2)
30
-
31
  thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2)
32
  thresh = cv2.dilate(thresh, kernel, iterations=1)
33
  thresh = cv2.erode(thresh, kernel, iterations=1)
34
-
35
  pil = Image.fromarray(thresh).convert("RGB")
36
  pil = pil.filter(ImageFilter.SHARPEN)
37
  pil = ImageEnhance.Contrast(pil).enhance(3.0)
38
  pil = ImageEnhance.Brightness(pil).enhance(1.5)
39
- pil = pil.filter(ImageFilter.MedianFilter(size=3))
 
 
 
 
 
 
 
 
 
40
  return pil
41
 
42
  def run_ocr(pil_image):
@@ -86,16 +93,16 @@ def genRotations(svg):
86
  img2 = create_rotated_image(disable_anim, a, secondcoords)
87
  combo = combine_images(img2, img1)
88
 
89
- processed = advanced_preprocess(combo)
90
- res = run_ocr(processed)
91
-
92
- if 4 <= len(res) <= 5 and res.isalnum():
93
- score = len(res) * 100 + (10 if res.isalnum() else 0)
94
- if score > best_score:
95
- best_score = score
96
- best_result = res
97
- if len(res) in (4, 5):
98
- return res
99
 
100
  return best_result if best_result else ""
101
 
 
22
  model.eval()
23
  model = torch.compile(model, mode="reduce-overhead")
24
 
25
+ def preprocess_v1(cv_image):
26
  gray = cv2.cvtColor(cv_image, cv2.COLOR_BGR2GRAY)
27
  gray = cv2.bilateralFilter(gray, 9, 75, 75)
28
  kernel = np.ones((3,3), np.uint8)
29
  gray = cv2.morphologyEx(gray, cv2.MORPH_OPEN, kernel, iterations=2)
 
30
  thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2)
31
  thresh = cv2.dilate(thresh, kernel, iterations=1)
32
  thresh = cv2.erode(thresh, kernel, iterations=1)
 
33
  pil = Image.fromarray(thresh).convert("RGB")
34
  pil = pil.filter(ImageFilter.SHARPEN)
35
  pil = ImageEnhance.Contrast(pil).enhance(3.0)
36
  pil = ImageEnhance.Brightness(pil).enhance(1.5)
37
+ return pil
38
+
39
+ def preprocess_v2(cv_image):
40
+ gray = cv2.cvtColor(cv_image, cv2.COLOR_BGR2GRAY)
41
+ _, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
42
+ kernel = np.ones((2,2), np.uint8)
43
+ thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
44
+ pil = Image.fromarray(thresh).convert("RGB")
45
+ pil = pil.filter(ImageFilter.SHARPEN)
46
+ pil = ImageEnhance.Contrast(pil).enhance(2.8)
47
  return pil
48
 
49
  def run_ocr(pil_image):
 
93
  img2 = create_rotated_image(disable_anim, a, secondcoords)
94
  combo = combine_images(img2, img1)
95
 
96
+ for prep_func in [preprocess_v1, preprocess_v2]:
97
+ processed = prep_func(combo)
98
+ res = run_ocr(processed)
99
+ if 4 <= len(res) <= 5 and res.isalnum():
100
+ score = len(res) * 100 + (20 if res.isalnum() else 0)
101
+ if score > best_score:
102
+ best_score = score
103
+ best_result = res
104
+ if len(res) in (4, 5):
105
+ return res
106
 
107
  return best_result if best_result else ""
108