欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python3+OpenCV实现简单交通标志识别流程分析

程序员文章站 2022-06-16 11:33:20
由于该项目是针对中小学生竞赛并且是第一次举行,所以识别的目标交通标志仅仅只有直行、右转、左转和停车让行。数据集:链接: https://pan.baidu.com/s/1sl0qe-qd4cuatmf...

由于该项目是针对中小学生竞赛并且是第一次举行,所以识别的目标交通标志仅仅只有直行、右转、左转和停车让行。

数据集:

链接: https://pan.baidu.com/s/1sl0qe-qd4cuatmfzenuk0q 提取码: vuvi 

源代码:https://github.com/ccxiao5/traffic_sign_recognition

整体流程如下:

  • 数据集收集(包括训练集和测试集的分类)
  • 图像预处理
  • 图像标注
  • 根据标注分割得到目标图像
  • hog特征提取
  • 训练得到模型
  • 将模型带入识别算法进行识别

我的数据目录树。其中test_images/train_images是收集得到原始数据集。realtest/realtrain是预处理后的图像。datatest/datatrain是经过分类处理得到的图像,hogtest/hogtrain是通过xml标注后裁剪得到的图像。hogtest_affine/hogtrain_affine是经过仿射变换处理扩充的训练集和测试集。imgtest_hog.txt/imgtrain_hog.txt是测试集和训练集的hog特征

Python3+OpenCV实现简单交通标志识别流程分析

一、图像处理

  由于得到的数据集图像大小不一(如下),我们首先从中心区域裁剪并调整正方形图像的大小,然后将处理后的图像保存到realtrain和realtest里面。

Python3+OpenCV实现简单交通标志识别流程分析

图片名称对应关系如下:

img_label = {
"000":"speed_limit_5",
"001":"speed_limit_15",
"002":"speed_limit_30",
"003":"speed_limit_40",
"004":"speed_limit_50",
"005":"speed_limit_60",
"006":"speed_limit_70",
"007":"speed_limit_80",
"008":"no straight or right turn",
"009":"no straight or left turn",
"010":"no straight",
"011":"no left turn",
"012":"do not turn left and right",
"013":"no right turn",
"014":"no overhead",
"015":"no u-turn",
"016":"no motor vehicle",
"017":"no whistle",
"018":"unrestricted speed_40",
"019":"unrestricted speed_50",
"020":"straight or turn right",
"021":"straight",
"022":"turn left",
"023":"turn left or turn right",
"024":"turn right",
"025":"drive on the left side of the road",
"026":"drive on the right side of the road",
"027":"driving around the island",
"028":"motor vehicle driving",
"029":"whistle",
"030":"non-motorized",
"031":"u-turn",
"032":"left-right detour",
"033":"traffic light",
"034":"drive cautiously",
"035":"caution pedestrians",
"036":"attention non-motor vehicle",
"037":"mind the children",
"038":"sharp turn to the right",
"039":"sharp turn to the left",
"040":"downhill steep slope",
"041":"uphill steep slope",
"042":"go slow",
"044":"right t-shaped cross",
"043":"left t-shaped cross",
"045":"village",
"046":"reverse detour",
"047":"railway crossing-1",
"048":"construction",
"049":"continuous detour",
"050":"railway crossing-2",
"051":"accident-prone road section",
"052":"stop",
"053":"no passing",
"054":"no parking",
"055":"no entry",
"056":"deceleration and concession",
"057":"stop for check"
}
def center_crop(img_array, crop_size=-1, resize=-1, write_path=none):
    ##从中心区域裁剪并调整正方形图像的大小。
    rows = img_array.shape[0]
    cols = img_array.shape[1]

    if crop_size==-1 or crop_size>max(rows,cols):
        crop_size = min(rows, cols)
    row_s = max(int((rows-crop_size)/2), 0)
    row_e = min(row_s+crop_size, rows) 
    col_s = max(int((cols-crop_size)/2), 0)
    col_e = min(col_s+crop_size, cols)

    img_crop = img_array[row_s:row_e,col_s:col_e,]

    if resize>0:
        img_crop = cv2.resize(img_crop, (resize, resize))

    if write_path is not none:
        cv2.imwrite(write_path, img_crop)
    return img_crop

  然后根据得到的realtrain和realtest自动生成带有<size><width><height><depth><filename>的xml文件

def write_img_to_xml(imgfile, xmlfile):
    
    img = cv2.imread(imgfile)
    img_folder, img_name = os.path.split(imgfile)
    img_height, img_width, img_depth = img.shape
    doc = document()

    annotation = doc.createelement("annotation")
    doc.appendchild(annotation)

    folder = doc.createelement("folder")
    folder.appendchild(doc.createtextnode(img_folder))
    annotation.appendchild(folder)

    filename = doc.createelement("filename")
    filename.appendchild(doc.createtextnode(img_name))
    annotation.appendchild(filename)

    size = doc.createelement("size")
    annotation.appendchild(size)

    width = doc.createelement("width")
    width.appendchild(doc.createtextnode(str(img_width)))
    size.appendchild(width)

    height = doc.createelement("height")
    height.appendchild(doc.createtextnode(str(img_height)))
    size.appendchild(height)

    depth = doc.createelement("depth")
    depth.appendchild(doc.createtextnode(str(img_depth)))
    size.appendchild(depth)

    with open(xmlfile, "w") as f:
        doc.writexml(f, indent="\t", addindent="\t", newl="\n", encoding="utf-8")
