-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera.py
75 lines (68 loc) · 3.03 KB
/
camera.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
65
66
67
68
69
70
71
72
73
74
75
import cv2
import numpy as np
from pypylon import pylon
from threading import Thread
import time
#-----------------------------------------------------
# Camera class
# Reads the video frame from a Pylon-compatible camera
# (e.g. the Basler ac4024)
# The camera process runs in a thread
# The most recent frame is accessible via self.latestFrame
#-----------------------------------------------------
class Camera:
#-----------------------------------------------------
def __init__(self, width, height):
#-----------------------------------------------------
self.width = width
self.height = height
self.camera = pylon.InstantCamera(pylon.TlFactory.GetInstance().CreateFirstDevice())
self.camera.Open()
self.camera.Width.SetValue(self.width)
self.camera.Height.SetValue(self.height)
self.camera.CenterX.SetValue(True)
self.camera.CenterY.SetValue(True)
# =======>>>>> TODO: set the automatic gain mode
# ... I could not find the corresponding command... The only way so far is to use Pylon Viewer...
# self.camera.GainAuto.SetValue(GainAuto_Continuous)
self.camera.StartGrabbing(pylon.GrabStrategy_LatestImageOnly)
self.converter = pylon.ImageFormatConverter()
self.converter.OutputPixelFormat = pylon.PixelType_Mono8
self.converter.OutputBitAlignment = pylon.OutputBitAlignment_MsbAligned
self.stopped = False
#-----------------------------------------------------
def getFrame(self):
#-----------------------------------------------------
grabResult = self.camera.RetrieveResult(5000, pylon.TimeoutHandling_ThrowException)
if grabResult.GrabSucceeded():
convertedGrab = self.converter.Convert(grabResult)
frame = convertedGrab.GetArray()
return True, frame
else:
return False, None
#-----------------------------------------------------
def close(self):
#-----------------------------------------------------
self.camera.StopGrabbing()
self.camera.Close()
self.latestFrame = None
#-----------------------------------------------------
def start(self):
#-----------------------------------------------------
self.stopped = False
Thread(target = self.getFrameAsync, args = ()).start()
return self
#-----------------------------------------------------
def getFrameAsync(self):
#-----------------------------------------------------
while not self.stopped:
time.sleep(0.01)
grabResult = self.camera.RetrieveResult(5000, pylon.TimeoutHandling_ThrowException)
if grabResult.GrabSucceeded():
convertedGrab = self.converter.Convert(grabResult)
frame = convertedGrab.GetArray()
self.latestFrame = frame
#-----------------------------------------------------
def stop(self):
#-----------------------------------------------------
self.stopped = True