Use PointGrey camera with Python (PyCapture2)

The official release of the Python wrapper for the FlyCapture SDK has been released, so I tried using it. The software can be downloaded from: (User registration required) https://www.ptgrey.com/support/downloads

testing environment

** Environment / Software **

camera

Image acquisition from camera

I want to process the acquired image with OpenCV, so after capturing it with PyCapture, convert it to a ~~ Numpy array. ~~ Save and reopen! This is faster! !! !!

capture.py


# -*- coding: utf-8 -*-
import os
import cv2
import numpy as np
import PyCapture2

# set Camera
bus = PyCapture2.BusManager()
cam = PyCapture2.Camera()
uid = bus.getCameraFromIndex(0)
cam.connect(uid)

def capIm():
  try:
    img = cam.retrieveBuffer()
  except PyCapture2.Fc2error as fc2Err:
    print("Error retrieving buffer :", fc2Err)
    return False, []

  #Convert to Numpy array
  # data = np.array(img.getData(), dtype=np.uint8)
  # data = data.reshape((img.getRows(), img.getCols()))
  
  #Save as bitmap and open with OpenCV
  img.save('tmp.bmp'.encode("utf-8"), PyCapture2.IMAGE_FILE_FORMAT.BMP)
  data = cv2.imread('tmp.bmp', 0)
  return True, data


#Specify the acquired image size.
width, height = 2048, 2048
fmt7info, supported = cam.getFormat7Info(0)
offsetX = int((fmt7info.maxWidth-width)/2)
offsetY = int((fmt7info.maxHeight-height)/2)
pxfmt = PyCapture2.PIXEL_FORMAT.RAW8
fmt7imgSet = PyCapture2.Format7ImageSettings(0, offsetX, offsetY, width, height, pxfmt)
fmt7pktInf, isValid = cam.validateFormat7Settings(fmt7imgSet)
cam.setFormat7ConfigurationPacket(fmt7pktInf.recommendedBytesPerPacket, fmt7imgSet)

#Camera property settings
cam.setProperty(type = PyCapture2.PROPERTY_TYPE.AUTO_EXPOSURE, autoManualMode = True)
cam.setProperty(type = PyCapture2.PROPERTY_TYPE.SHARPNESS, autoManualMode = True)
cam.setProperty(type = PyCapture2.PROPERTY_TYPE.SHUTTER, autoManualMode = False)
cam.setProperty(type = PyCapture2.PROPERTY_TYPE.GAIN, autoManualMode = False)
cam.setProperty(type = PyCapture2.PROPERTY_TYPE.FRAME_RATE, autoManualMode = True)
cam.setProperty(type = PyCapture2.PROPERTY_TYPE.AUTO_EXPOSURE, onOff = True)
cam.setProperty(type = PyCapture2.PROPERTY_TYPE.FRAME_RATE, onOff = True)
cam.setProperty(type = PyCapture2.PROPERTY_TYPE.GAMMA, onOff = True)
cam.setProperty(type = PyCapture2.PROPERTY_TYPE.SHARPNESS, onOff = False)

SHUTTER, GAIN = 500, 100
cam.setProperty(type = PyCapture2.PROPERTY_TYPE.SHUTTER, absValue = SHUTTER/40)
cam.setProperty(type = PyCapture2.PROPERTY_TYPE.GAIN, absValue = GAIN/20)

cam.startCapture()



def nothing(x):
  pass

cv2.namedWindow('PyCapImg', cv2.WINDOW_GUI_NORMAL)
cv2.resizeWindow('PyCapImg', 500,550)
cv2.createTrackbar('SHUTTER', 'PyCapImg', SHUTTER, 1000, nothing)
cv2.createTrackbar('GAIN', 'PyCapImg', GAIN, 1000, nothing)


