代码改写,将机动车牌识别改为非机动车牌识别

算法文件代码predict.py

import cv2
import numpy as np
from numpy.linalg import norm
import sys
import os
import json

SZ = 20          #训练图片长宽
MAX_WIDTH = 1000 #原始图片最大宽度
Min_Area = 2000  #车牌区域允许最大面积
PROVINCE_START = 1000
#读取图片文件
def imreadex(filename):
    return cv2.imdecode(np.fromfile(filename, dtype=np.uint8), cv2.IMREAD_COLOR)
    
def point_limit(point):
    if point[0] < 0:
        point[0] = 0
    if point[1] < 0:
        point[1] = 0

#根据设定的阈值和图片直方图,找出波峰,用于分隔字符
def find_waves(threshold, histogram):
    up_point = -1#上升点
    is_peak = False
    if histogram[0] > threshold:
        up_point = 0
        is_peak = True
    wave_peaks = []
    for i,x in enumerate(histogram):
        if is_peak and x < threshold:
            if i - up_point > 2:
                is_peak = False
                wave_peaks.append((up_point, i))
        elif not is_peak and x >= threshold:
            is_peak = True
            up_point = i
    if is_peak and up_point != -1 and i - up_point > 4:
        wave_peaks.append((up_point, i))
    return wave_peaks

#根据找出的波峰,分隔图片,从而得到逐个字符图片
def seperate_card(img, waves):
    part_cards = []
    for wave in waves:
        part_cards.append(img[:, wave[0]:wave[1]])
    return part_cards

#来自opencv的sample,用于svm训练
def deskew(img):
    m = cv2.moments(img)
    if abs(m['mu02']) < 1e-2:
        return img.copy()
    skew = m['mu11']/m['mu02']
    M = np.float32([[1, skew, -0.5*SZ*skew], [0, 1, 0]])
    img = cv2.warpAffine(img, M, (SZ, SZ), flags=cv2.WARP_INVERSE_MAP | cv2.INTER_LINEAR)
    return img
#来自opencv的sample,用于svm训练
def preprocess_hog(digits):
    samples = []
    for img in digits:
        gx = cv2.Sobel(img, cv2.CV_32F, 1, 0)
        gy = cv2.Sobel(img, cv2.CV_32F, 0, 1)
        mag, ang = cv2.cartToPolar(gx, gy)
        bin_n = 16
        bin = np.int32(bin_n*ang/(2*np.pi))
        bin_cells = bin[:10,:10], bin[10:,:10], bin[:10,10:], bin[10:,10:]
        mag_cells = mag[:10,:10], mag[10:,:10], mag[:10,10:], mag[10:,10:]
        hists = [np.bincount(b.ravel(), m.ravel(), bin_n) for b, m in zip(bin_cells, mag_cells)]
        hist = np.hstack(hists)
        
        # transform to Hellinger kernel
        eps = 1e-7
        hist /= hist.sum() + eps
        hist = np.sqrt(hist)
        hist /= norm(hist) + eps
        
        samples.append(hist)
    return np.float32(samples)
#不能保证包括所有省份
provinces = [
"zh_cuan", "川",
"zh_e", "鄂",
"zh_gan", "赣",
"zh_gan1", "甘",
"zh_gui", "贵",
"zh_gui1", "桂",
"zh_hei", "黑",
"zh_hu", "沪",
"zh_ji", "冀",
"zh_jin", "津",
"zh_jing", "京",
"zh_jl", "吉",
"zh_liao", "辽",
"zh_lu", "鲁",
"zh_meng", "蒙",
"zh_min", "闽",
"zh_ning", "宁",
"zh_qing", "靑",
"zh_qiong", "琼",
"zh_shan", "陕",
"zh_su", "苏",
"zh_sx", "晋",
"zh_wan", "皖",
"zh_xiang", "湘",
"zh_xin", "新",
"zh_yu", "豫",
"zh_yu1", "渝",
"zh_yue", "粤",
"zh_yun", "云",
"zh_zang", "藏",
"zh_zhe", "浙"
]
class StatModel(object):
    def load(self, fn):
        self.model = self.model.load(fn)  
    def save(self, fn):
        self.model.save(fn)
class SVM(StatModel):
    def __init__(self, C = 1, gamma = 0.5):
        self.model = cv2.ml.SVM_create()
        self.model.setGamma(gamma)
        self.model.setC(C)
        self.model.setKernel(cv2.ml.SVM_RBF)
        self.model.setType(cv2.ml.SVM_C_SVC)
#训练svm
    def train(self, samples, responses):
        self.model.train(samples, cv2.ml.ROW_SAMPLE, responses)
#字符识别
    def predict(self, samples):
        r = self.model.predict(samples)
        return r[1].ravel()

