Python OpenCV 在Ubuntu虚拟机上使用Camera

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012005313/article/details/82146385

VMWare 虚拟机 Ubuntu 中打开笔记本前置摄像头,利用 Python OpenCV 调用 Camera

主机操作系统版本:Windows 10, 64-bit

VMWare:VMware® Workstation 14 Pro

虚拟机操作系统版本:Ubuntu 16.04.5 LTS


主要内容:

  1. VMWare 摄像头设置以及 Ubuntu 摄像头测试
  2. Python OpenCV 实现

VMWare 摄像头设置以及 Ubuntu 摄像头测试

参考:

ubuntu 虚拟机下使用摄像头

虚拟机Linux如何使用笔记本电脑的前置摄像头

Using VMware Workstation Pro

VMWare 摄像头设置

首先,确定主机包含摄像头,可通过 设备管理器 -> 照相机 查看

这里写图片描述

当虚拟机获取摄像头的控制之后,将无法在主机设备管理器找到照相机选项

然后,可以在 VMWare 菜单项中设置摄像头,打开 Ubuntu 虚拟机后,在 VMWare 菜单 虚拟机(M) -> 可移动设备(D) -> Realtek USB Composite Device -> 连接(断开与主机的连接)

这里写图片描述

Note 1:不同的 Camera 名可能不一致

Note 2: 当虚拟机获取摄像头控制权后,每次启动后 VMWare 会有提示

这里写图片描述

虚拟机连接上摄像头后,可在 VMWare 右下角发现一个摄像头标志

这里写图片描述

问题:如果没有找到 camera 设置

可以查看主机是否启动了 VMWareUSB 服务,点击菜单键 -> service,查找 VMware USB Arbitration Service 服务是否已启动

这里写图片描述

Ubuntu 摄像头测试

完成 VMWare 设置后,Ubuntu 系统可通过命令查看 camera 设备是否以加载

ls /dev/video*

// 理想结果
/dev/video0

可通过软件 cheese 打开摄像头

sudo apt-get install cheese

cheese

问题:打开 cheese 发现黑屏

点击 VMWare 菜单项 虚拟机(M) -> 设置(S),找到 硬件 -> USB 控制器,将 USB 兼容性改成 3.0

这里写图片描述


Python OpenCV 实现

参考:tedmiston/webcam-cv2.py

测试代码如下:

import cv2    

def show_webcam(mirror=False):
    cam = cv2.VideoCapture(0)
    while True:
        ret_val, img = cam.read()
        if mirror: 
            img = cv2.flip(img, 1)
        cv2.imshow('my webcam', img)
        if cv2.waitKey(1) == 27: 
            break  # esc to quit
    cv2.destroyAllWindows()

def main():
    show_webcam(mirror=True)

if __name__ == '__main__':
    main()

问题:cv2.imshow("img", img) 失效

OpenCV(3.4.1) Error: Unspecified error (The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script) in cvShowImage, file /opt/conda/conda-bld/opencv-suite_1527005194613/work/modules/highgui/src/window.cpp, line 636
Traceback (most recent call last):
  File "/home/zhujian/face_detection/show_image.py", line 5, in <module>
    cv2.imshow("img", img)
cv2.error: OpenCV(3.4.1) /opt/conda/conda-bld/opencv-suite_1527005194613/work/modules/highgui/src/window.cpp:636: error: (-2) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function cvShowImage

参考:

OpenCV on Wheels

Ubuntu下调用opencv的cv2.imshow()错误

OpenCV not working properly with python on Linux with anaconda. Getting error that cv2.imshow() is not implemented

解决:

卸载已安装的 opencv

conda remove opencv

安装 gtkpkg-config

sudo apt-get install libgtk2.0-dev pkg-config

安装 opencv

pip install opencv-contrib-python

猜你喜欢

转载自blog.csdn.net/u012005313/article/details/82146385