Example #1
0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 22 13:54:19 2019

@author: jahirmedinacs
"""
from removebg import RemoveBg
import base64

rmbg = RemoveBg("YOUR-API-KEY", "error.log")
with open("Average.jpg", "rb") as image_file:
	encoded_string = base64.b64encode(image_file.read())
    rmbg.remove_background_from_base64_img(encoded_string)
Example #2
0
def index(request):
    if(request.method == 'GET'):
        if request.user.is_anonymous:
            context = {'title': 'Detection'}
            return render(request, 'index.html', context)
        else:
            history = HistImage.objects.all().filter(
                user=request.user).order_by('id').reverse()
            args = {}
            args['history'] = history
            args['title'] = 'Detection'
            return render(request, 'index.html', args)
    else:
        imag = request.FILES.get('pic')

        b64_image = base64.b64encode(imag.file.read()).decode('ascii')

        stringLength = 10
        letters = string.ascii_lowercase
        name = ''
        for i in range(stringLength):
            name = name + random.choice(letters)
        file_name = name + '.png'
        # rmbg = RemoveBg("qLr2rBHAaemsVF2YN5SbxKuq",
        #                 "error.log")  # nihar1kashyap7
        rmbg = RemoveBg("TSLJyBXT5SU6VLjwyvMXbQpq", "error.log")  # prachurjya
        # rmbg = RemoveBg("nhahVkAMQTPACG5Z8NiEuWxs", "error.log") #niharkashap17

        # I Put this part in try-catch because the api may fail if 50 requests over or image has no background
        try:
            rmbg.remove_background_from_base64_img(b64_image, file_name)
            noImgPath = os.path.join(settings.BASE_DIR, file_name)
            prediction = prediict(noImgPath)
            with open(noImgPath, "rb") as image_file:
                b64_image_new = base64.b64encode(
                    image_file.read()).decode('ascii')
        except:
            b64_image_new = b64_image
            prediction = prediict(imag)
            pass

        remedy = Remedies.objects.all().filter(disease=prediction)

        # These if else specify the previous detections section
        if request.user.is_anonymous:
            args = {}
            args['disease'] = prediction
            args['image'] = b64_image
            args['remedy'] = remedy
            args['title'] = 'Detection'
            args['image_2'] = b64_image_new
        else:
            history = HistImage(user=request.user, img=imag, result=prediction)
            history.save()
            history = HistImage.objects.all().filter(
                user=request.user).order_by('id').reverse()

            args = {}
            args['disease'] = prediction
            args['image'] = b64_image
            args['remedy'] = remedy
            args['history'] = history
            args['title'] = 'Detection'
            args['image_2'] = b64_image_new

        return render(request, 'index.html', args)