【问题标题】:Loading files using file paths with opencv in google colaboratory在google colaboratory中使用带有opencv的文件路径加载文件
【发布时间】:2018-08-13 17:37:55
【问题描述】:

在 Google colaboratory 中,当尝试使用 opencv haar-cascade 分类器并加载分类器数据时,库使用路径来确定分类器数据的位置。

如何在协作笔记本中指定此路径,因为通常文件是从谷歌驱动器加载或上传的?如果opencv加载图片,这也能做到?

Colab 笔记本的外观如下:

# 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)

【问题讨论】:

  • 你得到答案了吗?

标签: python opencv google-colaboratory


【解决方案1】:

我也是新手,希望下面的回答对你有帮助。让我知道你的 cmets。

从 github 仓库下载模型

!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

设置根路径,如 cascades_path。

cascades_path = '驱动器/凝视/'

添加相应的路径

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

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

这里有完整的解决方案 https://drive.google.com/file/d/11s5IQkI8H-kIn00Kg6Sqp-dD3RwsICdE/view?usp=sharing

【讨论】:

    【解决方案2】:

    我不知道在Google Colab中查找路径的具体过程,但我在Google Colab中使用复制粘贴解决了上述问题:

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

    【讨论】:

      猜你喜欢
      • 2019-09-12
      • 1970-01-01
      • 1970-01-01
      • 2019-05-21
      • 2013-11-26
      • 2018-03-05
      • 2020-05-06
      • 1970-01-01
      相关资源
      最近更新 更多