본문 바로가기

코딩/파이썬(Python)

[파이썬] 머신러닝으로 민원24, 대법원 캡챠(Captcha) 뚫기

반응형

 

민원24 등 정부캡챠

정부 민원24 홈페이지에 보면 비회원으로 증명서 등 열람을 하기 위해서는 캡챠 입력이 필요하다.

업무 자동화를 하다보면, 번번히 마주치는게 캡챠이긴 한데, 민원24의 캡챠는 OCR이 어려워서 까다로운 편이긴 하다.

 

그래서 이번에 머신러닝으로 민원24 캡챠의 숫자를 인식하는 알고리즘을 만들어봤다.

 

 

 

데이터셋 수집

우선은 데이터셋이 필요하니, 캡챠 이미지를 크롤링을 통해서 500개 정도 수집을 하고, 하나씩 모두 직접 맞는 숫자를 이미지 파일 이름을 입력해주었다. (노가다.....)

 

 

 

이미지 전처리

그리고 그다음 모델의 정확도를 높이기 위해서 이미지 전처리가 필요하다.

다행히 글자에 반전이 있다거나 투명도가 다르다거나 하는 복잡한 형태가 아니라서, 그냥 회색으로 변환한 다음 흰색(255) 아니면 검은색(0)으로 변환하였다. (opencv 라이브러리 사용)

 

 

 

그리고 각 숫자별로 학습할 수 있도록 5개의 숫자로 이루어진 이미지를 5개의 개별 이미지로 분할한다.

고정폭 글자가 아니고, 숫자가 겹쳐 있어서 정확하게 분할은 어려워서 그냥 일정간격으로 분할해버렸다.

 

여기서 중요한 점은, 개별 이미지를 동일한 사이즈로 변환해야 한다는 점이다. 

자르다 보면 가로, 세로 사이즈가 다른 이미지들이 생기는데, 강제로 통일시켜 줘야 한다. 그렇게 해야 정확도가 올라감

 

데이터 라벨링

 

이렇게 0부터 9까지 모든 숫자에 대해서 작업을 해준 다음, 모델에 학습을 시킨다.

 

 

 

나는 Python에서 Scikit-learn 라이브러리를 사용했는데, 데이터 전처리만 잘되어 있다면 사용하기가 무척 편리하다.

각 개별 숫자 이미지를 opencv로 읽어와서 numpy 행렬로 변환하고, 레이블을 달아준 다음 모델에 넣어준다.

 

학습모델 성능 체크

scikit-learn에서 cross_val_score 함수를 사용하면 트레이닝셋을 알아서 여러개로 나눠가면서 반복적으로 모델의 성능을 간편하게 테스트할 수 있다.

 

https://scikit-learn.org/stable/modules/cross_validation.html#cross-validation-iterators

 

3.1. Cross-validation: evaluating estimator performance — scikit-learn 0.23.2 documentation

3.1. Cross-validation: evaluating estimator performance Learning the parameters of a prediction function and testing it on the same data is a methodological mistake: a model that would just repeat the labels of the samples that it has just seen would have

scikit-learn.org

 

 

그리고 cross_val_score 로 랜덤포레스트(RandomForest)와 신경망(Neural network) 모델의 성능을 테스트해봤다.

 

랜덤포레스트 

classifier = RandomForestClassifier(max_depth=100, n_estimators=100)
scores = cross_val_score(classifier, train_data, train_target, cv=10)
print(scores)
print(np.mean(scores))
[0.91208791 0.86006289 0.91666667 0.94164038 0.93512658 0.94126984
 0.93799682 0.94098884 0.92663477 0.94098884]
0.9253463534474708

트레이닝셋을 10개로 나누어서 측정한 결과 각 글자당 평균 정확도는 92.5% 수준이다. 

 

그리고 테스트셋에 대한 테스트 결과 성공률은 92%

Answer : 01608 / Prediction : 01608 True
Answer : 03074 / Prediction : 03074 True
Answer : 06325 / Prediction : 06325 True
Answer : 11351 / Prediction : 11351 True
Answer : 11411 / Prediction : 11411 True
Answer : 12134 / Prediction : 12734 False
Answer : 13920 / Prediction : 13920 True
Answer : 15465 / Prediction : 25465 False
Answer : 17923 / Prediction : 17923 True
Answer : 17997 / Prediction : 17997 True
Answer : 20937 / Prediction : 20937 True
Answer : 22084 / Prediction : 22084 True
Answer : 22104 / Prediction : 22104 True
Answer : 22631 / Prediction : 22631 True
Answer : 24136 / Prediction : 24136 True
Answer : 29678 / Prediction : 29678 True
Answer : 30760 / Prediction : 30760 True
Answer : 31432 / Prediction : 31432 True
Answer : 33844 / Prediction : 33844 True
Answer : 33926 / Prediction : 33926 True
Answer : 34090 / Prediction : 34090 True
Answer : 36068 / Prediction : 36068 True
Answer : 37842 / Prediction : 37842 True
Answer : 38764 / Prediction : 38764 True
Answer : 42803 / Prediction : 42803 True
Answer : 43352 / Prediction : 43352 True
Answer : 50076 / Prediction : 60076 False
Answer : 52354 / Prediction : 52354 True
Answer : 54795 / Prediction : 54795 True
Answer : 58772 / Prediction : 58772 True
Answer : 59006 / Prediction : 59006 True
Answer : 61172 / Prediction : 61172 True
Answer : 63843 / Prediction : 63843 True
Answer : 64419 / Prediction : 64419 True
Answer : 65626 / Prediction : 65626 True
Answer : 66450 / Prediction : 66450 True
Answer : 68127 / Prediction : 68127 True
Answer : 68550 / Prediction : 68550 True
Answer : 68619 / Prediction : 68619 True
Answer : 71550 / Prediction : 71550 True
Answer : 73160 / Prediction : 73160 True
Answer : 73586 / Prediction : 73586 True
Answer : 74031 / Prediction : 79031 False
Answer : 80031 / Prediction : 80031 True
Answer : 81637 / Prediction : 81637 True
Answer : 82159 / Prediction : 82159 True
Answer : 84387 / Prediction : 84387 True
Answer : 86642 / Prediction : 86642 True
Answer : 96192 / Prediction : 96192 True
Answer : 97430 / Prediction : 97430 True
Success Rate : 0.92
[Finished in 5.0s]

 

