当前位置:
首页 > temp > 简明python教程 >
-
【Python | opencv+PIL】常见操作(创建、添加帧、绘图、读取等)的效率对比及其(2)
from PIL import Image, ImageDraw, ImageFont
8 import numpy as np
9
10 # Class
11 class FunctionTimer(object):
12 MAX_WAIT_SEC = 0.5
13 INF = 2147483647
14 SMART_LOOP = -1
15
16 def __init__(self, timer=None, count=None):
17 self._timer = timer if timer != None else time.perf_counter
18 self._count = count if count != None else 100
19
20 def _get_single_time(self, func, *args, **kwargs):
21 s = self._timer()
22 ret = func(*args, **kwargs)
23 f = self._timer()
24 return ret, f - s
25
26 def _get_repeat_time(self, number, func, *args, **kwargs):
27 time_min, time_max, time_sum = self.INF, 0, 0
28 for i in range(number):
29 ret, delta = self._get_single_time(func, *args, **kwargs)
30 time_min = min(time_min, delta)
31 time_max = max(time_max, delta)
32 time_sum += delta
33 return func, ret, number, time_sum / number, time_min, time_max, time_sum
34
35 def gettime(self, func, *args, **kwargs):
36 if self._count != self.SMART_LOOP:
37 return self._get_repeat_time(self._count, func, *args, **kwargs)
38 else:
39 # Arrange loop count automatically
40 # Refer to Lib/timeit.py
41 i = 1
42 while True:
43 for j in 1, 2, 5:
44 number = i * j
45 func, ret, number, time_ave, time_min, time_max, time_sum = self._get_repeat_time(number, func, *args, **kwargs)
46 if time_sum >= self.MAX_WAIT_SEC:
47 return func, ret, number, time_ave, time_min, time_max, time_sum
48 i *= 10
49
50 def better_print(self, params):
51 func, ret, count, ave, minn, maxn, sumn = params
52 print('========================================')
53 print(' Function name:')
54 print(' ' + func.__name__)
55 print('========================================')
56 print(' Function has the return content below:')
57 print(' ' + ret.__repr__())
58 print('========================================')
59 print(' Summary of Function Timer:')
60 print(' Count of loops: {}'.format(count))
61 print(' Average time of loops: {} (sec)'.format(ave))
62 print(' Minimum of every loop time: {} (sec)'.format(minn))
63 print(' Maximum of every loop time: {} (sec)'.format(maxn))
64 print(' Total time of loops: {} (sec)'.format(sumn))
65 print('========================================')
66
67 # Function
68 # Debug
69 def testfunc(x=10000000):
70 for i in range(x):
71 pass
72 return i
73
74 # Test Function
75 def task_1():
76 vw = cv2.VideoWriter('out.mp4', cv2.VideoWriter_fourcc(*'mp4v'), 60, (1920, 1080))
77
78 def task_2():
79 cap = cv2.VideoCapture('in.mp4')
80 while cap.isOpened():
81 ret, frame = cap.read()
82 if not ret:
83 break
84 cap.release()
85
86 def task_3(vw, frame): # Use a new blank video file when testing
87 vw.write(frame)
88
89 def task_4(vw):
90 vw.release()
91
92 def task_5_matrix():
93 arr = np.zeros((1080, 1920, 3), dtype=np.uint8)
94
95 def task_5_pillow():
96 img = Image.new('RGB', (1920, 1080))
97
98 def task_6_opencv():
99 arr = cv2.imread('in.jpg')
100
101 def task_6_pillow():
102 img = Image.open('in.jpg')
103
104 def task_7_list(img):
105 arr1 = list(img.im)
106
107 def task_7_asarray(img):
108 arr2 = np.asarray(img)
109
110 def task_7_array(img):
111 arr3 = np.array(img)
112
113 def task_8_matrix(arr3):
114 arr3[0][0] = (255, 255, 255)
115
116 def task_8_pillow_putpixel(img):
117 img.putpixel((0, 0), (255, 255, 255))
118
119 def task_8_pillow_point(draw):
120 draw.point((0, 0), (255, 255, 255))
121
122 def task_9_line_matrix(arr3):
123 for x in range(100, 500):
124 arr3[100][x] = (255, 255, 255)
125
126 def task_9_line_pillow(draw):
127 draw.line((100, 100, 500, 100), (255, 255, 255))
128
129 def task_9_line_opencv(arr):
130 cv2.line(arr, (100, 100), (500, 100), (255, 255, 255), 1)
131
132 def task_9_rectangle_matrix(arr3):
133 for x in range(100, 500):
134 for y in range(100, 500):
135 arr3[y][x] = (255, 255, 255)
136
137 def task_9_rectangle_pillow(draw):
138 draw.rectangle((100, 100, 500, 500), (255, 255, 255))
139
140 def task_9_rectangle_opencv(arr):
141 cv2.rectangle(arr, (100, 100), (500, 500), (255, 255, 255), -1)
142
143 def task_9_circle_pillow_arc(draw):
144 draw.arc((100, 100, 500, 500), 0, 360, (255, 255, 255))
145
146 def task_9_circle_pillow_ellipse(draw):
147 draw.ellipse((100, 100, 500, 500), (255, 255, 255))
148
149 def task_9_circle_opencv_circle(arr):
150 cv2.circle(arr, (300, 300), 200, (255, 255, 255), -1)
151
152 def task_9_circle_opencv_ellipse(arr):
153 cv2.ellipse(arr, (300, 300), (200, 200), 0, 0, 360, (255, 255, 255), -1)
154
155 def task_9_ellipse_pillow(draw):
156 draw.ellipse((100, 100, 700, 500), (255, 255, 255))
157
158 def task_9_ellipse_opencv(arr):
159 cv2.ellipse(arr, (400, 300), (300, 200), 0, 0, 360, (255, 255, 255), -1)
160
161 def task_9_text_pillow(draw, font):
162 draw.text((100, 100), 'Hello, world!', (255, 255, 255), font)
163
164 def task_9_text_opencv(arr, font):
165 cv2.putText(arr, 'Hello, world!', (100, 200), font, 2, (255, 255, 255), 1, cv2.LINE_AA)
166
167 def task_10():
168 pass
169
170 def task_11_pillow(img):
171 img.save('out.jpg')
172
173 def task_11_opencv_imread(arr):
174 cv2.imwrite('out.jpg', arr)
175
176 def task_11_opencv_asarray(arr2):
177 cv2.imwrite('out.jpg', arr2)
178
179 def task_11_opencv_array(arr3):
180 cv2.imwrite('out.jpg', arr3)
181
182 # Main Function
183 if __name__ == '__main__':
184 timer = FunctionTimer()
185 # timer.better_print(timer.gettime(func, *args, **kwargs))
186 timer.better_print(timer.gettime(task_1))
187 vw = cv2.VideoWriter('out.mp4', cv2.VideoWriter_fourcc(*'mp4v'), 60, (1920, 1080))
188 # timer.better_print(timer.gettime(task_2)) # task_2 takes up much time and we don't test it!
189 frame = np.zeros((1080, 1920, 3), dtype=np.uint8)
190 timer.better_print(timer.gettime(task_3, vw, frame))
191 timer.better_print(timer.gettime(task_4, vw))
192 timer.better_print(timer.gettime(task_5_matrix))
193 timer.better_print(timer.gettime(task_5_pillow))
194 timer.better_print(timer.gettime(task_6_opencv))
195 arr = cv2.imread('in.jpg')
196 timer.better_print(timer.gettime(task_6_pillow))
197 img = Image.new('RGB', (1920, 1080))
198 timer.better_print(timer.gettime(task_7_list, img))
199 timer.better_print(timer.gettime(task_7_asarray, img))
200 timer.better_print(timer.gettime(task_7_array, img))
201 arr2 = np.asarray(img)
202 arr3 = np.array(img)
203 timer.better_print(timer.gettime(task_8_matrix, arr3))
204 timer.better_print(timer.gettime(task_8_pillow_putpixel, img))
205 draw = ImageDraw.Draw(img)
206 timer.better_print(timer.gettime(task_8_pillow_point, draw))
207 timer.better_print(timer.gettime(task_9_line_matrix, arr3))
208 timer.better_print(timer.gettime(task_9_line_pillow, draw))
209 timer.better_print(timer.gettime(task_9_line_opencv, arr))
210 timer.better_print(timer.gettime(task_9_rectangle_matrix, arr3))
211 timer.better_print(timer.gettime(task_9_rectangle_pillow, draw))
212 timer.better_print(timer.gettime(task_9_rectangle_opencv, arr))
213 timer.better_print(timer.gettime(task_9_circle_pillow_arc, draw))
214 timer.better_print(timer.gettime(task_9_circle_pillow_ellipse, draw))
215 timer.better_print(timer.gettime(task_9_circle_opencv_circle, arr))
216 timer.better_print(timer.gettime(task_9_circle_opencv_ellipse, arr))
217 timer.better_print(timer.gettime(task_9_ellipse_pillow, draw))
218 timer.better_print(timer.gettime(task_9_ellipse_opencv, arr))
219 font = ImageFont.truetype('simkai.ttf', 32)
220 timer.better_print(timer.gettime(task_9_text_pillow, draw, font))
221 font = cv2.FONT_HERSHEY_SIMPLEX
222 timer.better_print(timer.gettime(task_9_text_opencv, arr, font))
223 timer.better_print(timer.gettime(task_11_pillow, img))
224 timer.better_print(timer.gettime(task_11_opencv_imread, arr))
225 timer.better_print(timer.gettime(task_11_opencv_asarray, arr2))
226 timer.better_print(timer.gettime(task_11_opencv_array, arr3))
在此我先停一下,各位可以猜猜哪种方式更胜一筹。
flag
flag
flag
flag
flag
flag
flag
flag
flag
flag
flag
flag
flag
五、结果
1.现象
其中task_2(读取视频文件)占用时间过多,我们不予循环测试,下面的结果栏中将给出单次运行的结果(取第一次)。
In [10]: import time
In [11]: s = time.perf_counter(); op.task_2(); f = time.perf_counter(); f - s
Out[11]: 8.617467135000027
In [12]: s = time.perf_counter(); op.task_2(); f = time.perf_counter(); f - s
Out[12]: 8.663589091999995
cmder.exe中运行结果:
E:\test1
$ python3 opencv_pil_time.py
========================================
Function name:
task_1
========================================
Function has the return content below:
None
========================================
Summary of Function Timer:
Count of loops: 100
Average time of loops: 0.0016054189199999984 (sec)
Minimum of every loop time: 0.0013979550000000063 (sec)
Maximum of every loop time: 0.0057973939999999835 (sec)
Total time of loops: 0.16054189199999985
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
SQL Server -- 解决存储过程传入参数作为s
关于JS定时器的整理
JS中使用Promise.all控制所有的异步请求都完
js中字符串的方法
import-local执行流程与node模块路径解析流程
检测数据类型的四种方法
js中数组的方法,32种方法
前端操作方法
数据类型
window.localStorage.setItem 和 localStorage.setIte
如何完美解决前端数字计算精度丢失与数