while cv2.waitKey(1)&0xFF != 27:
  ret, im = capIm()
  if not ret:
    break

  ret = cv2.getTrackbarPos('SHUTTER', 'PyCapImg')
  if ret/40 != SHUTTER:
    SHUTTER = ret
    cam.stopCapture()
    cam.setProperty(type = PyCapture2.PROPERTY_TYPE.SHUTTER, absValue = SHUTTER/40)
    cam.startCapture()

  ret = cv2.getTrackbarPos('GAIN', 'PyCapImg')
  if ret/20 != GAIN:
    GAIN = ret
    cam.stopCapture()
    cam.setProperty(type = PyCapture2.PROPERTY_TYPE.GAIN, absValue = GAIN/20)
    cam.startCapture()

  sh = cam.getProperty(PyCapture2.PROPERTY_TYPE.SHUTTER)
  ga = cam.getProperty(PyCapture2.PROPERTY_TYPE.GAIN)
  cv2.displayOverlay('PyCapImg', 'SHUTTER:{:.2f}, GAIN:{:.2f}'.format(sh.absValue, ga.absValue))

  cv2.imshow('PyCapImg', im)



#Restore the settings
cam.stopCapture()
fmt7imgSet = PyCapture2.Format7ImageSettings(0, 0, 0, fmt7info.maxWidth, fmt7info.maxHeight, pxfmt)
fmt7pktInf, isValid = cam.validateFormat7Settings(fmt7imgSet)
cam.setFormat7ConfigurationPacket(fmt7pktInf.recommendedBytesPerPacket, fmt7imgSet)
cam.setProperty(type = PyCapture2.PROPERTY_TYPE.SHUTTER, autoManualMode = True)
cam.setProperty(type = PyCapture2.PROPERTY_TYPE.GAIN, autoManualMode = True)
cam.setProperty(type = PyCapture2.PROPERTY_TYPE.SHARPNESS, onOff = True)
cam.disconnect()

cv2.destroyAllWindows()

try:
  os.remove('tmp.bmp')
except:
  pass

Now you can use the Point Gray camera as well as cam.read (). im.png

~~ Actually, I wanted to specify the shutter speed, etc., but ... (Addition) It seems that it is necessary to stop the camera capture once to change the properties such as SHUTTER and GAIN, so I am trying to process only when it is changed in the track bar. You can see the camera properties that can be set by checking the standard viewer attached to FlyCapture.

img.png

Recommended Posts

Use PointGrey camera with Python (PyCapture2)
[Python] Use JSON with Python
Use mecab with Python3
Use DynamoDB with Python
Use Python 3.8 with Anaconda
Use python with docker
Use Twitter API with Python
Camera capture with Python + OpenCV
Use TUN / TAP with Python
Use subsonic API with python3
Python: How to use async with
Use vl53l0x with Raspberry Pi (python)
[Python] Use Basic/Digest authentication with Flask
Use NAIF SPICE TOOLKIT with Python
Use rospy with virtualenv in Python3
Use Python in pyenv with NeoVim
Face recognition with camera with opencv3 + python2.7
How to use FTP with Python
Use Windows 10 speech synthesis with Python
Use camera calibration file with OpenCvSharp4
Use OpenCV with Python 3 in Window
Use PostgreSQL with Lambda (Python + psycopg2)
Use smbus with python3 under pyenv environment
Use DeepL with python (for dissertation translation)
Use Amazon Simple Notification Service with Python
[Python] Use string data with scikit-learn SVM
[Introduction to Python] Let's use foreach with Python
Use PIL and Pillow with Cygwin Python
Use cryptography library cryptography with Docker Python image
Single pixel camera to experience with Python
Image acquisition from camera with Python + OpenCV
Use Application Insights with Python 3 (including bottles)
Use C ++ functions from python with pybind11
Use selenium phantomjs webdriver with python unittest
Until you can use opencv with python
Use Python and MeCab with Azure Functions
[Python] Real-time object detection with iPad camera
FizzBuzz with Python3
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Cast with python
I want to use MATLAB feval with python
Use additional Python packages with Serverless Framework (v1.x)
How to use Raspberry Pi pie camera Python
Specify the Python executable to use with virtualenv
Use logger with Python for the time being
Python> Run with run-time arguments> Use import argparse
Use multiple versions of python environment with pyenv