배경
COVID 19 사태 이후 온라인 강의가 증가하고 있다. 오프라인에서 온라인 강의로 전향되면서 대학교 강의 1개에 수강할 수 있는 수강생 인원이 크게 증가하였다. COVID 19 사태 전까지만 해도 일반 전공 수업을 1개의 강의에 20~40명 정도였다. 하지만 온라인 강의가 가능한 지금은 수용 가능한 수강생 최대 인원이 100명으로 증가하고 이런 영향으로 조교 업무를 맡은 나에게는 현재 92명을 일주일에 2번 출석체크해야 하는 상황이다. 이는 대학원생인 나에게 시간 낭비를 시키는 영향을 준다. 그래서 현재 인원의 상태를 스크린 숏으로 이미지를 저장 후 나머지 출결 정보는 컴퓨터에게 맡기는 프로그램을 개발하기로 했다. 심플할수록 좋다. 기능은 OCR, 학번 별 오름차순 정렬, 결과 출력이다.
요구 사항
1. 현재 강의를 듣고 있는 수강생들의 ID(학번 이름)을 엑셀 파일에 학번별 오름차 순으로 정렬하여 저장.
2. 인원이 많을 경우 수강들의 ID(학번 이름)을 저장하고 있는 사진들을 한 폴더에 저장 후 프로그램 실행하여 해당 경로를 입력 시 해당 경로의 사진의 글자를 인식하여 N번 기능을 수행.
3. 출석체크 결과는 python shall에 엑셀로 복사 붙여 넣기 하기 쉽게 출력
필요 기능
1. OCR
2. excel 파일 읽기
3. 출석부 명단 데이터 정리
개발 환경
IDLE : Anaconda, Spyder
API : OpenCV, tesseract, pandas, numpy
Language : Python 3.7 이상
예상 개발 기간
7일
테스트 방법
실제 사진과 결과 비교
구현
사용 라이브러리
import cv2 import os try: from PIL import Image except ImportError: import Image import pytesseract import numpy as np import pandas as pd
유틸 함수 1
설명 : 지정 경로 폴더 안에 있는 모든 이미지 경로 가져오기
Paramter
- folderPath : 지정할 폴더의 절대 경로
def getFileList(folderPath): fileList = os.listdir(folderPath) answerFileList = [] for fileName in fileList: answerFileList.append(folderPath + '\\' + fileName) return answerFileList
영상 처리 함수
설명 : 지정한 경로의 이미지를 열고 이미지 전처리 후 pytesseract를 이용한 ocr 결과 가져오기
Parameter
- loadImagePath : 불러올 이미지의 절대 경로
- savePath : 이미지 전처리 후 이미지를 저장할 절대 경로
def GetNameList(loadImagePath, savePath): # 설치한 tesseract 프로그램 경로 (64비트) pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract' image = cv2.imread(loadImagePath) #bgr to gray gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) #이미지 크기 조절 gray = cv2.resize(gray, dsize=(0, 0), fx=1.5, fy=1.5, interpolation=cv2.INTER_LINEAR) #컨볼루션 sharpening = np.array([[-1, -1, -1, -1, -1], [-1, 2, 2, 2, -1], [-1, 2, 9, 2, -1], [-1, 2, 2, 2, -1], [-1, -1, -1, -1, -1]]) / 9.0 gray = cv2.filter2D(gray, -1, sharpening) # write the grayscale image to disk as a temporary file so we can # 글자 프로세싱을 위해 Gray 이미지 임시파일 형태로 저장. #filename = '{}.bmp'.format(os.getpid()) filename = savePath cv2.imwrite(filename, gray) # Simple image to string text = pytesseract.image_to_string(Image.open(filename), lang='kor') #1차 필터링 : 줄 단위 save = text.split('\n') #2차 필터링 : 글자수 6개 이상 #3차 필터링 : 글자에 숫자가 7개이상 firstfilter = [] for name in save: if len(name) >= 6 and len(name) <= 15: digitCount = 0 for checkDigit in name: if checkDigit.isdigit(): digitCount += 1 if digitCount >= 7: firstfilter.append(name) return firstfilter
사용 함수 1
설명 : 지정한 폴더 경로에 접근하여 영상 처리 함수를 사용 후 결과 명단 가져오기
Parameter
- folderPath : 체크할 이미지들이 들어있는 폴더 경로
(절대 경로 (ex. C:\Projects\MS\attendanceCheck))
def GetAllNameList(folderPath): fileList = getFileList(folderPath) AllNameList = [] for filePath in fileList: file_name, file_ext = os.path.splitext(filePath) savefilelist = list(file_name) savefilelist.insert((len(savefilelist)), '_.bmp') saveAfterProcessImagePath = ''.join(savefilelist) temp = GetNameList(filePath, saveAfterProcessImagePath) for name in temp: AllNameList.append(name) #모든 이름 내림차순 정렬 AllNameList.sort() return AllNameList
사용 함수 2
설명 : 지정한 엑셀 파일 경로에 접근하여 출석 명단을 가져온다.
Parameter
- excelPath : 출석 명단 원본 경로
(절대 경로 (ex. C:\Projects\MS\attendanceCheck\check.xlsc))
def UpdateCheckList(excelPath): df = pd.read_excel(excelPath) result = [] for index in range(len(df['number'])): temp = [] temp.append(str(df['number'][index])) temp.append(df['name'][index]) temp.append(False) result.append(temp) return result
사용 함수 3
설명 : 지정한 엑셀 파일 경로에 접근하여 출석 명단을 가져온 후 이전 전처리 과정에서 찾은 명단과 비교하는 함수
Parameter
- excelPath : 출석 명단 원본 경로
(절대 경로 (ex. C:\Projects\MS\attendanceCheck\check.xlsc))
- nameList : 영상처리로 찾아낸 명단 리스트
- result : '사용 함수 2' 과정에서 가져온 명단 리스트
def GetResult(excelPath, nameList, result): df = pd.read_excel(excelPath) for index in range(len(df['number'])): for checkAttendance in nameList: if checkAttendance.find(str(df['number'][index])) >= 0 or checkAttendance.find(str(df['name'][index])) >= 0: result[index][2] |= True break else: result[index][2] |= False return result
데이터 추출 명령어
설명 : 4번의 검사를 수행한다. 1번 수행할 때마다 이미지를 리사이징 해서 체크한다. 정확도가 올라가는 효과를 기대할 수 있다.
exelPath = "엑셀 파일 경로" imagePath = "폴더 경로" result = UpdateCheckList(exelPath) #총 4번 다시 체크 이미지 사이즈 조절 하면서 체크 많이 할수록 정확도가 올라감 for i in range(4): arr = GetAllNameList(imagePath) result = GetResult(exelPath, arr, result)
출력 결과

학번, 이름, 출석 결과로 출력함.
, 로 구분 짓기 때문에 엑셀로 복붙 하면 끝!
자 그럼 이제 출석체크 업무는
1. 강의 참가자 명단 스크린 샷 찍기

2. 테스트할 폴더에 넣기

3. 프로그램 돌리기
4. 결과 복사 붙여 넣기
5. 혹시나 x 표 실시간 줌에서 검색해서 찾기
끝
참고 사이트
ansan-survivor.tistory.com/313
[Python OpenCV] 파이썬 글자 인식, 파이썬 OCR, 파이썬 Tesseract 사용
파이썬을 이용해서 글자를 인식하는 프로그램이다. 아래 블로거님을 참고해서 제작 했다. (참고 링크) junyoung-jamong.github.io/computer/vision,/ocr/2019/01/30/Python%EC%97%90%EC%84%9C-Tesseract%EB%A5%BC-..
ansan-survivor.tistory.com
'M.S > Toy project' 카테고리의 다른 글
위치 기반 사용자 적응형 키오스크 프로젝트 (0) | 2022.08.20 |
---|