├── README.md └── main1.py /README.md: -------------------------------------------------------------------------------- 1 | # NIGHT-HEAD-LIGHT -------------------------------------------------------------------------------- /main1.py: -------------------------------------------------------------------------------- 1 | import tkinter as tk 2 | from tkinter import filedialog 3 | from tkinter import * 4 | from PIL import ImageTk, Image 5 | from tkinter import PhotoImage 6 | import numpy as np 7 | import cv2 8 | import pytesseract as tess 9 | def clean2_plate(plate): 10 | gray_img = cv2.cvtColor(plate, cv2.COLOR_BGR2GRAY) 11 | 12 | _, thresh = cv2.threshold(gray_img, 110, 255, cv2.THRESH_BINARY) 13 | num_contours,hierarchy = cv2.findContours(thresh.copy(),cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) 14 | 15 | if num_contours: 16 | contour_area = [cv2.contourArea(c) for c in num_contours] 17 | max_cntr_index = np.argmax(contour_area) 18 | 19 | max_cnt = num_contours[max_cntr_index] 20 | max_cntArea = contour_area[max_cntr_index] 21 | x,y,w,h = cv2.boundingRect(max_cnt) 22 | 23 | if not ratioCheck(max_cntArea,w,h): 24 | return plate,None 25 | 26 | final_img = thresh[y:y+h, x:x+w] 27 | return final_img,[x,y,w,h] 28 | 29 | else: 30 | return plate,None 31 | 32 | def ratioCheck(area, width, height): 33 | ratio = float(width) / float(height) 34 | if ratio < 1: 35 | ratio = 1 / ratio 36 | if (area < 1063.62 or area > 73862.5) or (ratio < 3 or ratio > 6): 37 | return False 38 | return True 39 | 40 | def isMaxWhite(plate): 41 | avg = np.mean(plate) 42 | if(avg>=115): 43 | return True 44 | else: 45 | return False 46 | 47 | def ratio_and_rotation(rect): 48 | (x, y), (width, height), rect_angle = rect 49 | 50 | if(width>height): 51 | angle = -rect_angle 52 | else: 53 | angle = 90 + rect_angle 54 | 55 | if angle>15: 56 | return False 57 | 58 | if height == 0 or width == 0: 59 | return False 60 | 61 | area = height*width 62 | if not ratioCheck(area,width,height): 63 | return False 64 | else: 65 | return True 66 | 67 | top=tk.Tk() 68 | top.geometry('900x700') 69 | top.title('Number Plate Recognition') 70 | # top.wm_iconbitmap('/home/shivam/Dataflair/Keras Projects_CIFAR/GUI/logo.ico') 71 | img = cv2.imread(r"/content/image.png") 72 | # img = ImageTk.PhotoImage(img) 73 | # img = ImageTk.PhotoImage(Image.open("/image.png")) 74 | # top.iconphoto(True,img) 75 | top.configure(background='#CDCDCD') 76 | label=Label(top,background='#CDCDCD', font=('arial',35,'bold')) 77 | # label.grid(row=0,column=1) 78 | sign_image = Label(top,bd=10) 79 | plate_image=Label(top,bd=10) 80 | def classify(file_path): 81 | 82 | ####################################################### 83 | 84 | res_text=[0] 85 | res_img=[0] 86 | img = cv2.imread(file_path) 87 | # cv2.imshow("input",img) 88 | 89 | # if cv2.waitKey(0) & 0xff == ord('q'): 90 | # pass 91 | img2 = cv2.GaussianBlur(img, (3,3), 0) 92 | img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY) 93 | 94 | img2 = cv2.Sobel(img2,cv2.CV_8U,1,0,ksize=3) 95 | _,img2 = cv2.threshold(img2,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) 96 | 97 | element = cv2.getStructuringElement(shape=cv2.MORPH_RECT, ksize=(17, 3)) 98 | morph_img_threshold = img2.copy() 99 | cv2.morphologyEx(src=img2, op=cv2.MORPH_CLOSE, kernel=element, dst=morph_img_threshold) 100 | num_contours, hierarchy= cv2.findContours(morph_img_threshold,mode=cv2.RETR_EXTERNAL,method=cv2.CHAIN_APPROX_NONE) 101 | cv2.drawContours(img2, num_contours, -1, (0,255,0), 1) 102 | 103 | 104 | for i,cnt in enumerate(num_contours): 105 | 106 | min_rect = cv2.minAreaRect(cnt) 107 | 108 | if ratio_and_rotation(min_rect): 109 | 110 | x,y,w,h = cv2.boundingRect(cnt) 111 | plate_img = img[y:y+h,x:x+w] 112 | print("Number identified number plate...") 113 | # cv2.imshow("num plate image",plate_img) 114 | # if cv2.waitKey(0) & 0xff == ord('q'): 115 | # pass 116 | res_img[0]=plate_img 117 | cv2.imwrite("result.png",plate_img) 118 | if(isMaxWhite(plate_img)): 119 | clean_plate, rect = clean2_plate(plate_img) 120 | 121 | if rect: 122 | fg=0 123 | x1,y1,w1,h1 = rect 124 | x,y,w,h = x+x1,y+y1,w1,h1 125 | plate_im = Image.fromarray(clean_plate) 126 | text = tess.image_to_string(plate_im, lang='eng') 127 | res_text[0]=text 128 | if text: 129 | break 130 | # print("Number Detected Plate Text : ",text) 131 | 132 | ####################################################### 133 | label.configure(foreground='#011638', text=res_text[0]) 134 | # plate_img.configure() 135 | uploaded=Image.open("result.png") 136 | im=ImageTk.PhotoImage(uploaded) 137 | plate_image.configure(image=im) 138 | plate_image.image=im 139 | plate_image.pack() 140 | plate_image.place(x=560,y=320) 141 | def show_classify_button(file_path): 142 | classify_b=Button(top,text="Classify Image",command=lambda: classify(file_path),padx=10,pady=5) 143 | classify_b.configure(background='#364156', foreground='white',font=('arial',15,'bold')) 144 | classify_b.place(x=490,y=550) 145 | # classify_b.pack(side=,pady=60) 146 | def upload_image(): 147 | try: 148 | file_path=filedialog.askopenfilename() 149 | uploaded=Image.open(file_path) 150 | uploaded.thumbnail(((top.winfo_width()/2.25),(top.winfo_height()/2.25))) 151 | im=ImageTk.PhotoImage(uploaded) 152 | 153 | sign_image.configure(image=im) 154 | sign_image.image=im 155 | label.configure(text='') 156 | show_classify_button(file_path) 157 | except: 158 | pass 159 | upload=Button(top,text="Upload an image",command=upload_image,padx=10,pady=5) 160 | upload.configure(background='#364156', foreground='white',font=('arial',15,'bold')) 161 | upload.pack() 162 | upload.place(x=210,y=550) 163 | # sign_image.pack(side=BOTTOM,expand=True) 164 | sign_image.pack() 165 | sign_image.place(x=70,y=200) 166 | 167 | # label.pack(side=BOTTOM,expand=True) 168 | label.pack() 169 | label.place(x=500,y=220) 170 | heading = Label(top,image=img) 171 | heading.configure(background='#CDCDCD',foreground='#364156') 172 | heading.pack() 173 | top.mainloop() 174 | 175 | --------------------------------------------------------------------------------