蓝天分割(去白云)

蓝色识别

def test():
    image_name = sys.argv[1]
    img = cv2.imread(image_name)  
    height, width = img.shape[:2]
    #size = (int(width * 0.1), int(height * 0.1))  # bgr
    #img = cv2.resize(img, size, interpolation=cv2.INTER_AREA)

    HSV=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
    cv2.imshow("imageHSV",HSV)
    cv2.imshow('image',img)
    color = [
        ([95,60,60], [125, 255, 255])  # white: [100,0,200], [119, 90, 255]
    ]

    for (lower, upper) in color:
        lower = np.array(lower, dtype="uint8")  
        upper = np.array(upper, dtype="uint8") 

        mask = cv2.inRange(HSV, lower, upper)    
        #mask = 255-mask                          
        output = cv2.bitwise_and(img, img, mask=mask)    
        #bgroutput = cv2.cvtColor(output,cv2.COLOR_HSV2BGR)

        cv2.imshow("images", np.hstack([img, output]))
        contours, hierarchy = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        #print(mask.shape)
        #print(mask[0])
        #print(len(contours))
        cv2.drawContours(img, contours, -1, (0, 0, 255), 1)
        for i in contours:
            #print(cv2.contourArea(i))  
            x, y, w, h = cv2.boundingRect(i)  
            cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 1)
        #cv.imwrite(show_result_path, match_img_color)
        cv2.imshow("detect",img)
        cv2.imshow("chanle", img)
        cv2.waitKey(0)
    cv2.waitKey(0)

阿里天空分割

#!/usr/bin/env python
#coding:utf-8

import os
import json
import time
import requests
from viapi.fileutils import FileUtils
from aliyunsdkcore.client import AcsClient
from aliyunsdkimageseg.request.v20191230 import SegmentSkyRequest

if __name__ == "__main__":

    input_dir = "/data/xywang/dataset/Segmentation/sky/crawler_sky/xywang_20210714_backup"
    save_dir  = "tmp"   

    if not os.path.exists(input_dir):
        print("[Error]: please input correct images directory!")
        exit(1)
    if not os.path.exists(save_dir):
        os.makedirs(save_dir)
    allList = os.listdir(input_dir)

    id_file = open("/data/xywang/dataset/Segmentation/sky/crawler_sky/xywang_20210714_data/data_list/train_filter_aliyun.txt","r")
    id_list = id_file.read().splitlines()
    id_list = [i+".jpg" for i in id_list]
    allList = list(set(allList).intersection(set(id_list)))
    print(len(allList))

    for img in allList:
        img = os.path.join(input_dir, img)
        file_utils = FileUtils("xxx",
                               "xxx")
        client     = AcsClient("LTAI5tLinFNbsCtKe3ZwtqNa",
                               "xxx",
                               "cn-shanghai")
        request    = SegmentSkyRequest.SegmentSkyRequest()
        request.set_accept_format('json')
        img_base = os.path.splitext(os.path.basename(img))
        img_file = img.split('/')[-1].replace(img_base[-1], '.png')
        save_path = os.path.join(save_dir, img_file)
        targer_list = os.listdir(save_dir)
        if img_file in targer_list:
            continue
        print(img)
        try:
            oss_url = file_utils.get_oss_url(img, img_base[-1], True)
            request.set_ImageURL(oss_url)
            response = client.do_action_with_exception(request)
            new_url = json.loads(response, encoding='utf-8')['Data']['ImageURL']
            photo = requests.get(new_url)
            with open(save_path, 'wb')as f:
                f.write(photo.content)
        except Exception as e:
            time.sleep(1)
            try:
                oss_url = file_utils.get_oss_url(img, img_base[-1], True)
                request.set_ImageURL(oss_url)
                response = client.do_action_with_exception(request)
                new_url = json.loads(response, encoding='utf-8')['Data']['ImageURL']
                photo = requests.get(new_url)
                with open(save_path, 'wb')as f:
                    f.write(photo.content)
            except Exception as e:
                print(img, e)
                continue
            

合并代码

#encoding:utf-8		
import numpy as np
import cv2
import sys
import os
import threadpool

def test():
    image_name = sys.argv[1]
    img = cv2.imread(image_name)  
    height, width = img.shape[:2]
    #size = (int(width * 0.1), int(height * 0.1))  # bgr
    #img = cv2.resize(img, size, interpolation=cv2.INTER_AREA)

    HSV=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
    cv2.imshow("imageHSV",HSV)
    cv2.imshow('image',img)
    color = [
        ([95,60,60], [125, 255, 255])  # white: [100,0,200], [119, 90, 255]
    ]

    for (lower, upper) in color:
        lower = np.array(lower, dtype="uint8")  
        upper = np.array(upper, dtype="uint8") 

        mask = cv2.inRange(HSV, lower, upper)    
        #mask = 255-mask                          
        output = cv2.bitwise_and(img, img, mask=mask)    
        #bgroutput = cv2.cvtColor(output,cv2.COLOR_HSV2BGR)

        cv2.imshow("images", np.hstack([img, output]))
        contours, hierarchy = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        #print(mask.shape)
        #print(mask[0])
        #print(len(contours))
        cv2.drawContours(img, contours, -1, (0, 0, 255), 1)
        for i in contours:
            #print(cv2.contourArea(i))  
            x, y, w, h = cv2.boundingRect(i)  
            cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 1)
        #cv.imwrite(show_result_path, match_img_color)
        cv2.imshow("detect",img)
        cv2.imshow("chanle", img)
        cv2.waitKey(0)
    cv2.waitKey(0)

