大作业:使用MindStudio成功复现昇腾社区中的MindX SDK应用案例
选题:黑白图像上色
使用MindStudio打开下载到的工程文件
创建data、model、out文件夹
准备测试图片
这里从网上随机下载了一张照片,并用ps去色处理了一下,路径 data/test.png
使用Tools->Deployment->Upload to将工程文件上传到服务器
cd到项目根目录
下载model文件并解压缩
cd model
wget .png --no-check-certificate
unzip model.zip
带上–no-check-certificate参数否则会报错
转换模型,得到文件
cd ../scripts
bash atc_run.sh
运行
cd ../src
python3 main.py ../data/test.png
使用Tools->Deployment->Download from工具下载回本地
运行结果:(out_test.png)
原图:
增加二次元浓度再试一次
太为难这个模型了!
初始化StreamManagerApi并加载pipeline文件
streamManagerApi = StreamManagerApi()ret = streamManagerApi.InitManager()pipeline = b"../pipeline/colorization.pipeline" ret = streamManagerApi.CreateMultipleStreamsFromFile(pipeline)
传入数据
streamName = b'colorization'inPluginId = 0key0 = b"appsrc0"protobufVec = InProtobufVector()protobuf = MxProtobufIn()protobuf.key = pe = b'MxTools.MxpiTensorPackageList'protobuf.protobuf = tensorPackageList.SerializeToString()protobufVec.push_back(protobuf)uniqueId = streamManagerApi.SendProtobuf(streamName, inPluginId, protobufVec)
将图像L通道的数据写入tensorVec中,用SendProtobuf发送给appsrc0元件
取出结果数据
key1 = b"mxpi_tensorinfer0"keyVec = StringVector()keyVec.push_back(key1) inferRes = streamManagerApi.GetProtobuf(streamName, inPluginId, keyVec)
合成上色后的结果图
tensorList = MxpiDataType.MxpiTensorPackageList()tensorList.ParseFromString(inferRes[0].messageBuf)res = np.sorPackageVec[0].tensorVec[0].dataStr, dtype=np.float32)postprocess(res, inputPic, origShape, origL)
从代码阅读中可以看出,图像的前处理和模型后处理都在Python代码中完成了,整个pipeline流程仅包含一次推理,加上输入输出
输入appsrc:设置block size为合适大小,案例中设置了409600
模型推理:设置模型文件路径…/;同时还手动指定了dataSource为appsrc0
输出appsink, block size大小与输入元件一致
尝试给视频上色,仅需代码部分改一行,流程编排部分不需要改动
seq_list = []temp_list = []vc = cv.VideoCapture('../data/test.mp4')c = 0rval = vc.isOpened()while rval:c = c + 1rval, frame = vc.read()if rval:cv.imwrite('../data/'+str(c) + '.png', frame)seq_list.append('../data/'+str(c) + '.png')temp_list.append('../out/out_'+str(c) + '.png')lease()for pic in seq_list:run(pic, streamManagerApi) #此处是原先代码中流的输入输出部分os.remove(pic)videoWriter = cv.VideoWriter('../out_video/out_test.mp4', cv.VideoWriter_fourcc(*'mp4v'), 30, (1280, 720))for pic in temp_list:img = cv.imread(pic)img = cv.resize(img, (1280, 720))videoWriter.write(ve(lease()
结果:
太为难这个模型了!
本文发布于:2024-02-02 19:22:02,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170687292245905.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |