Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from pypylon import pylon
from pypylon import genicam
# This is a helper function for showing an image on the screen if Windows is used,
# and for printing the first bytes of the image.
def show_image(image, message):
print(message)
pBytes = image.Array
print("Bytes of the image: \n")
print(pBytes)
try:
# Create the converter and set parameters.
converter = pylon.ImageFormatConverter()
converter.OutputPixelFormat = pylon.PixelType_Mono8
# Try to get a grab result for demonstration purposes.
print("Waiting for an image to be grabbed.")
try:
camera = pylon.InstantCamera(pylon.TlFactory.GetInstance().CreateFirstDevice())
grabResult = camera.GrabOne(1000)
show_image(grabResult, "Grabbed image.")
targetImage = pylon.PylonImage.Create(pylon.PixelType_Mono8, grabResult.GetWidth(), grabResult.GetHeight());
print(converter.IsSupportedOutputFormat(pylon.PixelType_Mono8))
# Now we can check if conversion is required.
if converter.ImageHasDestinationFormat(grabResult):
# No conversion is needed. It can be skipped for saving processing
# time.
show_image(grabResult, "Grabbed image.")
def _run_continuous_shot(self, grab_strategy=pylon.GrabStrategy_LatestImageOnly,
window_size=None, image_folder='.'):
self._camera.StopGrabbing()
# converting to opencv bgr format
converter = pylon.ImageFormatConverter()
converter.OutputPixelFormat = pylon.PixelType_BGR8packed
converter.OutputBitAlignment = pylon.OutputBitAlignment_MsbAligned
if(not self._impro_own_window):
cv2.namedWindow('camera_image', cv2.WINDOW_NORMAL | cv2.WINDOW_GUI_NORMAL)
if(window_size is not None):
cv2.resizeWindow('camera_image', window_size[0], window_size[1])
self._camera.StartGrabbing(grab_strategy)
try:
while(self._camera.IsGrabbing()):
grab_result = self._camera.RetrieveResult(5000, pylon.TimeoutHandling_ThrowException)
if grab_result.GrabSucceeded():
# Access the image data
image = converter.Convert(grab_result)
img = image.load_img('pic.png', target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
return np.asarray(x)
prediction_list=['cardboard', 'glass', 'metal', 'paper', 'plastic', 'trash']
model=load_model('models/model1.h5', custom_objects={'relu6': mobilenet.relu6})
camera = pylon.InstantCamera(pylon.TlFactory.GetInstance().CreateFirstDevice())
numberOfImagesToGrab = 100
camera.StartGrabbingMax(numberOfImagesToGrab)
converter = pylon.ImageFormatConverter()
converter.OutputPixelFormat = pylon.PixelType_BGR8packed
converter.OutputBitAlignment = pylon.OutputBitAlignment_MsbAligned
i=0
while camera.IsGrabbing():
time.sleep(0.005)
grabResult = camera.RetrieveResult(5000, pylon.TimeoutHandling_ThrowException)
if grabResult.GrabSucceeded():
# Access the image data.
print("SizeX: ", grabResult.Width)
print("SizeY: ", grabResult.Height)
#import ipdb; ipdb.set_trace()
img = converter.Convert(grabResult).GetArray()
cv2.imwrite('pic.png', img)
pred_img=pp_image(img)
yo=model.predict(pred_img)
def get_image(self):
"""Returns grabbed image or impro function return value, if specified
Returns
-------
openCV image
"""
if self._camera is None or not self._camera.IsOpen():
raise ValueError("Camera object {} is closed.".format(self._camera))
converter = pylon.ImageFormatConverter()
converter.OutputPixelFormat = pylon.PixelType_BGR8packed
converter.OutputBitAlignment = pylon.OutputBitAlignment_MsbAligned
grab_result = self._camera.GrabOne(5000)
image = converter.Convert(grab_result)
img = image.GetArray()
if self._impro_function:
img = self._impro_function(img)
return img
def save_image(self, filename):
"""Saves grabbed image or impro function return value, if specified
Parameters
----------
filename : str
Filename of grabbed image
Returns
-------
None
"""
if self._camera is None or not self._camera.IsOpen():
raise ValueError("Camera object {} is closed.".format(self._camera))
converter = pylon.ImageFormatConverter()
converter.OutputPixelFormat = pylon.PixelType_BGR8packed
converter.OutputBitAlignment = pylon.OutputBitAlignment_MsbAligned
grab_result = self._camera.GrabOne(5000)
image = converter.Convert(grab_result)
img = image.GetArray()
if self._impro_function:
img = self._impro_function(img)
cv2.imwrite(filename, img)
try:
camera = pylon.InstantCamera(
pylon.TlFactory.GetInstance().CreateFirstDevice())
camera.Open()
result = camera.GrabOne(100)
camera.Close()
return result
except genicam.GenericException as e:
print("Could not grab an image: ", e.GetDescription())
try:
# The image format converter basics.
# First the image format converter class must be created.
converter = pylon.ImageFormatConverter()
# Second the converter must be parameterized.
converter.OutputPixelFormat = pylon.PixelType_Mono16
converter.OutputBitAlignment = "MsbAligned"
# or alternatively
converter.OutputBitAlignment = pylon.OutputBitAlignment_MsbAligned
# Then it can be used to convert input images to
# the target image format.
# Grab an image
image = grab_image()
show_image(image, "Source Image")
# Now we can check if conversion is required.