def get_dir_mask():
    image_dir = "/data/xywang/dataset/Segmentation/sky/crawler_sky/xywang_20210714_backup"
    image_list = os.listdir(image_dir)
    image_list.sort(key= lambda x:int("".join(list(filter(str.isdigit, x)))))

    lower, upper = [95,90,80], [125, 255, 255] # white: [100,0,200], [119, 90, 255],bule sky:[95,90,80], [125, 255, 255]
    lower = np.array(lower, dtype="uint8")
    upper = np.array(upper, dtype="uint8")

    for image_name in image_list:
        full_image_name = os.path.join(image_dir, image_name)
        image_bgr = cv2.imread(full_image_name)
        image_hsv=cv2.cvtColor(image_bgr, cv2.COLOR_BGR2HSV)
        
        mask = cv2.inRange(image_hsv, lower, upper)
        # mask = 255 - mask
        
        cv2.imshow("mask",mask)
        cv2.imshow("bgr",image_bgr)
        key = cv2.waitKey(0)
        if key == 27:
            break

def get_data_list():
    filename = "/data/xywang/dataset/Segmentation/sky/crawler_sky/xywang_20210714_data/data_list/train.txt"
    image_dir = "/data/xywang/dataset/Segmentation/sky/crawler_sky/xywang_20210714_data/images_clean"
    image_list = os.listdir(image_dir)

    with open(filename,'w') as f:
        for i in image_list:
            f.write(i[:-4])
            f.write("\n")

def conbine_aliyun():
    image_dir = "/data/ISP/AWB/sky/xywang_20210714_sky_seg"
    save_dir_vis = "/data/xywang/dataset/Segmentation/sky/crawler_sky/xywang_20210714_data/vis"
    save_dir_images = "/data/xywang/dataset/Segmentation/sky/crawler_sky/xywang_20210714_data/images"
    save_dir_masks_noclouds = "/data/xywang/dataset/Segmentation/sky/crawler_sky/xywang_20210714_data/mask_noclouds"
    save_dir_masks = "/data/xywang/dataset/Segmentation/sky/crawler_sky/xywang_20210714_data/mask"

    if not os.path.exists(save_dir_vis):
        os.makedirs(save_dir_vis)
    if not os.path.exists(save_dir_images):
        os.makedirs(save_dir_images)
    if not os.path.exists(save_dir_masks):
        os.makedirs(save_dir_masks)
    image_list = os.listdir(image_dir)
    image_list.sort(key= lambda x:int("".join(list(filter(str.isdigit, x)))))

    lower, upper = [90,60,60], [125, 255, 255] # white: [100,0,200], [119, 90, 255],bule sky:[95,40,80], [125, 255, 255]
    lower = np.array(lower, dtype="uint8")
    upper = np.array(upper, dtype="uint8")

    for image_name in image_list:
        print(image_name)
        full_image_name = os.path.join(image_dir, image_name)
        image = cv2.imread(full_image_name,-1)
        if image.shape[2] == 4:
            image_bgr = image[:,:,0:3]
            image_mask = image[:,:,3]
            image_mask[image_mask==255] = 2
            cv2.imwrite(os.path.join(save_dir_masks,image_name[:-4]+".png"),image_mask)
            
            image_hsv=cv2.cvtColor(image_bgr, cv2.COLOR_BGR2HSV)
            hsv_mask = cv2.inRange(image_hsv, lower, upper)

            image_mask_final = np.multiply(image_mask,hsv_mask)
            image_mask_final[image_mask_final !=0] = 255
            output = cv2.bitwise_and(image_bgr, image_bgr, mask=image_mask_final)  
            cv2.imwrite(os.path.join(save_dir_vis,image_name),np.hstack([image_bgr, output]))
            cv2.imwrite(os.path.join(save_dir_images,image_name[:-4]+".jpg"),image_bgr)

            image_mask_save = np.multiply(image_mask,hsv_mask)
            image_mask_save[image_mask_save !=0] = 2
            cv2.imwrite(os.path.join(save_dir_masks,image_name[:-4]+".png"),image_mask_save)

            # cv2.imshow("contrast", np.hstack([image_bgr, output]))
            # cv2.imshow("image_hsv",image_hsv)
            # cv2.imshow("image_mask_final",image_mask_final)
            # cv2.imshow("mask",image_mask)
            # cv2.imshow("hsv_mask",hsv_mask)
            # cv2.imshow("bgr",image_bgr)
            # gray = cv2.cvtColor(image_bgr,cv2.COLOR_BGR2GRAY)
            # cv2.imshow("gray",gray)
            # key = cv2.waitKey(0)
            # if key == 27:
            #     break
    get_data_list()

def process(image_name):
    print(image_name)
    image_dir = "/home/xywang/code/tools/aliyun_sky_demo/tmp"
    save_dir_vis = "/data/xywang/dataset/Segmentation/sky/crawler_sky/xywang_20210714_data/vis"
    save_dir_images = "/data/xywang/dataset/Segmentation/sky/crawler_sky/xywang_20210714_data/images_clean"
    save_dir_masks_noclouds = "/data/xywang/dataset/Segmentation/sky/crawler_sky/xywang_20210714_data/masks_clean_noclouds_"
    save_dir_masks = "/data/xywang/dataset/Segmentation/sky/crawler_sky/xywang_20210714_data/masks_clean"
    
    lower = np.array([90,60,60], dtype="uint8")
    upper = np.array([125, 255, 255], dtype="uint8")

    full_image_name = os.path.join(image_dir, image_name)
    image = cv2.imread(full_image_name,-1)
    if image.shape[2] == 4:
        image_bgr = image[:,:,0:3]
        image_mask = image[:,:,3]
        image_mask[image_mask==255] = 2
        #cv2.imwrite(os.path.join(save_dir_masks,image_name[:-4]+".png"),image_mask)
        image_hsv=cv2.cvtColor(image_bgr, cv2.COLOR_BGR2HSV)
        hsv_mask = cv2.inRange(image_hsv, lower, upper)

        image_mask_vis = np.multiply(image_mask,hsv_mask)
        image_mask_vis[image_mask_vis !=0] = 255
        # cv2.imshow("test",image_mask_vis)
        # key = cv2.waitKey(0)
        # if key == 27:
        #     assert False
        output = cv2.bitwise_and(image_bgr, image_bgr, mask=image_mask_vis)  
        #cv2.imwrite(os.path.join(save_dir_vis,image_name),np.hstack([image_bgr, output]))
        #cv2.imwrite(os.path.join(save_dir_images,image_name[:-4]+".jpg"),image_bgr)

        image_mask_save = np.multiply(image_mask,hsv_mask)
        image_mask_save[image_mask_save !=0] = 2
        #cv2.imwrite(os.path.join(save_dir_masks_noclouds,image_name[:-4]+".png"),image_mask_save)

