mask rcnn 将mask 转json数据02

1、功能:当 .json _json  pic文件放置一起的时候,进行分类

#功能:当 .json _json  pic文件放置一起的时候,进行分类

import os 
import shutil
path0='test/'
filelist=os.listdir(path0)
for dir_name in filelist:
    print(dir_name)
    name=dir_name[-5:]
    if name=='.json':
        shutil.move(path0+dir_name, 'json/' )
    elif name=='_json':
        shutil.move(path0+dir_name, 'labelme_json/' )
    else:
        shutil.move(path0+dir_name, 'pic/' )

2、#提取10分之一的图片  需要两份就运行两次

#功能:当 .json _json  pic文件放置一起的时候,进行分类

import os 
import shutil
path0='json/'
path1='pic/'
path2='cv2_mask/'
path3='labelme_json/'
path4='mask/'


dirs='./test/json/'
dirs1='./test/pic/'
dirs2='./test/cv2_mask/'
dirs3='./test/labelme_json/'
dirs4='./test/mask/'

if not os.path.exists(dirs):
    os.makedirs(dirs)
if not os.path.exists(dirs1):
    os.makedirs(dirs1)
if not os.path.exists(dirs2):
    os.makedirs(dirs2)
if not os.path.exists(dirs3):
    os.makedirs(dirs3)
if not os.path.exists(dirs4):
    os.makedirs(dirs4)
filelist=os.listdir(path0)
for i in range(0,len(filelist),10):
    name0 = filelist[i].split('.',3)[0]
    shutil.move(path0+filelist[i], dirs )
    shutil.move(path1+name0+'.jpg', dirs1 )
    shutil.move(path2+name0+'.png', dirs2 )
    shutil.move(path3+name0+'_json', dirs3 )
    shutil.move(path4+name0+'.png', dirs4 )
    print(i)

3、针对mask 统计其信息, 针对一个bbox的检测程序,输出x , y, w, h 文件名

#针对一个box的检测程序,输出x , y, w, h 文件名

import cv2
import os
import operator
path='mask/'
 

def takeSecond1(elem):          
    return list(elem)[2]*list(elem)[3]
list1=[ ]
filelist = os.listdir(path)
for item in filelist:

    bgr_img = cv2.imread(path+item)
    gray_img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2GRAY)
    gray_img = cv2.blur(gray_img, (5,5))
    th, binary = cv2.threshold(gray_img, 125, 255, cv2.THRESH_OTSU)
    contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    bounding_boxes = [cv2.boundingRect(cnt) for cnt in contours]
 
    bounding_boxes.sort(key=takeSecond1)
    list1.append([item,bgr_img.shape,bounding_boxes[-1]])
 
    # cv2.drawContours(bgr_img, contours, -1, (0, 0, 255), 3)
    # for i in range(len(bounding_boxes)):
    #     print(i)
 
    # cv2.imshow("name", bgr_img)
    # cv2.waitKey(6000)
    # cv2.destroyAllWindows()
#针对一个box的检测程序,输出x , y, w, h 文件名

import cv2
import os
import operator
path='C:/Users/11549/Desktop/dataset/CASIA_2.0/mask/'
 

def takeSecond1(elem):          
    return list(elem)[2]*list(elem)[3]
list1=[ ]
filelist = os.listdir(path)
for item in filelist:

    bgr_img = cv2.imread(path+item)
    gray_img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2GRAY)
    gray_img = cv2.blur(gray_img, (5,5))
    
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 2))
    dst = cv2.dilate(gray_img, kernel)
    # dst = cv2.erode(binary, kernel)
    
    th, binary = cv2.threshold(dst, 254, 255, cv2.THRESH_OTSU)
    contours, hierarchy = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    new_contours=[]
    for i in range(len(contours)):
        area = cv2.contourArea(contours[i])
        if area>50:
            new_contours.append(contours[i])
            bounding_box=cv2.boundingRect(contours[i])
            list1.append([item,bgr_img.shape,bounding_box])
            
 
    # bounding_boxes = [cv2.boundingRect(cnt) for cnt in new_contours]
 
    # bounding_boxes.sort(key=takeSecond1) #取点数最多的框
    # list1.append([item,bgr_img.shape,bounding_boxes])
 
    # cv2.drawContours(bgr_img, new_contours, -1, (0, 0, 255), 3)
 
    # cv2.imshow("name", bgr_img)
    # cv2.waitKey(6000)
    # cv2.destroyAllWindows()

 

你可能感兴趣的:(python,mask制作,python)