<annotation>
<folder>/home/xiao5/desktop/test2/data/realtest/pngimages</folder>
<filename>000_1_0001_1_j.png</filename>
<size>
<width>640</width>
<height>640</height>
<depth>3</depth>
</size>
</annotation>

然后对realtrain和realtest的图片进行标注,向默认xml添加新的信息(矩形信息)。

<annotation>
<folder>pngimages</folder>
<filename>021_1_0001_1_j.png</filename>
<path>
c:\users\xiao5\desktop\realtest\pngimages\021_1_0001_1_j.png
</path>
<source>
<database>unknown</database>
</source>
<size>
<width>640</width>
<height>640</height>
<depth>3</depth>
</size>
<segmented>0</segmented>
<object>
<name>straight</name>
<pose>unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>13</xmin>
<ymin>22</ymin>
<xmax>573</xmax>
<ymax>580</ymax>
</bndbox>
</object>
</annotation>

处理完后利用我们添加的矩形将图片裁剪下来并且重命名进行分类。主要思路是:解析xml文档,根据<name>标签进行分类,如果是直行、右转、左转、停止,那么就把它从原图中裁剪下来并重命名,如果没有<object>那么就认为是负样本,其中在处理负样本的时候,我进行了颜色识别,把一张负样本图片根据颜色(红色、蓝色)裁剪成几张负样本,这样做的好处是:我们在进行交通标志的识别时,也是使用的颜色识别来选取到交通标志,我们从负样本中分割出来的相近颜色样本有利于负样本的训练,提高模型精度。

def produce_proposals(xml_dir, write_dir, square=false, min_size=30):
                ##返回proposal_num对象
    proposal_num = {}
    for cls_name in classes_name:
        proposal_num[cls_name] = 0

    index = 0
    for xml_file in os.listdir(xml_dir):
        img_path, labels = parse_xml(os.path.join(xml_dir,xml_file))
        img = cv2.imread(img_path)
        ##如果图片中没有出现定义的那几种交通标志就把它当成负样本
        if len(labels) == 0:
            neg_proposal_num = produce_neg_proposals(img_path, write_dir, min_size, square, proposal_num["background"])
            proposal_num["background"] = neg_proposal_num
        else:
            proposal_num = produce_pos_proposals(img_path, write_dir, labels, min_size, square=true, proposal_num=proposal_num)
            
        if index%100 == 0:
            print ("total xml file number = ", len(os.listdir(xml_dir)), "current xml file number = ", index)
            print ("proposal num = ", proposal_num)
        index += 1

    return proposal_num

Python3+OpenCV实现简单交通标志识别流程分析

为了提高模型的精确度,还对目标图片(四类图片)进行仿射变换来扩充训练集。

def affine(img, delta_pix):
    rows, cols, _ = img.shape
    pts1 = np.float32([[0,0], [rows,0], [0, cols]])
    pts2 = pts1 + delta_pix
    m = cv2.getaffinetransform(pts1, pts2)
    res = cv2.warpaffine(img, m, (rows, cols))
    return res


def affine_dir(img_dir, write_dir, max_delta_pix):
    img_names = os.listdir(img_dir)
    img_names = [img_name for img_name in img_names if img_name.split(".")[-1]=="png"]
    for index, img_name in enumerate(img_names):
        img = cv2.imread(os.path.join(img_dir,img_name))
        save_name = os.path.join(write_dir, img_name.split(".")[0]+"f.png")
        delta_pix = np.float32(np.random.randint(-max_delta_pix,max_delta_pix+1,[3,2]))
        img_a = affine(img, delta_pix)
        cv2.imwrite(save_name, img_a)

Python3+OpenCV实现简单交通标志识别流程分析

二、hog特征提取

处理好图片后分别对训练集和测试集进行特征提取得到imgtest_hog.txt和imgtrain_hog.txt

def hog_feature(img_array, resize=(64,64)):
    ##提取hog特征

    img = cv2.cvtcolor(img_array, cv2.color_bgr2gray)
    img = cv2.resize(img, resize)
    bins = 9
    cell_size = (8, 8)
    cpb = (2, 2)
    norm = "l2"
    features = ft.hog(img, orientations=bins, pixels_per_cell=cell_size, 
                        cells_per_block=cpb, block_norm=norm, transform_sqrt=true)
    return features

def extra_hog_features_dir(img_dir, write_txt, resize=(64,64)):
    ##提取目录中所有图像hog特征
   
    img_names = os.listdir(img_dir)
    img_names = [os.path.join(img_dir, img_name) for img_name in img_names]
    if os.path.exists(write_txt):
        os.remove(write_txt)
    
    with open(write_txt, "a") as f:
        index = 0
        for img_name in img_names:
            img_array = cv2.imread(img_name)
            features = hog_feature(img_array, resize)
            label_name = img_name.split("/")[-1].split("_")[0]
            label_num = img_label[label_name]
            row_data = img_name + "\t" + str(label_num) + "\t"
            
            for element in features:
                row_data = row_data + str(round(element,3)) + " "
            row_data = row_data + "\n"
            f.write(row_data)
            
            if index%100 == 0:
                print ("total image number = ", len(img_names), "current image number = ", index)
            index += 1