def multi_thread_conbine():
    image_dir = "/home/xywang/code/tools/aliyun_sky_demo/tmp"
    save_dir_vis = "/data/xywang/dataset/Segmentation/sky/crawler_sky/xywang_20210714_data/vis"
    save_dir_images = "/data/xywang/dataset/Segmentation/sky/crawler_sky/xywang_20210714_data/images_clean"
    save_dir_masks_noclouds = "/data/xywang/dataset/Segmentation/sky/crawler_sky/xywang_20210714_data/masks_clean_noclouds_"
    save_dir_masks = "/data/xywang/dataset/Segmentation/sky/crawler_sky/xywang_20210714_data/masks_clean"
    if not os.path.exists(save_dir_vis):
        os.makedirs(save_dir_vis)
    if not os.path.exists(save_dir_images):
        os.makedirs(save_dir_images)
    if not os.path.exists(save_dir_masks_noclouds):
        os.makedirs(save_dir_masks_noclouds)
    if not os.path.exists(save_dir_masks):
        os.makedirs(save_dir_masks)

    image_list = os.listdir(image_dir)
    image_list.sort(key= lambda x:int("".join(list(filter(str.isdigit, x)))))

    pool = threadpool.ThreadPool(8)
    requests = threadpool.makeRequests(process, image_list)
    [pool.putRequest(req) for req in requests]
    pool.wait()

    #get_data_list()



if __name__ == "__main__":
    #test()
    # get_dir_mask()
    #conbine_aliyun()
    multi_thread_conbine()

蓝天加人(皮肤)数据合成

import cv2
import numpy as np
import random
import imgviz
import os

def vis(lbl, img):
    viz = imgviz.label2rgb(
                label=lbl,
                img=imgviz.rgb2gray(img),
                font_size=15,
                loc="rb",
            )
    return viz

def get_crop_bbox(image, target_w, target_h):
    if image.shape[1] - target_w > 0:
        x1 = np.random.randint(image.shape[1] - target_w)
    else: 
        x1 = 0
    if image.shape[0] - target_h > 0:
        y1 = np.random.randint(image.shape[0] - target_h)
    else:
        y1 = 0
    x2 = x1 + target_w
    y2 = y1 + target_h
    
    return [x1, y1, x2, y2]

def crop(image,bbox):
    x1 = bbox[0]
    y1 = bbox[1]
    x2 = bbox[2]
    y2 = bbox[3]
    return image[y1:y2,x1:x2]

def test():
    person_images_labels = "/home/xywang/code/tools/aliyun_seg/aliyun_person_demo/tmp/kinect_0335.png"
    person_images = "/data/xywang/dataset/Segmentation/skin/skin_voc_seg_12dataset/VOCdevkit/VOC2012/JPEGImages/kinect_0335.jpg"
    skin_labels = "/data/xywang/dataset/Segmentation/skin/skin_voc_seg_12dataset/VOCdevkit/VOC2012/SegmentationClassRaw/kinect_0335.png"
    sky_images = "/data/xywang/dataset/Segmentation/sky/crawler_sky/xywang_20210714_data/images_clean/xywang20210714_15069_resize.jpg"
    sky_labels = "/data/xywang/dataset/Segmentation/sky/crawler_sky/xywang_20210714_data/masks_clean/xywang20210714_15069_resize.png"

    label_person = cv2.imread(person_images_labels,-1)[:,:,3]
    skin_label = cv2.imread(skin_labels,-1)

    assert label_person.shape == skin_label.shape

    image_sky = cv2.imread(sky_images)
    label_sky = cv2.imread(sky_labels,-1)
    
    
    image_person = cv2.imread(person_images)
    print(image_sky.shape,image_person.shape)
    target_w, target_h = image_person.shape[1], image_person.shape[0]

    resize_radio = max(target_h / image_sky.shape[0], target_w / image_sky.shape[1])
    if resize_radio > 1:
        print("bule sky shape small")
        print(int(image_sky.shape[1]*resize_radio)+1,int(image_sky.shape[0]*resize_radio)+1)
        image_sky = cv2.resize(image_sky,(int(image_sky.shape[1]*resize_radio)+1,int(image_sky.shape[0]*resize_radio)+1))
        label_sky = cv2.resize(label_sky,(int(label_sky.shape[1]*resize_radio)+1,int(label_sky.shape[0]*resize_radio)+1),interpolation=cv2.INTER_NEAREST)

    crop_bbox = get_crop_bbox(image_sky, target_w, target_h)
    for _ in range(5):
        seg_temp = crop(label_sky, crop_bbox)
        labels, cnt = np.unique(seg_temp, return_counts=True)
        cnt = cnt[labels != 255]
        print(len(cnt) > 1 and np.max(cnt) / np.sum(cnt))
        if len(cnt) > 1 and np.max(cnt) / np.sum(
                cnt) < 0.75:
            break
        crop_bbox = get_crop_bbox(image_sky, target_w, target_h)
        print("crop_bbox:",crop_bbox)

    image_sky = crop(image_sky, crop_bbox)
    label_sky = crop(label_sky, crop_bbox)

    label_person[label_person!=0] = 255
    image_person = cv2.bitwise_and(image_person,image_person,mask = label_person)
    label_person_inv = cv2.bitwise_not(label_person)
    image_sky = cv2.bitwise_and(image_sky,image_sky,mask = label_person_inv)
    label_sky = cv2.bitwise_and(label_sky,label_sky,mask = label_person_inv)
    image_compose = cv2.add(image_sky,image_person)

    cv2.imshow("image_compose",image_compose)
    cv2.imshow("label_person_inv",label_person_inv)
    cv2.imshow("label_person",label_person)
    cv2.imshow("image_sky",image_sky)
    cv2.imshow("image_person",image_person)
    

    label_compose = label_sky + skin_label
    label_compose[label_compose == 3] = 1
    show_compose = vis(label_compose, image_compose)
    cv2.imshow("show_compose",show_compose)
    cv2.waitKey(0)

