-
Notifications
You must be signed in to change notification settings - Fork 0
/
CameraClass.py
64 lines (58 loc) · 2.38 KB
/
CameraClass.py
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
"""
Last update: 22 July 2020
Created: 10 July 2020
Camera class which runs in a separate thread.
@author: Victor Rogalev
"""
from pypylon import pylon
from pypylon import genicam
from PyQt5.QtCore import QObject, QRunnable, pyqtSignal, pyqtSlot
import time
class CameraGrabber(QRunnable):
def __init__(self, imv, *args, **kwargs):
super(self.__class__, self).__init__()
self.imageWindow = imv
"Connect camera"
try:
self.camera = pylon.InstantCamera(pylon.TlFactory.GetInstance().CreateFirstDevice())
self.camera.Open()
print("Using device ", self.camera.GetDeviceInfo().GetModelName())
# The parameter MaxNumBuffer can be used to control the count of buffers
# allocated for grabbing. The default value of this parameter is 10.
self.camera.MaxNumBuffer = 5
self.camera.ExposureTime = 300000 # exposure
self.camera.PixelFormat = "RGB8" # options: Mono8, BayerRG8, BayerRG12, BayerRG12p, YCbCr422_8, BGR8, RGB8.
self.camera.Gain = 5
except Exception as e:
# Error handling.
print("An exception occurred. Can not connect camera or set up camera parameters")
print(e)
pass
@pyqtSlot()
def run(self):
# Start the grabbing.
try:
self.camera.StartGrabbing(pylon.GrabStrategy_LatestImageOnly)
while self.camera.IsGrabbing():
try:
grabResult = self.camera.RetrieveResult(1000, pylon.TimeoutHandling_ThrowException)
# Image grabbed successfully?
if grabResult.GrabSucceeded():
self.imageWindow.setImage(grabResult.Array)
else:
print("Error: ", grabResult.ErrorCode, grabResult.ErrorDescription)
time.sleep(0.02)
grabResult.Release()
except genicam.GenericException as e:
# Error handling.
print("An exception occurred.")
print(e.GetDescription())
# exitCode = 1
pass
except Exception as e:
print ("An exception occurred. No camera image")
print (Exception)
pass
def stop(self):
self.camera.StopGrabbing()
self.camera.Close()