国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

opencv 案例實(shí)戰(zhàn)02-停車場(chǎng)車牌識(shí)別SVM模型訓(xùn)練及驗(yàn)證

這篇具有很好參考價(jià)值的文章主要介紹了opencv 案例實(shí)戰(zhàn)02-停車場(chǎng)車牌識(shí)別SVM模型訓(xùn)練及驗(yàn)證。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

1. 整個(gè)識(shí)別的流程圖:

opencv 案例實(shí)戰(zhàn)02-停車場(chǎng)車牌識(shí)別SVM模型訓(xùn)練及驗(yàn)證,計(jì)算機(jī)視覺,opencv實(shí)戰(zhàn),人工智能,opencv,支持向量機(jī),人工智能
2. 車牌定位中分割流程圖:


三、車牌識(shí)別中字符分割流程圖:

1.準(zhǔn)備數(shù)據(jù)集

下載車牌相關(guān)字符樣本用于訓(xùn)練和測(cè)試,本文使用14個(gè)漢字樣本和34個(gè)數(shù)字跟字母樣本,每個(gè)字符樣本數(shù)為40,樣本尺寸為28*28。

opencv 案例實(shí)戰(zhàn)02-停車場(chǎng)車牌識(shí)別SVM模型訓(xùn)練及驗(yàn)證,計(jì)算機(jī)視覺,opencv實(shí)戰(zhàn),人工智能,opencv,支持向量機(jī),人工智能

opencv 案例實(shí)戰(zhàn)02-停車場(chǎng)車牌識(shí)別SVM模型訓(xùn)練及驗(yàn)證,計(jì)算機(jī)視覺,opencv實(shí)戰(zhàn),人工智能,opencv,支持向量機(jī),人工智能
數(shù)據(jù)集下載地址

https://download.csdn.net/download/hai411741962/88248392

下載不了,評(píng)論區(qū)留言
2. 編碼訓(xùn)練代碼

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

SZ = 20          #訓(xùn)練圖片長(zhǎng)寬
MAX_WIDTH = 1000 #原始圖片最大寬度
Min_Area = 2000  #車牌區(qū)域允許最大面積
PROVINCE_START = 1000
#不能保證包括所有省份
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)#從文件載入訓(xùn)練好的模型
	def save(self, fn):
		self.model.save(fn)#保存訓(xùn)練好的模型到文件中

class SVM(StatModel):
	def __init__(self, C = 1, gamma = 0.5):
		self.model = cv2.ml.SVM_create()#生成一個(gè)SVM模型
		self.model.setGamma(gamma) #設(shè)置Gamma參數(shù),demo中是0.5
		self.model.setC(C)# 設(shè)置懲罰項(xiàng), 為:1
		self.model.setKernel(cv2.ml.SVM_RBF)#設(shè)置核函數(shù)
		self.model.setType(cv2.ml.SVM_C_SVC)#設(shè)置SVM的模型類型:SVC是分類模型,SVR是回歸模型
	#訓(xùn)練svm
	def train(self, samples, responses):
		self.model.train(samples, cv2.ml.ROW_SAMPLE, responses)#訓(xùn)練
	#字符識(shí)別
	def predict(self, samples):
		r = self.model.predict(samples)#預(yù)測(cè)
		return r[1].ravel()

#來自opencv的sample,用于svm訓(xùn)練
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訓(xùn)練
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)


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

def train_svm():
	#識(shí)別英文字母和數(shù)字
	model = SVM(C=1, gamma=0.5)
	#識(shí)別中文
	modelchinese = SVM(C=1, gamma=0.5)
	if os.path.exists("svm.dat"):
		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))
		#print(chars_train)
		chars_train = preprocess_hog(chars_train)
		#print(chars_train)
		#chars_train = chars_train.reshape(-1, 20, 20).astype(np.float32)
		chars_label = np.array(chars_label)
		model.train(chars_train, chars_label)
	if os.path.exists("svmchinese.dat"):
		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是拼音對(duì)應(yīng)的漢字
			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)
		modelchinese.train(chars_train, chars_label)

	save_traindata(model,modelchinese)


train_svm()

運(yùn)行代碼后會(huì)生成兩個(gè)模型文件,下面驗(yàn)證兩個(gè)模型文件。