def main():
    person_images_labels_onlyperson_dir = "/home/xywang/code/tools/aliyun_seg/aliyun_person_demo/tmp"
    person_images_dir = "/data/xywang/dataset/Segmentation/skin/skin_voc_seg_12dataset/VOCdevkit/VOC2012/JPEGImages"
    skin_labels_dir = "/data/xywang/dataset/Segmentation/skin/skin_voc_seg_12dataset/VOCdevkit/VOC2012/SegmentationClassRaw"
    sky_images_dir = "/data/xywang/dataset/Segmentation/sky/crawler_sky/xywang_20210714_data/images_clean"
    sky_labels_dir = "/data/xywang/dataset/Segmentation/sky/crawler_sky/xywang_20210714_data/masks_clean"
    sky_labels_noclouds_dir = "/data/xywang/dataset/Segmentation/sky/crawler_sky/xywang_20210714_data/masks_clean_nocloudsv2"
    save_images_dir = "/data/xywang/dataset/Segmentation/mix_skin_sky/images"
    save_labels_dir = "/data/xywang/dataset/Segmentation/mix_skin_sky/labels"
    save_labels_noclouds_dir = "/data/xywang/dataset/Segmentation/mix_skin_sky/labels_noclouds"

    skin_id_list = [i[:-4] for i in os.listdir(person_images_labels_onlyperson_dir)]
    sky_id_list = [i[:-4] for i in os.listdir(sky_images_dir)]
    if len(sky_id_list) < len(skin_id_list):
        L = len(skin_id_list)-len(sky_id_list)
        start = np.random.randint(L)
        sky_id_list.extend(sky_id_list[start:start+L])

    print("skin_id_list:",len(skin_id_list),"sky_id_list:",len(sky_id_list))

    for index, id in enumerate(skin_id_list):
        print(index)
        person_images_labels = os.path.join(person_images_labels_onlyperson_dir,id+".png")
        person_images = os.path.join(person_images_dir,id+".jpg")
        skin_labels = os.path.join(skin_labels_dir,id+".png")
        sky_images = os.path.join(sky_images_dir,sky_id_list[index]+".jpg")
        sky_labels = os.path.join(sky_labels_dir,sky_id_list[index]+".png")
        sky_labels_noclouds = os.path.join(sky_labels_noclouds_dir,sky_id_list[index]+".png")

        label_person = cv2.imread(person_images_labels,-1)[:,:,3]
        skin_label = cv2.imread(skin_labels,-1)
        # print(person_images_labels, skin_labels,sky_images)
        assert label_person.shape == skin_label.shape

        image_sky = cv2.imread(sky_images)
        label_sky = cv2.imread(sky_labels,-1)
        label_noclouds_sky = cv2.imread(sky_labels_noclouds,-1)
        image_person = cv2.imread(person_images)

        target_w, target_h = image_person.shape[1], image_person.shape[0]

        resize_radio = max(target_h / image_sky.shape[0], target_w / image_sky.shape[1])
        if resize_radio >= 1:
            # print("bule sky shape small")
            image_sky = cv2.resize(image_sky,(int(image_sky.shape[1]*resize_radio)+1,int(image_sky.shape[0]*resize_radio)+1))
            label_sky = cv2.resize(label_sky,(int(label_sky.shape[1]*resize_radio)+1,int(label_sky.shape[0]*resize_radio)+1),interpolation=cv2.INTER_NEAREST)
            label_noclouds_sky = cv2.resize(label_noclouds_sky,(int(label_noclouds_sky.shape[1]*resize_radio)+1,int(label_noclouds_sky.shape[0]*resize_radio)+1),interpolation=cv2.INTER_NEAREST)
            # print(image_sky.shape)

        crop_bbox = get_crop_bbox(image_sky, target_w, target_h)
        for _ in range(5):
            seg_temp = crop(label_sky, crop_bbox)
            labels, cnt = np.unique(seg_temp, return_counts=True)
            cnt = cnt[labels != 255]
            # print(np.max(cnt) / np.sum(cnt))
            if len(cnt) > 1 and np.max(cnt) / np.sum(
                    cnt) < 0.75:
                break
            crop_bbox = get_crop_bbox(image_sky, target_w, target_h)
            # print("crop_bbox:",crop_bbox)

        image_sky = crop(image_sky, crop_bbox)
        label_sky = crop(label_sky, crop_bbox)
        label_noclouds_sky = crop(label_noclouds_sky, crop_bbox)

        label_person[label_person!=0] = 255
        image_person = cv2.bitwise_and(image_person,image_person,mask = label_person)
        label_person_inv = cv2.bitwise_not(label_person)
        image_sky = cv2.bitwise_and(image_sky,image_sky,mask = label_person_inv)
        label_sky = cv2.bitwise_and(label_sky,label_sky,mask = label_person_inv)
        label_noclouds_sky = cv2.bitwise_and(label_noclouds_sky,label_noclouds_sky,mask = label_person_inv)
        image_compose = cv2.add(image_sky,image_person)

        #cv2.imshow("image_compose",image_compose)
        # cv2.imshow("label_person_inv",label_person_inv)
        # cv2.imshow("label_person",label_person)
        # cv2.imshow("image_sky",image_sky)
        # cv2.imshow("image_person",image_person)

        label_compose = label_sky + skin_label
        label_compose[label_compose == 3] = 1
        label_noclouds_compose = label_noclouds_sky + skin_label
        label_noclouds_compose[label_noclouds_compose == 3] = 1

        # show_compose = vis(label_compose, image_compose)
        # cv2.imshow("show_compose",show_compose)
        # show_compose_noclouds = vis(label_noclouds_compose, image_compose)
        # cv2.imshow("show_compose_noclouds",show_compose_noclouds)
        # key = cv2.waitKey(0)
        # if key == 27:
        #     break

        cv2.imwrite(os.path.join(save_images_dir,"compose_05%d.jpg" % index),image_compose)
        cv2.imwrite(os.path.join(save_labels_dir,"compose_05%d.png" % index),label_compose)
        cv2.imwrite(os.path.join(save_labels_noclouds_dir,"compose_05%d.png" % index),label_noclouds_compose)

