server {
listen 80;
server_name onejin.link;
rewrite ^ https://$server_name$request_uri? permanent;
location = /favicon.ico { access_log off; log_not_found off; }
location /static {
alias /home/ubuntu/projects/mysite/static;
}
location / {
include proxy_params;
proxy_pass http://unix:/tmp/gunicorn.sock;
}
}
사용자로부터 입력받은 이미지에서 일본어를 추출하는데 성공하였다.
이제는 이 일본어 단어들을 한국어로 번역을 해주어야 한다.
사진에서 한국어를 추출해도 되지만 불완전하게 추출을 하는 것보다는 추출된 일본어를 사용자에게 검토받고
검증된 단어들을 한국어로 번역하는 기능을 이용하는 편이 더 낫다라고 판단하였다.
번역 API는 이번에서 구글의 Cloud Translation API를 사용해 볼까 한다. (I love Google)
여러 옵션중에서
Translation API Basic
가 적합해보인다.
자 이제 클라이언트 라이브러리를 설치해보자. 필자는 파이썬 기준이다.
pip install google-cloud-translate==2.0.1
명령줄(예: curl 명령어)을 사용하기 위해
gcloud init
자 다시,
위 api는 어렵고 유료일까 하니
쉽고 무료이며 키발급도 필요없는 googletrans를 사용해보겠다.
공식문서의 예제를 보면 pip install googletrans로 설치하지만 구버전으로 설치되어 에러가 발생할 수 있다. 그래서 아래의
!pip install googletrans==3.1.0a0
알파버전으로 설치하자.
하나의 예제를 보자.
from googletrans import Translator
translator = Translator()
result = translator.translate('廊下', dest='ko')
print(result.text)
현재 사진으로부터 단어를 추출해서 보여주는 페이지가 ocr.html이다. 여기에 번역결과를 볼 수 있는 페이지로 넘어갈 수 있는 버튼을 만들었다.
ocr.html
<a href="{% url 'pybo:translate' %}" class="btn btn-primary">Trans</a>
url.py
path('trans/', base_views.translate, name='translate'),
base_views.py
def translate(request):
from googletrans import Translator
translator = Translator()
global trans
trans = []
for i in texts:
result = translator.translate(i, dest='ko')
trans.append(result.text)
global combined_list
combined_list = list(zip(texts, trans))
context = {'combined_list': combined_list}
# print(result.text)
return render(request, 'pybo/trans.html', context)
<table>
<tr><td colspan="3" style="border: none;"><a href="{% url 'pybo:shuffle' %}" style="width: 100%" class="btn btn-primary float-right">단어섞기</a></td>
<tr>
<th>일본어</th><th>한국어</th>
</tr>
{% for text, translation in combined_list %}
<tr>
<td>
{{text}}
</td>
<td>
{{ translation }}
</td>
</tr>
{% endfor %}
</table>
결과값
'Django' 카테고리의 다른 글
Django 추출된 단어 번역하기 (0) | 2023.11.20 |
---|---|
Django 추출단어 랜덤 (0) | 2023.11.16 |
Django 배포하기(git, AWS) (0) | 2023.10.31 |
Django 사용자로부터 이미지 받고 일본어 단어 추출하기 (1) | 2023.10.30 |
Django 에서 내 프로젝트 적용하기 (1) | 2023.10.16 |