llmat commited on
Commit
bea12a5
·
verified ·
1 Parent(s): c02f435

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -5
app.py CHANGED
@@ -1,9 +1,9 @@
1
  import gradio as gr
2
  import cv2
3
-
4
  import easyocr
5
  from PIL import Image
6
 
 
7
  def get_grayscale(image):
8
  return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
9
 
@@ -12,8 +12,8 @@ def thresholding(src):
12
 
13
  def ocr_with_easy(img):
14
  gray_scale_image = get_grayscale(img)
15
- thresholding(gray_scale_image)
16
- cv2.imwrite('image.png', gray_scale_image)
17
  reader = easyocr.Reader(['en'])
18
  bounds = reader.readtext('image.png', paragraph="False", detail=0)
19
  bounds = ''.join(bounds)
@@ -28,10 +28,66 @@ def generate_ocr(img):
28
  raise gr.Error("Please upload an image!!!!")
29
  return text_output
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  image = gr.Image()
32
  output = gr.Textbox(label="Output")
33
 
34
- app = gr.Interface(
35
  generate_ocr,
36
  image,
37
  output,
@@ -39,4 +95,10 @@ app = gr.Interface(
39
  css=".gradio-container {background-color: lightgray} #radio_div {background-color: #FFD8B4; font-size: 40px;}"
40
  )
41
 
42
- app.launch()
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import cv2
 
3
  import easyocr
4
  from PIL import Image
5
 
6
+ # Functions for OCR steps
7
  def get_grayscale(image):
8
  return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
9
 
 
12
 
13
  def ocr_with_easy(img):
14
  gray_scale_image = get_grayscale(img)
15
+ thresholded_image = thresholding(gray_scale_image)
16
+ cv2.imwrite('image.png', thresholded_image)
17
  reader = easyocr.Reader(['en'])
18
  bounds = reader.readtext('image.png', paragraph="False", detail=0)
19
  bounds = ''.join(bounds)
 
28
  raise gr.Error("Please upload an image!!!!")
29
  return text_output
30
 
31
+ # Interactive questions
32
+ questions = [
33
+ {
34
+ "question": "What is the first step in OCR?",
35
+ "options": ["Binarization", "Grayscale conversion", "Edge detection"],
36
+ "answer": "Grayscale conversion"
37
+ },
38
+ {
39
+ "question": "What is the purpose of thresholding in OCR?",
40
+ "options": ["To detect edges", "To convert image to grayscale", "To binarize the image"],
41
+ "answer": "To binarize the image"
42
+ },
43
+ {
44
+ "question": "Which library is used for OCR in this app?",
45
+ "options": ["Tesseract", "EasyOCR", "OpenCV"],
46
+ "answer": "EasyOCR"
47
+ },
48
+ {
49
+ "question": "What format is the image saved in after preprocessing?",
50
+ "options": ["JPG", "PNG", "TIFF"],
51
+ "answer": "PNG"
52
+ },
53
+ {
54
+ "question": "What does OCR stand for?",
55
+ "options": ["Optical Character Recognition", "Optical Character Reading", "Optical Code Recognition"],
56
+ "answer": "Optical Character Recognition"
57
+ }
58
+ ]
59
+
60
+ def quiz_interface():
61
+ def check_answer(question_idx, selected):
62
+ if questions[question_idx]["answer"] == selected:
63
+ return "Correct!"
64
+ else:
65
+ return "Incorrect. The correct answer is: " + questions[question_idx]["answer"]
66
+
67
+ interfaces = []
68
+ for idx, question in enumerate(questions):
69
+ radio = gr.Radio(choices=question["options"], label=question["question"])
70
+ button = gr.Button("Submit")
71
+ output = gr.Textbox(label="Result")
72
+
73
+ def create_submit_fn(idx):
74
+ def submit(selected):
75
+ return check_answer(idx, selected)
76
+ return submit
77
+
78
+ interfaces.append(gr.Interface(
79
+ create_submit_fn(idx),
80
+ radio,
81
+ output,
82
+ live=True
83
+ ))
84
+
85
+ return interfaces
86
+
87
  image = gr.Image()
88
  output = gr.Textbox(label="Output")
89
 
90
+ ocr_app = gr.Interface(
91
  generate_ocr,
92
  image,
93
  output,
 
95
  css=".gradio-container {background-color: lightgray} #radio_div {background-color: #FFD8B4; font-size: 40px;}"
96
  )
97
 
98
+ quiz_app = gr.TabbedInterface(
99
+ [ocr_app] + quiz_interface(),
100
+ ["OCR Tool"] + [f"Question {i+1}" for i in range(len(questions))],
101
+ title="OCR Tutorial and Quiz"
102
+ )
103
+
104
+ quiz_app.launch()