[英]Memory error when working with CV2 and Skimage in Powershell
So, I have a python script that uses CV2 and Skimage to compare all images in a filepath and returns the closest match.所以,我有一个 python 脚本,它使用 CV2 和 Skimage 来比较文件路径中的所有图像并返回最接近的匹配。 It is tested and works.
它经过测试并有效。
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
I then modified it to be called from a powershell script (by setting what were input arguments equal to sys.argv[1] for TestDir and sys.argv[2] for reference):然后,我将其修改为从 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")
However when I call it from Powershell I get the following error:但是,当我从 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
The PS script is simply: 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
What can be done to resolve this?可以做些什么来解决这个问题?
The problem was that I tested it in Spyder, and Powershell was trying to run it in the 32-bit python.问题是我在 Spyder 中测试了它,而 Powershell 试图在 32 位 python 中运行它。 Uninstalling 32bit python and installing 64bit python solved the issue.
卸载 32 位 python 并安装 64 位 python 解决了这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.