简体   繁体   中英

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. 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):

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:

    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:

$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. Uninstalling 32bit python and installing 64bit python solved the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM