🏋️ 6. Administrator mode

Contents

Ch6 Admin & Optimization

Video Content

Ch6 OpenCV+WebApp - Admin & Optimization

Create Superuser

Admin setup

Go to "opencv_webapp/admin.py"

from .models import ImageUploadModel

class upload_image_Admin(admin.ModelAdmin):
    list_display = ('description', 'document')

admin.site.register(ImageUploadModel, upload_image_Admin)

Create superuser

(envpy3) $ python manage.py createsuperuser
Username: admin
Email address: admin@admin.com
Password:
Password (again):
Superuser created successfully.

Upgrade Face Detection Code and Data Delete

Image resize optimization

Go to "opencv_webapp/opencv_dface.py"

...
factor = 1
if img.shape[1] > 640:
    factor = 640.0 / img.shape[1]
elif img.shape[0] > 480:
    factor = 480.0 / img.shape[0]

if factor != 1:
    w = img.shape[1] * factor
    h = img.shape[0] * factor
    img = cv2.resize(img, (int(w), int(h)))
...

Automatic data cleanup

Go to "opencv_webapp/view.py"

from .models import ImageUploadModel
...
# db delete
if ImageUploadModel.objects.all().count() > 100:
    obs = ImageUploadModel.objects.all().first()
    if obs:
        obs.delete()
...

File deletion on model delete

Go to "opencv_webapp/models.py"

# Receive the pre_delete signal and delete the file associated with the model instance.
from django.db.models.signals import pre_delete
from django.dispatch.dispatcher import receiver

@receiver(pre_delete, sender=ImageUploadModel)
def mymodel_delete(sender, instance, **kwargs):
    # Pass false so FileField doesn't save the model.
    instance.document.delete(False)

🙇🏻 Thank you!

🎁 Source code, Material(pdf) and example images 👇

https://www.marearts.com/products/opencv-django-webapp-lecture-materials

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.