DurAIn_ni / app.py
Tanaanan's picture
Update app.py
ed35056
import streamlit as st #Web App
from PIL import Image, ImageOps #Image Processing
import time
import numpy as np
from icevision.all import *
from icevision.models.checkpoint import *
st.sidebar.image("./durian_logo.png")
st.sidebar.header("DurAIn-ni Webapp." + "\n" + "ระบบวิเคราะห์อาการป่วยของต้นทุเรียนด้วย AI")
def load_image(image_file):
img = Image.open(image_file)
return img
activities = ["Detection (วิเคราะห์โรค)", "About (เกี่ยวกับ)"]
choice = st.sidebar.selectbox("Select option.. (เลือกโหมด)",activities)
#set default size as image_scale x image_scale
def img_resize(input_path,img_size): # padding size
desired_size = img_size
im = Image.open(input_path)
im = ImageOps.exif_transpose(im) # fix image rotating
width, height = im.size # get img_input size
if (width == image_scale) and (height == image_scale):
new_im = im
else:
#im = im.convert('L') #Convert to gray
old_size = im.size # old_size[0] is in (width, height) format
ratio = float(desired_size)/max(old_size)
new_size = tuple([int(x*ratio) for x in old_size])
im = im.resize(new_size, Image.ANTIALIAS)
new_im = Image.new("RGB", (desired_size, desired_size))
new_im.paste(im, ((desired_size-new_size[0])//2,
(desired_size-new_size[1])//2))
return new_im
checkpoint_path = "./Leaf_disease_rcnn.pth"
checkpoint_and_model = model_from_checkpoint(checkpoint_path,
model_name='mmdet.faster_rcnn',
backbone_name='resnet101_fpn_2x',
img_size=768,
is_coco=False)
model_type = checkpoint_and_model["model_type"]
backbone = checkpoint_and_model["backbone"]
class_map = checkpoint_and_model["class_map"]
img_size = checkpoint_and_model["img_size"]
#model_type, backbone, class_map, img_size
model = checkpoint_and_model["model"]
device=next(model.parameters()).device
img_size = checkpoint_and_model["img_size"]
valid_tfms = tfms.A.Adapter([*tfms.A.resize_and_pad(img_size), tfms.A.Normalize()])
image_scale = 768 # change image input / ouput size here
def get_accuracy_str(raw_list):
label_found = raw_list
get_label = []
label = []
for i in range(len(label_found)):
label.append(label_found[i][0])
label = list(set(label)) # save a only 1 found label
#print(label)
current_label = ""
count = 0
sum_result = []
for j in range(len(label_found)): # get only 1 labels and most accuracy in result_list
for k in range(len(label)):
#print(label_found[j], label[k])
if (label_found[j][0] == label[k]):
if current_label != label[k]:
current_label = label[k]
count += 1
if count <= len(label):
sum_result.append(label_found[j])#, label[k])
if len(sum_result) > 0:
for each_label in sum_result: # print detection result
labels = each_label[0].lower()
acc = round((each_label[1]*100), 2)
if labels == "powdery mildew":
labels = "โรคราแป้ง"
recommend = "None"
treatment = "- หลังจากเก็บเกี่ยวผลผลิตแล้ว ให้ทำลายเศษซากพืชที่เคยเป็นโรคโดยไถกลบ และปลูกพืชหมุนเวียน" + "\n" + "- ฉีดพ่นสารป้องกันกำจัดเชื้อรา เช่น ไตรอะดิมีฟอน (triadimefon) ไมโคลบิวทานิล (myclobutanil) โพรพิโคนาโซล (propiconazole) อะซอกชีสโตรบิน (azoxystrobin)"
elif labels == "n_loss":
labels = "อาการขาดธาตุ ไนโตรเจน"
recommend = "None"
treatment = "- ผสมปุ๋ยทางดิน : ผสมปุ๋ย NPK ที่มีอัตราส่วนของค่า N มากที่สุด และสังเกตปริมาณการใช้ตามอาการของใบ" + "\n" "- ผสมปุ๋ยทางใบ : ใช้ปุ๋ยเคมีที่มีค่า N สูงๆ หรือ ใช้ยูเรียน้ำ สูตรไนโตรเจนสูง ผสมแล้วพ้นปุ๋ยนํ้า"
elif labels == "blight":
labels = "โรคใบไหม้"
recommend = "- ตัดแต่งใบที่เป็นโรค รวมทั้งกำจัดวัชพืชบริเวณแปลงปลูก เพื่อลดแหล่งสะสมของเชื้อสาเหตุ"
treatment = "- ตัดกิ่งที่เป็นโรคออกเผาทำลาย (ถ้าเป็นกิ่งใหญ่ควรทาลอยปูนแดงหรือสารประกอบทองแดง) แล้วให้ฉีดพ่นด้วยสารเคมีคาร์เบ็นดาชิม (carbendazim) 60 % WP อัตรา 10 กรัมต่อน้ำ 20 ลิตร หรือสารเคมีคอปเปอร์ออกซีคลอไรด์ (copper oxychloride) 85 % WP อัตรา 50 กรัมต่อน้ำ 20 ลิตร ให้ทั่วทั้งภายใน และภายนอก"
elif labels == "spot":
labels = "โรคใบจุด"
recommend = "- ตัดแต่งกิ่งทุเรียนให้โปร่ง แสงแดดส่องได้ทั่วถึง" + "\n" "- เมื่อพบกิ่ง และใบเริ่มแสดงอาการของโรคเพียงเล็กน้อย ให้ตัด และรวบรวมเผาทำลาย รวมทั้งให้รวบรวมใบที่ร่วงหล่นอยู่เผาทิ้งด้วยเพื่อลดการสะสมเชื้อโรค และลดการระบาดในปีต่อไป "
treatment = "- ฉีดพ่นด้วยสารป้องกันกำจัดเชื้อรา เช่น โพรคลอราซ (prochloraz) แมนโคเซบ (mancozeb) ไดฟีโนโคนาโซล (difenoconazole) เป็นต้น"
st.success(f"มีโอกาสเป็น : {labels} {round(acc, 2)} % .")
if recommend != "None":
st.write(f"คำแนะนำในการรักษา : {labels}")
st.write(recommend)
st.write(f"วิธีการรักษา : {labels}")
st.write(treatment)
else:
st.warning("Warning : ไม่พบโรคจากใบทุเรียน !! ")
def get_detection(img_path):
detect_list = []
#Get_Idcard_detail(file_path=img_path)
img = Image.open(img_path)
img = ImageOps.exif_transpose(img) # fix image rotating
width, height = img.size # get img_input size
if (width == image_scale) and (height == image_scale):
pred_dict = model_type.end2end_detect(img, valid_tfms, model, class_map=class_map, detection_threshold=0.5)
else:
#im = im.convert('L') #Convert to gray
old_size = img.size # old_size[0] is in (width, height) format
ratio = float(image_scale)/max(old_size)
new_size = tuple([int(x*ratio) for x in old_size])
img = img.resize(new_size, Image.ANTIALIAS)
new_im = Image.new("RGB", (image_scale, image_scale))
new_im.paste(img, ((image_scale-new_size[0])//2,
(image_scale-new_size[1])//2))
pred_dict = model_type.end2end_detect(new_im, valid_tfms, model, class_map=class_map, detection_threshold=0.5)
for i,j in zip(pred_dict['detection']['labels'], pred_dict['detection']['scores']):
detect_list.append([i, j])
return get_accuracy_str(detect_list)
def get_img_detection(img_path):
#Get_Idcard_detail(file_path=img_path)
img = Image.open(img_path)
img = ImageOps.exif_transpose(img) # fix image rotating
width, height = img.size # get img_input size
if (width == image_scale) and (height == image_scale):
new_im = img
else:
#im = im.convert('L') #Convert to gray
old_size = img.size # old_size[0] is in (width, height) format
ratio = float(image_scale)/max(old_size)
new_size = tuple([int(x*ratio) for x in old_size])
img = img.resize(new_size, Image.ANTIALIAS)
new_im = Image.new("RGB", (image_scale, image_scale))
new_im.paste(img, ((image_scale-new_size[0])//2,
(image_scale-new_size[1])//2))
pred_dict = model_type.end2end_detect(new_im, valid_tfms, model, class_map=class_map, detection_threshold=0.5)
return pred_dict['img']
if choice =='About (เกี่ยวกับ)' :
st.header("About... (เกี่ยวกับ)")
st.subheader("DurAIn-ni คืออะไร ?")
st.write("- เป็นระบบที่วิเคราะห์โรคที่เกิดได้จากต้นทุเรียนผ่านใบ พร้อม แนะนำวิธีในการรักษาเบื้องต้น โดยใช้ ระบบ AI")
st.subheader("DurAIn-ni ทำอะไรได้บ้าง ?")
st.write("- วิเคราะห์โรคจากต้นทึเรียนโดยใช้ใบ (Object detection)")
st.write("- ให้คำแนะนำในการรักษาเบื้องต้น")
st.write("- โดยในปัจจุบันสามารถวิเคราะหได้ 5 โรค หลักๆ ได้แก่ " + "\n" + " 1. โรคราแป้ง (Powdery mildew disease)" + "\n" + " 2. โรคใบไหม้ (Leaf blight disease)" + "\n" + " 3. โรคใบจุด (Leaf spot disease)" + "\n" + " 4. อาการขาดธาตุ แมกนีเซียม (Magnesium deficiency)" + "\n" + " 5. อาการขาดธาตุ ไนโตรเจน (Nitrogen deficiency)")
st.subheader("ทำไมถึงต้องเลือกใช้ ADurAIn-ni ")
st.write("- สามารถใช้วินิจฉัยโรคที่เกิดจากต้นทุเรียนเบื้องต้นได้ด้วยตัวเองในทันที โดยไม่จำเป็นที่จะต้องมีผู้เชี่ยวชาญในการวิเคราะห์ตลอดเวลา พร้อมบอกรายละเอียดโรค และ วิธีการรักษาในเบื้องต้น ดังนั้น เกษตรกร และ คนทั่วไป สามารถวิเคราห์โรคที่เกิดจากต้นทุเรียน และ สามารถที่จะรักษาได้ได้โดยทันท่วงที ")
st.subheader("คำแนะนำในการใช้งาน")
st.write("- ภาพไม่ควรมีแสงที่สว่างมากเกืนไป และ มืดเกินไป มิฉะนั้นอาจทำให้การตรวจจับคลาดเคลื่อนเอาได้")#4
st.write("- ภาพไม่ควรที่จะอยู่ไกลเกินไป และ ควรมีความชัด มิฉะนั้นอาจทำให้การตรวจจับคลาดเคลื่อน หรือ ไม่สามารถตรวจจับได้")#5
st.subheader("รายละเอียดเพิ่มเติม")
st.write('[โรคพื้นฐานที่เกิดในต้นทุเรียน](https://kasetgo.com/t/topic/401483)')
st.write('[สถิติการส่งออกทุเรียนของไทย](http://impexp.oae.go.th/service/export.php?S_YEAR=2560&E_YEAR=2565&PRODUCT_GROUP=5252&PRODUCT_ID=4977&wf_search=&WF_SEARCH=Y)')
elif choice == "Detection (วิเคราะห์โรค)":
st.header("Detection (วิเคราะห์โรค)")
image = st.file_uploader(label = "Upload Durian leaf here.. (ใส่รูปภาพตรงนี้)",type=['png','jpg','jpeg'])
if image is not None:
st.write("## Detection result.. (สรุปผลการวิเคราะห์)")
new_img = img_resize(image, image_scale)
st.image(get_img_detection(image))
t1 = time.perf_counter()
get_detection(image)
t2 = time.perf_counter()
st.write('time taken to run: {:.2f} sec'.format(t2-t1))
else:
st.write("## Waiting for image.. (รอผู้ใช้งานใส่รูปภาพ)")
st.image('ania.png')
st.caption("Made by Tanaanan .M")
st.sidebar.markdown('---')
st.sidebar.subheader('More image for test..')
st.sidebar.write('[Github img test set.](https://github.com/Tanaanan/DurAIn-ni-/tree/main/testset_imgs)')
st.sidebar.subheader('Made by Tanaanan .M')
st.sidebar.write("Contact : mjsalyjoh@gmail.com")