700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Basler 工业相机 Python开发采集数据 保存照片

Basler 工业相机 Python开发采集数据 保存照片

时间:2022-07-18 13:36:46

相关推荐

Basler 工业相机 Python开发采集数据 保存照片

Python 安装pypylon

建议先下载pypylon轮子文件(下载地址),下载后Pip install 轮子文件, 安装即可。

注意不要用Python 3.8, pypylon 只能支持到Python3.7。

Basler相机

连上电源、插上网线,用Pylon Viewer 配置相机参数

定义几个函数采集数据、保存照片

from pypylon import pylonimport cv2from datetime import date, datetime# search device and get devicedef search_get_device():tl_factory = pylon.TlFactory.GetInstance()for dev_info in tl_factory.EnumerateDevices():print("DeviceClass:", dev_info.GetDeviceClass())if dev_info.GetDeviceClass() == 'BaslerGigE': # 千兆网(GigE)print(f"ModelName:{dev_info.GetModelName()}\n"f"IP:{dev_info.GetIpAddress()}")camera = pylon.InstantCamera(tl_factory.CreateDevice(dev_info))breakelse:raise EnvironmentError("no GigE device found")return cameradef save_multi_image():cam = search_get_device()img = pylon.PylonImage()num_img_to_save = 5cam.Open()cam.StartGrabbing() # Starts the grabbing for a maximum number of images.for i in range(num_img_to_save):with cam.RetrieveResult(2000) as result:# Calling AttachGrabResultBuffer creates another reference to the# grab result buffer. This prevents the buffer's reuse for grabbing.img.AttachGrabResultBuffer(result)# print("Img reference:",img)# print("Result reference",result)# The JPEG format that is used here supports adjusting the image# quality (100 -> best quality, 0 -> poor quality).ipo = pylon.ImagePersistenceOptions()quality = 100 - i * 20# quality = 100ipo.SetQuality(quality)filename = f"saved_pypylon_img_{quality}.jpeg"img.Save(pylon.ImageFileFormat_Jpeg, filename)#, ipo)img.Release()cam.StopGrabbing()cam.Close()def grab_show_image():cam = search_get_device()cam.Open()# Grabing Continusely (video) with minimal delaycam.StartGrabbing(pylon.GrabStrategy_LatestImageOnly)converter = pylon.ImageFormatConverter()# converting to opencv bgr formatconverter.OutputPixelFormat = pylon.PixelType_BGR8packedconverter.OutputBitAlignment = pylon.OutputBitAlignment_MsbAligned# Wait for an image and then retrieve it. A timeout of 5000 ms is used.grabResult = cam.RetrieveResult(5000, pylon.TimeoutHandling_ThrowException)# Image grabbed successfully?# print(dir(grabResult))if grabResult.GrabSucceeded():# Access the image data.print("SizeX: ", grabResult.Width)print("SizeY: ", grabResult.Height)# img type class 'numpy.ndarray', shape 1944*2592*2img = grabResult.Arrayprint("Gray value of first pixel: ", img[0, 0])# After convert to image(ndarray) shape 1944*2592*3image = converter.Convert(grabResult)weld_img = image.GetArray()# cv2.namedWindow('test', cv2.WINDOW_NORMAL)# cv2.imshow('test', weld_img)# cv2.waitKey(0)# cv2.destroyAllWindows()print('weld_img_type',type(weld_img))print('img_type', type(img))print(weld_img[weld_img[:,:,1] != img[:,:,0]])else:print("Error: ", grabResult.ErrorCode, grabResult.ErrorDescription)grabResult.Release()cam.Close()# grab_show_image()def grab_image_save():cam = search_get_device()cam.Open()save_img = pylon.PylonImage()# Grabing Continusely (video) with minimal delaycam.StartGrabbing(pylon.GrabStrategy_LatestImageOnly)# Wait for an image and then retrieve it. A timeout of 5000 ms is used.grabResult = cam.RetrieveResult(5000, pylon.TimeoutHandling_ThrowException)# Image grabbed successfully# print(dir(grabResult))if grabResult.GrabSucceeded():# save imagefilename = f"Image{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.jpeg"save_img.AttachGrabResultBuffer(grabResult)ipo = pylon.ImagePersistenceOptions()ipo.SetQuality(quality=100)save_img.Save(pylon.ImageFileFormat_Jpeg, filename, ipo)save_img.Release()# Access the image data.# img type class 'numpy.ndarray', shape 1944*2592*2img = grabResult.Arrayprint('img_type', type(img))else:print("Error: ", grabResult.ErrorCode, grabResult.ErrorDescription)grabResult.Release()cam.Close()return img

参考资料:

(1)Basler 官网 ,开发手册,不过都是用C、C++。。。

(2)Github pypylon /basler/pypylon所能找到的Python开发的所有/最全的参考资料了

Window 下建议先安装Pylon Viewer 64位:安装文件为Basler_pylon_5.2.0.13457.exe,Basler 官网上下载,下载后做一些IP和相机配置比较方便。

博主QQ 562604218

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。