Python Forum
cv2-python will n ot compile??
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
cv2-python will n ot compile??
#1
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!
Reply
#2
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
Reply
#3
(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
Reply
#4
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.
Reply
#5
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
Reply
#6
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to compile a Python script for a Windows / Linux executable? netanelst 2 1,341 May-24-2022, 07:02 AM
Last Post: netanelst
  Python Compile error ajitnayak1987 4 3,666 Jun-03-2021, 12:11 PM
Last Post: ibreeden
  Embedded python fails to compile on Raspberry Pi tryfon 2 3,495 Dec-22-2020, 02:06 PM
Last Post: tryfon
  Python/C API project - compile to exe MikeTrusky 0 1,528 Jul-22-2020, 03:55 PM
Last Post: MikeTrusky
  How to cross compile python for ARM ? pankaj 4 5,699 Mar-06-2019, 05:59 AM
Last Post: pankaj
  Compile python project to single portable .exe file Campbell 5 10,717 Dec-05-2017, 01:20 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020