File size: 3,116 Bytes
619b3b4
12a9b20
 
 
 
 
 
 
 
 
 
 
619b3b4
12a9b20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import gradio as gr
from keras.models import load_model, Sequential
from PIL import Image
from numpy import asarray
import glob
import os
import requests
import cv2
from bs4 import BeautifulSoup
from bs4.element import SoupStrainer
import bs4

model = load_model("./dl.h5")
class_names = ['bacterial-leaf-spot', 'early-blight', 'late-blight', 'mosaic-virus', 'powdery-mildew']

def predict_image(img):
  img_4d=img.reshape((-1,180,180,3))
  prediction=model.predict(img_4d)[0]
  return {class_names[i]: float(prediction[i]) for i in range(5)}


def listToString(s):
    str1 = ""
    for ele in s:
        str1 = " "+ele + " " + str1+ " " 
    return str1

def cause(img):
  te = predict_image(img)
  res = max(te, key=te.get)

  #TREATMENT
  page=requests.get(f"https://www.planetnatural.com/pest-problem-solver/plant-disease/{res}/")
  url=f"https://www.planetnatural.com/pest-problem-solver/plant-disease/{res}/"

  src = page.content
  soup=BeautifulSoup(src, "lxml")
  details=[url,]
  for elem in soup.find_all("div",{'class':'post-entry'}):
      ol = elem.find('ol')
      if ol:
        treat=ol.text
        details.append(treat)

  #SUGGESTIONS
  url2 = 'https://google.com/search?q=treatment+' + res
  request_result=requests.get(url2)
  soup = bs4.BeautifulSoup(request_result.text,
                          "html.parser")
  links = soup.find_all("a")
  suggestions=[]
  for link in links:
      link_href = link.get('href')
      if "url?q=" in link_href and not "webcache" in link_href:
        title = link.find_all('h3', limit=3)
        if len(title)> 0:
              #suggestions.append(title[0].getText())
              #suggestions.append(link.get('href').split("?q=")[1].split("&sa=U")[0])
              suggestions.append(title[0].getText()+" "+link.get('href').split("?q=")[1].split("&sa=U")[0])

              
  if res == "powdery-mildew":
          cause = "The causes of POWDERY MILDEW are high humidity."
          sug=listToString(suggestions)
          return(cause,treat,sug)
      
  elif res == "early-blight":
          cause = "the cause is fungus Alternaria solani which survives in plant debris or on infected plants."
          print(cause)
  elif res == "bacterial-leaf-spot":
          print("the causes of", res,
                " is Xanthomonas campestris pv. vesicatoria. It is a gram-negative, rod-shaped bacterium that can survive in seeds and plant debris from one season to another")
  elif res == "late-blight":
          cause = "the cause is fungus Phytophthora infestans."
  elif res == "Leaf_mold":
          cause = "the cause is the fungus Passalora fulva."
          # print("the cause of",res,"is the fungus Passalora fulva.")
  elif res == "mosaic-virus":
          cause = "the cause is that aphid activity in fields is high"
  else:
          text = "Undetected disease"
          cause = "unknown"
          treat = "unknown"
          print (text, cause, treat)


image = gr.inputs.Image(shape=(180 , 180))
label = gr.outputs.Textbox()

gr.Interface(fn=cause, inputs=image, outputs=['text','text','text'],interpretation='default').launch(debug='True')