ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

OpenVINO Model Server的服务化部署——step4(实现天空替换)

2020-10-29 07:00:27  阅读:200  来源: 互联网

标签:OpenVINO matCloud img 服务化 cv2 Server shape matMask out


前期已经基于OpenVINO搭建成功了天空识别模型,并且能够得到着色的结果图片,下一步就是继续来实现“天空替换”一、天空替换重构在OpenVINO着色结果基础上,重新编写c++和python版本的天空替换代码。// 天空之子算法研究
// 2019年11月30日
#include "pch.h"
#include "cv_helper.h"
using namespace cv;
using namespace std;
int main()
{
    Mat matSrc = imread("E:\\未来项目\\天空替换(天空分割)\\测试图片\\测试图片\\sky14.jpg");
    Mat matCloud = imread("E:\\未来项目\\你的名字滤镜\\算法实验\\算法实验\\sky1.png");
    Mat matMask = imread("E:\\未来项目\\天空替换(天空分割)\\skyInBlue.png",0);
    matMask = (matMask == 124);
    //统一按照matSrc的大小进行缩放
    resize(matCloud, matCloud, matSrc.size());
    resize(matMask, matMask, matSrc.size());
    Point center = Point (matMask.cols / 2, matMask.rows / 2);
    //Mat normal_clone;
    //cartoonifyImage(matCloud, matCloud);
    Mat normal_clone;
    seamlessClone(matCloud, matSrc, matMask, center, normal_clone, MIXED_CLONE);
    cv::waitKey();
}

import cv2import numpy as npmatSrc =cv2.imread('E:/template/sky14.jpg')matCloud = cv2.imread('E:/template/cloud3.jpg')matMask = cv2.imread('E:/template/skyInBlue.png',0)rows,cols=matMask.shapefor i in range(rows):    for j in range(cols):        if (matMask[i,j]==124):            matMask[i,j]=255        else:            matMask[i,j]=0

height,width=matSrc.shape[:2]matCloud=cv2.resize(matCloud,(width,height),interpolation=cv2.INTER_CUBIC)matMask=cv2.resize(matMask,(width,height),interpolation=cv2.INTER_CUBIC)
center = (width // 2, height // 2) # Seamlessly clone src into dst and put the results in outputnormal_clone = cv2.seamlessClone(matCloud, matSrc, matMask, center, cv2.NORMAL_CLONE)mixed_clone = cv2.seamlessClone(matCloud, matSrc, matMask, center, cv2.MIXED_CLONE)

cv2.imshow('normal_clone',normal_clone)cv2.imshow('mixed_clone',mixed_clone)cv2.waitKey(0)

原图:替换图:这个已经实现了天空替换的结果,但是颜色还需要调亮一点。二、相关的代码融合修改现有view,主要是融入代码:def process_detail(request,param1):    options = [('grpc.max_receive_message_length', 100 * 1024 * 1024),('grpc.max_send_message_length', 100 * 1024 * 1024)]    channel = grpc.insecure_channel("{}:{}".format('localhost',9000),options = options)    stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)    batch_size = 1    #TODO filepath    output_str='filepath'    imgfile = os.path.join('/root/mysites/goApp/images',param1)    print(imgfile)    img = load_image(imgfile)    imgs = np.zeros((0,3,1024,2048), np.dtype('<f'))    imgs = np.append(imgs, img, axis=0)
    request = predict_pb2.PredictRequest()    request.model_spec.name = "semantic-segmentation-adas"    print("\nRequest shape", img.shape)
    img = imgs[0:1]    request.inputs["data"].CopyFrom(make_tensor_proto(img, shape=(img.shape)))      result = stub.Predict(request, 10.0)    # result includes a dictionary with all model outputs print(img.shape)     output = make_ndarray(result.outputs["4455.1"])
    for y in range(0,img.shape[0]):  # iterate over responses from all images in the batch        img_out = output[y,:,:,:]        print("image in batch item",y, ", output shape",img_out.shape)        img_out = img_out.transpose(1,2,0)        print("saving result to",os.path.join('/root/mysites/goApp/results',param1+'.result.jpg'))        out_h, out_w,_ = img_out.shape        print(out_h)        print(out_w)        for batch, data in enumerate(output):            classes_map = np.zeros(shape=(out_h, out_w, 3), dtype=np.int)            for i in range(out_h):                for j in range(out_w):                    if len(data[:, i, j]) == 1:                        pixel_class = int(data[:, i, j])                    else:                        pixel_class = np.argmax(data[:, i, j])                    classes_map[i, j, :] = classes_color_map[min(pixel_class, 20)]                    classes_map = np.uint8(classes_map)            matMask = cv2.cvtColor(classes_map,cv2.COLOR_BGR2GRAY)            matMask = np.uint8(matMask)            matCloud = cv2.imread('/root/mysites/goApp/images/cloud3.jpg')            rows,cols=matMask.shape            for i in range(rows):                for j in range(cols):                    if (matMask[i,j]==134):                        matMask[i,j]=255                    else:                        matMask[i,j]=0            matsrc = cv2.imread(imgfile)            matsrc = cv2.resize(matsrc,(out_w,out_h),interpolation=cv2.INTER_CUBIC)            matCloud=cv2.resize(matCloud,(out_w,out_h),interpolation=cv2.INTER_CUBIC)            matMask=cv2.resize(matMask,(out_w,out_h),interpolation=cv2.INTER_CUBIC)
            center = (out_w // 2, out_h // 2)
            normal_clone = cv2.seamlessClone(matCloud,matsrc, matMask, center, cv2.NORMAL_CLONE)            output_str = os.path.join('/root/mysites/goApp/results',param1+'.result.jpg')            cv2.imwrite(output_str,normal_clone)    return HttpResponse(output_str)最后实现在浏览器中的调用时正常的。三、目标导向我最终想实现的是完全自可主控的类似https://cloud.baidu.com/product/imageprocess/sky_seg的服务
包括网站服务,后端调用等。当然这个界面比较复杂,我自己的实现比较简单,如果能够找到 Django的模板的话,我也会来进行实现。

来自为知笔记(Wiz)

标签:OpenVINO,matCloud,img,服务化,cv2,Server,shape,matMask,out
来源: https://www.cnblogs.com/jsxyhelu/p/13894565.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有