[openCV/Python]基于openCV的图片区域标注系统V1

应用范围:包括但不限于YOLOV3的真实框标注


【——目录——】

一、import相关库

二、声明全局变量

三、定义tagging函数以进行标注

四、定义getFileList函数以进行文件遍历

五、遍历文件并进行标注


一、import相关库

import cv2 as cv
import os

完成遍历文件并进行标注功能需要用到opencv(即cv2)库和os库


 二、声明全局变量

使用全局变量便于基于该系统进行数据输出

global x1_onmouse
global y1_onmouse
global x2_onmouse
global y2_onmouse

x1_onmouse = 0
x2_onmouse = 0
y1_onmouse = 0
y2_onmouse = 0

 三、定义tagging函数以进行标注

按下,松开鼠标左键以分别创建矩形的对角线上的两个顶点

def tagging(img):
    def onmouse(event, x, y, flags, param):
        cv.imshow("img", img)

        if event == cv.EVENT_LBUTTONDOWN:
            global x1_onmouse
            global y1_onmouse
            x1_onmouse = x
            y1_onmouse = y
        if event == cv.EVENT_LBUTTONUP:
            global x2_onmouse
            global y2_onmouse
            x2_onmouse = x
            y2_onmouse = y
            if x2_onmouse < x1_onmouse:
                t = x1_onmouse
                x1_onmouse = x2_onmouse
                x2_onmouse = t
            if y2_onmouse < y1_onmouse:
                t = y1_onmouse
                y1_onmouse = y2_onmouse
                y2_onmouse = t
            strtext1 = "(%s,%s)" % (x1_onmouse, y1_onmouse)
            strtext2 = "(%s,%s)" % (x2_onmouse, y2_onmouse)
            strtext3 = "(%s,%s)" % (x1_onmouse, y2_onmouse)
            strtext4 = "(%s,%s)" % (x2_onmouse, y1_onmouse)
            cv.imshow("img", cv.rectangle(img, (x1_onmouse, y1_onmouse), (x2_onmouse, y2_onmouse), (0, 0, 255), 1))

            cv.circle(img, (x1_onmouse, y1_onmouse), 1, (0, 0, 255), -1)
            cv.putText(img, strtext1, (x1_onmouse + 2, y1_onmouse + 15), cv.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 200), 1)
            cv.circle(img, (x2_onmouse, y2_onmouse), 1, (0, 0, 255), -1)
            cv.putText(img, strtext2, (x2_onmouse + 2, y2_onmouse + 15), cv.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 200), 1)
            cv.circle(img, (x1_onmouse, y2_onmouse), 1, (0, 0, 255), -1)
            cv.putText(img, strtext3, (x1_onmouse + 2, y2_onmouse + 15), cv.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 200), 1)
            cv.circle(img, (x2_onmouse, y1_onmouse), 1, (0, 0, 255), -1)
            cv.putText(img, strtext4, (x2_onmouse + 2, y1_onmouse + 15), cv.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 200), 1)
            strtext_lu = "(%s,%s)" % (x1_onmouse, y1_onmouse)
            strtext_rd = "(%s,%s)" % (x2_onmouse, y2_onmouse)
            print("\n左上角顶点为" + strtext_lu)
            print("右下角顶点为" + strtext_rd)

    print("shape为" + str(img.shape))
    cv.namedWindow("img")
    cv.setMouseCallback("img", onmouse)

    cv.waitKey(0)
    cv.destroyAllWindows()

四、定义getFileList函数以进行文件遍历

def getFileList(dir, Filelist, ext=None):
    """
    获取文件夹及其子文件夹中文件列表
    输入 dir:文件夹根目录
    输入 ext: 扩展名
    返回: 文件路径列表
    """
    newDir = dir
    if os.path.isfile(dir):
        if ext is None:
            Filelist.append(dir)
        else:
            if ext in dir[-3:]:
                Filelist.append(dir)

    elif os.path.isdir(dir):
        for s in os.listdir(dir):
            newDir = os.path.join(dir, s)
            getFileList(newDir, Filelist, ext)

    return Filelist

[openCV/Python]基于openCV的图片区域标注系统V1_第1张图片


 五、遍历文件并进行标注

path_image = 'D:\pythonProject4_camera\\beforeGroundTruthBoxImage'
imgfolder = path_image  # 搜索路径,会自动搜索子文件夹
imglist = getFileList(imgfolder, [])  # 取出文件路径,为字符串类型数组
print("\n检测到" + imgfolder + "下有" + str(len(imglist)) + '个文件')  # 输出文件数量
img_num = 0
for imgpath in imglist:
    img_num+=1
    img = cv.imread(path_image+"\\"+str(img_num)+".jpg")
    tagging(img)

 


 (·——·)感谢观看!

你可能感兴趣的:(Python,openCV,opencv,python,计算机视觉)