From 75a7ce8e8f8fc3f7230ee3f0b71eed2a3cf55652 Mon Sep 17 00:00:00 2001 From: pixel Date: Sat, 24 Mar 2018 12:59:47 +0530 Subject: [PATCH] OCR at /api/ocr/form --- db.sqlite3 | Bin 167936 -> 167936 bytes studentapp/static/css/ocr_form.css | 70 +++++++++++++++++++++++++++++ studentapp/static/js/ocr_form.js | 41 +++++++++++++++++ studentapp/templates/ocr_form.html | 46 +++++++++++++++++++ studentapp/urls.py | 3 ++ studentapp/views.py | 23 ++++++++++ 6 files changed, 183 insertions(+) create mode 100644 studentapp/static/css/ocr_form.css create mode 100644 studentapp/static/js/ocr_form.js create mode 100644 studentapp/templates/ocr_form.html diff --git a/db.sqlite3 b/db.sqlite3 index 498ec9f7a98a8ca5300bcf83e4ea69d146cb81bf..2ab7428c8200375a962670343d28d8b70d24c987 100644 GIT binary patch delta 2404 zcmb_dPixdb6z{ecrBI__QBY{fsR!$JrGG?(MT&}0#fy3<5@(ay&Cu*5%*>W9%aVd0 zK>Y?qFJ5I2J$V;DgWtf5couZtB$K?PJ+#ohWaho!@4c6o_kMeDICpS3_u*z^`{S*x z+3l%2H@<%jJ}q8bo^if8Kb`l^7w3-?Ij^0zvpl)__Of$*!I^0`q#6X2E0QE$Pmm;( z)9>2Nqjt0L<9B0W(z-H#F}?I_>hsHy>qkUND&6}_BUi++(g*hmP__gmDi$kXTv=aT zb8j!Xoq#~QIm1Sfr3 zfJ~l1U+-$?KA8JN>`E{X;l^Vx?s^Fkx(UYGWs#x+>e(5&5Gnw1aCJM{Z}N)S_d`kxP$~D5gs#94H!uET1;7~@+66f7(yu0 zqwoj`516k%8qES2qGX+j@t%1L>4$L~0hI15K|FsT1l;oX2#7LqieB_64``0aeF4*7 zhxsZsQFt$6GV}^UpQuolSLrj#eOekqrYkJ3L@CN4&_HyEIXtGp01VIar%Ym@z#s_U z^)v?B2%jiDQ@75)S*d%LZk>O#O#e^St#WinXux1!?KoD(t}?W`**U00XLFgB;|&AB zduGq7vx;+6JvweZPqu7!s4=zb$*E@l%wlN+%s*!2`%q7T_XESR9aYwWT>2syP;8HwJh%FW935I7D)IU^-6&*y-^H*3;CeeyG;Z#)*V%siZ8N)oW)z}YuPzf=3@v204mHApkydF6pv`ZD z8Y8W87s$NOOvy2_(DpjiYCQ&9o9#iW|8Py)$It~EVf*RxN1(NOs)-qvjh242v}MyC g?iGhMZ~+_89`Bx#FrJ^%zsYR!Z}I5Di|N1Z|C=W0>;M1& delta 101 zcmZozz}2vTYl1YR=R_H2R!;`qk2^M|ERq+|Wai(?e}})He+B<1{v7^retUk+&4LO1 z{9+>f%=('); + }; + })(file); + fileReader.readAsDataURL(file); + }); + $("[data-js-go-button]").click(function(event) { + event.stopPropagation(); + event.preventDefault(); + data = new FormData(); + data.append('image', $imageInput[0].files[0]); + $.post({ + url: "ocr", + data: data, + cache: false, + contentType: false, + processData: false + }).done(function(data) { + console.log(data); + $resultContainer.removeClass("result-default result-error"); + $resultContainer.addClass("result-success"); + $resultContainer.html(data.utf8_text); + }) + .fail(function(jqXHR) { + $resultContainer.removeClass("result-default result-success"); + $resultContainer.addClass("result-error"); + $resultContainer.html('I AM ERROR'); + }); + }); +}); diff --git a/studentapp/templates/ocr_form.html b/studentapp/templates/ocr_form.html new file mode 100644 index 0000000..981f3d5 --- /dev/null +++ b/studentapp/templates/ocr_form.html @@ -0,0 +1,46 @@ + +{% load static %} + + + + + + + OCR With Django + + + + + + + + + + + +
+
+
+
+ + +
+
+
+
+
 
+
+
+
+
+ + + + + + + + diff --git a/studentapp/urls.py b/studentapp/urls.py index 4b43bed..6a88424 100644 --- a/studentapp/urls.py +++ b/studentapp/urls.py @@ -2,6 +2,7 @@ from django.urls import path, include from . import views from django.contrib.auth import views as auth_views +from studentapp.views import ocr_view, ocr_form_view #importing class based views urlpatterns = [ @@ -17,4 +18,6 @@ path('get_student_data//', views.getStudentData, name='getStudentData'), path('studentform/new', views.studentform, name = 'studentform'), path('chatbot', views.chatbot, name='chatbot'), + path('ocr/ocr', ocr_view, name='ocr_view'), + path('ocr/form', ocr_form_view, name='ocr_form_view'), ] \ No newline at end of file diff --git a/studentapp/views.py b/studentapp/views.py index 89ba894..f76dceb 100644 --- a/studentapp/views.py +++ b/studentapp/views.py @@ -8,6 +8,11 @@ import datetime from pprint import pprint from .forms import StudentForm +from django.views.generic.base import View, TemplateView +#install a few things to get the next two imports working +from PIL import Image, ImageFilter +from tesserocr import PyTessBaseAPI +from django.views import View # Create your views here. def index(req): @@ -416,3 +421,21 @@ def chatbot(request): return JsonResponse({ 'error': 'false', }) + +class OcrFormView(TemplateView): + template_name = 'ocr_form.html' +ocr_form_view = OcrFormView.as_view() + + +class OcrView(View): + def post(self, request, *args, **kwargs): + with PyTessBaseAPI() as api: + with Image.open(request.FILES['image']) as image: + image = image.convert('RGB') + sharpened_image = image.filter(ImageFilter.SHARPEN) + api.SetImage(sharpened_image) + utf8_text = api.GetUTF8Text() + + return JsonResponse({'utf8_text': utf8_text}) +ocr_view = csrf_exempt(OcrView.as_view()) +