同乐承体育太阳「陨落」再无天光?公鹿G6回师主场盼封王

go中redigo操作,获取多个数据

  返回  

蓝天识别(去白云)

2021/7/20 18:31:43 浏览:

蓝色识别

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()

联系我们

如果您对我们的服务有兴趣,请及时和我们联系!

服务热线:18288888888
座机:18288888888
传真:
邮箱:888888@qq.com
地址:郑州市文化路红专路93号