Search code examples
pythonvbscript

Passing return value from Python script to VB script


I have a VB script ( UFT) code that triggers Python script to just check if the image as required color or not and python script is returning the string back to VB script with the color value. I was able to call the python script from VB script and python script also executed and returned value. My problem is am not able to save the return value in any variable it is just blank

Below is my VB script code that calls the python script

Function Verifycolor(icolor,ipath,rValue)

Dim pyr
Set pyShell = CreateObject("Wscript.Shell")
color = icolor
Imagepath = ipath
pyColorVerify = "python ""C:\Users\a272099\Desktop\Python\codes\Verifycolor.py""" & " " & color & " " & ImagePath
Set pyOperation = pyShell.Exec(pyColorVerify)
Set pyResult = pyOperation.StdOut
wait(1)
msgbox pyResult.ReadAll


pyr = pyResult.ReadAll

msgbox pyr

'Do 
'pyRet = pyResult.ReadAll
''msgbox pyReturn
'wait(1)
'Loop Until pyReturn <> empty

If pyr = rValue  Then
        Reporter.ReportEvent micPass,"Preconditions are not set to" &color& "color","preconditions are set"
    Else
        Reporter.ReportEvent micFail,"Preconditions are set  to" &color& "color","preconditions are set"
End If

msgbox pyResult.ReadAll

End Function

Below is the python script that returns value to VB script (Verify color)

import sys
import numpy as np
import pytesseract
from PIL import Image
from pytesseract import image_to_string
import cv2
color = sys.argv[1]
src_path = sys.argv[2]
#src_path = "C:\\Users\\a291068\\Desktop\\Python\\PreconditionsImage.png"
img=cv2.imread(src_path)
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
if color == "RED":
    mask_red = cv2.inRange(img_hsv, (0,50,20), (5,255,255))
    if cv2.countNonZero(mask_red) > 0:
        Return = "Red is present!"
    else:
        Return = "Red is not present!"
else:
    if color == "GREEN":
        mask_green = cv2.inRange(img_hsv, (36,25,25), (70,255,255))
        if cv2.countNonZero(mask_green) > 0:
            Return= "Green is present!"
        else:
            Return = "Green is not present!"
print(Return) 

Problem is in my VB script when python is returning value when i do Msgbox PyResult.Readall am getting correct message that was returned from Python either Green is present or Green is not present.

If i assign that value to any variable like pyr. pyr is always empty and nothing is getting stored.

Please help me to resolve this issue of storing pyResult.ReadAll to a variable so that i can use it in other conditions.

Let me know if further information is required.


Solution

  • It looks like the ReadAll method also clears the content in the StdOut, so you can only use it once.

    In your example, it works for the line

    msgbox pyResult.ReadAll
    

    But after this call your pyResult is empty. Trying to use this method a second time doesn't work anymore so in this line

    pyr = pyResult.ReadAll
    

    It returns an empty string.

    Simply removing the first call

    msgbox pyResult.ReadAll
    

    and only call ReadAll once to store the returned value into variable like this

    pyr = pyResult.ReadAll
    

    From then on only use pyr if you need that value, and don't call ReadAll anymore.