Oved D
Oved D

Reputation: 7442

Loading files using file paths with opencv in google colaboratory

In Google colaboratory, when trying to use opencv haar-cascade classifiers and loading the classifier data, the library uses a path to determine where the classifier data is.

How can this path be specified in a colaboratory notebook, since typically files are loaded from google drive or uploaded? How can this also be done if opencv loads images?

The Colab notebook looks like:

# install opencv
!apt-get -qq install -y libsm6 libxext6 && pip install -q -U opencv-python
import cv2

# load the cascades.  I'd like to know how to properly set the below path if using google drive for uploading this data.  Or if there is another recommended approach
cascades_path = '/usr/share/opencv/haarcascades/'
face_cascade = cv2.CascadeClassifier(cascades_path + 'haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier(cascades_path + 'haarcascade_eye.xml')

# load an image.  How should this path be specified with opencv in google colaboratory?  
img = cv2.imread('images/image2.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
face_detections = face_cascade.detectMultiScale(gray, 1.3, 5)

Upvotes: 2

Views: 3030

Answers (2)

Kekayan
Kekayan

Reputation: 53

I am also new to this,hope below answer ll help.Let me know your comments.

Download the models from the github repository

!wget https://raw.githubusercontent.com/opencv/opencv/master/data/haarcascades/haarcascade_eye.xml -P drive/gaze

!wget https://raw.githubusercontent.com/opencv/opencv/master/data/haarcascades/haarcascade_frontalface_default.xml -P drive/gaze

Set the root path like cascades_path.

cascades_path = 'drive/gaze/'

Add the respective paths

 face_cascade = cv2.CascadeClassifier(cascades_path + 'haarcascade_frontalface_default.xml')

eye_cascade = cv2.CascadeClassifier(cascades_path + 'haarcascade_eye.xml')

Full solution here https://drive.google.com/file/d/11s5IQkI8H-kIn00Kg6Sqp-dD3RwsICdE/view?usp=sharing

Upvotes: 3

tf wrk
tf wrk

Reputation: 1

I don't know the exact process of finding the path in Google Colab, but I solved the above problem using copy paste in Google Colab:

(!wget http://alereimondo.no-ip.org/OpenCV/uploads/34/frontalFace10.zip)

!unzip  frontalFace10.zip

Upvotes: -1

Related Questions