Edu_Siri์์ ํ๋ก๊ทธ๋จ ๋์ ํ๋ฆ์ ์๋์ ๊ฐ๋ค.
์ํ๋ ๋์์ ์ฝ์
-> ์์ง์ ๊ฐ์ง ๋ฐ ํ๋ฉด ์บก์ฒ -> ์บก์ฒ๋ ์ฌ์ง ๊ฐ ์ ์ฌ๋ ๋น๊ต -> ๊ธ์ ์ธ์
์์ง์ ๊ฐ์ง ๋ฐ ํ๋ฉด ์บก์ฒ ๊ด๋ จ์ ์ด์ ๊ธ์ ์ฐธ๊ณ ํ์. ์ด์ ๊ธ์์ ํ๋ฉด ์บก์ฒ ์, ์ฌ์ฉ์๊ฐ ๋๋ ํ ๋ฆฌ๋ฅผ ์ง์ ํ๋ฉด ์ง์ ๋ ์ด๋ฆ์ผ๋ก ๋๋ ํ ๋ฆฌ ์์ฑ ํ, ์บก์ฒ๋ณธ์ ํด๋น ํ์ผ ์์ ์ ์ฅํ๋ค. ์ด ์บก์ฒ๋ณธ์ ํ์ผ๋ฉด์ ์ด์ ์ฌ์ง๊ณผ ๋ค์ ์ฌ์ง ๊ฐ์ ์ ์ฌ๋๋ฅผ ์ฒดํฌํ๋ฉด ๋๋ค
1. Check directory & Similarity between images
# ํ์ผ ์ ์ด๋ฏธ์ง๋ค ๋ชจ๋ ๊ฒ์ฌ
def file_listing(path)->str:
files = os.listdir(path)
for i in range(1, len(files)):
comparePic(path, files[i-1], files[i])
print("\n+=====================================+")
print("| Organizing class materials is Done! |")
print("+=====================================+")
# ์ฌ์ง ๊ฐ ์ ์ฌ๋ ์ฒดํฌ
def comparePic(path, image1, image2) -> str:
image1_path = path + "/" + image1
image2_path = path + "/" + image2
pre = cv.imread(image1_path)
post = cv.imread(image2_path)
result = pre.copy()
grayPre = cv.cvtColor(pre, cv.COLOR_BGR2GRAY)
grayPost = cv.cvtColor(post, cv.COLOR_BGR2GRAY)
(Similarity, diff) = compare_ss(grayPre, grayPost, full=True)
diff = (diff * 255).astype('uint8')
if(Similarity < 0.95):
PicToText(post)
comparePIC ํจ์๋ฅผ ์ค์ ์ ์ผ๋ก ๋ณด์. ์ฌ์ง ๊ฐ ์ ์ฌ๋๋ฅผ ์ฒดํฌํ๋ ์๊ณ ๋ฆฌ์ฆ์ ๊ฝค ๋จ์ํ๋ค. pre์ post์ ๊ฐ๊ฐ ์ด๋ฏธ์ง๋ฅผ ๋ถ๋ฌ์จ ํ cvtColorํจ์์ COLOR_BGR2GRAY์ต์ ์ ์ค์ ํ๋ค. ์ด๋ ๊ฒ ์ค์ ๋ ์ด๋ฏธ์ง๋ค์ ์๋์ ๊ฐ์ด ๋ณํ๋๋ค.
pre์ post์ ๋ด๊ธด ์ฌ์ง์ ๋ชจ๋ ํ๋ฐฑ์ผ๋ก ๋ณ๊ฒฝํ์ผ๋, compare_ss ํจ์์ ๋ ์ด๋ฏธ์ง๋ฅผ ์ธ์๋ก ๋ฃ๊ณ full=True๋ก ๋์ด ์ด๋ฏธ์ง ์ ์ฒด์ ๋ํด ๊ตฌ์กฐ ๋น๊ต๋ฅผ ์ํํ๊ฒ ํ๋ค. ์ด ์ํ์ผ๋ก Similarity๊ฐ์ ์ถ์ถํ ์ ์๋ค.
( Similarity๋ ๋ ์ ๋ ฅ ์ด๋ฏธ์ง ์ฌ์ด์ ๊ตฌ์กฐ์ ์ ์ฌ์ฑ ์ง์๋ฅผ ๋ํ๋ด๋ ๊ฒ์ผ๋ก ์๋ฒฝ ๋ถ์ผ์น์ธ -1๊ณผ ์๋ฒฝ ์ผ์น์ธ 1 ์ฌ์ด์ ๊ฐ๋ง์ ๊ฐ์ง๋ค. )
์ ์ฌ๋ ํ๋ณ์ ์ฐ์ด๋ ์ฌ์ง์ด ๊ฐ์ ์๋ฃ์ธ ๊ฒ์ ๊ฐ์ํ๋ฉด, ๋ฏธ์ธํ ๊ธ์ ๋ณํ์๋ ๊ฐ์งํ์ฌ ๊ธ์ ์ธ์์ ์ํฌ ์ ์์ด์ผ ํ๋ค. ์ฌ๋ฌ๋ฒ์ ํ ์คํธ ๊ฒฐ๊ณผ 0.95 ๊ฐ ๊ฐ์ฅ ์ต์ ํ๋ ๊ฐ์ด๋ผ ํ๋จ๋์ด ๊ธฐ์ค๊ฐ์ ์ด์ ๊ฐ์ด ์ก์๋ค.
2. Set configuration before detecting Text
pip install pillow
pip install pytesseract
๊ธ์ ์ธ์์ ํ๋ ค๋ฉด ๊ธฐ์ด ์ธํ ์ด ํ์ํ๋ค. ์ 2๊ฐ๋ถํฐ ์ค์นํ์. pytesseract๋ ์์ด, ํ๊ธ ๋ ์ธ์ด๋ฅผ ๋ชจ๋ ์ธ์์ํฌ ์ ์๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ผ ์ฑํํ๊ฒ ๋๋ค.
def PicToText(path)->str:
# Config Parser ์ด๊ธฐํ & Config File ์ฝ๊ธฐ
config = configparser.ConfigParser()
config.read(os.path.dirname(os.path.realpath(__file__)) + os.sep + 'env' + os.sep + 'property.ini')
ocrToStr(path, 'eng')
4๋ฒ ์งธ ์ค๋ก ์ด์์ฒด์ ์ ๋ง๋ ํจ์ค ์ ๊ทผ์๋ฅผ ์ด์ฉํ์ฌ property.ini ์ค์ ํ์ผ์ ๋ก๋ํ๋ค.
# /env/property.ini
[Bigdata Ocr Extract]
Version= 1.0
[Path]
OcrTxtPath= \\resource\\ocr_result_txt # ์ถ์ถ๋ ํ
์คํธ ํ์ผ์ด ์ ์ฅ๋ ๊ฒฝ๋ก
์ ์ฅ๋ ๊ฒฝ๋ก๋ฅผ ์ง์ ํด์ฃผ๋ ๊ฒ ์ผ๋ฐ์ ์ด์ง๋ง, EDU_Siri์์๋ ์ป์ ํ ์คํธ ํ์ผ์ ํ์์ ๋ง๊ฒ ์์ ํ์ฌ ์ง์ ๋ ํ์ผ์ ์ ์ฅํ๋ฏ๋ก PATH ๋ถ๋ถ์ ๋ฌด์ํด๋ ์๊ด ์๋ค. ( configParser ํจํค์ง์ ์์ธํ ๋ด์ฉ์ ์ฌ๊ธฐ๋ฅผ ์ฐธ๊ณ ํ์. )
3. Detecting Text & Save
def ocrToStr(fullPath, lang='eng'):
img = Image.fromarray(fullPath)
#preserve_interword_spaces : ๋จ์ด ๊ฐ๊ฒฉ ์ต์
์ ์กฐ์ ํ๋ฉด์ ์ถ์ถ ์ ํ๋๋ฅผ ํ์ธํ๋ค.
#psm(ํ์ด์ง ์ธ๊ทธ๋จผํธ ๋ชจ๋ : ์ด๋ฏธ์ง ์์ญ์์์ ํ
์คํธ ์ถ์ถ ๋ฒ์ ๋ชจ๋)
#psm ๋ชจ๋ : https://github.com/tesseract-ocr/tesseract/wiki/Command-Line-Usage
#์ถ์ถ(์ด๋ฏธ์งํ์ผ, ์ถ์ถ์ธ์ด, ์ต์
)
outText = image_to_string(img, lang=lang, config='--psm 1 -c preserve_interword_spaces=1')
strToText(outText)
def strToText(outText):
with open('./course_material/ppt_content.txt', 'a', encoding='utf-8') as f:
f.write(outText)
f.write("==========================================================\n")
์ด๋ฏธ์ง ์ถ์ถ ํ ์ป์ ํ ์คํธ ๊ฐ์ ์ง์ ๋ ๊ฒฝ๋ก ํ์ผ์ ์ฐ๋ฉด ํด๋น ๊ธฐ๋ฅ์ ์ข ๋ฃ๋๋ค.
Full Code
def comparePic(path, image1, image2) -> str:
image1_path = path + "/" + image1
image2_path = path + "/" + image2
pre = cv.imread(image1_path)
post = cv.imread(image2_path)
result = pre.copy()
grayPre = cv.cvtColor(pre, cv.COLOR_BGR2GRAY)
grayPost = cv.cvtColor(post, cv.COLOR_BGR2GRAY)
(Similarity, diff) = compare_ss(grayPre, grayPost, full=True)
diff = (diff * 255).astype('uint8')
if(Similarity < 0.95):
PicToText(post)
# ๊ธ์ ์ธ์์ ์ํ ํจ์ 1
def ocrToStr(fullPath, lang='eng'):
img = Image.fromarray(fullPath)
#preserve_interword_spaces : ๋จ์ด ๊ฐ๊ฒฉ ์ต์
์ ์กฐ์ ํ๋ฉด์ ์ถ์ถ ์ ํ๋๋ฅผ ํ์ธํ๋ค.
#psm(ํ์ด์ง ์ธ๊ทธ๋จผํธ ๋ชจ๋ : ์ด๋ฏธ์ง ์์ญ์์์ ํ
์คํธ ์ถ์ถ ๋ฒ์ ๋ชจ๋)
#psm ๋ชจ๋ : https://github.com/tesseract-ocr/tesseract/wiki/Command-Line-Usage
#์ถ์ถ(์ด๋ฏธ์งํ์ผ, ์ถ์ถ์ธ์ด, ์ต์
)
outText = image_to_string(img, lang=lang, config='--psm 1 -c preserve_interword_spaces=1')
strToText(outText)
def strToText(outText):
with open('./course_material/ppt_content.txt', 'a', encoding='utf-8') as f:
f.write(outText)
f.write("==========================================================\n")
# ๊ธ์ ์ธ์ํ๋ main ํจ์
def PicToText(path)->str:
# Config Parser ์ด๊ธฐํ & Config File ์ฝ๊ธฐ
config = configparser.ConfigParser()
config.read(os.path.dirname(os.path.realpath(__file__)) + os.sep + 'env' + os.sep + 'property.ini')
ocrToStr(path, 'eng')
def file_listing(path)->str:
files = os.listdir(path)
for i in range(1, len(files)):
comparePic(path, files[i-1], files[i])
print("\n+=====================================+")
print("| Organizing class materials is Done! |")
print("+=====================================+")