Python 安装pypylon
建议先下载pypylon轮子文件(下载地址),下载后Pip install 轮子文件, 安装即可。
注意不要用Python 3.8, pypylon 只能支持到Python3.7。
Basler相机
连上电源、插上网线,用Pylon Viewer 配置相机参数
定义几个函数采集数据、保存照片
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | from pypylon import pylon import cv2 from datetime import date, datetime # search device and get device def 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)) break else: raise EnvironmentError("no GigE device found") return camera def save_multi_image(): cam = search_get_device() img = pylon.PylonImage() num_img_to_save = 5 cam.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 = 100 ipo.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 delay cam.StartGrabbing(pylon.GrabStrategy_LatestImageOnly) converter = pylon.ImageFormatConverter() # converting to opencv bgr format converter.OutputPixelFormat = pylon.PixelType_BGR8packed converter.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*2 img = grabResult.Array print("Gray value of first pixel: ", img[0, 0]) # After convert to image(ndarray) shape 1944*2592*3 image = 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 delay cam.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 image filename = 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*2 img = grabResult.Array print('img_type', type(img)) else: print("Error: ", grabResult.ErrorCode, grabResult.ErrorDescription) grabResult.Release() cam.Close() return img |
参考资料:
(1)Basler 官网 https://www.baslerweb.com,开发手册,不过都是用C、C++。。。
(2)Github pypylon https://github.com/basler/pypylon所能找到的Python开发的所有/最全的参考资料了
Window 下建议先安装Pylon Viewer 64位:安装文件为Basler_pylon_5.2.0.13457.exe,Basler 官网上下载,下载后做一些IP和相机配置比较方便。
博主QQ 562604218