深度学习 | Python下使用 flycapture 相机

前言

最近发现网上关于flycapture相机在opencv中的调用多是C++版本的,所以在网上查询了一些资料,然后总结出一份教程

教程

第一步、安装Cython

Cython 是一个编译python当中的C扩展的插件,由于flycapture中的接口是c的,所以需要他来编译

pip install Cython

第二步、安装 flycapture2

试了半天,都以失败告终,总是说文件不存在,的确我也没有把.h .c .dll,不知道是不是这个原因,之后就开始求助单身交友平台Github,然后就找到一个库 https://github.com/jordens/pyflycapture2

通过这行代码轻松的安装上了flycapture2

pip install git+https://github.com/jordens/pyflycapture2.git

错误的解决办法

如果出现了以下错误,说明你的win驱动没有装到默认的位置,比如:C:/Program Files (x86)/Point Grey Research/FlyCapture2

深度学习 | Python下使用 flycapture 相机_第1张图片

这时需要你手动clone此代码到本地

git clone https://github.com/jordens/pyflycapture2.git

随后修改其中setup.py的pointgrey_win为你的驱动位置

深度学习 | Python下使用 flycapture 相机_第2张图片

最后使用以下代码进行安装

python setup.py install

第三步、测试

我修改了一下他的代码,现在在我的相机上能用了

不知道为什么set_video_mode_and_frame_rate方法不能用,难道是我的相机是灰度的原因嘛,下面的 贴上修改后的代码

# encoding = utf-8
"""
@version: 0.1
@author: Jack Wang
@file: testcamera.py
@time: 2018/11/28 下午 6:09
"""
# !/usr/bin/python
# -*- coding: utf-8 -*-
#
#   pyflycapture2 - python bindings for libflycapture2_c
#   Copyright (C) 2012 Robert Jordens 
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see .

import flycapture2 as fc2
import numpy as np
import cv2


def test():
    print(fc2.get_library_version())
    c = fc2.Context()
    print(c.get_num_of_cameras())
    c.connect(*c.get_camera_from_index(0))
    print(c.get_camera_info())
    # c.set_video_mode_and_frame_rate(fc2.VIDEOMODE_FORMAT7, fc2.FRAMERATE_60)
    m, f = c.get_video_mode_and_frame_rate()
    print(m, f)
    # print(c.get_video_mode_and_frame_rate_info(m, f))
    print(c.get_property_info(fc2.FRAME_RATE))
    p = c.get_property(fc2.FRAME_RATE)
    print(p)
    c.set_property(**p)
    c.start_capture()
    while 1:
        im = fc2.Image()
        c.retrieve_buffer(im)
        # print([np.array(c.retrieve_buffer(im)).sum() for i in range(80)])
        a = np.array(im)
        cv2.imshow('x', a)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    print(a.shape, a.base)
    c.stop_capture()
    c.disconnect()


if __name__ == "__main__":
    test()

 

你可能感兴趣的:(问题解决,深度学习)