opencv 案例實(shí)戰(zhàn)02-停車場(chǎng)車牌識(shí)別SVM模型訓(xùn)練及驗(yàn)證,計(jì)算機(jī)視覺,opencv實(shí)戰(zhàn),人工智能,opencv,支持向量機(jī),人工智能

import cv2
import numpy as np

import json
import train

SZ = 20          #訓(xùn)練圖片長(zhǎng)寬
MAX_WIDTH = 1000 #原始圖片最大寬度
Min_Area = 2000  #車牌區(qū)域允許最大面積
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

#根據(jù)設(shè)定的閾值和圖片直方圖,找出波峰,用于分隔字符
def find_waves(threshold, histogram):
	up_point = -1#上升點(diǎn)
	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

#根據(jù)找出的波峰,分隔圖片,從而得到逐個(gè)字符圖片
def seperate_card(img, waves):
	part_cards = []
	for wave in waves:
		part_cards.append(img[:, wave[0]:wave[1]])
	return part_cards

class CardPredictor:
	def __init__(self):
		#車牌識(shí)別的部分參數(shù)保存在json中,便于根據(jù)圖片分辨率做調(diào)整
		f = open('config.json')
		j = json.load(f)
		for c in j["config"]:
			if c["open"]:
				self.cfg = c.copy()
				break
		else:
			raise RuntimeError('沒有設(shè)置有效配置參數(shù)')


	def load_svm(self):
		#識(shí)別英文字母和數(shù)字
		self.model = train.SVM(C=1, gamma=0.5)#SVM(C=1, gamma=0.5)
		#識(shí)別中文
		self.modelchinese = train.SVM(C=1, gamma=0.5)#SVM(C=1, gamma=0.5)
		self.model.load("module\\svm.dat")
		self.modelchinese.load("module\\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 resize_rate != 1:
			img = cv2.resize(img, (int(pic_width*resize_rate), int(pic_hight*resize_rate)), interpolation=cv2.INTER_AREA)
			pic_hight, pic_width = img.shape[:2]
		#cv2.imshow('img',img)
		#cv2.waitKey(0)
		print("h,w:", pic_hight, pic_width)
		blur = self.cfg["blur"]
		#高斯去噪
		if blur > 0:
			img = cv2.GaussianBlur(img, (blur, blur), 0)#圖片分辨率調(diào)整
		oldimg = img
		img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

		#去掉圖像中不會(huì)是車牌的區(qū)域
		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)#邊緣檢測(cè)

		#使用開運(yùn)算和閉運(yùn)算讓圖像邊緣成為一個(gè)整體
		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)

		#查找圖像邊緣整體形成的矩形區(qū)域,可能有很多,車牌就在其中一個(gè)矩形區(qū)域中
		contours, hierarchy = cv2.findContours(img_edge2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
		print('len(contours)', len(contours))#找出區(qū)域
		contours = [cnt for cnt in contours if cv2.contourArea(cnt) > Min_Area]
		print('len(contours)', len(contours))#cv2.contourArea計(jì)算面積
		#一一排除不是車牌的矩形區(qū)域
		car_contours = []
		for cnt in contours:
			rect = cv2.minAreaRect(cnt)#minAreaRect
			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#長(zhǎng)寬比
			#print(wh_ratio)
			#要求矩形區(qū)域長(zhǎng)寬比在25.5之間,25.5是車牌的長(zhǎng)寬比,其余的矩形排除
			if wh_ratio > 2 and wh_ratio < 5.5:
				car_contours.append(rect)
				box = cv2.boxPoints(rect)#cv2.boxPoints()可獲取該矩形的四個(gè)頂點(diǎn)坐標(biāo)。
				print(box)
				box = np.int0(box) #轉(zhuǎn)成整數(shù)
				print(box)
			oldimg = cv2.drawContours(oldimg, [box], 0, (0, 0, 255), 2)
			
		print(len(car_contours))

		print("精確定位")
		card_imgs = []
		#矩形區(qū)域可能是傾斜的矩形,需要矯正,以便使用顏色定位
		for rect in car_contours:
			if rect[2] > -1 and rect[2] < 1:#創(chuàng)造角度,使得左、高、右、低拿到正確的值
				angle = 1
			else:
				angle = rect[2]
			rect = (rect[0], (rect[1][0]+5, rect[1][1]+5), angle)#擴(kuò)大范圍,避免車牌邊緣被排除

			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])]
				if(len(card_img)>0):
					card_imgs.append(card_img)

			elif left_point[1] > right_point[1]:#負(fù)角度

				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)
		#開始使用顏色定位,排除不是車牌的矩形,目前只識(shí)別藍(lán)、綠、黃車牌
		colors = []
		for card_index,card_img in enumerate(card_imgs):
			print(len(card_imgs))
			green = yello = blue = black = white = 0
			card_img_hsv = cv2.cvtColor(card_img, cv2.COLOR_BGR2HSV)
			print("card_img_hsv.shape")
			print(card_img_hsv.shape)
			#有轉(zhuǎn)換失敗的可能,原因來自于上面矯正矩形出錯(cuò)
			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:#圖片分辨率調(diào)整
						yello += 1
					elif 35 < H <= 99 and S > 34:#圖片分辨率調(diào)整
						green += 1
					elif 99 < H <= 124 and S > 34:#圖片分辨率調(diào)整
						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"
            #根據(jù)HSV判斷車牌顏色
			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"

			colors.append(color)
			print("blue, green, yello, black, white, card_img_count:")
			print(blue,"   " ,green,"   ", yello,"   ", black,"   ", white,"   ", card_img_count)
			print("車牌顏色:",color)
			# cv2.imshow("color", card_img)
			# cv2.waitKey(0)
			if limit1 == 0:
				continue
			#以上為確定車牌顏色
			#以下為根據(jù)車牌顏色再定位,縮小邊緣非車牌邊界
			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]
		#以上為車牌定位
		#以下為識(shí)別車牌中的字符
		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)
			
				#黃、綠車牌字符比背景暗、與藍(lán)車牌剛好相反,所以黃、綠車牌需要反向
				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
				#認(rèn)為水平方向,最大的波峰為車牌區(qū)域
				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個(gè)像素,避免白邊影響閾值判斷
				gray_img = gray_img[1:row_num-1]
				# cv2.imshow("gray_img", gray_img)#二值化
				# cv2.waitKey(0)
				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會(huì)被分成兩半

				wave_peaks = find_waves(y_threshold, y_histogram)

		
				#車牌字符數(shù)應(yīng)大于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]
				#判斷是否是左側(cè)車牌邊緣
				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)

				#去除車牌上的分隔點(diǎn)
				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 = 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.destroyAllWindows()
	
					part_card = train.preprocess_hog([part_card])#preprocess_hog([part_card])
					if i == 0:
						resp = self.modelchinese.predict(part_card)#第一個(gè)字符調(diào)用中文svm模型
						charactor = train.provinces[int(resp[0]) - PROVINCE_START]
					else:
						resp = self.model.predict(part_card)#其他字符調(diào)用字母數(shù)字svm模型
						charactor = chr(resp[0])
					#判斷最后一個(gè)數(shù)是否是車牌邊緣,假設(shè)車牌邊緣被認(rèn)為是1
					if charactor == "1" and i == len(part_cards)-1:
						if part_card_old.shape[0]/part_card_old.shape[1] >= 8:#1太細(xì),認(rèn)為是邊緣
							print(part_card_old.shape)
							continue
					predict_result.append(charactor)
				roi = card_img
				card_color = color
				break

		return predict_result, roi, card_color#識(shí)別到的字符、定位的車牌圖像、車牌顏色

