easyOCR 을 이용하여 한글을 추출해 보도록 하겠습니다.
결과 화면 보시고 분석하도록 할께요.
OCR 분석중

OCR 결과

OCR 해석하기 위한 프로그램 순서는 아래와 같습니다.
- 첨부파일 업로드
- 업로드된 첨부파일로 easyOCR 분석
- 분석완료된 결과 출력
해당 프로그램은 php 로 파일업로드 기능을 수행한 뒤
파이썬을 이용하여 OCR 분석한다.
index.php
<html> <head> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> <style> .loader { border: 16px solid #f3f3f3; /* Light grey */ border-top: 16px solid #3498db; /* Blue */ border-radius: 50%; width: 120px; height: 120px; animation: spin 2s linear infinite; } @keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } } #result { width:90%; padding: 10px; margin: 10px; border: 1px solid red; } #result_explode { width:90%; padding: 10px; margin: 10px; border: 1px solid blue; } </style> </head> <body> <!-- <form method="post" enctype="multipart/form-data" action="http://221.146.90.7/fileUpload.php"> <input type="file" name="file"> <input type="submit"> </form> --> <p><input type="file" name="sample_image" /> </p> <div class="text-center" id="uploaded_image"></div> <div class="text-center" id="result"></div> <div class="text-center" id="result_explode"></div> <div class="loader" id="loading" style="display:none"><br><br>OCR 분성중....<br>1분이상....<br>오래걸림</div> <script> const sample_image = document.getElementsByName('sample_image')[0]; sample_image.addEventListener('change', () => { upload_image(sample_image.files[0]); clear(); }); const upload_image = (file) => { // check file type if(!['image/jpeg', 'image/png'].includes(file.type)) { document.getElementById('uploaded_image').innerHTML = '<div class="alert alert-danger">Only .jpg and .png image are allowed</div>'; document.getElementsByName('sample_image')[0].value = ''; return; } // check file size (< 2MB) if(file.size > 2 * 1024 * 1024) { document.getElementById('uploaded_image').innerHTML = '<div class="alert alert-danger">File must be less than 2 MB</div>'; document.getElementsByName('sample_image')[0].value = ''; return; } const form_data = new FormData(); form_data.append('sample_image', file); fetch("fileUpload.php", { method:"POST", body : form_data }).then(function(response){ return response.json(); }).then(function(responseData){ document.getElementById('uploaded_image').innerHTML = '<img src="'+responseData.image_source+'" class="img-thumbnail" />'; document.getElementsByName('sample_image')[0].value = ''; easyOCR(responseData.image_source); }); } const clear =_=>{ document.getElementById('result').style.display = 'none'; document.getElementById('result_explode').style.display = 'none'; } const show =_=>{ document.getElementById('result').style.display = 'block'; document.getElementById('result_explode').style.display = 'block'; } const easyOCR = fn => { document.getElementById('loading').style.display = 'block'; fetch("php_py_exec.php?"+new URLSearchParams({ fn: fn }), { method:"GET", }).then(function(response) { document.getElementById('loading').style.display = 'none'; return response.json(); }).then(function(responseData) { show(); console.log(responseData.result); console.log(responseData.result_explode); document.getElementById('result').innerHTML = responseData.result; document.getElementById('result_explode').innerHTML = responseData.result_explode; }); } </script> </body> </html>
fileUpload.php
<?php set_time_limit(0); if(isset($_FILES['sample_image'])) { $extension = pathinfo($_FILES['sample_image']['name'], PATHINFO_EXTENSION); $new_name = time() . '.' . $extension; move_uploaded_file($_FILES['sample_image']['tmp_name'], 'data/' . $new_name); $data = array( 'image_source' => 'data/' . $new_name ); echo json_encode($data); } /* $fullPath = '/home/naya/www/data/example.jpeg'; exec("PYTHONIOENCODING=utf-8 python3 /home/naya/easyOCR-binary-centos-main/tests/php_client.py $fullPath 2>&1", $output); $result = $output[0]; $arr2 = explode("),", $result); print_r($arr2); exit; */ /* $allowedExts = array("gif", "jpeg", "jpg", "png"); if (isset($_FILES)) { $file = $_FILES["file"]; $error = $file["error"]; $name = $file["name"]; $type = $file["type"]; $size = $file["size"]; $tmp_name = $file["tmp_name"]; if ( $error > 0 ) { echo "Error: " . $error . "<br>"; } else { $temp = explode(".", $name); $extension = end($temp); if ( ($size/1024/1024) < 200. && in_array($extension, $allowedExts) ) { //echo "Upload: " . $name . "<br>"; //echo "Type: " . $type . "<br>"; //echo "Size: " . ($size / 1024 / 1024) . " Mb<br>"; //echo "Stored in: " . $tmp_name; if (file_exists("data/" . $name)) { echo $name . " already exists. "; } else { move_uploaded_file($tmp_name, "data/" . $name); echo "Stored in: " . "data/" . $name; } } else { echo ($size/1024/1024) . " Mbyte is bigger than 2 Mb "; echo $extension . "format file is not allowed to upload ! "; } } } else { echo "File is not selected"; } //Header("Location: php_py_exec.php?fn=$name"); */ ?>
php_py_exec.php
<?php set_time_limit(0); $fn = $_GET['fn']; $fullPath = '/home/ㅋㅋㅋ/www/'.$fn; exec("PYTHONIOENCODING=utf-8 python3 /home/ㅋㅋㅋ/easyOCR-binary-centos-main/tests/php_client.py $fullPath 2>&1", $output); $result = $output[0]; $arr2 = implode("<br>",explode("),", $result)); $res = []; $res['result'] = $result; $res['result_explode'] = $arr2; echo json_encode($res);
'프로그래밍 > Php' 카테고리의 다른 글
php 로 만드는 socket Server 와 socket Client (0) | 2023.02.16 |
---|---|
php 디자인 패턴의 템플릿 메소드 패턴 크롤링 puppeteer 이용한 예제 (0) | 2023.02.02 |
PHP에서 Fetch API를 사용하여 JavaScript로 파일 업로드 (1) | 2021.10.28 |
php exec 사용하여 파이썬 호출후 한글이 안나올때 (0) | 2021.10.26 |
mysql error ERROR 1366 (HY000): Incorrect string value: '\xF0\x9F\x98\x9C' for column 'comment' at row 1 (0) | 2020.11.09 |