if __name__ == "__main__":
    #test()
    main()

获取mask最小矩形框

import cv2
import os
import numpy as np
import imgviz

def vis(lbl, img):
    viz = imgviz.label2rgb(
                label=lbl,
                img=imgviz.rgb2gray(img),
                font_size=15,
                loc="rb",
            )
    return viz

def main():
    person_images_labels_onlyperson_dir = "/home/xywang/code/tools/aliyun_seg/aliyun_person_demo/tmp"
    person_images_dir = "/data/xywang/dataset/Segmentation/skin/skin_voc_seg_12dataset/VOCdevkit/VOC2012/JPEGImages"
    skin_labels_dir = "/data/xywang/dataset/Segmentation/skin/skin_voc_seg_12dataset/VOCdevkit/VOC2012/SegmentationClassRaw"
    save_images_dir = "/data/xywang/dataset/Segmentation/skin/skin_copypaste/person_min_rect/images"
    save_person_labels_dir = "/data/xywang/dataset/Segmentation/skin/skin_copypaste/person_min_rect/person_labels"
    save_skins_labels_dir = "/data/xywang/dataset/Segmentation/skin/skin_copypaste/person_min_rect/skin_labels"

    skin_id_list = [i[:-4] for i in os.listdir(person_images_labels_onlyperson_dir)]

    for index, id in enumerate(skin_id_list):
        print(index, id)
        person_images_labels = os.path.join(person_images_labels_onlyperson_dir,id+".png")
        person_images = os.path.join(person_images_dir,id+".jpg")
        skin_labels = os.path.join(skin_labels_dir,id+".png")

        label_person = cv2.imread(person_images_labels,-1)[:,:,3]
        skin_label = cv2.imread(skin_labels,-1)

        assert label_person.shape == skin_label.shape

        image_person = cv2.imread(person_images)

        target_w, target_h = image_person.shape[1], image_person.shape[0]

        label_person[label_person!=0] = 255
        
        image_person = cv2.bitwise_and(image_person, image_person, mask = label_person)
        label_person_inv = cv2.bitwise_not(label_person)

        try:
            locs = np.where(label_person==255)
            x0 = np.min(locs[1])
            x1 = np.max(locs[1])
            y0 = np.min(locs[0])
            y1 = np.max(locs[0])
            # cv2.rectangle(image_person, (x0,y0), (x1,y1), (255,0,0), 1, 4)

            image_person_target = image_person[y0:y1,x0:x1:]
            label_person = label_person[y0:y1,x0:x1]
            skin_label = skin_label[y0:y1,x0:x1]
            skin_label[label_person==0] = 0

            # compose = vis(skin_label,image_person_target)
            # cv2.imshow("image_person_target",image_person_target)
            # cv2.imshow("compose",compose)
            # key = cv2.waitKey(0)
            # if key == 27:
            #     break

            cv2.imwrite(os.path.join(save_images_dir,"min_rect_%05d.jpg" % index),image_person_target)
            cv2.imwrite(os.path.join(save_skins_labels_dir,"min_rect_%05d.png" % index),skin_label)
            cv2.imwrite(os.path.join(save_person_labels_dir,"min_rect_%05d.png" % index),label_person)
        except:
            print("warning:",id)
        

if __name__ == "__main__":
    main()

copypaste

import cv2
import numpy as np
import random
import imgviz
import os
from tqdm import tqdm

def vis(lbl, img):
    viz = imgviz.label2rgb(
                label=lbl,
                img=imgviz.rgb2gray(img),
                font_size=15,
                loc="rb",
            )
    return viz

def get_crop_bbox(image, target_w, target_h):
    if image.shape[1] - target_w > 0:
        x1 = np.random.randint(image.shape[1] - target_w)
    else: 
        x1 = 0
    if image.shape[0] - target_h > 0:
        y1 = np.random.randint(image.shape[0] - target_h)
    else:
        y1 = 0
    x2 = x1 + target_w
    y2 = y1 + target_h
    
    return [x1, y1, x2, y2]

