matplotlib中plt用法实例
import torch
from models.models import Model
import cv2
from PIL import Image
import numpy as np from matplotlib.animation import FFMpegWriter
import time
import matplotlib.pyplot as plt from torchvision.transforms import functional exp_name = './xxxx_results'
dataRoot = 'xxxx.mp4'
model_path = './checkpoint_best.pth' def pre_image(image):
image = Image.fromarray(cv2.cvtColor(image,cv2.COLOR_BGR2RGB))
input_image = image.copy()
# image.show()
height, width = image.size[1], image.size[0]
height = round(height / 16) * 16
width = round(width / 16) * 16
image = image.resize((width, height), Image.BILINEAR) image = functional.to_tensor(image)
image = functional.normalize(image, [0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
return input_image,torch.unsqueeze(image,0) if __name__ == '__main__': device = torch.device('cuda:0') # load model
model=Model()
checkpoint = torch.load(model_path)
model.load_state_dict(checkpoint['model']) model.cuda()
model.eval() # input video
video = cv2.VideoCapture(dataRoot)
fps = video.get(cv2.CAP_PROP_FPS)
print(fps)
frameCount = video.get(cv2.CAP_PROP_FRAME_COUNT)
print(frameCount)
size = (int(video.get(cv2.CAP_PROP_FRAME_WIDTH)), int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))) # metadata = dict(title='Video Test', artist='Matplotlib', comment='Movie support!')
# writer = FFMpegWriter(fps=25, metadata=metadata) # videoWriter = cv2.VideoWriter('trans.mp4', cv2.VideoWriter_fourcc(*'MP4V'), fps, size)
success, frame = video.read()
index = 1 figure = plt.figure()
while success:
# time1=time.time()
src_image,frame = pre_image(frame)
images = frame.to(device) # time1 = time.time() # ground truth
# gt_path = dataRoot + '/den/' + filename_no_ext + '.csv' # predict
dense_map,atten_map = model(images)
# test = time.time() - time1 dense_map = dense_map.cpu().data.numpy()[0,0,:,:]
# test=time.time()-time1 dense_pred_count = np.sum(dense_map)
dense_map = dense_map/np.max(dense_map+1e-20) # cv2.imshow("image", dense_map)
# cv2.waitKey(0) plt.subplot(121)
plt.imshow(src_image)
# plt.title('original image')
plt.axis('off') plt.subplot(122)
plt.imshow(dense_map)
# plt.title('dense map')
plt.text(25, 25, 'pred crowd count:%.4f ' % dense_pred_count, fontdict={'size': 10, 'color': 'red'})
plt.axis('off') plt.tight_layout(pad=0.3, w_pad=0, h_pad=1) # anni=animation.FuncAnimation(fig, animate, init_func=init,frames=200, interval=20, blit=True)
# anim.save('sin.gif', fps=75, writer='imagemagick')
plt.savefig(exp_name + '/'+ str('%05d' % index) + '_' + str(int(dense_pred_count)) + '.png', bbox_inches='tight', pad_inches=0, dpi=150) # plt.show()
plt.clf() success, frame = video.read()
index += 1 video.release()
matplotlib中plt用法实例的更多相关文章
- Matplotlib中plt.rcParams用法(设置图像细节)
import numpy as np import matplotlib.pyplot as plt from matplotlib.colors import ListedColormap %mat ...
- Nhibernate中CreateSQLQuery用法实例
说明: 使用原生SQL查询时,若要通过addEntity方法引入对象,则查询结果列中必须包含该对象的所有属性,否则会抛出System.IndexOutOfRangeException异常. 结论: 若 ...
- matplotlib中plt.scatter()参数详解
scatter(x, y, s=None, c=None, marker=None, cmap=None, norm=None, vmin=None, vmax=None, alpha=None, l ...
- C#中FormsAuthentication用法实例
....本文纯属抄袭.... using System; using System.Web; using System.Web.Security; namespace AuthTest { ...
- .NET中StringBuilder用法实例分析
string s1 = "33"; string s2 = "44"; string s3 = "55"; //需求是把s1 s2 s3拼接 ...
- matplotlib中 plt.plot() 函数中**kwargs的参数形式
plt.plot(x, y, **kwargs) **kwargs的参数大致有如下几种: color: 颜色 linestyle: 线条样式 marker: 标记风格 markerfacecolor: ...
- matplotlib中subplots的用法
1.matplotlib中如果只画一张图的话,可以直接用pyplot,一般的做法是: import matplotlib.pyplot as plt plt.figure(figsize=(20,8) ...
- c#初学-多线程中lock用法的经典实例
本文转载自:http://www.cnblogs.com/promise-7/articles/2354077.html 一.Lock定义 lock 关键字可以用来确保代码块完成运行,而不会被 ...
- php中的curl使用入门教程和常见用法实例
摘要: [目录] php中的curl使用入门教程和常见用法实例 一.curl的优势 二.curl的简单使用步骤 三.错误处理 四.获取curl请求的具体信息 五.使用curl发送post请求 六.文件 ...
随机推荐
- 百度地图四(Android百度地图Poi检索开发总结)
https://blog.csdn.net/wenzhi20102321/article/details/54575999
- Oracle12c 使用总结
/*创建临时表空间 */create temporary tablespace BBB tempfile 'D:\APP\oracle\oradata\orcl\BBB.dbf' size 500m ...
- win10 配置 maven | 下载与配置
<!-- 阿里云仓库 --> <mirrors> <mirror> <id>nexus-aliyun</id> <mirrorOf&g ...
- Java安全之URLDNS链
Java安全之URLDNS链 0x00 前言 在学习Java的反序列化漏洞的时候,就不得不学习他的一个利用链.很多刚刚入门的对于利用链这个词可能比较陌生.那么这里先来了解一下Java反序列化和反序列化 ...
- 星涛:采用java递归复制文件夹
package com.botao; import java.io.*; /** * @author cbt28 */ public class FileUtil { public static St ...
- 温故知新————c++ 多态
参考: 1. https://blog.csdn.net/weixin_42678507/article/details/89414998 (直接说明原理) 2 .https://www.cnblo ...
- 数字PLL,什么是数字PLL
来源:http://www.elecfans.com/baike/bandaoti/bandaotiqijian/20100323203306.html 数字PLL,什么是数字PLL 数字PLL PL ...
- WJQ与机房
sample input 5 6 7 2 3 1 1 5 0 6 0 0 8 6 6 5 3 4 3 7 8 2 4 0 0 6 9 sample output 20 样例解释: 分别以(2,1)为左 ...
- rustup命令速度慢
通过以下命令更换镜像: $ENV:RUSTUP_DIST_SERVER='https://mirrors.ustc.edu.cn/rust-static' $ENV:RUSTUP_UPDATE_ROO ...
- ASP。NET MVC警告横幅使用Bootstrap和AngularUI Bootstrap
Watch this script in action - demo 下载Source Code from GitHub 下载Source Code from CodeProject (1.1 MB) ...