commit db98352fde25488671584147de1fcacb928059dd Author: Mohamed Nouffer Date: Tue Dec 7 13:25:14 2021 +0530 init diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..a35bd53 Binary files /dev/null and b/.DS_Store differ diff --git a/21/12/02/ja.jpeg b/21/12/02/ja.jpeg new file mode 100644 index 0000000..1a765f6 Binary files /dev/null and b/21/12/02/ja.jpeg differ diff --git a/db.sqlite3 b/db.sqlite3 new file mode 100644 index 0000000..251df60 Binary files /dev/null and b/db.sqlite3 differ diff --git a/manage.py b/manage.py new file mode 100755 index 0000000..a97d19e --- /dev/null +++ b/manage.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" +import os +import sys + + +def main(): + """Run administrative tasks.""" + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ocr.settings') + try: + from django.core.management import execute_from_command_line + except ImportError as exc: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) from exc + execute_from_command_line(sys.argv) + + +if __name__ == '__main__': + main() diff --git a/ocr/__init__.py b/ocr/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/ocr/__pycache__/__init__.cpython-39.pyc b/ocr/__pycache__/__init__.cpython-39.pyc new file mode 100644 index 0000000..45521fc Binary files /dev/null and b/ocr/__pycache__/__init__.cpython-39.pyc differ diff --git a/ocr/__pycache__/settings.cpython-39.pyc b/ocr/__pycache__/settings.cpython-39.pyc new file mode 100644 index 0000000..50b0073 Binary files /dev/null and b/ocr/__pycache__/settings.cpython-39.pyc differ diff --git a/ocr/__pycache__/urls.cpython-39.pyc b/ocr/__pycache__/urls.cpython-39.pyc new file mode 100644 index 0000000..eef0d9e Binary files /dev/null and b/ocr/__pycache__/urls.cpython-39.pyc differ diff --git a/ocr/__pycache__/wsgi.cpython-39.pyc b/ocr/__pycache__/wsgi.cpython-39.pyc new file mode 100644 index 0000000..de356a4 Binary files /dev/null and b/ocr/__pycache__/wsgi.cpython-39.pyc differ diff --git a/ocr/asgi.py b/ocr/asgi.py new file mode 100644 index 0000000..b3758aa --- /dev/null +++ b/ocr/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for ocr project. + +It exposes the ASGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/3.2/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ocr.settings') + +application = get_asgi_application() diff --git a/ocr/settings.py b/ocr/settings.py new file mode 100644 index 0000000..283cf4f --- /dev/null +++ b/ocr/settings.py @@ -0,0 +1,127 @@ +""" +Django settings for ocr project. + +Generated by 'django-admin startproject' using Django 3.2.9. + +For more information on this file, see +https://docs.djangoproject.com/en/3.2/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/3.2/ref/settings/ +""" + +from pathlib import Path + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'django-insecure-#-a9k=#&y3e&a&@%rn7mhl@fk3smxceqh^@1)#9t@c-qzx&k^b' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = [] + + +# Application definition + +INSTALLED_APPS = [ + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + 'rest_framework', + 'sumasen_easyocr', +] + +MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', +] + +ROOT_URLCONF = 'ocr.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +WSGI_APPLICATION = 'ocr.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/3.2/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + } +} + + +# Password validation +# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/3.2/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_L10N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/3.2/howto/static-files/ + +STATIC_URL = '/static/' + +# Default primary key field type +# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' diff --git a/ocr/urls.py b/ocr/urls.py new file mode 100644 index 0000000..fec44da --- /dev/null +++ b/ocr/urls.py @@ -0,0 +1,22 @@ +"""ocr URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/3.2/topics/http/urls/ +Examples: +Function views + 1. Add an import: from my_app import views + 2. Add a URL to urlpatterns: path('', views.home, name='home') +Class-based views + 1. Add an import: from other_app.views import Home + 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') +Including another URLconf + 1. Import the include() function: from django.urls import include, path + 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) +""" +from django.contrib import admin +from django.urls import path, include + +urlpatterns = [ + path('admin/', admin.site.urls), + path('api/v1/', include('sumasen_easyocr.urls'), name='ocr') +] diff --git a/ocr/wsgi.py b/ocr/wsgi.py new file mode 100644 index 0000000..8ae95f7 --- /dev/null +++ b/ocr/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for ocr project. + +It exposes the WSGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/3.2/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ocr.settings') + +application = get_wsgi_application() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..c765219 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,29 @@ +asgiref==3.4.1 +cycler==0.11.0 +Django==3.2.9 +easyocr==1.4.1 +fonttools==4.28.2 +imageio==2.9.0 +kiwisolver==1.3.2 +matplotlib==3.5.0 +networkx==2.6.3 +numpy==1.21.4 +opencv-python-headless==4.5.4.60 +packaging==21.3 +Pillow==8.2.0 +pyparsing==3.0.6 +python-bidi==0.4.2 +python-dateutil==2.8.2 +pytz==2021.3 +PyWavelets==1.2.0 +PyYAML==6.0 +scikit-image==0.18.3 +scipy==1.7.3 +setuptools-scm==6.3.2 +six==1.16.0 +sqlparse==0.4.2 +tifffile==2021.11.2 +tomli==1.2.2 +torch==1.10.0 +torchvision==0.11.1 +typing_extensions==4.0.1 diff --git a/sumasen_easyocr/__init__.py b/sumasen_easyocr/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/sumasen_easyocr/__pycache__/__init__.cpython-39.pyc b/sumasen_easyocr/__pycache__/__init__.cpython-39.pyc new file mode 100644 index 0000000..c49c43f Binary files /dev/null and b/sumasen_easyocr/__pycache__/__init__.cpython-39.pyc differ diff --git a/sumasen_easyocr/__pycache__/admin.cpython-39.pyc b/sumasen_easyocr/__pycache__/admin.cpython-39.pyc new file mode 100644 index 0000000..2232900 Binary files /dev/null and b/sumasen_easyocr/__pycache__/admin.cpython-39.pyc differ diff --git a/sumasen_easyocr/__pycache__/apps.cpython-39.pyc b/sumasen_easyocr/__pycache__/apps.cpython-39.pyc new file mode 100644 index 0000000..cea0472 Binary files /dev/null and b/sumasen_easyocr/__pycache__/apps.cpython-39.pyc differ diff --git a/sumasen_easyocr/__pycache__/models.cpython-39.pyc b/sumasen_easyocr/__pycache__/models.cpython-39.pyc new file mode 100644 index 0000000..21345b4 Binary files /dev/null and b/sumasen_easyocr/__pycache__/models.cpython-39.pyc differ diff --git a/sumasen_easyocr/__pycache__/urls.cpython-39.pyc b/sumasen_easyocr/__pycache__/urls.cpython-39.pyc new file mode 100644 index 0000000..92b35ba Binary files /dev/null and b/sumasen_easyocr/__pycache__/urls.cpython-39.pyc differ diff --git a/sumasen_easyocr/__pycache__/views.cpython-39.pyc b/sumasen_easyocr/__pycache__/views.cpython-39.pyc new file mode 100644 index 0000000..16b97cf Binary files /dev/null and b/sumasen_easyocr/__pycache__/views.cpython-39.pyc differ diff --git a/sumasen_easyocr/admin.py b/sumasen_easyocr/admin.py new file mode 100644 index 0000000..926b7ef --- /dev/null +++ b/sumasen_easyocr/admin.py @@ -0,0 +1,4 @@ +from django.contrib import admin +from .models import Document + +admin.site.register(Document) diff --git a/sumasen_easyocr/apps.py b/sumasen_easyocr/apps.py new file mode 100644 index 0000000..d385c03 --- /dev/null +++ b/sumasen_easyocr/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class SumasenEasyocrConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'sumasen_easyocr' diff --git a/sumasen_easyocr/migrations/0001_initial.py b/sumasen_easyocr/migrations/0001_initial.py new file mode 100644 index 0000000..37f4f72 --- /dev/null +++ b/sumasen_easyocr/migrations/0001_initial.py @@ -0,0 +1,31 @@ +# Generated by Django 3.2.9 on 2021-12-02 10:05 + +import datetime +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.CreateModel( + name='Document', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=512, verbose_name='name')), + ('file', models.FileField(upload_to='%y/%m/%d')), + ('uploaded_date', models.DateField(default=datetime.date.today)), + ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ], + options={ + 'verbose_name_plural': 'Documents', + }, + ), + ] diff --git a/sumasen_easyocr/migrations/__init__.py b/sumasen_easyocr/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/sumasen_easyocr/migrations/__pycache__/0001_initial.cpython-39.pyc b/sumasen_easyocr/migrations/__pycache__/0001_initial.cpython-39.pyc new file mode 100644 index 0000000..96d128a Binary files /dev/null and b/sumasen_easyocr/migrations/__pycache__/0001_initial.cpython-39.pyc differ diff --git a/sumasen_easyocr/migrations/__pycache__/__init__.cpython-39.pyc b/sumasen_easyocr/migrations/__pycache__/__init__.cpython-39.pyc new file mode 100644 index 0000000..f1778e1 Binary files /dev/null and b/sumasen_easyocr/migrations/__pycache__/__init__.cpython-39.pyc differ diff --git a/sumasen_easyocr/models.py b/sumasen_easyocr/models.py new file mode 100644 index 0000000..f204def --- /dev/null +++ b/sumasen_easyocr/models.py @@ -0,0 +1,51 @@ +from django.db import models +from django.db.models.deletion import CASCADE +from django.utils.translation import gettext_lazy as _ +from django.contrib.auth.models import User +import datetime +import os +from django.db.models.signals import post_save, post_delete +from django.dispatch import receiver +import easyocr + + +class Document(models.Model): + name=models.CharField(_('name'), max_length=512) + user = models.ForeignKey(User, on_delete=CASCADE) + file = models.FileField(upload_to='%y/%m/%d', blank=False) + uploaded_date = models.DateField(default=datetime.date.today) + + class Meta: + verbose_name_plural = "Documents" + + def __str__(self): + return self.name + + +@receiver(post_save, sender=Document) +def publish_date(sender, instance, created, **kwargs): + file = instance.file.path + file_format = os.path.basename(file).split('.')[-1] + file_name = os.path.basename(file).split('.')[0] + file_path = os.path.dirname(file) + name = instance.name + + #os.remove(file) + + try: + reader = easyocr.Reader(['ja','en']) + result = reader.readtext(file) + print('@@@@@@@@@@') + print(result) + print('@@@@@@@@@@') + + except Exception as e: + pass + + + +@receiver(post_delete, sender=Document) +def delete_layer(sender, instance, **kwargs): + if instance.file: + if os.path.isfile(instance.file.path): + os.remove(instance.file.path) \ No newline at end of file diff --git a/sumasen_easyocr/tests.py b/sumasen_easyocr/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/sumasen_easyocr/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/sumasen_easyocr/uploaded/jp22.jpg b/sumasen_easyocr/uploaded/jp22.jpg new file mode 100644 index 0000000..1a765f6 Binary files /dev/null and b/sumasen_easyocr/uploaded/jp22.jpg differ diff --git a/sumasen_easyocr/urls.py b/sumasen_easyocr/urls.py new file mode 100644 index 0000000..b14f28e --- /dev/null +++ b/sumasen_easyocr/urls.py @@ -0,0 +1,7 @@ +from django.contrib import admin +from django.urls import path, re_path +from .views import FileUploadView + +urlpatterns = [ + re_path(r'^upload/', FileUploadView.as_view()) +] diff --git a/sumasen_easyocr/views.py b/sumasen_easyocr/views.py new file mode 100644 index 0000000..30e8406 --- /dev/null +++ b/sumasen_easyocr/views.py @@ -0,0 +1,28 @@ +from rest_framework.views import APIView +from rest_framework.parsers import MultiPartParser +from rest_framework.response import Response +from rest_framework import status +import easyocr + +class FileUploadView(APIView): + parser_classes = [MultiPartParser,] + + def post(self, request, format='jpg'): + up_file = request.FILES['file'] + destination = open('/Users/mohamednouffer/workspace/akira_san/sumasen_ocr/sumasen_easyocr/uploaded/' + up_file.name, 'wb+') + for chunk in up_file.chunks(): + destination.write(chunk) + destination.close() + + try: + reader = easyocr.Reader(['ja','en']) + result = reader.readtext('/Users/mohamednouffer/workspace/akira_san/sumasen_ocr/sumasen_easyocr/uploaded/' + up_file.name) + #print('@@@@@@@@@@') + #print(result) + #print('@@@@@@@@@@') + return Response(result, status.HTTP_201_CREATED) + #return Response({''}, status.HTTP_201_CREATED) + + except Exception as e: + print(e) + return Response({'Error': "Error occured"}, status.HTTP_201_CREATED) \ No newline at end of file