Code
Code
txt
import cv2 #thu vien cv2
import tkinter as tk #thu vien GUI
from tkinter import *
from tkinter.ttk import *
import numpy as np # thu vien mang array de xu ly anh
import tkinter.font as font
import PIL.Image,PIL.ImageTk # thu vien chuyen doi anh de hien thi
from tkinter import filedialog
import imutils
from tkinter import messagebox
window_=Tk()
window_.title("License Plate Recognize")
frame_camera=tk.Frame(window_, height=400, background="bisque")
frame_button=tk.Frame(window_, width=100, height=100,background="bisque")
canv= Canvas(frame_camera,width=500,height=400,bg='black')
canv_2=Canvas(frame_camera,width=500,height=400,bg='black')
dir_file=""
select_now= 0
img=np.zeros((504,304,3),np.uint8)
#new_image=np.zeros((504,304,3),np.uint8)
char_list = '0123456789ABCDEFGHKLMNPRSTUVXYZ'
dim=(0,0)
def sort_contours(cnts):
reverse = False
i = 0
boundingBoxes = [cv2.boundingRect(c) for c in cnts]
(cnts, boundingBoxes) = zip(*sorted(zip(cnts, boundingBoxes),
key=lambda b: b[1][i], reverse=reverse))
return cnts
def start():
global img
global dim
print(dim)
#global new_image
#print("start")
try:
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.bilateralFilter(gray, 13, 15, 15)
edged = cv2.Canny(gray, 30, 200)
contours = cv2.findContours(edged.copy(), cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)
contours = imutils.grab_contours(contours)
contours = sorted(contours, key=cv2.contourArea, reverse=True)[:10]
screenCnt = None
for c in contours:
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.018 * peri, True)
if len(approx) == 4:
screenCnt = approx
break
if screenCnt is None:
detected = 0
print("No contour detected")
else:
detected = 1
if detected == 1:
cv2.drawContours(img, [screenCnt], -1, (255, 0, 0), 3)
mask = np.zeros(gray.shape, np.uint8)
new_image = cv2.drawContours(mask, [screenCnt], 0, 255, -1, )
new_image = cv2.bitwise_and(img, img, mask=mask)
(x, y) = np.where(mask == 255)
(topx, topy) = (np.min(x), np.min(y))
(bottomx, bottomy) = (np.max(x), np.max(y))
Cropped = gray[topx:bottomx + 1, topy:bottomy + 1]
#new_image=cv2.convertScaleAbs(new_image, alpha=(255.0))
roi=Cropped
digit_w = 30
digit_h = 60
model_svm = cv2.ml.SVM_load('D:/file_D/File_/svm.xml')
binary = cv2.threshold(Cropped,120, 255,
cv2.THRESH_BINARY_INV)[1] # (110-180) para1
kernel_ = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 2)) ## (2,2)
(3,3) #para2
thre_mor = cv2.morphologyEx(binary, cv2.MORPH_DILATE, kernel_)
cont, _ = cv2.findContours(thre_mor, cv2.RETR_LIST,
cv2.CHAIN_APPROX_SIMPLE)
plate_info = ""
max_y=0
min_x=100
y_list=[]
x_list=[]
x_des =0
max_height = 0.5 #0.4->0.7 #para3
for c in sort_contours(cont):
(x, y, w, h) = cv2.boundingRect(c)
ratio = h / w
#print(ratio)
if 1 <= ratio <= 4.5:
def select():
global img
global dim
print("select")
window_.filename = filedialog.askopenfile(title="Select", filetypes=(("jpg",
"*.jpg"), ("All", "*.*")))
#print(window_.filename.name)
img = cv2.imread(window_.filename.name)
para_max = max(img.shape[1],img.shape[0])
if(para_max == img.shape[1]):
scale_percent = (int)(canv.winfo_width()/img.shape[1] *100)
else:
scale_percent = (int)(canv.winfo_height() / img.shape[0] * 100)
#print(scale_percent)
width = int(img.shape[1] * scale_percent / 100)
height = int(img.shape[0] * scale_percent / 100)
dim = (width, height)
# print(dim)
# print("size:",canv.winfo_width(),canv.winfo_height())
# resize image
img_res = cv2.resize(img, dim, interpolation=cv2.INTER_AREA)
#cv2.imshow("s",img)
#cv2.waitKey()
picture = PIL.ImageTk.PhotoImage(image=PIL.Image.fromarray(img_res))
window_.picture=picture
canv.create_image(0, 0, image=picture, anchor=tk.NW)