Feat : Result Category

This commit is contained in:
EdyAtthoillah123 2023-09-20 01:57:13 +07:00
parent d7ecb4bd15
commit b0fcf988e3
11 changed files with 120 additions and 20 deletions

View File

@ -124,3 +124,7 @@ MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field # https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
# settings.py
ALLOWED_HOSTS = ['192.168.128.97', 'localhost', '127.0.0.1']

View File

@ -0,0 +1,26 @@
# Generated by Django 4.2.4 on 2023-09-19 15:00
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Image',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('Nitrogen', models.FloatField()),
('Fosfor', models.FloatField()),
('Kalium', models.FloatField()),
('image', models.ImageField(upload_to='images/')),
('tanaman', models.CharField(max_length=100)),
('perbaikan', models.BooleanField(default=False)),
],
),
]

View File

@ -1,3 +1,14 @@
from django.db import models from django.db import models
# Create your models here. # Create your models here.
class Image(models.Model):
Nitrogen = models.FloatField()
Fosfor = models.FloatField()
Kalium = models.FloatField()
image = models.ImageField(upload_to='images/') # Kolom untuk gambar
tanaman = models.CharField(max_length=100)
perbaikan = models.BooleanField(default=False)
def __str__(self):
return f"Image - Tanaman: {self.tanaman}"

View File

@ -5,7 +5,8 @@ from django.conf.urls.static import static
urlpatterns = [ urlpatterns = [
# path("", views.home, name="home"), # path("", views.home, name="home"),
path("", views.lbp, name="grayscale"), # path("", , name="grayscale"),
path('upload_image/', views.upload_image, name='upload_image'),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) ]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

View File

@ -2,8 +2,9 @@ from django.shortcuts import render
import cv2 import cv2
import numpy as np import numpy as np
from matplotlib import pyplot as plt from matplotlib import pyplot as plt
from django.http import HttpResponse from django.http import JsonResponse
from scipy import stats from django.views.decorators.csrf import csrf_exempt
import os
def get_pixel(img, center, x, y): def get_pixel(img, center, x, y):
new_value = 0 new_value = 0
@ -51,7 +52,7 @@ def find_mode_pixel_value(img):
return mode_value return mode_value
def lbp(request): def lbp(request):
path = 'media/kebonangung 50cm.jpeg' path = 'media/lahan 4 50 cm.jpeg'
img_bgr = cv2.imread(path, 1) img_bgr = cv2.imread(path, 1)
img_gray = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY) img_gray = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY)
mode_pixel = find_mode_pixel_value(img_gray) mode_pixel = find_mode_pixel_value(img_gray)
@ -106,22 +107,79 @@ def lbp(request):
print("Sangat Tinggi") print("Sangat Tinggi")
else: else:
print("Lebih dari atau sama dengan 2") print("Lebih dari atau sama dengan 2")
# Hitung histogram LBP yang telah dinormalisasi
lbp_histogram = calculate_normalized_lbp_histogram(img_gray) lbp_histogram = calculate_normalized_lbp_histogram(img_gray)
print("Program LBP selesai")
lbp(None)
# Cetak nilai-nilai LBP yang telah dinormalisasi
# print("Nilai-nilai LBP yang telah dinormalisasi:")
# for i, nilai in enumerate(lbp_histogram):
# print(f'LBP Nilai {i}: {nilai}')
# Plot histogram LBP yang telah dinormalisasi @csrf_exempt
# plt.bar(range(len(lbp_histogram)), lbp_histogram) def upload_image(request):
# plt.xlabel('Nilai LBP') if request.method == 'POST' and request.FILES['image']:
# plt.ylabel('Frekuensi yang Dinormalisasi') uploaded_image = request.FILES['image']
# plt.title('Histogram LBP yang Dinormalisasi') image_path = os.path.join('media', 'uploaded_images', uploaded_image.name)
# plt.show()
with open(image_path, 'wb+') as destination:
for chunk in uploaded_image.chunks():
destination.write(chunk)
path = image_path
img_bgr = cv2.imread(path, 1)
img_gray = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY)
mode_pixel = find_mode_pixel_value(img_gray)
print(f"Nilai pixel yang paling sering muncul: {mode_pixel}")
normalized_mode_pixel = mode_pixel / 255.0
print(f"Nilai yang sering muncul yang telah dinormalisasi: {normalized_mode_pixel}")
hasil_operasi_Natrium = (normalized_mode_pixel * 0.1928 + 0.021)
print(f"hasil Nilai N (Natrium): {hasil_operasi_Natrium}")
N = hasil_operasi_Natrium
if N < 1:
print("Sangat rendah")
elif N >= 1 and N < 2:
print("Rendah")
elif N >= 2.001 and N < 3:
print("Sedang")
elif N >= 3.001 and N < 5:
print("Tinggi")
elif N >= 5.001:
print("Sangat Tinggi")
else:
print("Ketegori Tidak Ditemukan")
hasil_operasi_fosfor = (normalized_mode_pixel * -10.725) + 16.533
print(f"hasil Nilai P (Fosfor): {hasil_operasi_fosfor}")
P = hasil_operasi_fosfor
if P < 10:
print("Sangat rendah")
elif P >= 10 and P <= 25:
print("Rendah")
elif P >= 26 and P <= 45:
print("Sedang")
elif P >= 46 and P <= 60:
print("Tinggi")
elif P > 60:
print("Sangat Tinggi")
else:
print("Lebih dari atau sama dengan 2")
hasil_operasi_Kalium = (normalized_mode_pixel * -0.1864 + 0.2471)
print(f"hasil Nilai K (Kalium): {hasil_operasi_Kalium}")
K = hasil_operasi_Kalium
if K < 0.1:
print("Sangat rendah")
elif K >= 0.1 and K <= 0.3:
print("Rendah")
elif K >= 0.4 and K <= 0.5:
print("Sedang")
elif K >= 0.6 and K <= 1.0:
print("Tinggi")
elif K > 1.0:
print("Sangat Tinggi")
else:
print("Lebih dari atau sama dengan 2")
lbp_histogram = calculate_normalized_lbp_histogram(img_gray)
print("Program LBP selesai") print("Program LBP selesai")
lbp(None) return JsonResponse({'message': 'Gambar berhasil diunggah dan diproses'})
else:
return JsonResponse({'message': 'Permintaan tidak valid'})

Binary file not shown.