Ask Your Question

Importing cv2 through VB for web app

asked 2018-05-16 04:45:27 -0500

Attropos gravatar image

updated 2018-05-28 02:04:41 -0500

I have a functional python script that performs facial recognition using dlib and opencv.

My problem: as soon as I run the python script through VB as a web app, I receive an error "System.Exception: 1".

I have isolated the problem; the error only occurs when I include the line "import cv2" in the python script being executed.

Once again, this only happens when I run the python script through VB. When I run the python script through Spyder or command prompt it runs perfectly. I have successfully installed OpenCV in the the environment that VB is using, and I have reinstalled the module in an attempt to solve the issue, but to no avail.

VB code running Python script with image path as an argument:

Dim Processtask As New System.Diagnostics.Process()
Processtask.StartInfo.FileName = "cmd.exe"
Processtask.StartInfo.Arguments = "/c python " + path.ToString + " " + ImageURL.ToString
Processtask.StartInfo.UseShellExecute = False
Processtask.StartInfo.RedirectStandardOutput = True
Processtask.StartInfo.RedirectStandardError = True
Processtask.StartInfo.CreateNoWindow = True
output = Processtask.StandardOutput.ReadToEnd()

Python code snippet receiving image path:

import sys 
import cv2
if __name__ == "__main__":
    im = str(sys.argv[1])

I have run out of ideas as to what could cause this problem. Any advice on this would be greatly appreciated.


I managed to find the full error message which reads as follows:

System.Exception: System.IO.StreamReader
System.InvalidOperationException: Process has exited, so the requested information is not available. 
at System.Diagnostics.Process.EnsureState(State state) at System.Diagnostics.Process.get_ProcessName() 
at System.Diagnostics.Process.ToString()
edit retag flag offensive close merge delete


though VB is entirely off-topic here, please add your code to the question.

and one of the problems might be: while your python ide properly sets environment vars, before running it, your VB script might inherit some other env, thus not finding the path to the site-libs and such.

berak gravatar imageberak ( 2018-05-16 04:55:12 -0500 )edit

Thanks for the response. It is strange that my .py script run through VB is able to import all the other modules used in the program (quite a long list of them). Only cv2 produces an error.

Attropos gravatar imageAttropos ( 2018-05-16 05:17:27 -0500 )edit

do you have more than 1 python on your box ?

there must also be a way to reach the printed error msg

berak gravatar imageberak ( 2018-05-16 06:58:31 -0500 )edit

The server only has the Anaconda distribution of Python installed. I've updated the post with the full error message.

Attropos gravatar imageAttropos ( 2018-05-28 02:05:23 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2018-05-29 07:37:40 -0500

Attropos gravatar image

Got the solution eventually, I'll post it here in case anyone else runs into a similar problem:

The dll files of opencv installed onto the server which hosted the web app had different access rights. The files were denied access when being called from the web application, whereas the rest of the modules called had no issue.

I used sysinternals process monitor to trace which files were being denied access and was able to change the rights by hand. Not very elegant but it worked out.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2018-05-16 04:45:27 -0500

Seen: 39 times

Last updated: May 29 '18