├── DETECTION.py ├── README.md ├── buzz.mp3 ├── index2.jpg ├── index4.jpg └── outputscreenshot.png /DETECTION.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import pygame 4 | import cv2.cv as cv 5 | import time 6 | #import smtplib 7 | from matplotlib import pyplot as plt 8 | 9 | 10 | im = cv2.imread('index4.jpg') 11 | # CODE TO CONVERT TO GRAYSCALE 12 | 13 | 14 | gray1 = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY) 15 | # save the image 16 | cv2.imwrite('graypothholeresult.jpg', gray1) 17 | #CONTOUR DETECTION CODE 18 | imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY) 19 | ret,thresh = cv2.threshold(imgray,127,255,0) 20 | 21 | contours1, _ = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE) 22 | contours2, _ = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) 23 | 24 | #img1 = im.copy() 25 | img2 = im.copy() 26 | 27 | #out = cv2.drawContours(img1, contours1, -1, (255,0,0), 2) 28 | out = cv2.drawContours(img2, contours2, -1, (250,250,250),1) 29 | #out = np.hstack([img1, img2]) 30 | cv2.imshow('img1',img2) 31 | cv2.waitKey(0) 32 | plt.subplot(331),plt.imshow(im),plt.title('GRAY') 33 | plt.xticks([]), plt.yticks([]) 34 | img = cv2.imread('index2.jpg',0) 35 | ret,thresh = cv2.threshold(img,127,255,0) 36 | contours,hierarchy = cv2.findContours(thresh, 1, 2) 37 | cnt = contours[0] 38 | M = cv2.moments(cnt) 39 | 40 | #print M 41 | perimeter = cv2.arcLength(cnt,True) 42 | #print perimeter 43 | area = cv2.contourArea(cnt) 44 | #print area 45 | epsilon = 0.1*cv2.arcLength(cnt,True) 46 | approx = cv2.approxPolyDP(cnt,epsilon,True) 47 | #print epsilon 48 | #print approx 49 | for c in contours: 50 | rect = cv2.boundingRect(c) 51 | if rect[2] < 100 or rect[3] < 100: continue 52 | #print cv2.contourArea(c) 53 | x,y,w,h = rect 54 | cv2.rectangle(img2,(x,y),(x+w,y+h),(0,255,0),8) 55 | cv2.putText(img2,'Moth Detected',(x+w+40,y+h),0,2.0,(0,255,0)) 56 | cv2.imshow("Show",img) 57 | #cv2.waitKey() 58 | #cv2.destroyAllWindows() 59 | k = cv2.isContourConvex(cnt) 60 | 61 | #to check convexity 62 | print k 63 | #blur 64 | blur = cv2.blur(im,(5,5)) 65 | #guassian blur 66 | gblur = cv2.GaussianBlur(im,(5,5),0) 67 | #median 68 | median = cv2.medianBlur(im,5) 69 | #erosion 70 | kernel = np.ones((5,5),np.uint8) 71 | erosion = cv2.erode(median,kernel,iterations = 1) 72 | dilation = cv2.dilate(erosion,kernel,iterations = 5) 73 | #erosion followed dilation 74 | closing = cv2.morphologyEx(dilation, cv2.MORPH_CLOSE, kernel) 75 | #canny edge detection 76 | edges = cv2.Canny(dilation,9,220) 77 | #plotting using matplotlib 78 | plt.subplot(332),plt.imshow(blur),plt.title('BLURRED') 79 | plt.xticks([]), plt.yticks([]) 80 | plt.subplot(333),plt.imshow(gblur),plt.title('guassianblur') 81 | plt.xticks([]), plt.yticks([]) 82 | plt.subplot(334),plt.imshow(median),plt.title('Medianblur') 83 | plt.xticks([]), plt.yticks([]) 84 | plt.subplot(337),plt.imshow(img,cmap = 'gray') 85 | plt.title('dilated Image'), plt.xticks([]), plt.yticks([]) 86 | plt.subplot(338),plt.imshow(edges,cmap = 'gray') 87 | plt.title('Edge Image'), plt.xticks([]), plt.yticks([]) 88 | plt.subplot(335),plt.imshow(erosion),plt.title('EROSION') 89 | plt.xticks([]), plt.yticks([]) 90 | plt.subplot(336),plt.imshow(closing),plt.title('closing') 91 | plt.xticks([]), plt.yticks([]) 92 | plt.show() 93 | #alerting the driver 94 | pygame.init() 95 | pygame.mixer.music.load("buzz.mp3") 96 | pygame.mixer.music.play() 97 | time.sleep(5) 98 | 99 | #content ="detection of pothole in locality basapura road hosur road junction " 100 | #mail = smtplib.SMTP('smtp.gmail.com',587) 101 | #mail.ehlo() 102 | #mail.starttls() 103 | #mail.login('harika3196@gmail.com','hariammu3196@gmail.com') 104 | #mail.sendmail('fromemail','receiver',content) 105 | #mail.close() 106 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # pothole-detection-source-code 2 | Detection of Pothole using Image Processing (Open CV) 3 | 4 | Applied Contour detection and Canny edge filter algorithms in Open CV to alert the vehicle driver in the presence of potholes. It also updates municipal authorities. 5 | 6 | Tools: 7 | python 8 | opencv 9 | -------------------------------------------------------------------------------- /buzz.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harika-3196/pothole-detection-source-code/85ec4d43d50a3893f95adb99ed3481a9da57cd20/buzz.mp3 -------------------------------------------------------------------------------- /index2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harika-3196/pothole-detection-source-code/85ec4d43d50a3893f95adb99ed3481a9da57cd20/index2.jpg -------------------------------------------------------------------------------- /index4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harika-3196/pothole-detection-source-code/85ec4d43d50a3893f95adb99ed3481a9da57cd20/index4.jpg -------------------------------------------------------------------------------- /outputscreenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harika-3196/pothole-detection-source-code/85ec4d43d50a3893f95adb99ed3481a9da57cd20/outputscreenshot.png --------------------------------------------------------------------------------