Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def init(self): # TODO: init or __init__?
if (self.live == 1):
self.icam = py.InstantCamera(py.TlFactory.GetInstance().CreateFirstDevice())
self.icam.Close()
self.icam.RegisterConfiguration(
py.AcquireContinuousConfiguration(),
py.RegistrationMode_ReplaceAll,
py.Cleanup_Delete
)
self.icam.Open()
self.icam.PixelFormat = "RGB8"
self.icam.MaxNumBuffer = 50
#self.icam.MaxNumBuffer = 2000
# Wait for an image and then retrieve it. A timeout of 5000 ms is used.
grabResult = camera.RetrieveResult(5000, pylon.TimeoutHandling_ThrowException)
# Image grabbed successfully?
if grabResult.GrabSucceeded():
# Access the image data.
print("SizeX: ", grabResult.Width)
print("SizeY: ", grabResult.Height)
img = grabResult.Array
print("Gray value of first pixel: ", img[0, 0])
else:
print("Error: ", grabResult.ErrorCode, grabResult.ErrorDescription)
grabResult.Release()
camera.Close()
except genicam.GenericException as e:
# Error handling.
print("An exception occurred.")
print(e.GetDescription())
exitCode = 1
sys.exit(exitCode)
def init(self): # TODO: init or __init__?
if (self.live == 1):
self.icam = py.InstantCamera(py.TlFactory.GetInstance().CreateFirstDevice())
self.icam.Close()
self.icam.RegisterConfiguration(
py.AcquireContinuousConfiguration(),
py.RegistrationMode_ReplaceAll,
py.Cleanup_Delete
)
self.icam.Open()
self.icam.PixelFormat = "RGB8"
self.icam.MaxNumBuffer = 50
#self.icam.MaxNumBuffer = 2000
pass
else:
self.cap = cv2.VideoCapture('video.avi')
grabResult.Release()
return img
else:
return None
# return res.Array
def release(self):
""" """
pass
# self.camera.stopGrabbing()
if __name__ == "__main__":
camera = pylon.InstantCamera(pylon.TlFactory.GetInstance().CreateFirstDevice())
i = camera.GetNodeMap()
# camera.Open()
# camera.StartGrabbing(pylon.GrabStrategy_LatestImageOnly)
# res = camera.RetrieveResult(0, pylon.TimeoutHandling_Return)
# print(res)
# re.
# print(res.Array)
# camera.stopGrabbing()
# camera.Close()
# camera = pylon.InstantCamera(
# pylon.TlFactory.GetInstance().CreateFirstDevice())
camera.StartGrabbing(pylon.GrabStrategy_OneByOne)
camera.FrameRate = 10
def grab_image():
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())
# when explicitly released or when the smart pointer object is destroyed.
# ===============================================================================
from pypylon import pylon
from pypylon import genicam
import sys
# Number of images to be grabbed.
countOfImagesToGrab = 100
# The exit code of the sample application.
exitCode = 0
try:
# Create an instant camera object with the camera device found first.
camera = pylon.InstantCamera(pylon.TlFactory.GetInstance().CreateFirstDevice())
camera.Open()
# Print the model name of the camera.
print("Using device ", camera.GetDeviceInfo().GetModelName())
# demonstrate some feature access
new_width = camera.Width.GetValue() - camera.Width.GetInc()
if new_width >= camera.Width.GetMin():
camera.Width.SetValue(new_width)
# The parameter MaxNumBuffer can be used to control the count of buffers
# allocated for grabbing. The default value of this parameter is 10.
camera.MaxNumBuffer = 5
# Start the grabbing of c_countOfImagesToGrab images.
# The camera device is parameterized with a default configuration which
def __init__(self, identifier, *args, **kw):
# available_cameras = pypylon.factory.find_devices()
factory = pylon.TlFactory.GetInstance()
available_cameras = factory.EnumerateDevices()
self.debug('Available cameras {}'.format(available_cameras))
try:
try:
dev = available_cameras[int(identifier)]
except ValueError:
dev = next((c for c in available_cameras if c.user_defined_name == identifier), None)
cam = pylon.InstantCamera(factory.CreateDevice(dev))
# cam = pypylon.factory.create_device(dev)
except (IndexError, NameError):
cam = None
self._cam = cam
self.pixel_depth = 255
self._grabber = None
self._setting_config = False
camera.Width.SetValue(new_width)
# The parameter MaxNumBuffer can be used to control the count of buffers
# allocated for grabbing. The default value of this parameter is 10.
camera.MaxNumBuffer = 5
# Start the grabbing of c_countOfImagesToGrab images.
# The camera device is parameterized with a default configuration which
# sets up free-running continuous acquisition.
camera.StartGrabbingMax(countOfImagesToGrab)
# Camera.StopGrabbing() is called automatically by the RetrieveResult() method
# when c_countOfImagesToGrab images have been retrieved.
while camera.IsGrabbing():
# Wait for an image and then retrieve it. A timeout of 5000 ms is used.
grabResult = camera.RetrieveResult(5000, pylon.TimeoutHandling_ThrowException)
# Image grabbed successfully?
if grabResult.GrabSucceeded():
# Access the image data.
print("SizeX: ", grabResult.Width)
print("SizeY: ", grabResult.Height)
img = grabResult.Array
print("Gray value of first pixel: ", img[0, 0])
else:
print("Error: ", grabResult.ErrorCode, grabResult.ErrorDescription)
grabResult.Release()
camera.Close()
except genicam.GenericException as e:
# Error handling.
print("An exception occurred.")
import time
import cv2
import os
def pp_image(img):
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)
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.")
else:
# Conversion is needed.
show_image(grabResult, "Grabbed image.")
show_image(targetImage, "Converted image.")
except genicam.GenericException as e:
print("Could not grab an image: ", e.GetDescription())
except genicam.GenericException as e: