-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcsi_camera.py
147 lines (129 loc) · 4.57 KB
/
csi_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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# MIT License
# Copyright (c) 2019,2020 JetsonHacks
# See license in root folder
# CSI_Camera is a class which encapsulates an OpenCV VideoCapture element
# The VideoCapture element is initialized via a GStreamer pipeline
# The camera is read in a separate thread
# The class also tracks how many frames are read from the camera;
# The calling application tracks the frames_displayed
# Let's use a repeating Timer for counting FPS
import cv2
import threading
class RepeatTimer(threading.Timer):
def run(self):
while not self.finished.wait(self.interval):
self.function(*self.args, **self.kwargs)
class CSI_Camera:
def __init__ (self) :
# Initialize instance variables
# OpenCV video capture element
self.video_capture = None
# The last captured image from the camera
self.frame = None
self.grabbed = False
# The thread where the video capture runs
self.read_thread = None
self.read_lock = threading.Lock()
self.running = False
self.fps_timer=None
self.frames_read=0
self.frames_displayed=0
self.last_frames_read=0
self.last_frames_displayed=0
def open(self, gstreamer_pipeline_string):
try:
self.video_capture = cv2.VideoCapture(
gstreamer_pipeline_string, cv2.CAP_GSTREAMER
)
except RuntimeError:
self.video_capture = None
print("Unable to open camera")
return
# Grab the first frame to start the video capturing
self.grabbed, self.frame = self.video_capture.read()
def start(self):
if self.running:
return None
# create a thread to read the camera image
if self.video_capture != None:
self.running=True
self.read_thread = threading.Thread(target=self.updateCamera)
self.read_thread.start()
return self
def stop(self):
self.running=False
self.read_thread.join()
def updateCamera(self):
# This is the thread to read images from the camera
while self.running:
try:
grabbed, frame = self.video_capture.read()
with self.read_lock:
self.grabbed=grabbed
self.frame=frame
self.frames_read += 1
except RuntimeError:
print("Could not read image from camera")
# FIX ME - stop and cleanup thread
# Something bad happened
def read(self):
with self.read_lock:
frame = self.frame#.copy()
grabbed=self.grabbed
return grabbed, frame
def release(self):
if self.video_capture != None:
self.video_capture.release()
self.video_capture = None
# Kill the timer
#self.fps_timer.cancel()
#self.fps_timer.join()
# Now kill the thread
if self.read_thread != None:
self.read_thread.join()
def update_fps_stats(self):
self.last_frames_read=self.frames_read
self.last_frames_displayed=self.frames_displayed
# Start the next measurement cycle
self.frames_read=0
self.frames_displayed=0
def start_counting_fps(self):
self.fps_timer=RepeatTimer(1.0,self.update_fps_stats)
self.fps_timer.start()
@property
def gstreamer_pipeline(self):
return self._gstreamer_pipeline
# Currently there are setting frame rate on CSI Camera on Nano through gstreamer
# Here we directly select sensor_mode 3 (1280x720, 59.9999 fps)
def create_gstreamer_pipeline(
self,
sensor_id=0,
sensor_mode=3,
capture_width = 1280,
capture_height = 720,
display_width=1280,
display_height=720,
framerate=60,
flip_method=0,
):
self._gstreamer_pipeline = (
"nvarguscamerasrc sensor-id=%d !" #sensor-mode=%d ! "
"video/x-raw(memory:NVMM), "
"width=(int)%d, height=(int)%d, " #"exposuretimerange='10000 10000' "
"format=(string)NV12, framerate=(fraction)%d/1 ! "
"nvvidconv flip-method=vertical-flip ! "
"video/x-raw, width=(int)%d, height=(int)%d, format=(string)BGRx ! "
"videoconvert ! "
"video/x-raw, format=(string)BGR ! appsink"
% (
sensor_id,
#sensor_mode,
capture_width,
capture_height,
#exp_time_str,
framerate,
#flip_method,
display_width,
display_height,
)
)