bonrix commited on
Commit
9c4bed0
·
1 Parent(s): e0bfc0e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -14
app.py CHANGED
@@ -4,28 +4,23 @@ import numpy as np
4
  import pytesseract
5
  import time
6
 
7
- # set the path to the Tesseract executable
8
- pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe' #Tresseract path
9
 
10
- def text(file):
11
 
12
-
13
  start_time = time.time()
14
 
15
-
16
- # load the image and convert it to grayscale
17
- image = cv2.imdecode(np.frombuffer(file.read(), np.uint8), cv2.IMREAD_UNCHANGED)
18
  gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
19
 
20
-
21
 
22
- # apply thresholding to preprocess the image
23
  thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
24
 
25
- # perform OCR on the thresholded image
26
  text = pytesseract.image_to_string(thresh, lang='eng')
27
 
28
- # split the OCR output into a list of lines
29
  lines = [line.strip() for line in text.split('\n') if line.strip()]
30
 
31
  end_time = time.time()
@@ -36,10 +31,9 @@ def text(file):
36
  'time_taken': time_taken
37
  }
38
 
39
- file_input = gr.inputs.File(label="Upload Image")
40
  text_output = gr.outputs.JSON(label="OCR Result")
41
 
42
- iface = gr.Interface(fn=text, inputs=file_input, outputs=text_output, title="Text Detection Using Pytesseract",
43
- description="Upload an image to detect text.")
44
  iface.launch()
45
 
 
4
  import pytesseract
5
  import time
6
 
 
 
7
 
 
8
 
9
+ def text(image):
10
  start_time = time.time()
11
 
12
+ # Convert the image to grayscale
 
 
13
  gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
14
 
15
+
16
 
17
+ # Apply thresholding to preprocess the image
18
  thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
19
 
20
+ # Perform OCR on the thresholded image
21
  text = pytesseract.image_to_string(thresh, lang='eng')
22
 
23
+ # Split the OCR output into a list of lines
24
  lines = [line.strip() for line in text.split('\n') if line.strip()]
25
 
26
  end_time = time.time()
 
31
  'time_taken': time_taken
32
  }
33
 
34
+ image_input = gr.inputs.Image(label="Upload Image")
35
  text_output = gr.outputs.JSON(label="OCR Result")
36
 
37
+ iface = gr.Interface(fn=text, inputs=image_input, outputs=text_output)
 
38
  iface.launch()
39