Dunnoguy
Posts: 10
Joined: Wed Oct 16, 2019 8:13 pm

OpenCV "Error 2" while installing

Thu Feb 13, 2020 10:53 pm

Before I go into the topic, I'd like to know if it's possible for me to utilize OpenCV on my windows PC then transfer the relevant code and files (like images made for Haar Cascade Training) to the Raspberry Pi or must I work from the Pi itself in order to complete my tasks?

I've tried multiple times installing OpenCV and it always gets stuck at the "make -j4" part where I'll wait for hours for it to make and at around 60% it will fail and say "Error 2". I have tried 3 different methods of installing OpenCV and all of them gets stuck at this part. What can be the issue here?

ankith26
Posts: 234
Joined: Mon Mar 25, 2019 11:08 am
Location: /home/pi/pythonprojects/test.py
Contact: Website

Re: OpenCV "Error 2" while installing

Fri Feb 14, 2020 2:30 am

You can code your PC and transfer the files to your pi, but if your project uses cv2, it must be installed on both systems.

How do you install open cv.
I hope you use

Code: Select all

pip3 install opencv-python
I sat thinking for 5 minutes on what to put here. Finally I put something like this.
Check out my github page @ https://github.com/ankith26

Dunnoguy
Posts: 10
Joined: Wed Oct 16, 2019 8:13 pm

Re: OpenCV "Error 2" while installing

Fri Feb 14, 2020 2:36 am

ankith26 wrote:
Fri Feb 14, 2020 2:30 am
You can code your PC and transfer the files to your pi, but if your project uses cv2, it must be installed on both systems.

How do you install open cv.
I hope you use

Code: Select all

pip3 install opencv-python
I did not use that, I followed about 4 different tutorials online and none of them worked, I'm not sure why it is so complicated to install OpenCV on my Pi? I used the code there and installed it, but how do I open the application? I am extremely new to this. I was looking for answers and I found that all the tutorials online is "Building it from source with CMake", I'm not sure what this means and the significance of it but I just want to simply use OpenCV.

ankith26
Posts: 234
Joined: Mon Mar 25, 2019 11:08 am
Location: /home/pi/pythonprojects/test.py
Contact: Website

Re: OpenCV "Error 2" while installing

Fri Feb 14, 2020 2:59 am

The tutorials seem to build the cv2 from source, on a machine like pi that will take a LOT of time.
Just enter the command I have given above in your shell and within seconds cv2 gets installed
I sat thinking for 5 minutes on what to put here. Finally I put something like this.
Check out my github page @ https://github.com/ankith26

Dunnoguy
Posts: 10
Joined: Wed Oct 16, 2019 8:13 pm

Re: OpenCV "Error 2" while installing

Fri Feb 14, 2020 3:05 am

ankith26 wrote:
Fri Feb 14, 2020 2:59 am
The tutorials seem to build the cv2 from source, on a machine like pi that will take a LOT of time.
Just enter the command I have given above in your shell and within seconds cv2 gets installed
How do I launch OpenCV from this point? How do I get numpy because that seems like something I also need.

ankith26
Posts: 234
Joined: Mon Mar 25, 2019 11:08 am
Location: /home/pi/pythonprojects/test.py
Contact: Website

Re: OpenCV "Error 2" while installing

Fri Feb 14, 2020 3:13 am

Just simply import cv2 into your projects
I sat thinking for 5 minutes on what to put here. Finally I put something like this.
Check out my github page @ https://github.com/ankith26

ankith26
Posts: 234
Joined: Mon Mar 25, 2019 11:08 am
Location: /home/pi/pythonprojects/test.py
Contact: Website

Re: OpenCV "Error 2" while installing

Fri Feb 14, 2020 3:14 am

Basically pip3 is a command that will install the “wheels” for all major python libraries out there.
It automatically handles everything. Users just enter one command.

For numpy, use

Code: Select all

pip3 install numpy
Check out https://pypi.org
Also check,
https://pypi.org/project/opencv-python
https://pypi.org/project/numpy
I sat thinking for 5 minutes on what to put here. Finally I put something like this.
Check out my github page @ https://github.com/ankith26

Dunnoguy
Posts: 10
Joined: Wed Oct 16, 2019 8:13 pm

Re: OpenCV "Error 2" while installing

Fri Feb 14, 2020 11:13 pm

ankith26 wrote:
Fri Feb 14, 2020 3:13 am
Just simply import cv2 into your projects
So I tried to import cv2 and I get the error:
"Traceback (most recent call last):
File "/home/pi/opencv_test1.py", line 1, in <module>
import cv2
File "/home/pi/.local/lib/python3.7/site-packages/cv2/__init__.py", line 3, in <module>
from .cv2 import *
ImportError: libQtGui.so.4: cannot open shared object file: No such file or directory"

This is my code for reference (Using Thonny IDE):

Code: Select all

import cv2
import numpy as np
cap = cv2.VideoCapture(0)
while True:
_, frame = cap.read()

cv2.imshow("Frame", frame)

key = cv2.waitKey(1)
if key == 27:
break

cap.release()
cv2.destroyAllWindows()


ankith26
Posts: 234
Joined: Mon Mar 25, 2019 11:08 am
Location: /home/pi/pythonprojects/test.py
Contact: Website

Re: OpenCV "Error 2" while installing

Sat Feb 15, 2020 4:00 am

Hmm.

Opencv is installed on your system , but it got installed on the wrong directory ./.local in your case.
I sat thinking for 5 minutes on what to put here. Finally I put something like this.
Check out my github page @ https://github.com/ankith26

ankith26
Posts: 234
Joined: Mon Mar 25, 2019 11:08 am
Location: /home/pi/pythonprojects/test.py
Contact: Website

Re: OpenCV "Error 2" while installing

Sat Feb 15, 2020 4:11 am

I have been using the pip command for about 2 years now, and from recently even I have started to experience this problems.

But I solved my problems by creating a “soft link” by,

Code: Select all

sudo ln -s /home/pi/.local/lib/python3.7/site-packages/* /usr/lib/python3/dist-packages/
I am NOT sure that this fix will work.
But it just worked for me so I am informing you.

I have recently installed buster, if you also have recently installed, then my fix may work even for you
Last edited by ankith26 on Sat Feb 15, 2020 4:15 am, edited 1 time in total.
I sat thinking for 5 minutes on what to put here. Finally I put something like this.
Check out my github page @ https://github.com/ankith26

ankith26
Posts: 234
Joined: Mon Mar 25, 2019 11:08 am
Location: /home/pi/pythonprojects/test.py
Contact: Website

Re: OpenCV "Error 2" while installing

Sat Feb 15, 2020 4:15 am

So the command I have given above basically creates a link between where your package is installed to where python would normally search for packages you import.
I sat thinking for 5 minutes on what to put here. Finally I put something like this.
Check out my github page @ https://github.com/ankith26

Return to “Beginners”