import cv2 import numpy as np import argparse, sys, os from GUIdriver import * import pandas as pd def endprogram(): print ("\nProgram terminated!") sys.exit() #Reading the image by parsing the argument text = str(ImageFile[0]) print ("\n*********************\nImage : " + text + "\n*********************") img = cv2.imread(text) img = cv2.resize(img ,((int)(img.shape[1]/5),(int)(img.shape[0]/5))) original = img.copy() neworiginal = img.copy() cv2.imshow('original',img) #Calculating number of pixels with shade of white(p) to check if exclusion of these pixels is required or not (if more than a fixed %) in order to differentiate the white background or white patches in image caused by flash, if present. p = 0 for i in range(img.shape[0]): for j in range(img.shape[1]): B = img[i][j][0] G = img[i][j][1] R = img[i][j][2] if (B > 110 and G > 110 and R > 110): p += 1 #finding the % of pixels in shade of white totalpixels = img.shape[0]*img.shape[1] per_white = 100 * p/totalpixels ''' print 'percantage of white: ' + str(per_white) + '\n' print 'total: ' + str(totalpixels) + '\n' print 'white: ' + str(p) + '\n' ''' #excluding all the pixels with colour close to white if they are more than 10% in the image if per_white > 10: img[i][j] = [200,200,200] cv2.imshow('color change', img) #Guassian blur blur1 = cv2.GaussianBlur(img,(3,3),1) #mean-shift algo newimg = np.zeros((img.shape[0], img.shape[1],3),np.uint8) criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER , 10 ,1.0) img = cv2.pyrMeanShiftFiltering(blur1, 20, 30, newimg, 0, criteria) cv2.imshow('means shift image',img) #Guassian blur blur = cv2.GaussianBlur(img,(11,11),1) #Canny-edge detection canny = cv2.Canny(blur, 160, 290) canny = cv2.cvtColor(canny,cv2.COLOR_GRAY2BGR) #creating border around image to close any open curve cut by the image border #bordered = cv2.copyMakeBorder(canny,10,10,10,10, cv2.BORDER_CONSTANT, (255,255,255)) #function not working(not making white coloured border) #bordered = cv2.rectangle(canny,(-2,-2),(275,183),(255,255,255),3) #cv2.imshow('Canny on meanshift bordered image',bordered) #contour to find leafs bordered = cv2.cvtColor(canny,cv2.COLOR_BGR2GRAY) _,contours,hierarchy = cv2.findContours(bordered, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) maxC = 0 for x in range(len(contours)): #if take max or one less than max then will not work in if len(contours[x]) > maxC: # pictures with zoomed leaf images maxC = len(contours[x]) maxid = x perimeter = cv2.arcLength(contours[maxid],True) #print perimeter Tarea = cv2.contourArea(contours[maxid]) cv2.drawContours(neworiginal,contours[maxid],-1,(0,0,255)) cv2.imshow('Contour',neworiginal) #cv2.imwrite('Contour complete leaf.jpg',neworiginal) #Creating rectangular roi around contour height, width, _ = canny.shape min_x, min_y = width, height max_x = max_y = 0 frame = canny.copy() # computes the bounding box for the contour, and draws it on the frame, for contour, hier in zip(contours, hierarchy): (x,y,w,h) = cv2.boundingRect(contours[maxid]) min_x, max_x = min(x, min_x), max(x+w, max_x) min_y, max_y = min(y, min_y), max(y+h, max_y) if w > 80 and h > 80: #cv2.rectangle(frame, (x,y), (x+w,y+h), (255, 0, 0), 2) #we do not draw the rectangle as it interferes with contour later on roi = img[y:y+h , x:x+w] originalroi = original[y:y+h , x:x+w] if (max_x - min_x > 0 and max_y - min_y > 0): roi = img[min_y:max_y , min_x:max_x] originalroi = original[min_y:max_y , min_x:max_x] #cv2.rectangle(frame, (min_x, min_y), (max_x, max_y), (255, 0, 0), 2) #we do not draw the rectangle as it interferes with contour cv2.imshow('ROI', frame) cv2.imshow('rectangle ROI', roi) img = roi #Changing colour-space #imghsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) imghls = cv2.cvtColor(roi, cv2.COLOR_BGR2HLS) cv2.imshow('HLS', imghls) imghls[np.where((imghls==[30,200,2]).all(axis=2))] = [0,200,0] cv2.imshow('new HLS', imghls) #Only hue channel huehls = imghls[:,:,0] cv2.imshow('img_hue hls',huehls) #ret, huehls = cv2.threshold(huehls,2,255,cv2.THRESH_BINARY) huehls[np.where(huehls==[0])] = [35] cv2.imshow('img_hue with my mask',huehls) #Thresholding on hue image ret, thresh = cv2.threshold(huehls,28,255,cv2.THRESH_BINARY_INV) cv2.imshow('thresh', thresh) #Masking thresholded image from original image mask = cv2.bitwise_and(originalroi,originalroi,mask = thresh) cv2.imshow('masked out img',mask) #Finding contours for all infected regions _,contours,heirarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) Infarea = 0 for x in range(len(contours)): cv2.drawContours(originalroi,contours[x],-1,(0,0,255)) cv2.imshow('Contour masked',originalroi) #Calculating area of infected region Infarea += cv2.contourArea(contours[x]) if Infarea > Tarea: Tarea = img.shape[0]*img.shape[1] print ('_________________________________________\n Perimeter: %.2f' %(perimeter) + '\n_________________________________________') print ('_________________________________________\n Total area: %.2f' %(Tarea) + '\n_________________________________________') #Finding the percentage of infection in the leaf print ('_________________________________________\n Infected area: %.2f' %(Infarea) + '\n_________________________________________') try: per = 100 * Infarea/Tarea except ZeroDivisionError: per = 0 print ('_________________________________________\n Percentage of infection region: %.2f' %(per) + '\n_________________________________________') print("\n*To terminate press and hold (q)*") cv2.imshow('orig',original) """****************************************update dataset*******************************************""" #Updating a dataset file to maintain log of the leaf images identified. print("\nDo you want to run the classifier(Y/N):") n = cv2.waitKey(0) & 0xFF if n==ord('q'or'Q'): endprogram() #import csv file library import csv directory = 'D:/8semproject/Automatic-leaf-infection-identifier-master' filename = directory+'/Datasetunlabelledlog.csv' imgid = "/".join(text.split('/')[-2:]) while True: if n==ord('y'or'Y'): fieldnames = ['fold num', 'imgid', 'feature1', 'feature2', 'feature3'] print ('Appending to ' + str(filename)+ '...') try: log = pd.read_csv(filename) logfn = int(log.tail(1)['fold num']) foldnum = (logfn+1)%10 L = [str(foldnum), imgid, str(Tarea), str(Infarea), str(perimeter)] my_df = pd.DataFrame([L]) my_df.to_csv(filename, mode='a', index=False, header=False) print ('\nFile ' + str(filename)+ ' updated!' ) except IOError: if directory not in os.listdir(): os.system('mkdir ' + directory) foldnum = 0 L = [str(foldnum), imgid, str(Tarea), str(Infarea), str(perimeter)] my_df = pd.DataFrame([fieldnames, L]) my_df.to_csv(filename, index=False, header=False) print ('\nFile ' + str(filename)+ ' updated!' ) finally: import classifier endprogram() elif n == ord('n' or 'N') : print ('File not updated! \nSuccessfully terminated!') break else: print ('invalid input!') break
Write, Run & Share Python code online using OneCompiler's Python online compiler for free. It's one of the robust, feature-rich online compilers for python language, supporting both the versions which are Python 3 and Python 2.7. Getting started with the OneCompiler's Python editor is easy and fast. The editor shows sample boilerplate code when you choose language as Python or Python2 and start coding.
OneCompiler's python online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample python program which takes name as input and print your name with hello.
import sys
name = sys.stdin.readline()
print("Hello "+ name)
Python is a very popular general-purpose programming language which was created by Guido van Rossum, and released in 1991. It is very popular for web development and you can build almost anything like mobile apps, web apps, tools, data analytics, machine learning etc. It is designed to be simple and easy like english language. It's is highly productive and efficient making it a very popular language.
When ever you want to perform a set of operations based on a condition IF-ELSE is used.
if conditional-expression
#code
elif conditional-expression
#code
else:
#code
Indentation is very important in Python, make sure the indentation is followed correctly
For loop is used to iterate over arrays(list, tuple, set, dictionary) or strings.
mylist=("Iphone","Pixel","Samsung")
for i in mylist:
print(i)
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while condition
#code
There are four types of collections in Python.
List is a collection which is ordered and can be changed. Lists are specified in square brackets.
mylist=["iPhone","Pixel","Samsung"]
print(mylist)
Tuple is a collection which is ordered and can not be changed. Tuples are specified in round brackets.
myTuple=("iPhone","Pixel","Samsung")
print(myTuple)
Below throws an error if you assign another value to tuple again.
myTuple=("iPhone","Pixel","Samsung")
print(myTuple)
myTuple[1]="onePlus"
print(myTuple)
Set is a collection which is unordered and unindexed. Sets are specified in curly brackets.
myset = {"iPhone","Pixel","Samsung"}
print(myset)
Dictionary is a collection of key value pairs which is unordered, can be changed, and indexed. They are written in curly brackets with key - value pairs.
mydict = {
"brand" :"iPhone",
"model": "iPhone 11"
}
print(mydict)
Following are the libraries supported by OneCompiler's Python compiler
Name | Description |
---|---|
NumPy | NumPy python library helps users to work on arrays with ease |
SciPy | SciPy is a scientific computation library which depends on NumPy for convenient and fast N-dimensional array manipulation |
SKLearn/Scikit-learn | Scikit-learn or Scikit-learn is the most useful library for machine learning in Python |
Pandas | Pandas is the most efficient Python library for data manipulation and analysis |
DOcplex | DOcplex is IBM Decision Optimization CPLEX Modeling for Python, is a library composed of Mathematical Programming Modeling and Constraint Programming Modeling |