1-4GPUSpeedTest.py

000: import cv2
001: import time
002:
003: def __main():
004: setPix = 300
005: REPEAT = 10000
006: img = cv2.imread(‘IMG_0379.JPG’)
007: img = getResize(img, 1280)
008: src = img.copy()
009:
010: timeStart = time.time()
011: for i in range(REPEAT):
012: dst1 = getResize(src, setPix)
013:
014: timeEnd = time.time()
015: print(“{0} = {1}”.format(‘CPU’, (timeEnd – timeStart) * 1000) + “/ms”)
016:
017: gpuImg = cv2.cuda_GpuMat()
018: gpuDst = cv2.cuda_GpuMat()
019: gpuImg.upload(img)
020:
021: timeStart = time.time()
022: for i in range(REPEAT):
023: gpuDst = getGpuResize(gpuImg, setPix)
024:
025: timeEnd = time.time()
026: print(“{0} = {1}”.format(‘GPU’, (timeEnd – timeStart) * 1000) + “/ms”)
027:
028: dst2 = gpuDst.download()
029: img = gpuImg.download()
030: cv2.imshow(‘Resize’, dst2)
031: cv2.imshow(‘Finish’, img)
032: cv2.waitKey(0)
033: cv2.destroyAllWindows()
034:
035: return 0
036:
037: def getResize(img, num):
038: basePixSize = num # 縦横で大きい辺の変更したいサイズ
039: height = img.shape[0]
040: width = img.shape[1]
041:
042: largeSize = max(height, width) # 大きい方の辺のサイズ
043: resizeRate = basePixSize / largeSize # 変更比率を計算
044: img = cv2.resize(img, (int(width * resizeRate), int(height * resizeRate)))
045:
046: return img
047:
048: def getGpuResize(gpuSrc, num):
049: basePixSize = num # 縦横で大きい辺の変更したいサイズ
050: width, height = gpuSrc.size()
051:
052: largeSize = max(height, width) # 大きい方の辺のサイズ
053: resizeRate = basePixSize / largeSize # 変更比率を計算
054: gpuSrc = cv2.cuda.resize(gpuSrc, (int(width * resizeRate), int(height * resizeRate)))
055:
056: return gpuSrc
057:
058: if __name__ == ‘__main__’:
059: print(cv2.__version__)
060:
061: __main()