首页 > 其他分享 >OpenVINO Model Server的服务化部署——step4(实现天空替换)

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

时间:2022-12-24 10:04:00浏览次数:33  
标签: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 cv2


import numpy  as np


matSrc =cv2.imread( 'E:/template/sky14.jpg')


matCloud = cv2.imread( 'E:/template/cloud3.jpg')


matMask = cv2.imread( 'E:/template/skyInBlue.png', 0)


rows,cols=matMask.shape


for 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 output


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


原图:


​​ OpenVINO Model Server的服务化部署——step4(实现天空替换)_算法实验 ​​



替换图:


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



这个已经实现了天空替换的结果,但是颜色还需要调亮一点。


二、 相关的代码融合

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)

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

最后实现在浏览器中的调用时正常的。

三、 目标导向


我最终想实现的是完全自可 主 控的类似https://cloud.baidu.com/product/imageprocess/sky_seg 的服务


​​ OpenVINO Model Server的服务化部署——step4(实现天空替换)_算法实验_04 ​​



包括网站服务,后端调用等。当然这个界面比较复杂,我自己的实现比较简单,如果能够找到 Django的模板的话,我也会来进行实现。



标签:OpenVINO,matCloud,img,服务化,cv2,Server,shape,matMask,out
From: https://blog.51cto.com/jsxyhelu2017/5967065

相关文章

  • 利用SQL Server XML拆分数据
    DECLARE@strIDVARCHAR(200)='1,2,3';DECLARE@xmlXML;SELECT@xml=CONVERT(XML,'<root><place><id>'+REPLACE(@strID,',','</id></place><place><id>')+'</id......
  • Vue开发环境中配置devServer
    简述在使用Axios时,在本地开发通常会出现跨域问题,解决方法也很简单。在开发环境下,可以通过配置Vue.config.js中的module.exports中devServer来解决。注:生产环境无效,没见d......
  • Oracle RAC -- VMware server 2.0 创建共享磁盘
    C:\ProgramFiles(x86)\VMware\VMwareServer> vmware-vdiskmanager.exe-c-s2Gb-alsilogic-t2G:\VirtualMachines\disks\OCR_VOTE.vmdkC:\ProgramFiles(x86)\VMw......
  • 利用VMWare Server 2.0创建共享磁盘
    前段时间要做共享型​​双机热备​​由于没有共享存储所以需要使用虚拟存储,搞了很久最后发现WorkStation创建的共享磁盘不能用在集群里面,特此整理了利用VMWare Server创建......
  • sqlserver/System.Data.SqlClient.SqlException (0x80131904): 用户 'NT AUTHORITY\I
    本人最近学习XAF框架项目,在本地运行可以连接到数据库,发布后无法连接,抛出用户'NTAUTHORITY\IUSR'登录失败问题截图:  问题原因:数据库连接使用的window身份验证时,W......
  • SQL Server 2019 新建一张表基础
    SQLServer2019新建一张表基础学习数据库,理论上来说,应该先从学习理论开始,但我觉得这种实践性强的,直接从实践开始是学习速度和效率最好一、新建一张学生表新建一张表和......
  • 【SQL Server】存储过程带参数输出——output
    在SQL Server 中,如果要用一个存储过程返回字符串应该怎么做?用output参数。错误方式接下来,展示一下,常见的错误方法CREATEPROCEDUREtestStringASBEGINRETURN......
  • MySQL Data source rejected establishment of connection, message from server: “T
    MySQL数据库中Datasourcerejectedestablishmentofconnection "Toomanyconnections"异常解决错误原因: 利用C3P0数据源,连接MySQL服务器时,产生了太多的连接数,数据库客......
  • PHP添加$_SERVER服务器环境变量
    PHP添加$_SERVER服务器环境变量通过nginx的fastcgi_param来设置通过php主配置文件php-fpm.conf来设置通过Apache设置环境变量NGINX设置通过nginx的fastcgi_para......
  • SQL Server 2019 附加数据库失败
    SQLServer2019附加数据库失败一、附加数据库失败将分离的数据重新附加回来,发现失败了...通过网上查询,原因是:分离出来的数据库文件(xxx.mdf和xxx_log.ldf)对于操作系......