VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 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
      



  

相关教程