HelloWorld (connecting to the camera)
The most basic program: it connects to the INFINICAM GbE-2 and displays information about the camera. The flow used here, “connect to the camera” and “close it when you are done”, is the foundation for every other sample.
-
Enter and save the program
Open Notepad, paste the following code, and save it as
hello_camera.pyinC:\infinicam_projects\python(set “Save as type” to All files and “Encoding” to UTF-8).from pypuclib import CameraFactory, PUCExceptionprint("Connecting to the camera...")try:# Connect to the GbE-2 at this IP addresscam = CameraFactory().create(ip="192.168.0.10")except PUCException as e:print("Error: could not connect to the camera.", e)raise SystemExit(1)print("Connected.")print("Serial number:", cam.serialNo())reso = cam.resolution()print("Resolution:", reso.width, "x", reso.height)print("Frame rate:", cam.framerate(), "fps")# Close the camera when you are done with itcam.close()print("Done.") -
Run it
Connect the camera and run the following 2 lines, one at a time, in Command Prompt (or PowerShell).
Terminal window cd C:\infinicam_projects\pythonTerminal window python hello_camera.pyIf you see output like the following, it worked (the serial number and frame rate depend on your camera).
Connecting to the camera...Connected.Serial number: 99334002087Resolution: 800 x 600Frame rate: 1000 fpsDone.
How the program works
Section titled “How the program works”1. Import pypuclib
Section titled “1. Import pypuclib”from pypuclib import CameraFactory, PUCExceptionThis takes the 2 parts the program uses out of pypuclib.
| Part | Role |
|---|---|
CameraFactory |
The “front desk” that finds and connects to cameras |
PUCException |
The type of error raised when a camera operation fails |
2. Connect to the camera
Section titled “2. Connect to the camera”try: cam = CameraFactory().create(ip="192.168.0.10")except PUCException as e: print("Error: could not connect to the camera.", e) raise SystemExit(1)create(ip="192.168.0.10")connects to the GbE-2 at this IP address. When the connection succeeds, you getcam, which you use to control the camera. From then on you control the camera in the formcam.something().try:…exceptis how Python lets you write “what to do if this fails”. If the camera cannot be found, for example, aPUCExceptionis raised, so the program prints the error and exits.raise SystemExit(1)ends the program at that point.
3. Read camera information
Section titled “3. Read camera information”print("Serial number:", cam.serialNo())
reso = cam.resolution()print("Resolution:", reso.width, "x", reso.height)print("Frame rate:", cam.framerate(), "fps")| Call | What it reads |
|---|---|
cam.serialNo() |
The serial number (the same number as on the label on the back of the camera) |
cam.resolution() |
The current resolution. Use .width and .height to get the width and height |
cam.framerate() |
The current frame rate (frames captured per second) |
4. Close the camera
Section titled “4. Close the camera”cam.close()Always close the camera with close() when you are done. A camera can be used by only one program at a time, so if you don’t close it, other programs or the sample software may not be able to connect.
Troubleshooting
Section titled “Troubleshooting”If you get an error, see Setting up a Python development environment. If you cannot connect to the camera, check these 2 things:
- The IP address settings in PC requirements and network settings
- The firewall rule in step 4-1 of Setting up a Python development environment