class CardPredictor:
    def __init__(self):
        #车牌识别的部分参数保存在js中,便于根据图片分辨率做调整
        f = open('config.js')
        j = json.load(f)
        for c in j["config"]:
            if c["open"]:
                self.cfg = c.copy()
                break
        else:
            raise RuntimeError('没有设置有效配置参数')

    def __del__(self):
        self.save_traindata()
    def train_svm(self):
        #识别英文字母和数字
        self.model = SVM(C=1, gamma=0.5)
        #识别中文
        self.modelchinese = SVM(C=1, gamma=0.5)
        if os.path.exists("svm.dat"):
            self.model.load("svm.dat")
        else:
            chars_train = []
            chars_label = []
            
            for root, dirs, files in os.walk("train\\chars2"):
                if len(os.path.basename(root)) > 1:
                    continue
                root_int = ord(os.path.basename(root))
                for filename in files:
                    filepath = os.path.join(root,filename)
                    digit_img = cv2.imread(filepath)
                    digit_img = cv2.cvtColor(digit_img, cv2.COLOR_BGR2GRAY)
                    chars_train.append(digit_img)
                    #chars_label.append(1)
                    chars_label.append(root_int)
            
            chars_train = list(map(deskew, chars_train))
            chars_train = preprocess_hog(chars_train)
            #chars_train = chars_train.reshape(-1, 20, 20).astype(np.float32)
            chars_label = np.array(chars_label)
            self.model.train(chars_train, chars_label)
        if os.path.exists("svmchinese.dat"):
            self.modelchinese.load("svmchinese.dat")
        else:
            chars_train = []
            chars_label = []
            for root, dirs, files in os.walk("train\\charsChinese"):
                if not os.path.basename(root).startswith("zh_"):
                    continue
                pinyin = os.path.basename(root)
                index = provinces.index(pinyin) + PROVINCE_START + 1 #1是拼音对应的汉字
                for filename in files:
                    filepath = os.path.join(root,filename)
                    digit_img = cv2.imread(filepath)
                    digit_img = cv2.cvtColor(digit_img, cv2.COLOR_BGR2GRAY)
                    chars_train.append(digit_img)
                    #chars_label.append(1)
                    chars_label.append(index)
            chars_train = list(map(deskew, chars_train))
            chars_train = preprocess_hog(chars_train)
            #chars_train = chars_train.reshape(-1, 20, 20).astype(np.float32)
            chars_label = np.array(chars_label)
            print(chars_train.shape)
            self.modelchinese.train(chars_train, chars_label)

    def save_traindata(self):
        if not os.path.exists("svm.dat"):
            self.model.save("svm.dat")
        if not os.path.exists("svmchinese.dat"):
            self.modelchinese.save("svmchinese.dat")

    def accurate_place(self, card_img_hsv, limit1, limit2, color):
        row_num, col_num = card_img_hsv.shape[:2]
        xl = col_num
        xr = 0
        yh = 0
        yl = row_num
        #col_num_limit = self.cfg["col_num_limit"]
        row_num_limit = self.cfg["row_num_limit"]
        col_num_limit = col_num * 0.8 if color != "green" else col_num * 0.5#绿色有渐变
        for i in range(row_num):
            count = 0
            for j in range(col_num):
                H = card_img_hsv.item(i, j, 0)
                S = card_img_hsv.item(i, j, 1)
                V = card_img_hsv.item(i, j, 2)
                if limit1 < H <= limit2 and 34 < S and 46 < V:
                    count += 1
            if count > col_num_limit:
                if yl > i:
                    yl = i
                if yh < i:
                    yh = i
        for j in range(col_num):
            count = 0
            for i in range(row_num):
                H = card_img_hsv.item(i, j, 0)
                S = card_img_hsv.item(i, j, 1)
                V = card_img_hsv.item(i, j, 2)
                if limit1 < H <= limit2 and 34 < S and 46 < V:
                    count += 1
            if count > row_num - row_num_limit:
                if xl > j:
                    xl = j
                if xr < j:
                    xr = j
        return xl, xr, yh, yl
        
    def predict(self, car_pic, resize_rate=1):
        if type(car_pic) == type(""):
            img = imreadex(car_pic)
        else:
            img = car_pic
        pic_hight, pic_width = img.shape[:2]
        if pic_width > MAX_WIDTH:
            pic_rate = MAX_WIDTH / pic_width
            img = cv2.resize(img, (MAX_WIDTH, int(pic_hight*pic_rate)), interpolation=cv2.INTER_LANCZOS4)
            pic_hight, pic_width = img.shape[:2]
        
        if resize_rate != 1:
            img = cv2.resize(img, (int(pic_width*resize_rate), int(pic_hight*resize_rate)), interpolation=cv2.INTER_LANCZOS4)
            pic_hight, pic_width = img.shape[:2]
            
        print("h,w:", pic_hight, pic_width)
        blur = self.cfg["blur"]
        #高斯去噪
        if blur > 0:
            img = cv2.GaussianBlur(img, (blur, blur), 0)#图片分辨率调整
        oldimg = img
        img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        #equ = cv2.equalizeHist(img)
        #img = np.hstack((img, equ))
        #去掉图像中不会是车牌的区域
        kernel = np.ones((20, 20), np.uint8)
        img_opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
        img_opening = cv2.addWeighted(img, 1, img_opening, -1, 0);

        #找到图像边缘
        ret, img_thresh = cv2.threshold(img_opening, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
        img_edge = cv2.Canny(img_thresh, 100, 200)
        #使用开运算和闭运算让图像边缘成为一个整体
        kernel = np.ones((self.cfg["morphologyr"], self.cfg["morphologyc"]), np.uint8)
        img_edge1 = cv2.morphologyEx(img_edge, cv2.MORPH_CLOSE, kernel)
        img_edge2 = cv2.morphologyEx(img_edge1, cv2.MORPH_OPEN, kernel)

        #查找图像边缘整体形成的矩形区域,可能有很多,车牌就在其中一个矩形区域中
        try:
            contours, hierarchy = cv2.findContours(img_edge2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
        except ValueError:
            image, contours, hierarchy = cv2.findContours(img_edge2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
        contours = [cnt for cnt in contours if cv2.contourArea(cnt) > Min_Area]
        print('len(contours)', len(contours))
        #一一排除不是车牌的矩形区域
        car_contours = []
        for cnt in contours:
            rect = cv2.minAreaRect(cnt)
            area_width, area_height = rect[1]
            if area_width < area_height:
                area_width, area_height = area_height, area_width
            wh_ratio = area_width / area_height
            #print(wh_ratio)
            #要求矩形区域长宽比在2到5.5之间,2到5.5是车牌的长宽比,其余的矩形排除
            if wh_ratio > 2 and wh_ratio < 5.5:
                car_contours.append(rect)
                box = cv2.boxPoints(rect)
                box = np.int0(box)
                #oldimg = cv2.drawContours(oldimg, [box], 0, (0, 0, 255), 2)
                #cv2.imshow("edge4", oldimg)
                #cv2.waitKey(0)

        print(len(car_contours))

        print("精确定位")
        card_imgs = []
        #矩形区域可能是倾斜的矩形,需要矫正,以便使用颜色定位
        for rect in car_contours:
            if rect[2] > -1 and rect[2] < 1:#创造角度,使得左、高、右、低拿到正确的值
                angle = 1
            else:
                angle = rect[2]
            rect = (rect[0], (rect[1][0]+5, rect[1][1]+5), angle)#扩大范围,避免车牌边缘被排除

            box = cv2.boxPoints(rect)
            heigth_point = right_point = [0, 0]
            left_point = low_point = [pic_width, pic_hight]
            for point in box:
                if left_point[0] > point[0]:
                    left_point = point
                if low_point[1] > point[1]:
                    low_point = point
                if heigth_point[1] < point[1]:
                    heigth_point = point
                if right_point[0] < point[0]:
                    right_point = point

            if left_point[1] <= right_point[1]:#正角度
                new_right_point = [right_point[0], heigth_point[1]]
                pts2 = np.float32([left_point, heigth_point, new_right_point])#字符只是高度需要改变
                pts1 = np.float32([left_point, heigth_point, right_point])
                M = cv2.getAffineTransform(pts1, pts2)
                dst = cv2.warpAffine(oldimg, M, (pic_width, pic_hight))
                point_limit(new_right_point)
                point_limit(heigth_point)
                point_limit(left_point)
                card_img = dst[int(left_point[1]):int(heigth_point[1]), int(left_point[0]):int(new_right_point[0])]
                card_imgs.append(card_img)
                #cv2.imshow("card", card_img)
                #cv2.waitKey(0)
            elif left_point[1] > right_point[1]:#负角度
                
                new_left_point = [left_point[0], heigth_point[1]]
                pts2 = np.float32([new_left_point, heigth_point, right_point])#字符只是高度需要改变
                pts1 = np.float32([left_point, heigth_point, right_point])
                M = cv2.getAffineTransform(pts1, pts2)
                dst = cv2.warpAffine(oldimg, M, (pic_width, pic_hight))
                point_limit(right_point)
                point_limit(heigth_point)
                point_limit(new_left_point)
                card_img = dst[int(right_point[1]):int(heigth_point[1]), int(new_left_point[0]):int(right_point[0])]
                card_imgs.append(card_img)
                #cv2.imshow("card", card_img)
                #cv2.waitKey(0)
        #开始使用颜色定位,排除不是车牌的矩形,目前只识别蓝、绿、黄车牌
        colors = []
        for card_index,card_img in enumerate(card_imgs):
            green = yello = blue = black = white = 0
            card_img_hsv = cv2.cvtColor(card_img, cv2.COLOR_BGR2HSV)
            #有转换失败的可能,原因来自于上面矫正矩形出错
            if card_img_hsv is None:
                continue
            row_num, col_num= card_img_hsv.shape[:2]
            card_img_count = row_num * col_num

            for i in range(row_num):
                for j in range(col_num):
                    H = card_img_hsv.item(i, j, 0)
                    S = card_img_hsv.item(i, j, 1)
                    V = card_img_hsv.item(i, j, 2)
                    if 11 < H <= 34 and S > 34:#图片分辨率调整
                        yello += 1
                    elif 35 < H <= 99 and S > 34:#图片分辨率调整
                        green += 1
                    elif 99 < H <= 124 and S > 34:#图片分辨率调整
                        blue += 1
                    
                    if 0 < H <180 and 0 < S < 255 and 0 < V < 46:
                        black += 1
                    elif 0 < H <180 and 0 < S < 43 and 221 < V < 225:
                        white += 1
            color = "no"

            limit1 = limit2 = 0
            if yello*2 >= card_img_count:
                color = "yello"
                limit1 = 11
                limit2 = 34#有的图片有色偏偏绿
            elif green*2 >= card_img_count:
                color = "green"
                limit1 = 35
                limit2 = 99
            elif blue*2 >= card_img_count:
                color = "blue"
                limit1 = 100
                limit2 = 124#有的图片有色偏偏紫
            elif black + white >= card_img_count*0.7:#TODO
                color = "bw"
            print(color)
            colors.append(color)
            print(blue, green, yello, black, white, card_img_count)
            #cv2.imshow("color", card_img)
            #cv2.waitKey(0)
            if limit1 == 0:
                continue
            #以上为确定车牌颜色
            #以下为根据车牌颜色再定位,缩小边缘非车牌边界
            xl, xr, yh, yl = self.accurate_place(card_img_hsv, limit1, limit2, color)
            if yl == yh and xl == xr:
                continue
            need_accurate = False
            if yl >= yh:
                yl = 0
                yh = row_num
                need_accurate = True
            if xl >= xr:
                xl = 0
                xr = col_num
                need_accurate = True
            card_imgs[card_index] = card_img[yl:yh, xl:xr] if color != "green" or yl < (yh-yl)//4 else card_img[yl-(yh-yl)//4:yh, xl:xr]
            if need_accurate:#可能x或y方向未缩小,需要再试一次
                card_img = card_imgs[card_index]
                card_img_hsv = cv2.cvtColor(card_img, cv2.COLOR_BGR2HSV)
                xl, xr, yh, yl = self.accurate_place(card_img_hsv, limit1, limit2, color)
                if yl == yh and xl == xr:
                    continue
                if yl >= yh:
                    yl = 0
                    yh = row_num
                if xl >= xr:
                    xl = 0
                    xr = col_num
            card_imgs[card_index] = card_img[yl:yh, xl:xr] if color != "green" or yl < (yh-yl)//4 else card_img[yl-(yh-yl)//4:yh, xl:xr]
        #以上为车牌定位
        #以下为识别车牌中的字符
        predict_result = []
        roi = None
        card_color = None
        for i, color in enumerate(colors):
            if color in ("blue", "yello", "green"):
                card_img = card_imgs[i]
                gray_img = cv2.cvtColor(card_img, cv2.COLOR_BGR2GRAY)
                #黄、绿车牌字符比背景暗、与蓝车牌刚好相反,所以黄、绿车牌需要反向
                if color == "green" or color == "yello":
                    gray_img = cv2.bitwise_not(gray_img)
                ret, gray_img = cv2.threshold(gray_img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
                #查找水平直方图波峰
                x_histogram  = np.sum(gray_img, axis=1)
                x_min = np.min(x_histogram)
                x_average = np.sum(x_histogram)/x_histogram.shape[0]
                x_threshold = (x_min + x_average)/2
                wave_peaks = find_waves(x_threshold, x_histogram)
                if len(wave_peaks) == 0:
                    print("peak less 0:")
                    continue
                #认为水平方向,最大的波峰为车牌区域
                wave = max(wave_peaks, key=lambda x:x[1]-x[0])
                gray_img = gray_img[wave[0]:wave[1]]
                #查找垂直直方图波峰
                row_num, col_num= gray_img.shape[:2]
                #去掉车牌上下边缘1个像素,避免白边影响阈值判断
                gray_img = gray_img[1:row_num-1]
                y_histogram = np.sum(gray_img, axis=0)
                y_min = np.min(y_histogram)
                y_average = np.sum(y_histogram)/y_histogram.shape[0]
                y_threshold = (y_min + y_average)/5#U和0要求阈值偏小,否则U和0会被分成两半

                wave_peaks = find_waves(y_threshold, y_histogram)

                #for wave in wave_peaks:
                #    cv2.line(card_img, pt1=(wave[0], 5), pt2=(wave[1], 5), color=(0, 0, 255), thickness=2) 
                #车牌字符数应大于6
                if len(wave_peaks) <= 6:
                    print("peak less 1:", len(wave_peaks))
                    continue
                
                wave = max(wave_peaks, key=lambda x:x[1]-x[0])
                max_wave_dis = wave[1] - wave[0]
                #判断是否是左侧车牌边缘
                if wave_peaks[0][1] - wave_peaks[0][0] < max_wave_dis/3 and wave_peaks[0][0] == 0:
                    wave_peaks.pop(0)
                
                #组合分离汉字
                cur_dis = 0
                for i,wave in enumerate(wave_peaks):
                    if wave[1] - wave[0] + cur_dis > max_wave_dis * 0.6:
                        break
                    else:
                        cur_dis += wave[1] - wave[0]
                if i > 0:
                    wave = (wave_peaks[0][0], wave_peaks[i][1])
                    wave_peaks = wave_peaks[i+1:]
                    wave_peaks.insert(0, wave)
                
                #去除车牌上的分隔点
                point = wave_peaks[2]
                if point[1] - point[0] < max_wave_dis/3:
                    point_img = gray_img[:,point[0]:point[1]]
                    if np.mean(point_img) < 255/5:
                        wave_peaks.pop(2)
                
                if len(wave_peaks) <= 6:
                    print("peak less 2:", len(wave_peaks))
                    continue
                part_cards = seperate_card(gray_img, wave_peaks)
                for i, part_card in enumerate(part_cards):
                    #可能是固定车牌的铆钉
                    if np.mean(part_card) < 255/5:
                        print("a point")
                        continue
                    part_card_old = part_card
                    #w = abs(part_card.shape[1] - SZ)//2
                    w = part_card.shape[1] // 3
                    part_card = cv2.copyMakeBorder(part_card, 0, 0, w, w, cv2.BORDER_CONSTANT, value = [0,0,0])
                    part_card = cv2.resize(part_card, (SZ, SZ), interpolation=cv2.INTER_AREA)
                    #cv2.imshow("part", part_card_old)
                    #cv2.waitKey(0)
                    #cv2.imwrite("u.jpg", part_card)
                    #part_card = deskew(part_card)
                    part_card = preprocess_hog([part_card])
                    if i == 0:
                        resp = self.modelchinese.predict(part_card)
                        charactor = provinces[int(resp[0]) - PROVINCE_START]
                    else:
                        resp = self.model.predict(part_card)
                        charactor = chr(resp[0])
                    #判断最后一个数是否是车牌边缘,假设车牌边缘被认为是1
                    if charactor == "1" and i == len(part_cards)-1:
                        if part_card_old.shape[0]/part_card_old.shape[1] >= 8:#1太细,认为是边缘
                            print(part_card_old.shape)
                            continue
                    predict_result.append(charactor)
                roi = card_img
                card_color = color
                break
                
        return predict_result, roi, card_color#识别到的字符、定位的车牌图像、车牌颜色

if __name__ == '__main__':
    c = CardPredictor()
    c.train_svm()
    r, roi, color = c.predict("2.jpg")
    print(r)
    


界面文件代码

import tkinter as tk
from tkinter.filedialog import *
from tkinter import ttk
import predict
import cv2
from PIL import Image, ImageTk
import threading
import time



class Surface(ttk.Frame):
    pic_path = ""
    viewhigh = 600
    viewwide = 600
    update_time = 0
    thread = None
    thread_run = False
    camera = None
    color_transform = {"green":("绿牌","#55FF55"), "yello":("黄牌","#FFFF00"), "blue":("蓝牌","#6666FF")}
        
    def __init__(self, win):
        ttk.Frame.__init__(self, win)
        frame_left = ttk.Frame(self)
        frame_right1 = ttk.Frame(self)
        frame_right2 = ttk.Frame(self)
        win.title("车牌识别")
        win.state("zoomed")
        self.pack(fill=tk.BOTH, expand=tk.YES, padx="5", pady="5")
        frame_left.pack(side=LEFT,expand=1,fill=BOTH)
        frame_right1.pack(side=TOP,expand=1,fill=tk.Y)
        frame_right2.pack(side=RIGHT,expand=0)
        ttk.Label(frame_left, text='原图:').pack(anchor="nw") 
        ttk.Label(frame_right1, text='车牌位置:').grid(column=0, row=0, sticky=tk.W)
        
        from_pic_ctl = ttk.Button(frame_right2, text="来自图片", width=20, command=self.from_pic)
        from_vedio_ctl = ttk.Button(frame_right2, text="来自摄像头", width=20, command=self.from_vedio)
        self.image_ctl = ttk.Label(frame_left)
        self.image_ctl.pack(anchor="nw")
        
        self.roi_ctl = ttk.Label(frame_right1)
        self.roi_ctl.grid(column=0, row=1, sticky=tk.W)
        ttk.Label(frame_right1, text='识别结果:').grid(column=0, row=2, sticky=tk.W)
        self.r_ctl = ttk.Label(frame_right1, text="")
        self.r_ctl.grid(column=0, row=3, sticky=tk.W)
        self.color_ctl = ttk.Label(frame_right1, text="", width="20")
        self.color_ctl.grid(column=0, row=4, sticky=tk.W)
        from_vedio_ctl.pack(anchor="se", pady="5")
        from_pic_ctl.pack(anchor="se", pady="5")
        self.predictor = predict.CardPredictor()
        self.predictor.train_svm()
        
    def get_imgtk(self, img_bgr):
        img = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
        im = Image.fromarray(img)
        imgtk = ImageTk.PhotoImage(image=im)
        wide = imgtk.width()
        high = imgtk.height()
        if wide > self.viewwide or high > self.viewhigh:
            wide_factor = self.viewwide / wide
            high_factor = self.viewhigh / high
            factor = min(wide_factor, high_factor)
            
            wide = int(wide * factor)
            if wide <= 0 : wide = 1
            high = int(high * factor)
            if high <= 0 : high = 1
            im=im.resize((wide, high), Image.ANTIALIAS)
            imgtk = ImageTk.PhotoImage(image=im)
        return imgtk
    
    def show_roi(self, r, roi, color):
        if r :
            roi = cv2.cvtColor(roi, cv2.COLOR_BGR2RGB)
            roi = Image.fromarray(roi)
            self.imgtk_roi = ImageTk.PhotoImage(image=roi)
            self.roi_ctl.configure(image=self.imgtk_roi, state='enable')
            self.r_ctl.configure(text=str(r))
            self.update_time = time.time()
            try:
                c = self.color_transform[color]
                self.color_ctl.configure(text=c[0], background=c[1], state='enable')
            except: 
                self.color_ctl.configure(state='disabled')
        elif self.update_time + 8 < time.time():
            self.roi_ctl.configure(state='disabled')
            self.r_ctl.configure(text="")
            self.color_ctl.configure(state='disabled')
        
    def from_vedio(self):
        if self.thread_run:
            return
        if self.camera is None:
            self.camera = cv2.VideoCapture(0)
            if not self.camera.isOpened():
                mBox.showwarning('警告', '摄像头打开失败!')
                self.camera = None
                return
        self.thread = threading.Thread(target=self.vedio_thread, args=(self,))
        self.thread.setDaemon(True)
        self.thread.start()
        self.thread_run = True
        
    def from_pic(self):
        self.thread_run = False
        self.pic_path = askopenfilename(title="选择识别图片", filetypes=[("jpg图片", "*.jpg")])
        if self.pic_path:
            img_bgr = predict.imreadex(self.pic_path)
            self.imgtk = self.get_imgtk(img_bgr)
            self.image_ctl.configure(image=self.imgtk)
            resize_rates = (1, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4)
            for resize_rate in resize_rates:
                print("resize_rate:", resize_rate)
                try:
                    r, roi, color = self.predictor.predict(img_bgr, resize_rate)
                except:
                    continue
                if r:
                    break
            #r, roi, color = self.predictor.predict(img_bgr, 1)
            self.show_roi(r, roi, color)

    @staticmethod
    def vedio_thread(self):
        self.thread_run = True
        predict_time = time.time()
        while self.thread_run:
            _, img_bgr = self.camera.read()
            self.imgtk = self.get_imgtk(img_bgr)
            self.image_ctl.configure(image=self.imgtk)
            if time.time() - predict_time > 2:
                r, roi, color = self.predictor.predict(img_bgr)
                self.show_roi(r, roi, color)
                predict_time = time.time()
        print("run end")
        
        
def close_window():
    print("destroy")
    if surface.thread_run :
        surface.thread_run = False
        surface.thread.join(2.0)
    win.destroy()
    
    
if __name__ == '__main__':
    win=tk.Tk()
    
    surface = Surface(win)
    win.protocol('WM_DELETE_WINDOW', close_window)
    win.mainloop()
    


你这个代码发的别人没法看
发代码要用代码段</>的文本形式,不然代码格式会出现错误。

img

车牌颜色不同,很容易识别

主要就是数据集和模型不一样吧,你要先制作好数据集,然后训练下模型,将模型替换一下,剩下的一些基本上都是通用的了。
你可以先试一下机动车模型来识别非机动车看下,基本上是可以识别出来的,不过准确度可能就没有单独的机动车那么高了

非机动车和机动车的车牌颜色不同,很容易识别,你直接识别颜色范围就行。(我的意思是找出特征,按特征识别)

得先识别非机动车,训练下模型,你这个改的话就是大改

将第一个py文件的159行和181行中的路径下的数据替换成非机动车的数据,重新运行第一个py文件,再运行第二个就ok了,不用大改。

for root, dirs, files in os.walk("train\\chars2"):

for root, dirs, files in os.walk("train\\charsChinese"):

用paddle做呀,又快又好

既然能识别机动车牌, 那么直接取反不就好了

兄弟,你现在这主要的问题有以下几个,我主要给你说一下问题,很多东西不太容易解决:

  1. 数据集的问题:车牌的数据集,由于都是国标,大家都一样,而且目前对于机动车车牌的数据集,存在一部分公开数据集,但是非机动车车牌的数据集,据我所知是真没有
  2. 非机动车牌本身的问题:由于非机动车牌本身不存在国标,因此都是各省市自己定标准,这就使得非机动车牌有很多的种类,想要单纯依靠分割的方法或者字符匹配的方法去识别全国各省市的车牌难度有点高。
  3. 代码里面的一些检测,我大致看了一下里面的代码,里面有一部分是依靠一些传统的图像处理方法来进行一部分的分割的问题,但是由于上面我所描述的这两个原因,使得对非机动车车牌的分割匹配的难度指数级上升。。。。。

所以个人感觉。。。这个东西可以放弃掉,换一个课题更好一些,比如一些车辆分类定位这种,比较简单一些,也容易做

我有

img


训练好的模型以及机动车非机动车训练数据。

img

img

img

好家伙,老师让我们看过的项目这个玩意

可看看哦~https://www.docin.com/p-2784033437.html

这个应该不用改吧,有图片就可以检测。

兄弟,你现在这主要的问题有以下几个,我主要给你说一下问题,很多东西不太容易解决:

数据集的问题:车牌的数据集,由于都是国标,大家都一样,而且目前对于机动车车牌的数据集,存在一部分公开数据集,但是非机动车车牌的数据集,据我所知是真没有
非机动车牌本身的问题:由于非机动车牌本身不存在国标,因此都是各省市自己定标准,这就使得非机动车牌有很多的种类,想要单纯依靠分割的方法或者字符匹配的方法去识别全国各省市的车牌难度有点高。
代码里面的一些检测,我大致看了一下里面的代码,里面有一部分是依靠一些传统的图像处理方法来进行一部分的分割的问题,但是由于上面我所描述的这两个原因,使得对非机动车车牌的分割匹配的难度指数级上升。。。。。
所以个人感觉。。。这个东西可以放弃掉,换一个课题更好一些,比如一些车辆分类定位这种,比较简单一些,也容易做

修改建议:

将代码中的“车牌”相关注释改为“非机动车牌”相关注释;
将车牌省份简称和全称对应的列表更改为非机动车牌的列表,包括非机动车类型、所属省份等信息;
将字符识别部分的预处理函数改为针对非机动车牌的特征提取函数;
将SVM模型进行非机动车牌训练;
修改后的代码如下:

import cv2
import numpy as np
from numpy.linalg import norm
import sys
import os
import json

SZ = 20          #训练图片长宽
MAX_WIDTH = 1000 #原始图片最大宽度
Min_Area = 2000  #非机动车牌区域允许最大面积
VEHICLE_TYPES = ["自行车", "电动自行车", "三轮车", "摩托车"]  # 非机动车类型
PROVINCES = [
    "鲁A", "鲁B", "鲁C", "鲁D", "鲁E", "鲁F", "鲁G", "鲁H", "鲁J", "鲁K",
    "鲁L", "鲁M", "鲁N", "鲁P", "鲁Q", "鲁R", "鲁S", "鲁U", "鲁V", "鲁Y", "鲁Z",
    "京A", "京B", "京C", "京D", "京E", "京F", "京G", "京H", "京J", "京K",
    "京L", "京M", "京N", "京O", "京P", "京Q", "京R", "京S", "京T", "京U", "京V", "京W", "京X", "京Y", "京Z",
    "沪A", "沪B", "沪C", "沪D", "沪E", "沪F", "沪G", "沪H", "沪J", "沪K",
    "沪L", "沪M", "沪N", "沪P", "沪Q", "沪R", "沪S", "沪T", "沪U", "沪V", "沪W", "沪X", "沪Y", "沪Z",
    "苏A", "苏B", "苏C", "苏D", "苏E", "苏F", "苏G", "苏H", "苏J", "苏K",
    "苏L", "苏M", "苏N", "苏P", "苏Q", "苏R", "苏S", "苏T", "苏U", "苏V", "苏W", "苏X", "苏Y", "苏Z",
    "浙A", "浙B", "浙C", "浙D.......

等等
如果对您有帮助,请给与采纳,谢谢。

是否解决了, 代码发出来看下哇

车牌识别的大致过程:车牌识别将每个字符单独从车牌中切割出来,字符校正,神经网络识别结果。
代码参考:
轻松使用 Python 检测和识别车牌(附代码):https://blog.csdn.net/weixin_38037405/article/details/128960826

predict.py:

import cv2
import numpy as np
from numpy.linalg import norm
import sys
import os
import json

SZ = 20          #训练图片长宽
MAX_WIDTH = 1000 #原始图片最大宽度
Min_Area = 2000  #车牌区域允许最大面积
PROVINCE_START = 1000

#读取图片文件
def imreadex(filename):
    return cv2.imdecode(np.fromfile(filename, dtype=np.uint8), cv2.IMREAD_COLOR)

def point_limit(point):
    if point[0] < 0:
        point[0] = 0
    if point[1] < 0:
        point[1] = 0

#根据设定的阈值和图片直方图,找出波峰,用于分隔字符
def find_waves(threshold, histogram):
    up_point = -1#上升点
    is_peak = False
    if histogram[0] > threshold:
        up_point = 0
        is_peak = True
    wave_peaks = []
    for i,x in enumerate(histogram):
        if is_peak and x < threshold:
            if i - up_point > 2:
                is_peak = False
                wave_peaks.append((up_point, i))
        elif not is_peak and x >= threshold:
            is_peak = True
            up_point = i
    if is_peak and up_point != -1 and i - up_point > 4:
        wave_peaks.append((up_point, i))
    return wave_peaks

#根据找出的波峰,分隔图片,从而得到逐个字符图片
def seperate_card(img, waves):
    part_cards = []
    for wave in waves:
        part_cards.append(img[:, wave[0]:wave[1]])
    return part_cards

#来自opencv的sample,用于svm训练
def deskew(img):
    m = cv2.moments(img)
    if abs(m['mu02']) < 1e-2:
        return img.copy()
    skew = m['mu11']/m['mu02']
    M = np.float32([[1, skew, -0.5*SZ*skew], [0, 1, 0]])
    img = cv2.warpAffine(img, M, (SZ, SZ), flags=cv2.WARP_INVERSE_MAP | cv2.INTER_LINEAR)
    return img

#来自opencv的sample,用于svm训练
def preprocess_hog(digits):
    samples = []
    for img in digits:
        gx = cv2.Sobel(img, cv2.CV_32F, 1, 0)
        gy = cv2.Sobel(img, cv2.CV_32F, 0, 1)
        mag, ang = cv2.cartToPolar(gx, gy)
        bin_n = 16
        bin = np.int32(bin_n*ang/(2*np.pi))
        bin_cells = bin[:10,:10], bin[10:,:10], bin[:10,10:], bin[10:,10:]
        mag_cells = mag[:10,:10], mag[10:,:10], mag[:10,10:], mag[10:,10:]
        hists = [np.bincount(b.ravel(), m.ravel(), bin_n) for b, m in zip(bin_cells, mag_cells)]
        hist = np.hstack(hists)
        
        # transform to Hellinger kernel
        eps = 1e-7
        hist /= hist.sum() + eps
        hist = np.sqrt(hist)
        hist /= norm(hist) + eps
        
        samples.append(hist)
    return np.float32(samples)

#不能保证包括所有省份
provinces = [
"zh_cuan", "川",
"zh_e", "鄂",
"zh_gan", "赣",
"zh_gan1", "甘",
"zh_gui", "贵",
"zh_gui1", "桂",
"zh_hei", "黑",
"zh_hu", "沪",
"zh_ji", "冀",
"zh_jin", "津",
"zh_jing", "京",
"zh_jl", "吉",
"zh_liao", "辽",
"zh_lu", "鲁",
"zh_meng", "蒙",
"zh_min", "闽",
"zh_ning", "宁",
"zh_qing", "靑",
"zh_qiong", "琼",
"zh_shan", "陕",
"zh_su", "苏",
"zh_sx", "晋",
"zh_wan", "皖",
"zh_xiang", "湘",
"zh_xin", "新",
"zh_yu", "豫",
"zh_yu1", "渝",
"zh_yue", "粤",
"zh_yun", "云",
"zh_zang", "藏",
"zh_zhe", "浙"
]

class StatModel(object):
    def load(self, fn):
        self.model = self.model.load(fn)  
    def save(self, fn):
        self.model.save(fn)

class SVM(StatModel):
    def __init__(self, C = 1, gamma = 0.5):
        self.model = cv2.ml.SVM_create()
        self.model.setGamma(gamma)
        self.model.setC(C)
        self.model.setKernel(cv2.ml.SVM_RBF)
        self.model.setType(cv2.ml.SVM_C_SVC)

    #训练svm
    def train(self, samples, responses):
        self.model.train(samples, cv2.ml.ROW_SAMPLE, responses)

    #字符识别
    def predict(self, samples):
        r = self.model.predict(samples)
        return r[1].ravel()

class CardPredictor:
    def __init__(self):
        #车牌识别的部分参数保存在js中,便于根据图片分辨率做调整
        f = open('config.js')
        j = json.load(f)
        for c in j["config"]:
            if c["open"]:
                self.cfg = c.copy()
                break
        else:
            raise RuntimeError('没有设置有效配置参数')

    def __del__(self):
        self.save_traindata()

    def train_svm(self):
        #识别中文
        self.modelchinese = SVM(C=1, gamma=0.5)
        if os.path.exists("svmchinese.dat"):
            self.modelchinese.load("svmchinese.dat")
        else:
            chars_train = []
            chars_label = []
            for root, dirs, files in os.walk("train\\charsChinese"):
                if not os.path.basename(root).startswith("zh_"):
                    continue
                pinyin = os.path.basename(root)
                index = provinces.index(pinyin) + PROVINCE_START + 1 #1是拼音对应的汉字
                for filename in files:
                    filepath = os.path.join(root,filename)
                    digit_img = cv2.imread(filepath)
                    digit_img = cv2.cvtColor(digit_img, cv2.COLOR_BGR2GRAY)
                    chars_train.append(digit_img)
                    chars_label.append(index)
            chars_train = list(map(deskew, chars_train))
            chars_train = preprocess_hog(chars_train)
            chars_label = np.array(chars_label)
            self.modelchinese.train(chars_train, chars_label)

    def save_traindata(self):
        if not os.path.exists("svmchinese.dat"):
            self.modelchinese.save("svmchinese.dat")

    def accurate_place(self, card_img_hsv, limit1, limit2, color):
        row_num, col_num = card_img_hsv.shape[:2]
        xl = col_num
        xr = 0
        yh = 0
        yl = row_num
        row_num_limit = self.cfg["row_num_limit"]
        col_num_limit = col_num * 0.8 if color != "green" else col_num * 0.5 #绿色有渐变
        for i in range(row_num):
            count = 0
            for j in range(col_num):
                H = card_img_hsv.item(i, j, 0)
                S = card_img_hsv.item(i, j, 1)
                V = card_img_hsv.item(i, j, 2)
                if limit1 < H <= limit2 and 34 < S and 46 < V:
                    count += 1
            if count > col_num_limit:
                if yl > i:
                    yl = i
                if yh < i:
                    yh = i
        for j in range(col_num):
            count = 0
            for i in range(row_num):
                H = card_img_hsv.item(i, j, 0)
                S = card_img_hsv.item(i, j, 1)
                V = card_img_hsv.item(i, j, 2)
                if limit1 < H <= limit2 and 34 < S and 46 < V:
                    count += 1
            if count > row_num - row_num_limit:
                if xl > j:
                    xl = j
                if xr < j:
                    xr = j
        return xl, xr, yh, yl

    def predict(self, car_pic, resize_rate=1):
        if type(car_pic) == type(""):
            img = imreadex(car_pic)
        else:
            img = car_pic
        pic_hight, pic_width = img.shape[:2]
        if pic_width > MAX_WIDTH:
            pic_rate = MAX_WIDTH / pic_width
            img = cv2.resize(img, (MAX_WIDTH, int(pic_hight*pic_rate)), interpolation=cv2.INTER_LANCZOS4)
            pic_hight, pic_width = img.shape[:2]

        if resize_rate != 1:
            img = cv2.resize(img, (int(pic_width*resize_rate), int(pic_hight*resize_rate)), interpolation=cv2.INTER_LANCZOS4)
            pic_hight, pic_width = img.shape[:2]

        blur = self.cfg["blur"]
        #高斯去噪
        if blur > 0:
            img = cv2.GaussianBlur(img, (blur, blur), 0)
        oldimg = img
        img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        kernel = np.ones((20, 20), np.uint8)
        img_opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
        img_opening = cv2.addWeighted(img, 1, img_opening, -1, 0)
        ret, img_thresh = cv2.threshold(img_opening, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
        img_edge = cv2.Canny(img_thresh, 100, 200)
        kernel = np.ones((self.cfg["morphologyr"], self.cfg["morphologyc"]), np.uint8)
        img_edge1 = cv2.morphologyEx(img_edge, cv2.MORPH_CLOSE, kernel)
        img_edge2 = cv2.morphologyEx(img_edge1, cv2.MORPH_OPEN, kernel)
        try:
            contours, hierarchy = cv2.findContours(img_edge2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
        except ValueError:
            image, contours, hierarchy = cv2.findContours(img_edge2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
        contours = [cnt for cnt in contours if cv2.contourArea(cnt) > Min_Area]

望采纳。



import cv2
import numpy as np
from numpy.linalg import norm
import sys
import os
import json

SZ = 20          #训练图片长宽
MAX_WIDTH = 1000 #原始图片最大宽度
Min_Area = 2000  #非机动车牌区域允许最大面积
PROVINCE_START = 1000

# 其他代码保持不变

#查找图像边缘整体形成的矩形区域,可能有很多,非机动车牌就在其中一个矩形区域中
try:
    contours, hierarchy = cv2.findContours(img_edge2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
except ValueError:
    image, contours, hierarchy = cv2.findContours(img_edge2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = [cnt for cnt in contours if cv2.contourArea(cnt) > Min_Area]
print('len(contours)', len(contours))

# 其他代码保持不变

#一一排除不是非机动车牌的矩形区域
non_motor_vehicle_contours = []
for cnt in contours:
    rect = cv2.minAreaRect(cnt)
    area_width, area_height = rect[1]
    if area_width < area_height:
        area_width, area_height = area_height, area_width
    wh_ratio = area_width / area_height
    #要求矩形区域长宽比在2到5.5之间,2到5.5是非机动车牌的长宽比,其余的矩形排除
    if wh_ratio > 2 and wh_ratio < 5.5:
        non_motor_vehicle_contours.append(rect)
        box = cv2.boxPoints(rect)
        box = np.int0(box)

# 其他代码保持不变

#开始使用颜色定位,排除不是非机动车牌的矩形,目前只识别蓝、绿、黄车牌
colors = []
for card_index,card_img in enumerate(card_imgs):
    green = yello = blue = black = white = 0
    card_img_hsv = cv2.cvtColor(card_img, cv2.COLOR_BGR2HSV)
    #有转换失败的可能,原因来自于上面矫正矩形出错
    if card_img_hsv is None:
        continue
    row_num, col_num= card_img_hsv.shape[:2]
    card_img_count = row_num * col_num

    # 其他代码保持不变

微信小程序是一种轻量级的应用程序,可以在微信内部直接运行,提供了便捷的使用方式,对于图像识别应用开发也提供了很好的支持。在微信小程序中部署图像识别的模型,可以帮助用户快速地获取图片信息以及从中提取有价值的信息。

图像识别技术是人工智能领域的重要应用之一,其实现需要用到深度学习、神经网络等相关技术。在微信小程序中,可以通过调用云开发能力,实现图像识别模型的部署和调用。下面将介绍在微信小程序中部署图像识别模型的实现步骤:

  1. 准备模型

在部署图像识别模型之前,需要准备训练好的模型。可以使用 TensorFlow、PyTorch 等深度学习框架训练模型,并将模型导出为 TensorFlow Lite 格式。在导出模型时,需要指定模型的输入和输出节点名称,以便后续在小程序中调用模型时正确地指定输入和输出。

  1. 上传模型

将训练好的模型上传到云存储中,以便在小程序中调用。可以使用微信小程序提供的云开发功能,上传模型到云存储中。

  1. 编写云函数

在云开发控制台中创建一个云函数,用于调用上传的模型。在云函数中,需要先初始化 TensorFlow Lite Java API,并加载上传的模型。然后,根据模型的输入和输出节点名称,构造输入数据并传递给模型进行推理。最后,将推理结果返回给小程序端。

  1. 在小程序中调用云函数

在小程序中调用云函数,传递需要识别的图片数据。云函数会返回识别结果,包括图像中的物体类别、置信度等信息。可以将识别结果展示在小程序中,或者将结果保存到数据库中。

需要注意的是,在部署图像识别模型时,需要保证模型的准确性和效率。另外,为了提高用户的使用体验,可以对识别结果进行优化和美化,例如添加标签、引用相关的图片等。

总之,在微信小程序中部署图像识别模型,可以帮助用户更加快捷地获取图像信息,同时也提高了应用的交互性和趣味性。

非机动车牌识别

随着城市化的不断加速以及人们生活水平的提高,非机动车的使用量也不断增加。非机动车包括自行车、电动车、摩托车等,它们的使用范围广泛,是人们上下班、购物、娱乐的常用交通工具。因此,非机动车管理也成为城市交通管理的重要一环。

为了更好地管理非机动车,非机动车牌识别技术应运而生。非机动车牌识别技术通过识别非机动车的车牌号码,可以实现对非机动车的追踪管理、违规行为监管等,是当前非机动车管理中不可或缺的技术手段。

非机动车牌识别技术的实现

非机动车牌识别技术的实现需要使用图像识别技术和相关的算法模型。通常的识别过程如下:

  1. 采集图像

通过摄像头等设备对非机动车进行拍摄,获取车牌图像。

  1. 提取车牌区域

通过图像处理算法,提取出车牌区域,去除无用的背景信息。

  1. 字符分割

将车牌区域分割成单个字符,为后续的字符识别做准备。

  1. 字符识别

利用深度学习等相关算法模型对单个字符进行识别,最终得到车牌号码。

  1. 校验车牌号码

对识别出来的车牌号码进行校验,以保证识别正确率。

非机动车牌识别技术的应用

  1. 实现非机动车管理

非机动车牌识别技术可以实现对非机动车的大规模管理,通过系统对非机动车进行识别,可以对非机动车进行实时监控,实现非机动车的数据化管理。

  1. 违法行为监管

利用非机动车牌识别技术可以对非机动车进行实时监控,对非机动车的违法行为,如闯红灯等进行实时监管,以保证道路交通的畅通和安全。

  1. 对非机动车进行统计分析

通过对非机动车牌识别数据进行分析,可以了解非机动车的使用情况,如哪些地区有更多的非机动车流量,哪些时间段是非机动车使用高峰期等,这将有助于城市规划和交通管理。

  1. 提高城市交通效率

非机动车牌识别技术可以对非机动车进行快速识别和管理,有效提高城市交通效率,减少交通拥堵。

总结

非机动车牌识别技术在城市交通管理中的应用前景非常广阔。通过对非机动车的管理,可以提高城市交通效率,保障道路安全。在实际应用中,还需要解决一些技术难题,如复杂环境下的车牌识别等,但随着科技的不断进步,相信非机动车牌识别技术在未来将得到进一步的提高和发展。