testingcap commited on
Commit
07e8a7a
·
1 Parent(s): 9e2c5fe

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +110 -0
app.py ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from keras.models import load_model, Sequential
2
+ from PIL import Image
3
+ from numpy import asarray
4
+ import glob
5
+ import os
6
+ import requests
7
+ import cv2
8
+ from bs4 import BeautifulSoup
9
+ from bs4.element import SoupStrainer
10
+ import bs4
11
+ import gradio as gr
12
+
13
+ #path='/content/Data'
14
+ #files = os.listdir(path)
15
+ #paths = [os.path.join(path, basename) for basename in files]
16
+ #print(max(paths, key=os.path.getctime))
17
+ #photo=max(paths, key=os.path.getctime)
18
+ #img = Image.open(photo)
19
+ #numpydata = asarray(img)
20
+
21
+ #model = load_model("./dl.h5")
22
+ model = load_model("dl.h5")
23
+ class_names = ['bacterial-leaf-spot', 'early-blight', 'late-blight', 'mosaic-virus', 'powdery-mildew']
24
+
25
+ def predict_image(img):
26
+ #img = cv2.resize(numpydata , (180 , 180))
27
+ #print(img.shape)
28
+ img_4d=img.reshape((-1,180,180,3))
29
+ prediction=model.predict(img_4d)[0]
30
+ return {class_names[i]: float(prediction[i]) for i in range(5)}
31
+
32
+
33
+ def listToString(s):
34
+ # initialize an empty string
35
+ str1 = ""
36
+ # traverse in the string
37
+ for ele in s:
38
+ str1 = " "+ele + " " + str1+ " "
39
+
40
+ # return string
41
+ return str1
42
+
43
+ def cause(img):
44
+ te = predict_image(img)
45
+ res = max(te, key=te.get)
46
+
47
+ #TREATMENT
48
+ page=requests.get(f"https://www.planetnatural.com/pest-problem-solver/plant-disease/{res}/")
49
+ url=f"https://www.planetnatural.com/pest-problem-solver/plant-disease/{res}/"
50
+
51
+ src = page.content
52
+ soup=BeautifulSoup(src, "lxml")
53
+ details=[url,]
54
+ #details=[]
55
+ for elem in soup.find_all("div",{'class':'post-entry'}):
56
+ ol = elem.find('ol')
57
+ if ol:
58
+ treat=ol.text
59
+ details.append(treat)
60
+
61
+ #SUGGESTIONS
62
+ url2 = 'https://google.com/search?q=treatment+' + res
63
+ request_result=requests.get(url2)
64
+ soup = bs4.BeautifulSoup(request_result.text,
65
+ "html.parser")
66
+ links = soup.find_all("a")
67
+ suggestions=[]
68
+ for link in links:
69
+ link_href = link.get('href')
70
+ if "url?q=" in link_href and not "webcache" in link_href:
71
+ title = link.find_all('h3', limit=3)
72
+ if len(title)> 0:
73
+ #suggestions.append(title[0].getText())
74
+ #suggestions.append(link.get('href').split("?q=")[1].split("&sa=U")[0])
75
+ suggestions.append(title[0].getText()+" "+link.get('href').split("?q=")[1].split("&sa=U")[0])
76
+
77
+
78
+ if res == "powdery-mildew":
79
+ cause = "The causes are high humidity."
80
+ #print (cause)
81
+ #print("the treatment:", *details, sep = "\n")
82
+ #print("the suggestions:", *suggestions, sep = "\n")
83
+
84
+ #return (cause, *details, *suggestions)
85
+ sug=listToString(suggestions)
86
+ return(cause,treat,sug)
87
+ elif res == "early-blight":
88
+ cause = "the cause is fungus Alternaria solani which survives in plant debris or on infected plants."
89
+ print(cause)
90
+ elif res == "bacterial-leaf-spot":
91
+ print("the causes of", res,
92
+ " 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")
93
+ elif res == "late-blight":
94
+ cause = "the cause is fungus Phytophthora infestans."
95
+ elif res == "Leaf_mold":
96
+ cause = "the cause is the fungus Passalora fulva."
97
+ # print("the cause of",res,"is the fungus Passalora fulva.")
98
+ elif res == "mosaic-virus":
99
+ cause = "the cause is that aphid activity in fields is high"
100
+ else:
101
+ text = "Undetected disease"
102
+ cause = "unknown"
103
+ treat = "unknown"
104
+ print (text, cause, treat)
105
+
106
+
107
+ image = gr.inputs.Image(shape=(180 , 180))
108
+ label = gr.outputs.Textbox()
109
+
110
+ gr.Interface(fn=cause, inputs=image, outputs=['text','text','text'],interpretation='default').launch(debug='True')