if __name__ == '__main__':
	c = CardPredictor()
	c.load_svm()#加載訓(xùn)練好的模型
	img  = cv2.imread("test\\car20.jpg")
	img = cv2.resize(img, (1000, 1000), interpolation=cv2.INTER_AREA)
	r, roi, color = c.predict(img)
	print(r)

運(yùn)行結(jié)果:

車牌顏色: blue
['津', 'N', 'A', 'V', '8', '8', '8']

從結(jié)果看比上一節(jié)的準(zhǔn)確多了。文章來源地址http://www.zghlxwxcb.cn/news/detail-670188.html

到了這里,關(guān)于opencv 案例實(shí)戰(zhàn)02-停車場(chǎng)車牌識(shí)別SVM模型訓(xùn)練及驗(yàn)證的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • OpenCV圖像處理——停車場(chǎng)車位識(shí)別

    OpenCV圖像處理——停車場(chǎng)車位識(shí)別

    圖像處理總目錄←點(diǎn)擊這里 唐宇迪老師的——OPENCV項(xiàng)目實(shí)戰(zhàn) 學(xué)習(xí) 本項(xiàng)目的目的是設(shè)計(jì)一個(gè)停車場(chǎng)車位識(shí)別的系統(tǒng),能夠判斷出當(dāng)前停車場(chǎng)中哪些車位是空的。 任務(wù)共包含部分: 對(duì)圖像預(yù)處理 從停車場(chǎng)的監(jiān)控視頻中提取圖片 對(duì)圖片進(jìn)行一系列的預(yù)處理,去噪、識(shí)別圖像中的

    2024年02月13日
    瀏覽(29)
  • 基于OpenCV構(gòu)建停車場(chǎng)車位識(shí)別項(xiàng)目

    基于OpenCV構(gòu)建停車場(chǎng)車位識(shí)別項(xiàng)目

    OpenCV是一個(gè)基于(開源)發(fā)行的跨平臺(tái)計(jì)算機(jī)視覺庫(kù),能實(shí)現(xiàn)圖像處理和計(jì)算機(jī)視覺方面的很多通用算法。車位識(shí)別的圖像處理過程如圖所示。 在python中設(shè)置完所有內(nèi)容后, 最重要的依賴關(guān)系將是OpenCV庫(kù)。通過pip將其添加到虛擬環(huán)境中,可以運(yùn)行 pip install opencv-python 。 要檢

    2024年02月05日
    瀏覽(25)
  • 【opencv】計(jì)算機(jī)視覺:停車場(chǎng)車位實(shí)時(shí)識(shí)別

    【opencv】計(jì)算機(jī)視覺:停車場(chǎng)車位實(shí)時(shí)識(shí)別

    目錄 目標(biāo) 整體流程 背景 詳細(xì)講解 前言 前些天發(fā)現(xiàn)了一個(gè)巨牛的人工智能學(xué)習(xí)網(wǎng)站,通俗易懂,風(fēng)趣幽默,忍不住分享一下給大家。點(diǎn)擊跳轉(zhuǎn)到網(wǎng)站 目標(biāo) 我們想要在一個(gè)實(shí)時(shí)的停車場(chǎng)監(jiān)控視頻中,看看要有多少個(gè)車以及有多少個(gè)空缺車位。然后我們可以標(biāo)記空的,然后來

    2024年02月05日
    瀏覽(27)
  • OpenCV實(shí)踐小項(xiàng)目(三) - 停車場(chǎng)車位實(shí)時(shí)檢測(cè)

    OpenCV實(shí)踐小項(xiàng)目(三) - 停車場(chǎng)車位實(shí)時(shí)檢測(cè)

    今天整理OpenCV入門的第三個(gè)實(shí)戰(zhàn)小項(xiàng)目,前面的兩篇文章整理了信用卡數(shù)字識(shí)別以及文檔OCR掃描, 大部分用到的是OpenCV里面的基礎(chǔ)圖像預(yù)處理技術(shù),比如輪廓檢測(cè),邊緣檢測(cè),形態(tài)學(xué)操作,透視變換等, 而這篇文章的項(xiàng)目呢,不僅需要一些基礎(chǔ)的圖像預(yù)處理,還需要搭建模

    2024年02月07日
    瀏覽(34)
  • 智能停車場(chǎng)系統(tǒng):基于 pyqt5,opencv,MySQL

    這是一個(gè)相對(duì)復(fù)雜的項(xiàng)目,需要使用多個(gè)技術(shù)和模塊來實(shí)現(xiàn)。以下是一個(gè)簡(jiǎn)單的示例代碼,可以使用 Python 和 PyQt 實(shí)現(xiàn)一個(gè)簡(jiǎn)單的智能停車場(chǎng)管理系統(tǒng)。 在上面的示例代碼中,我們使用了 PyQt5 庫(kù)來創(chuàng)建 GUI 界面,使用了 OpenCV 庫(kù)來讀取攝像頭數(shù)據(jù),并使用了 PyTesseract 庫(kù)來識(shí)別

    2024年02月11日
    瀏覽(22)
  • 計(jì)算機(jī)視覺結(jié)合深度學(xué)習(xí)項(xiàng)目-智能停車場(chǎng)空車位實(shí)時(shí)識(shí)別

    計(jì)算機(jī)視覺結(jié)合深度學(xué)習(xí)項(xiàng)目-智能停車場(chǎng)空車位實(shí)時(shí)識(shí)別

    ?????? 歡迎來到本博客 ?????? 本次博客內(nèi)容將繼續(xù)講解關(guān)于OpenCV的相關(guān)知識(shí) ?? 作者簡(jiǎn)介 : ?????? 目前計(jì)算機(jī)研究生在讀。主要研究方向是人工智能和群智能算法方向。目前熟悉python網(wǎng)頁(yè)爬蟲、機(jī)器學(xué)習(xí)、計(jì)算機(jī)視覺(OpenCV)、群智能算法。然后正在學(xué)習(xí)深度

    2024年02月06日
    瀏覽(17)
  • 案例158:基于微信小程序的停車場(chǎng)管理系統(tǒng)設(shè)計(jì)與實(shí)現(xiàn)

    案例158:基于微信小程序的停車場(chǎng)管理系統(tǒng)設(shè)計(jì)與實(shí)現(xiàn)

    文末獲取源碼 開發(fā)語(yǔ)言:Java 框架:SSM JDK版本:JDK1.8 數(shù)據(jù)庫(kù):mysql 5.7 開發(fā)軟件:eclipse/myeclipse/idea Maven包:Maven3.5.4 小程序框架:uniapp 小程序開發(fā)軟件:HBuilder X 小程序運(yùn)行軟件:微信開發(fā)者 目錄 目錄 前言 系統(tǒng)展示 管理員功能實(shí)現(xiàn) 車輛停放管理 車輛駛出管理 停車費(fèi)用管

    2024年01月23日
    瀏覽(38)
  • 畢業(yè)設(shè)計(jì)|基于STM32單片機(jī)的語(yǔ)音識(shí)別控制智能停車場(chǎng)設(shè)計(jì)

    畢業(yè)設(shè)計(jì)|基于STM32單片機(jī)的語(yǔ)音識(shí)別控制智能停車場(chǎng)設(shè)計(jì)

    演示視頻 https://www.bilibili.com/video/BV1bC4y1579d/?spm_id_from=333.999.0.0vd_source=0fb8a603c3cd84c0c72e5fa5d4201605 本系統(tǒng)采用stm32f103c8t6單片機(jī)+晶振電路+復(fù)位電路+電源電路+車位檢測(cè)電路+OLED顯示電路+繼電器控制電路+語(yǔ)音識(shí)別電路構(gòu)成。 1,通過紅外對(duì)管模塊實(shí)時(shí)檢測(cè)車位是否占用,車位分為

    2024年02月04日
    瀏覽(114)
  • 停車場(chǎng)收費(fèi)管理系統(tǒng)/停車場(chǎng)管理系統(tǒng)的設(shè)計(jì)與實(shí)現(xiàn)

    停車場(chǎng)收費(fèi)管理系統(tǒng)/停車場(chǎng)管理系統(tǒng)的設(shè)計(jì)與實(shí)現(xiàn)

    摘 ?要 ABSTRACT 目 ? 錄 第 1 章 緒論 1.1背景及意義 1.2 國(guó)內(nèi)外研究概況 1.3 研究的內(nèi)容 第2章 相關(guān)技術(shù) 2.1 JSP技術(shù)介紹 2.2 SSM三大框架 2.3 MyEclipse開發(fā)環(huán)境 2.4 Tomcat服務(wù)器 2.5?MySQL數(shù)據(jù)庫(kù) 第3章 系統(tǒng)分析 3.1 需求分析 3.2 系統(tǒng)可行性分析 3.2.1技術(shù)可行性:技術(shù)背景 3.2.2經(jīng)濟(jì)可行性

    2024年02月12日
    瀏覽(25)
  • 共享停車位小程序,微信小程序停車場(chǎng)車位,微信小程序停車場(chǎng)系統(tǒng)畢設(shè)作品

    共享停車位小程序,微信小程序停車場(chǎng)車位,微信小程序停車場(chǎng)系統(tǒng)畢設(shè)作品

    ? 目的 :首先,在社會(huì)上“停車難”是一個(gè)眾所周知的問題,每個(gè)小區(qū),每個(gè)大廈都有自己的停車場(chǎng),但是在沒有進(jìn)入停車場(chǎng)之前,我們沒辦法知道是否有空車位,空車位在哪個(gè)地方。為了解決這個(gè)問題我們打算做一個(gè)停車場(chǎng)車位預(yù)約小程序,來解決車主在進(jìn)入停車場(chǎng)之前了

    2024年02月08日
    瀏覽(26)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包