Python Forum

Full Version: cv2-python will n ot compile??
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I hate it when a posted python example will not compile and I have no idea how to fix the problem. I have captured an example posted below. It works normally, compiles, but then will not run without errors. Any help here?

The example merely puts a webcam on my screen. This works when I use "python webcam.py" but it takes a while to start. It will compile but when run I get the errors below.
I used " pyinstaller --onefile webcam.py" to compile

Example:

import cv2
cap = cv2.VideoCapture(1)
# change 1 to 0 for the other we cam above

# Check if the webcam is opened correctly
if not cap.isOpened():
raise IOError("Cannot open webcam")

while True:
ret, frame = cap.read()
frame = cv2.resize(frame, None, fx=1.5, fy=1.5, interpolation=cv2.INTER_AREA)
cv2.imshow('Input', frame)
c = cv2.waitKey(1)
if c == 27:
#27 is the esc key
break
cap.release()

ccv2.destroyAllWindows()

Errors when run:

C:\Users\hamil\dist>webcam.exe
Traceback (most recent call last):
File "webcam.py", line 1, in <module>
File "PyInstaller\loader\pyimod03_importers.py", line 476, in exec_module
File "cv2\__init__.py", line 188, in <module>
File "cv2\__init__.py", line 118, in bootstrap
File "cv2\__init__.py", line 116, in load_first_config
ImportError: OpenCV loader: missing configuration file: ['config.py']. Check OpenCV installation.
[8488] Failed to execute script 'webcam' due to unhandled exception!
ap = cv2.VideoCapture(1)
First thing is the variable name should be cap

Also, if you only have one webcam installed on that machine then the device number would be 0.

This worked on my laptop with one webcam installed.

cap = cv2.VideoCapture(0)
/regards
(Dec-26-2021, 04:28 PM)popejose Wrote: [ -> ]
ap = cv2.VideoCapture(1)
First thing is the variable name should be cap

Also, if you only have one webcam installed on that machine then the device number would be 0.

This worked on my laptop with one webcam installed.

cap = cv2.VideoCapture(0)
/regards
Yes, I realized after my post that I had deleted the "c" in cap. Actually, I have two webcams. If I use 0 then one is displayed and if I use 1 the other one is displayed. For some reason, it takes about 5 seconds for the image to appear. Is this the case for you? Did you compile and run the program?? It works when not compiled.
Yes I was able to get pyinstaller's 50mb exe to run. I tried running it from different paths outside my venv and it still worked.

I did this from a clean venv. You might want to try that to see if it resolves the path errors you are getting.

As for the delays, it takes about 2-3 seconds if I run the script and about 4-5 with the exe version. (AMD A12-9720P, 8GB RAM)

/regards
I am not familiar with "venv" .Maybe this is my problem. I do not understand the delays however. I would think the script would have a longer delay and why is there any delay in the exe? I will continue to try different things.

Oh, I forgot to say that webcam.py DID compile, but when I tried to execute it there was the error messages I showed above. Also, when I tried to run the exe from Windows, I got a brief black screen, then the program closed??

Thanks