-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathImageSource.py
More file actions
133 lines (95 loc) · 3.58 KB
/
ImageSource.py
File metadata and controls
133 lines (95 loc) · 3.58 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#Copyright 2016 StudentCV
#Copyright and related rights are licensed under the
#Solderpad Hardware License, Version 0.51 (the “License”);
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at http://solderpad.org/licenses/SHL-0.51.
#Unless required by applicable law or agreed to in writing,
#software, hardware and materials distributed under this License
#is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
#either express or implied. See the License for the specific language
#governing permissions and limitations under the License.
import cv2
import pypylon.pylon as py
import time
class ImageSource:
"""
Class for any image source. It can be a Video, but also a live image from
a camera
"""
live = 0
#cap = cv2.VideoCapture(r'../Video.avi')
grab_status = False
frame_count = 0
frame_rate = 0
def init(self): # TODO: init or __init__?
if (self.live == 1):
self.icam = py.InstantCamera(py.TlFactory.GetInstance().CreateFirstDevice())
self.icam.Close()
self.icam.RegisterConfiguration(
py.AcquireContinuousConfiguration(),
py.RegistrationMode_ReplaceAll,
py.Cleanup_Delete
)
self.icam.Open()
self.icam.PixelFormat = "RGB8"
self.icam.MaxNumBuffer = 50
#self.icam.MaxNumBuffer = 2000
pass
else:
self.cap = cv2.VideoCapture('video.avi')
def start_grab(self):
if(self.live != 1):
self.grab_status = True
#return True
elif self.icam:
self.icam.StartGrabbing(py.GrabStrategy_LatestImages)
self.grab_status = True
return True
else:
self.grab_status = False
return False
def get_newest_frame(self):
if self.live == 1:
if self.grab_status:
with self.icam.RetrieveResult(200, py.TimeoutHandling_Return) as result:
image = cv2.cvtColor(result.Array, cv2.COLOR_RGB2HSV)
self.frame_count = self.frame_count + 1
self.__calc_frametime(time.time())
return image
else:
raise Exception('Camera not Grabbing')
else:
if self.cap.isOpened() or True:
ret, frame = self.cap.read()
self.frame_count = self.frame_count + 1
self.__calc_frametime(time.time())
return cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
else:
print('gooaaaaa')
self.cap.release()
cv2.destroyAllWindows()
def new_image_available(self):
"""
:return: Is there a new image to get?
"""
return True
pass
def get_var(self, _type):
if 'frame_count' == _type:
return self.frame_count
elif 'FrameRate' == _type:
return self.frame_rate
elif 'FrameTime' == _type:
return self.frametime
else:
return "" # False
last_timestamp = 0
def __calc_frametime(self, timestamp):
if 0 == self.last_timestamp:
self.last_timestamp = timestamp
else:
period = timestamp - self.last_timestamp
self.last_timestamp = timestamp
self.frame_rate = 1 / period
self.frametime = period
print(self.frame_rate)