[英]Memory error when working with CV2 and Skimage in Powershell
所以,我有一個 python 腳本,它使用 CV2 和 Skimage 來比較文件路徑中的所有圖像並返回最接近的匹配。 它經過測試並有效。
import os
from skimage.metrics import structural_similarity as ssim
import cv2
from shutil import copyfile
def Compare_Images(testDir, reference):
os.chdir(testDir)
testlist = os.listdir(testDir)
smax = 0
closest = 'null'
'''s = ssim(imageA, imageB)'''
for i in range(len(testlist)):
Reference = cv2.imread(reference)
Check = cv2.imread(testlist[i])
Reference = cv2.cvtColor(Reference, cv2.COLOR_BGR2GRAY)
Check = cv2.cvtColor(Check, cv2.COLOR_BGR2GRAY)
s= ssim(Reference, Check)
if (s>smax):
smax = s
closest = testlist[i]
NewPath = "C:\Localdata" + "\\" + closest
OldPath = testDir + "\\" + closest
copyfile(closest, NewPath)
print (closest)
return closest
然后,我將其修改為從 powershell 腳本中調用(通過將 TestDir 的輸入參數設置為等於 sys.argv[1] 和 sys.argv[2] 以供參考):
import os
from skimage.metrics import structural_similarity as ssim
import cv2
import sys
from shutil import copyfile
#def Compare_Images(testDir, reference):
testDir = sys.argv[1]
reference = sys.argv[2]
os.chdir(testDir)
testlist = os.listdir(testDir)
smax = 0
closest = 'null'
#s = ssim(imageA, imageB)
for i in range(len(testlist)):
Reference = cv2.imread(reference)
Check = cv2.imread(testlist[i])
Reference = cv2.cvtColor(Reference, cv2.COLOR_BGR2GRAY)
Check = cv2.cvtColor(Check, cv2.COLOR_BGR2GRAY)
s= ssim(Reference, Check)
if (s>smax):
smax = s
closest = testlist[i]
print (closest)
copyfile(closest, r"C:\Localdata")
但是,當我從 Powershell 調用它時,出現以下錯誤:
python : Traceback (most recent call last):
At C:\...\pythonpasserTest4.ps1:16 char:1
+ python $arg3 $arg1 $arg2
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (Traceback (most recent call last)::String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
File "C:\...\ImagecompareV2.py", line 45, in <module>
s= ssim(Reference, Check)
File "C:\Program Files (x86)\Python37-32\lib\site-packages\skimage\metrics\_structural_similarity.py", line 205, in structural_similarity
ux ** 2 + uy ** 2 + C1,
MemoryError: Unable to allocate array with shape (4032, 3024) and data type float64
PS腳本很簡單:
$env:Path += ";C:\Program Files (x86)\Python37-32";
$env:PATHEXT += ";.py"
$arg1 = 'C:\...\Test Pictures'
$arg2 = 'C:\...\IMG123.JPG'
$arg3 = 'C:\...\ImagecompareV2.py'
python $arg3 $arg1 $arg2
可以做些什么來解決這個問題?
問題是我在 Spyder 中測試了它,而 Powershell 試圖在 32 位 python 中運行它。 卸載 32 位 python 並安裝 64 位 python 解決了這個問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.