def crop(image,bbox):
    x1 = bbox[0]
    y1 = bbox[1]
    x2 = bbox[2]
    y2 = bbox[3]
    return image[y1:y2,x1:x2]

def test():
    person_images_labels = "/home/xywang/code/tools/aliyun_seg/aliyun_person_demo/tmp/kinect_0335.png"
    person_images = "/data/xywang/dataset/Segmentation/skin/skin_voc_seg_12dataset/VOCdevkit/VOC2012/JPEGImages/kinect_0335.jpg"
    skin_labels = "/data/xywang/dataset/Segmentation/skin/skin_voc_seg_12dataset/VOCdevkit/VOC2012/SegmentationClassRaw/kinect_0335.png"
    background_images = "/data/xywang/dataset/Segmentation/background/crawler_background/xywang_20210714_data/images_clean/xywang20210714_15069_resize.jpg"

    label_person = cv2.imread(person_images_labels,-1)[:,:,3]
    skin_label = cv2.imread(skin_labels,-1)

    assert label_person.shape == skin_label.shape

    image_background = cv2.imread(background_images)
    
    image_person = cv2.imread(person_images)
    print(image_background.shape,image_person.shape)
    target_w, target_h = image_person.shape[1], image_person.shape[0]

    resize_radio = max(target_h / image_background.shape[0], target_w / image_background.shape[1])
    if resize_radio > 1:
        print("bule background shape small")
        print(int(image_background.shape[1]*resize_radio)+1,int(image_background.shape[0]*resize_radio)+1)
        image_background = cv2.resize(image_background,(int(image_background.shape[1]*resize_radio)+1,int(image_background.shape[0]*resize_radio)+1))

    crop_bbox = get_crop_bbox(image_background, target_w, target_h)

    image_background = crop(image_background, crop_bbox)

    label_person[label_person!=0] = 255
    image_person = cv2.bitwise_and(image_person,image_person,mask = label_person)
    label_person_inv = cv2.bitwise_not(label_person)
    image_background = cv2.bitwise_and(image_background,image_background,mask = label_person_inv)
    image_compose = cv2.add(image_background,image_person)

    cv2.imshow("image_compose",image_compose)
    cv2.imshow("label_person_inv",label_person_inv)
    cv2.imshow("label_person",label_person)
    cv2.imshow("image_background",image_background)
    cv2.imshow("image_person",image_person)
    

    label_compose = skin_label
    label_compose[label_compose !=0] = 1
    show_compose = vis(label_compose, image_compose)
    cv2.imshow("show_compose",show_compose)
    cv2.waitKey(0)

def coco_noperson():
    from pycocotools.coco import COCO

    datasets_list=['train2017'] #val2017
    classes_names = ["person"] 
    dataDir= '/mldb/dataset/COCO/'  

    def id2name(coco):
        classes=dict()
        for cls in coco.dataset['categories']:
            classes[cls['id']]=cls['name']
        return classes

    person_list = []

    all_list = os.listdir("/mldb/dataset/COCO/train2017/")

    for dataset in datasets_list:
        annFile='{}/annotations/instances_{}.json'.format(dataDir,dataset)
        coco = COCO(annFile)
    
        classes = id2name(coco)

        for cls in classes_names:
            cls_id=coco.getCatIds(catNms=[cls])
            img_ids=coco.getImgIds(catIds=cls_id)

            for imgId in tqdm(img_ids):
                img = coco.loadImgs(imgId)[0]
                filename = img['file_name']
                person_list.append(filename)
            
    target_list = list(set(all_list).difference(set(person_list)))

    return target_list

def copypaste_v1():
    person_images_labels_onlyperson_dir = "/home/xywang/code/tools/aliyun_seg/aliyun_person_demo/tmp"
    person_images_dir = "/data/xywang/dataset/Segmentation/skin/skin_voc_seg_12dataset/VOCdevkit/VOC2012/JPEGImages"
    background_images_dir = "/mldb/dataset/COCO/train2017/"
    skin_labels_dir = "/data/xywang/dataset/Segmentation/skin/skin_voc_seg_12dataset/VOCdevkit/VOC2012/SegmentationClassRaw"
    save_images_dir = "/data/xywang/dataset/Segmentation/skin/skin_copypaste/images"
    save_labels_dir = "/data/xywang/dataset/Segmentation/skin/skin_copypaste/labels"

    skin_id_list = [i[:-4] for i in os.listdir(person_images_labels_onlyperson_dir)]
    background_id_list = coco_noperson()
    if len(background_id_list) < len(skin_id_list):
        L = len(skin_id_list)-len(background_id_list)
        start = np.random.randint(L)
        background_id_list.extend(background_id_list[start:start+L])

    print("skin_id_list:",len(skin_id_list),"background:",len(background_id_list))

    for index, id in enumerate(skin_id_list):
        print(index)
        person_images_labels = os.path.join(person_images_labels_onlyperson_dir,id+".png")
        person_images = os.path.join(person_images_dir,id+".jpg")
        skin_labels = os.path.join(skin_labels_dir,id+".png")
        background_images = os.path.join(background_images_dir,background_id_list[index])

        label_person = cv2.imread(person_images_labels,-1)[:,:,3]
        skin_label = cv2.imread(skin_labels,-1)
        # print(person_images_labels, skin_labels,background_images)
        assert label_person.shape == skin_label.shape

        image_background = cv2.imread(background_images)
        image_person = cv2.imread(person_images)

        target_w, target_h = image_person.shape[1], image_person.shape[0]

        resize_radio = max(target_h / image_background.shape[0], target_w / image_background.shape[1])
        if resize_radio >= 1:
            image_background = cv2.resize(image_background,(int(image_background.shape[1]*resize_radio)+1,int(image_background.shape[0]*resize_radio)+1))
            
        crop_bbox = get_crop_bbox(image_background, target_w, target_h)

        image_background = crop(image_background, crop_bbox)

        label_person[label_person!=0] = 255
        image_person = cv2.bitwise_and(image_person,image_person,mask = label_person)
        label_person_inv = cv2.bitwise_not(label_person)
        image_background = cv2.bitwise_and(image_background,image_background,mask = label_person_inv)
        image_compose = cv2.add(image_background,image_person)

        #cv2.imshow("image_compose",image_compose)
        # cv2.imshow("label_person_inv",label_person_inv)
        # cv2.imshow("label_person",label_person)
        # cv2.imshow("image_background",image_background)
        # cv2.imshow("image_person",image_person)

        label_compose = skin_label
        
        label_compose[label_person==0] = 0
        label_compose[label_compose !=0] = 1
        show_compose = vis(label_compose, image_compose)
        # cv2.imshow("show_compose",show_compose)
        # key = cv2.waitKey(0)
        # if key == 27:
        #     break

        cv2.imwrite(os.path.join(save_images_dir,"compose_coco_%05d.jpg" % index),image_compose)
        cv2.imwrite(os.path.join(save_labels_dir,"compose_coco_%05d.png" % index),label_compose)

