tobby48 4 years ago
parent
commit
a55cf353e9

+ 3
- 3
src/main/java/kr/co/swh/lecture/opensource/ocr/ImageToText.java View File

@@ -21,10 +21,10 @@ public class ImageToText {
21 21
 
22 22
 	public static void main(String[] args) {
23 23
 		// TODO Auto-generated method stub
24
-		File image = new File("src/main/resources/ocr/sample.jpg");
24
+		File image = new File("C:\\Users\\tobby48\\git\\swh-opensource\\src\\main\\python\\kr\\co\\swh\\lecture\\opensource\\ocr_tts\\data\\test3.PNG");
25 25
 		Tesseract tesseract = new Tesseract();
26
-		tesseract.setDatapath("src/main/resources/ocr/result");
27
-		tesseract.setLanguage("ko");
26
+		tesseract.setDatapath("C:\\Program Files\\Tesseract-OCR\\tessdata");
27
+		tesseract.setLanguage("kor");
28 28
 		tesseract.setPageSegMode(1);
29 29
 		tesseract.setOcrEngineMode(1);
30 30
 		try {

BIN
src/main/python/kr/co/swh/lecture/opensource/ocr_tts/data/hello.mp3 View File


BIN
src/main/python/kr/co/swh/lecture/opensource/ocr_tts/data/test3.PNG View File


BIN
src/main/python/kr/co/swh/lecture/opensource/ocr_tts/data/test4.PNG View File


+ 1
- 1
src/main/python/kr/co/swh/lecture/opensource/ocr_tts/s1_image_to_text.py View File

@@ -1,3 +1,3 @@
1 1
 import pytesseract
2 2
 pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files\\Tesseract-OCR\\tesseract'
3
-print(pytesseract.image_to_string('data\\test1.PNG',lang='kor'))
3
+print(pytesseract.image_to_string('data\\test4.PNG',lang='kor'))

+ 1
- 1
src/main/python/kr/co/swh/lecture/opensource/ocr_tts/s4_image_to_text_to_voice.py View File

@@ -3,7 +3,7 @@ import pygame
3 3
 import pytesseract
4 4
 
5 5
 pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files\\Tesseract-OCR\\tesseract'
6
-text = pytesseract.image_to_string('data\\test2.PNG',lang='kor')
6
+text = pytesseract.image_to_string('data\\test1.PNG',lang='kor')
7 7
 print(text)
8 8
 tts = gTTS(text=text, lang='ko')
9 9
 tts.save("data\\hello.mp3")

+ 17
- 0
src/main/python/kr/co/swh/lecture/opensource/ocr_tts/sspecial_image_to_text.py View File

@@ -0,0 +1,17 @@
1
+import pytesseract
2
+
3
+
4
+import cv2
5
+
6
+originalImage = cv2.imread('data\\test3.PNG')
7
+grayImage = cv2.cvtColor(originalImage, cv2.COLOR_BGR2GRAY) 
8
+(thresh, blackAndWhiteImage) = cv2.threshold(grayImage, 127, 255, cv2.THRESH_BINARY)
9
+
10
+cv2.imshow('Black white image', blackAndWhiteImage)
11
+cv2.imshow('Original image',originalImage)
12
+cv2.imshow('Gray image', grayImage)
13
+# cv2.waitKey(0)
14
+
15
+
16
+pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files\\Tesseract-OCR\\tesseract'
17
+print(pytesseract.image_to_string(blackAndWhiteImage,lang='kor'))

+ 40
- 0
src/main/python/kr/co/swh/lecture/opensource/ocr_tts/sspecial_image_to_text2.py View File

@@ -0,0 +1,40 @@
1
+# import the necessary packages
2
+from PIL import Image
3
+import pytesseract
4
+import argparse
5
+import cv2
6
+import os
7
+# construct the argument parse and parse the arguments
8
+# ap = argparse.ArgumentParser()
9
+# ap.add_argument("-i", "--image", required=True,
10
+#     help="path to input image to be OCR'd")
11
+# ap.add_argument("-p", "--preprocess", type=str, default="thresh",
12
+#     help="type of preprocessing to be done")
13
+# args = vars(ap.parse_args())
14
+
15
+# load the example image and convert it to grayscale
16
+image = cv2.imread('data\\test5.PNG')
17
+gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
18
+# check to see if we should apply thresholding to preprocess the
19
+# image
20
+# if args["preprocess"] == "thresh":
21
+gray = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
22
+# make a check to see if median blurring should be done to remove
23
+# noise
24
+# elif args["preprocess"] == "blur":
25
+#     gray = cv2.medianBlur(gray, 3)
26
+# write the grayscale image to disk as a temporary file so we can
27
+# apply OCR to it
28
+filename = "{}.png".format(os.getpid())
29
+cv2.imwrite(filename, gray)
30
+
31
+# load the image as a PIL/Pillow image, apply OCR, and then delete
32
+# the temporary file
33
+pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files\\Tesseract-OCR\\tesseract'
34
+text = pytesseract.image_to_string(Image.open(filename))
35
+os.remove(filename)
36
+print(text)
37
+# show the output images
38
+cv2.imshow("Image", image)
39
+cv2.imshow("Output", gray)
40
+cv2.waitKey(0)