1

I would like to write a Python script in Repl.it, but I can't seem to be able to import OpenCV. I am using Repl.it for the first time, so I am not really sure of what I should be doing.

Here are the different things that I tried :

  • Just having
    import opencv_python as cv2

in main.py results in the following console output:

Python 3.6.1 (default, Dec 2015, 13:05:11)
[GCC 4.8.2] on linux

Repl.it: Installing fresh packages
Repl.it:

Collecting opencv_python
  Using cached https://files.pythonhosted.org/packages/37/49/874d119948a5a084a7ebe98308214098ef3471d76ab74200f9800efeef15/opencv_python-4.0.0.21-cp36-cp36m-manylinux1_x86_64.whl
Collecting numpy>=1.11.3 (from opencv_python)
  Using cached https://files.pythonhosted.org/packages/f5/bf/4981bcbee43934f0adb8f764a1e70ab0ee5a448f6505bd04a87a2fda2a8b/numpy-1.16.1-cp36-cp36m-manylinux1_x86_64.whl
Installing collected packages: numpy, opencv-python
Successfully installed numpy-1.16.1 opencv-python-4.0.0.21
Target directory /home/runner/.site-packages/numpy-1.16.1.dist-info already exists. Specify --upgrade to force replacement.
Target directory /home/runner/.site-packages/numpy already exists. Specify --upgrade to force replacement.
Target directory /home/runner/.site-packages/opencv_python-4.0.0.21.dist-info already exists. Specify --upgrade to force replacement.
Target directory /home/runner/.site-packages/cv2 already exists. Specify --upgrade to force replacement.
You are using pip version 9.0.1, however version 19.0.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

Repl.it: package installation success

Traceback (most recent call last):
  File "main.py", line 1, in <module>
    import opencv_python as cv2
ModuleNotFoundError: No module named 'opencv_python'

It seems the package is installed correctely, but cannot be imported for some reason...?

  • I also tried adding a requirements.txt file :
    opencv-python==4.0.0.21

but this results in a similar output:

Python 3.6.1 (default, Dec 2015, 13:05:11)
[GCC 4.8.2] on linux

Repl.it: Installing fresh packages
Repl.it:

Collecting opencv-python==4.0.0.21 (from -r requirements.txt (line 1))
  Using cached https://files.pythonhosted.org/packages/37/49/874d119948a5a084a7ebe98308214098ef3471d76ab74200f9800efeef15/opencv_python-4.0.0.21-cp36-cp36m-manylinux1_x86_64.whl
Collecting numpy>=1.11.3 (from opencv-python==4.0.0.21->-r requirements.txt (line 1))
  Using cached https://files.pythonhosted.org/packages/f5/bf/4981bcbee43934f0adb8f764a1e70ab0ee5a448f6505bd04a87a2fda2a8b/numpy-1.16.1-cp36-cp36m-manylinux1_x86_64.whl
Installing collected packages: numpy, opencv-python
Successfully installed numpy-1.16.1 opencv-python-4.0.0.21
Target directory /home/runner/.site-packages/numpy-1.16.1.dist-info already exists. Specify --upgrade to force replacement.
Target directory /home/runner/.site-packages/numpy already exists. Specify --upgrade to force replacement.
Target directory /home/runner/.site-packages/opencv_python-4.0.0.21.dist-info already exists. Specify --upgrade to force replacement.
Target directory /home/runner/.site-packages/cv2 already exists. Specify --upgrade to force replacement.
You are using pip version 9.0.1, however version 19.0.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.


Repl.it: package installation success

Traceback (most recent call last):
  File "main.py", line 1, in <module>
    import opencv_python as cv2
ModuleNotFoundError: No module named 'opencv_python'
  • I also tried to import the opencv-python-headless package (both with and without a requirements file), with the same result.

  • Finally, I tried using the following line in main.py:

import cv2

which results in this output:

Python 3.6.1 (default, Dec 2015, 13:05:11)
[GCC 4.8.2] on linux
Traceback (most recent call last):
  File "/run_dir/repl.py", line 60, in <module>
    raise EOFError
EOFError
Traceback (most recent call last):
  File "main.py", line 1, in <module>
    import cv2
  File "/home/runner/.site-packages/cv2/__init__.py", line 3, in <module>
    from .cv2 import *
ImportError: libgthread-2.0.so.0: cannot open shared object file: No such file or directory

I would greatly appreciate any help in importing this package!

Daphné
  • 11
  • 1
  • 4

2 Answers2

1

You can do this using the repl.it package manager. Do this by pressing the cube icon in the left. where you can find the cube

Then, enter the desired package. where to enter desired package

Finally, press the plus button.

To import opencv-python, use

import cv2  # When installed opencv-python is already "cv2"
Nv7
  • 406
  • 6
  • 20
0

Import the OpenCV library

import cv2

Read the image from the file

image = cv2.imread("patrick_bateman.jpg")

Convert the image to grayscale

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

Load the face detection cascade

face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")

Detect the face in the image

faces = face_cascade.detectMultiScale(gray, 1.3, 5)

Loop over the detected faces

for (x, y, w, h) in faces: # Crop the face region from the image face = image[y:y+h, x:x+w]

# Convert the face region to HSV color space
hsv = cv2.cvtColor(face, cv2.COLOR_BGR2HSV)

# Change the hue and saturation values of the hair region to make it black
hsv[0:50, :, 0] = 0 # Hue
hsv[0:50, :, 1] = 255 # Saturation

# Convert the face region back to BGR color space
face = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)

# Apply a Gaussian blur to the hair region to make it look wet
face[0:50, :] = cv2.GaussianBlur(face[0:50, :], (5, 5), 0)

# Shift some hair pixels down to cover the eye
face[40:50, :] = face[30:40, :]

# Replace the face region in the original image with the modified one
image[y:y+h, x:x+w] = face

Show the modified image

cv2.imshow("Modified Image", image)

Wait for a key press to exit

cv2.waitKey(0)

Close all windows

cv2.destroyAllWindows()