다음은 신경망

classifier = MLPClassifier(solver='adam', hidden_layer_sizes=(1000,), max_iter=1000)
scores = cross_val_score(classifier, train_data, train_target, cv=10)
print(scores)
print(np.mean(scores))
[0.94191523 0.92295597 0.9591195  0.96687697 0.96202532 0.95555556
 0.96025437 0.9569378  0.96491228 0.96810207]
0.9558655068076838

측정 결과 각 글자당 정확도는 95.6%가 나온다.

 

그리고 모델 학습 후 테스트 셋을 넣어보면 성공률은 88% 수준이다.

Answer : 01608 / Prediction : 01608 True
Answer : 03074 / Prediction : 03074 True
Answer : 06325 / Prediction : 06325 True
Answer : 11351 / Prediction : 11351 True
Answer : 11411 / Prediction : 11411 True
Answer : 12134 / Prediction : 12134 True
Answer : 13920 / Prediction : 13920 True
Answer : 15465 / Prediction : 76465 False
Answer : 17923 / Prediction : 17973 False
Answer : 17997 / Prediction : 17997 True
Answer : 20937 / Prediction : 20937 True
Answer : 22084 / Prediction : 22084 True
Answer : 22104 / Prediction : 22104 True
Answer : 22631 / Prediction : 22630 False
Answer : 24136 / Prediction : 24136 True
Answer : 29678 / Prediction : 29678 True
Answer : 30760 / Prediction : 30760 True
Answer : 31432 / Prediction : 31432 True
Answer : 33844 / Prediction : 33844 True
Answer : 33926 / Prediction : 33926 True
Answer : 34090 / Prediction : 34090 True
Answer : 36068 / Prediction : 36068 True
Answer : 37842 / Prediction : 37842 True
Answer : 38764 / Prediction : 58764 False
Answer : 42803 / Prediction : 42803 True
Answer : 43352 / Prediction : 43352 True
Answer : 50076 / Prediction : 60076 False
Answer : 52354 / Prediction : 52354 True
Answer : 54795 / Prediction : 54795 True
Answer : 58772 / Prediction : 58772 True
Answer : 59006 / Prediction : 59006 True
Answer : 61172 / Prediction : 61172 True
Answer : 63843 / Prediction : 63843 True
Answer : 64419 / Prediction : 64419 True
Answer : 65626 / Prediction : 65626 True
Answer : 66450 / Prediction : 66450 True
Answer : 68127 / Prediction : 68127 True
Answer : 68550 / Prediction : 68550 True
Answer : 68619 / Prediction : 68619 True
Answer : 71550 / Prediction : 71550 True
Answer : 73160 / Prediction : 73160 True
Answer : 73586 / Prediction : 73586 True
Answer : 74031 / Prediction : 79031 False
Answer : 80031 / Prediction : 80031 True
Answer : 81637 / Prediction : 81637 True
Answer : 82159 / Prediction : 82159 True
Answer : 84387 / Prediction : 84387 True
Answer : 86642 / Prediction : 86642 True
Answer : 96192 / Prediction : 96192 True
Answer : 97430 / Prediction : 97430 True
Success Rate : 0.88
[Finished in 2.7s]

모델의 정확도가 상대적으로 더 낮은 RandomForest가 실제 테스트에서 성공률이 높은건 좀 아이러니 한데 어쨌든 두 모델 모두 90% 정도의 성공률을 보여, 만족스러운 수준이다. 

(처리속도는 신경망 모델이 약 2배 빠른 것으로 나타난다. 물론.. 학습속도는 훨씬 오래 걸림..)

 

아마, 테스트셋이 더 많아지면 신경망 모델이 더 나은 성능을 보일 것 같기도 하다.

 

그리고 각 모델을 pickle로 저장했을 때, 파일의 크기는 신경망 모델이 RandomForest보다 작아, 보다 효율적인 것으로 보인다.

 

 

실제 적용 화면

 

 

 

REST API 사용 문의

 

* 추가 (2020-08-31)

캡챠 Solver 테스트 API

머신러닝 모델을 테스트 해볼수 있도록 API를 제작하였습니다. 아래 링크에서 테스트하실수 있습니다.

(현재 정확도 약 90% 이상)

https://api.algoasset.kr/captcha/

 

 

반응형