三、模型训练

利用得到的hog特征进行训练模型得到svm_model.pkl

def load_hog_data(hog_txt):

    img_names = []
    labels = []
    hog_features = []
    with open(hog_txt, "r") as f:
        data = f.readlines()
        for row_data in data:
            row_data = row_data.rstrip()
            img_path, label, hog_str = row_data.split("\t")
            img_name = img_path.split("/")[-1]
            hog_feature = hog_str.split(" ")
            hog_feature = [float(hog) for hog in hog_feature]
            #print "hog feature length = ", len(hog_feature)
            img_names.append(img_name)
            labels.append(label)
            hog_features.append(hog_feature)
    return img_names, np.array(labels), np.array(hog_features)

def svm_train(hog_features, labels, save_path="./svm_model.pkl"):

    clf = svc(c=10, tol=1e-3, probability = true)
    clf.fit(hog_features, labels)
    joblib.dump(clf, save_path)
    print ("finished.")

四、交通标志识别及实验测试

交通标志识别的流程:颜色识别得到阈值范围内的二值图、然后进行轮廓识别、剔除多余矩阵。

def preprocess_img(imgbgr):
        ##将图像由rgb模型转化成hsv模型
    imghsv = cv2.cvtcolor(imgbgr, cv2.color_bgr2hsv)
    bmin = np.array([110, 43, 46])
    bmax = np.array([124, 255, 255])
        ##使用inrange(hsv,lower,upper)设置阈值去除背景颜色
    img_bbin = cv2.inrange(imghsv,bmin, bmax)
    rmin2 = np.array([165, 43, 46])
    rmax2 = np.array([180, 255, 255])
    img_rbin = cv2.inrange(imghsv,rmin2, rmax2)
    img_bin = np.maximum(img_bbin, img_rbin)
    return img_bin

'''
提取轮廓,返回轮廓矩形框
'''
def contour_detect(img_bin, min_area=0, max_area=-1, wh_ratio=2.0):
    rects = []
    ##检测轮廓,其中cv2.retr_external只检测外轮廓,cv2.chain_approx_none 存储所有的边界点
    ##findcontours返回三个值:第一个值返回img,第二个值返回轮廓信息,第三个返回相应轮廓的关系
    contours, hierarchy= cv2.findcontours(img_bin.copy(), cv2.retr_external, cv2.chain_approx_none)
    if len(contours) == 0:
        return rects
    max_area = img_bin.shape[0]*img_bin.shape[1] if max_area<0 else max_area
    for contour in contours:
        area = cv2.contourarea(contour)
        if area >= min_area and area <= max_area:
            x, y, w, h = cv2.boundingrect(contour)
            if 1.0*w/h < wh_ratio and 1.0*h/w < wh_ratio:
                rects.append([x,y,w,h])
    return rects

然后加载模型进行测验

if __name__ == "__main__":
    cap = cv2.videocapture(0)
    cv2.namedwindow('camera')
    cv2.resizewindow("camera",640,480)
    cols = int(cap.get(cv2.cap_prop_frame_width))
    rows = int(cap.get(cv2.cap_prop_frame_height))
    clf = joblib.load("/home/xiao5/desktop/test2/svm_model.pkl")
    i=0
    while (1):
        i+=1
        ret, img = cap.read()
        img_bin = preprocess_img(img)
        min_area = img_bin.shape[0]*img.shape[1]/(25*25)
        rects = contour_detect(img_bin, min_area=min_area)
        if rects:
            max_x=0
            max_y=0
            max_w=0
            max_h=0
            for r in rects:
                if r[2]*r[3]>=max_w*max_h:
                    max_x,max_y,max_w,max_h=r
            proposal = img[max_y:(max_y+max_h),max_x:(max_x+max_w)]##用numpy数组对图像像素进行访问时,应该先写图像高度所对应的坐标(y,row),再写图像宽度对应的坐标(x,col)。
            cv2.rectangle(img,(max_x,max_y), (max_x+max_w,max_y+max_h), (0,255,0), 2)
            cv2.imshow("proposal", proposal)
            cls_prop = hog_extra_and_svm_class(proposal, clf)
            cls_prop = np.round(cls_prop, 2)
            cls_num = np.argmax(cls_prop)##找到最大相似度的索引
            if cls_names[cls_num] is not "background": 
                print(cls_names[cls_num])
            else:
                print("n/a")
        cv2.imshow('camera',img)
        cv2.waitkey(40)
    cv2.destroyallwindows()
    cap.release()

Python3+OpenCV实现简单交通标志识别流程分析

到此这篇关于python3+opencv实现简单交通标志识别的文章就介绍到这了,更多相关python3 opencv交通标志识别内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!