def copypaste_v2():
    person_min_rect_person_labels_dir = "/data/xywang/dataset/Segmentation/skin/skin_copypaste/person_min_rect/person_labels"
    person_min_rect_skin_labels_dir = "/data/xywang/dataset/Segmentation/skin/skin_copypaste/person_min_rect/skin_labels"
    person_min_rect_images_dir = "/data/xywang/dataset/Segmentation/skin/skin_copypaste/person_min_rect/images"
    background_images_dir = "/mldb/dataset/COCO/train2017/"

    skin_id_list = [i[:-4] for i in os.listdir(person_min_rect_skin_labels_dir)]
    background_id_list = coco_noperson()

    # skin_id_list.sort(key= lambda x:int("".join(list(filter(str.isdigit, x)))))
    # background_id_list.sort(key= lambda x:int("".join(list(filter(str.isdigit, x)))))

    # if len(background_id_list) < len(skin_id_list):
    #     L = len(skin_id_list)-len(background_id_list)
    #     start = np.random.randint(L)
    #     background_id_list.extend(background_id_list[start:start+L])
    # elif len(background_id_list) > len(skin_id_list):
    #     L = len(background_id_list) - len(skin_id_list)
    #     start = np.random.randint(L)
    #     skin_id_list.extend(skin_id_list[start:start+L])

    print("skin_id_list:",len(skin_id_list),"background:",len(background_id_list))

    for index, id in enumerate(skin_id_list):
        print(index)
        person_min_rect_person_label = cv2.imread(os.path.join(person_min_rect_person_labels_dir,id+".png"),-1)
        person_min_rect_skin_label = cv2.imread(os.path.join(person_min_rect_skin_labels_dir,id+".png"),-1)
        person_min_rect_image = cv2.imread(os.path.join(person_min_rect_images_dir,id+".jpg"))
        image_background = cv2.imread(os.path.join(background_images_dir,background_id_list[index]))

        person_w, person_h = person_min_rect_image.shape[1], person_min_rect_image.shape[0]

        min_ratio, max_ratio = 0.75, 1.5
        base_scale = 512
        ratio = np.random.random_sample() * (max_ratio - min_ratio) + min_ratio
        radom_scale = int(base_scale*ratio)
        resize_ratio = radom_scale / max(person_w, person_h)
        target_size = (int(resize_ratio * person_w), int(resize_ratio * person_h))
        person_min_rect_image = cv2.resize(person_min_rect_image,target_size,interpolation=cv2.INTER_LINEAR)
        #cv2.imshow("person_min_rect_image",person_min_rect_image)
        person_min_rect_person_label = cv2.resize(person_min_rect_person_label,target_size,interpolation=cv2.INTER_NEAREST)
        person_min_rect_skin_label = cv2.resize(person_min_rect_skin_label,target_size,interpolation=cv2.INTER_NEAREST)

        resize_ratio_background = max(target_size[1] / image_background.shape[0], target_size[0] / image_background.shape[1])
        if resize_ratio_background > 1:
            image_background = cv2.resize(image_background,(int(image_background.shape[1]*resize_ratio_background+0.5),int(image_background.shape[0]*resize_ratio_background+0.5)))
        #cv2.imshow("image_background",image_background)

        final_w, final_h = image_background.shape[1], image_background.shape[0]
        delta_w = final_w - target_size[0]
        delta_h = final_h - target_size[1]

        final_skin_label = np.zeros((final_h,final_w),dtype=np.uint8)
        image_only_person = np.zeros((final_h,final_w,3),dtype=np.uint8)
        label_only_person = np.zeros((final_h,final_w),dtype=np.uint8)
        x1, y1 = 0, 0
        if delta_w > 0:
            x1 = np.random.randint(0,delta_w)
        if delta_h > 0:
            y1 = np.random.randint(0,delta_h)

        final_skin_label[y1:y1+target_size[1],x1:x1+target_size[0]] = person_min_rect_skin_label
        image_only_person[y1:y1+target_size[1],x1:x1+target_size[0],:] = person_min_rect_image
        label_only_person[y1:y1+target_size[1],x1:x1+target_size[0]] = person_min_rect_person_label

        label_only_person_inv = cv2.bitwise_not(label_only_person)
        image_background = cv2.bitwise_and(image_background,image_background,mask = label_only_person_inv)
        final_image = cv2.add(image_background,image_only_person)
        

        show_compose = vis(final_skin_label, final_image)
        cv2.imshow("show_compose",show_compose)
        cv2.imshow("final_image",final_image)
        key = cv2.waitKey(0)

        if key == 27:
            break

