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
** Environment / Software **
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 ()
.
~~ 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.
Recommended Posts