hoangkha1810 commited on
Commit
f7b982e
·
verified ·
1 Parent(s): 6762225

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -40
app.py CHANGED
@@ -23,13 +23,19 @@ Paddle OCR
23
  """
24
  def ocr_with_paddle(img):
25
  finaltext = ''
26
- ocr = PaddleOCR(lang='vi', use_angle_cls=True)
27
- # img_path = 'exp.jpeg'
28
- result = ocr.ocr(img)
29
-
30
- for i in range(len(result[0])):
31
- text = result[0][i][1][0]
32
- finaltext += ' '+ text
 
 
 
 
 
 
33
  return finaltext
34
 
35
  """
@@ -37,13 +43,12 @@ Keras OCR
37
  """
38
  def ocr_with_keras(img):
39
  output_text = ''
40
- pipeline=keras_ocr.pipeline.Pipeline()
41
- images=[keras_ocr.tools.read(img)]
42
- predictions=pipeline.recognize(images)
43
- first=predictions[0]
44
- for text,box in first:
45
- output_text += ' '+ text
46
- return output_text
47
 
48
  """
49
  easy OCR
@@ -56,38 +61,37 @@ def get_grayscale(image):
56
  def thresholding(src):
57
  return cv2.threshold(src,127,255, cv2.THRESH_TOZERO)[1]
58
  def ocr_with_easy(img):
59
- gray_scale_image=get_grayscale(img)
60
  thresholding(gray_scale_image)
61
- cv2.imwrite('image.png',gray_scale_image)
62
- reader = easyocr.Reader(['vi'])
63
- bounds = reader.readtext('image.png',paragraph="False",detail = 0)
64
- bounds = ''.join(bounds)
65
- return bounds
66
 
67
  """
68
  Generate OCR
69
  """
70
- def generate_ocr(Method,img):
71
-
 
 
72
  text_output = ''
73
- if (img).any():
74
- add_csv = []
75
- image_id = 1
76
- print("Method___________________",Method)
77
- if Method == 'EasyOCR':
78
- text_output = ocr_with_easy(img)
79
- if Method == 'KerasOCR':
80
- text_output = ocr_with_keras(img)
81
- if Method == 'PaddleOCR':
82
- text_output = ocr_with_paddle(img)
83
-
84
- try:
85
- flag(Method,text_output,img)
86
- except Exception as e:
87
- print(e)
88
- return text_output
89
- else:
90
- raise gr.Error("Please upload an image!!!!")
91
 
92
  # except Exception as e:
93
  # print("Error in ocr generation ==>",e)
 
23
  """
24
  def ocr_with_paddle(img):
25
  finaltext = ''
26
+ ocr = PaddleOCR(lang='en') # Không hỗ trợ đa ngôn ngữ kiểu 'vi|en', phải chạy 2 lần
27
+ result_en = ocr.ocr(img)
28
+
29
+ ocr_vi = PaddleOCR(lang='vi')
30
+ result_vi = ocr_vi.ocr(img)
31
+
32
+ def extract_text(result):
33
+ return ' '.join([line[1][0] for line in result[0]])
34
+
35
+ en_text = extract_text(result_en)
36
+ vi_text = extract_text(result_vi)
37
+
38
+ finaltext = f"[EN]: {en_text}\n[VI]: {vi_text}"
39
  return finaltext
40
 
41
  """
 
43
  """
44
  def ocr_with_keras(img):
45
  output_text = ''
46
+ pipeline = keras_ocr.pipeline.Pipeline()
47
+ images = [keras_ocr.tools.read(img)]
48
+ predictions = pipeline.recognize(images)
49
+ for text, box in predictions[0]:
50
+ output_text += ' ' + text
51
+ return "[Detected]: " + output_text
 
52
 
53
  """
54
  easy OCR
 
61
  def thresholding(src):
62
  return cv2.threshold(src,127,255, cv2.THRESH_TOZERO)[1]
63
  def ocr_with_easy(img):
64
+ gray_scale_image = get_grayscale(img)
65
  thresholding(gray_scale_image)
66
+ cv2.imwrite('image.png', gray_scale_image)
67
+ reader = easyocr.Reader(['vi', 'en']) # Hỗ trợ tiếng Việt và tiếng Anh
68
+ bounds = reader.readtext('image.png', paragraph=False, detail=0)
69
+ result_text = '\n'.join(bounds)
70
+ return result_text
71
 
72
  """
73
  Generate OCR
74
  """
75
+ def generate_ocr(Method, img):
76
+ if img is None or not (img).any():
77
+ raise gr.Error("Please upload an image!")
78
+
79
  text_output = ''
80
+ print("Method selected:", Method)
81
+
82
+ if Method == 'EasyOCR':
83
+ text_output = ocr_with_easy(img)
84
+ elif Method == 'KerasOCR':
85
+ text_output = ocr_with_keras(img)
86
+ elif Method == 'PaddleOCR':
87
+ text_output = ocr_with_paddle(img)
88
+
89
+ try:
90
+ flag(Method, text_output, img)
91
+ except Exception as e:
92
+ print("Flag error:", e)
93
+
94
+ return text_output
 
 
 
95
 
96
  # except Exception as e:
97
  # print("Error in ocr generation ==>",e)