def copypaste_v3():
    person_min_rect_person_labels_dir = "/data/xywang/dataset/Segmentation/skin/skin_copypaste/person_min_rect/person_labels"
    person_min_rect_skin_labels_dir = "/data/xywang/dataset/Segmentation/skin/skin_copypaste/person_min_rect/skin_labels"
    person_min_rect_images_dir = "/data/xywang/dataset/Segmentation/skin/skin_copypaste/person_min_rect/images"

    background_images_dir = "/data/xywang/dataset/Segmentation/skin/skin_voc_seg_12dataset/VOCdevkit/VOC2012/JPEGImages"
    background_labels_dir = "/data/xywang/dataset/Segmentation/skin/skin_voc_seg_12dataset/VOCdevkit/VOC2012/SegmentationClassRaw"

    skin_id_list = [i[:-4] for i in os.listdir(person_min_rect_skin_labels_dir)]
    background_id_list = [i[:-4] for i in os.listdir(background_labels_dir)]

    # skin_id_list.sort(key= lambda x:int("".join(list(filter(str.isdigit, x)))))
    # background_id_list.sort(key= lambda x:int("".join(list(filter(str.isdigit, x)))))

    # if len(background_id_list) < len(skin_id_list):
    #     L = len(skin_id_list)-len(background_id_list)
    #     start = np.random.randint(L)
    #     background_id_list.extend(background_id_list[start:start+L])
    # elif len(background_id_list) > len(skin_id_list):
    #     L = len(background_id_list) - len(skin_id_list)
    #     start = np.random.randint(L)
    #     skin_id_list.extend(skin_id_list[start:start+L])

    print("skin_id_list:",len(skin_id_list),"background:",len(background_id_list))

    for index, id in enumerate(skin_id_list):
        print(index)
        person_min_rect_person_label = cv2.imread(os.path.join(person_min_rect_person_labels_dir,id+".png"),-1)
        person_min_rect_skin_label = cv2.imread(os.path.join(person_min_rect_skin_labels_dir,id+".png"),-1)
        person_min_rect_image = cv2.imread(os.path.join(person_min_rect_images_dir,id+".jpg"))
        image_background = cv2.imread(os.path.join(background_images_dir,background_id_list[index])+".jpg")
        label_background = cv2.imread(os.path.join(background_labels_dir,background_id_list[index])+".png",-1)

        person_w, person_h = person_min_rect_image.shape[1], person_min_rect_image.shape[0]

        min_ratio, max_ratio = 0.75, 1.5
        base_scale = 512
        ratio = np.random.random_sample() * (max_ratio - min_ratio) + min_ratio
        radom_scale = int(base_scale*ratio)
        resize_ratio = radom_scale / max(person_w, person_h)
        target_size = (int(resize_ratio * person_w), int(resize_ratio * person_h))
        person_min_rect_image = cv2.resize(person_min_rect_image,target_size,interpolation=cv2.INTER_LINEAR)
        #cv2.imshow("person_min_rect_image",person_min_rect_image)
        person_min_rect_person_label = cv2.resize(person_min_rect_person_label,target_size,interpolation=cv2.INTER_NEAREST)
        person_min_rect_skin_label = cv2.resize(person_min_rect_skin_label,target_size,interpolation=cv2.INTER_NEAREST)

        resize_ratio_background = max(target_size[1] / image_background.shape[0], target_size[0] / image_background.shape[1])
        if resize_ratio_background > 1:
            image_background = cv2.resize(image_background,(int(image_background.shape[1]*resize_ratio_background+0.5),int(image_background.shape[0]*resize_ratio_background+0.5)))
            label_background = cv2.resize(label_background,(int(label_background.shape[1]*resize_ratio_background+0.5),int(label_background.shape[0]*resize_ratio_background+0.5)),interpolation=cv2.INTER_NEAREST)
        #cv2.imshow("image_background",image_background)

        final_w, final_h = image_background.shape[1], image_background.shape[0]
        delta_w = final_w - target_size[0]
        delta_h = final_h - target_size[1]

        final_skin_label = np.zeros((final_h,final_w),dtype=np.uint8)
        image_only_person = np.zeros((final_h,final_w,3),dtype=np.uint8)
        label_only_person = np.zeros((final_h,final_w),dtype=np.uint8)
        x1, y1 = 0, 0
        if delta_w > 0:
            x1 = np.random.randint(0,delta_w)
        if delta_h > 0:
            y1 = np.random.randint(0,delta_h)

        final_skin_label[y1:y1+target_size[1],x1:x1+target_size[0]] = person_min_rect_skin_label
        image_only_person[y1:y1+target_size[1],x1:x1+target_size[0],:] = person_min_rect_image
        label_only_person[y1:y1+target_size[1],x1:x1+target_size[0]] = person_min_rect_person_label

        label_only_person_inv = cv2.bitwise_not(label_only_person)
        image_background = cv2.bitwise_and(image_background,image_background,mask = label_only_person_inv)
        final_image = cv2.add(image_background,image_only_person)
        label_background = cv2.bitwise_and(label_background,label_background,mask = label_only_person_inv)
        final_skin_label = cv2.add(final_skin_label,label_background)

        show_compose = vis(final_skin_label, final_image)
        cv2.imshow("show_compose",show_compose)
        cv2.imshow("final_image",final_image)
        key = cv2.waitKey(0)

        if key == 27:
            break

if __name__ == "__main__":
    # test()
    # copypaste_v1()
    # copypaste_v2()
    copypaste_v3()
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值