-
Notifications
You must be signed in to change notification settings - Fork 3
/
videosource.py
129 lines (114 loc) · 4.63 KB
/
videosource.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on Mon Mar 6 18:28:47 2017
@author: Mark Dammer
FrameInput class to grab frames of given size from camera or file.
"""
from __future__ import division, print_function
import sys
import threading
import time
import cv2
try:
import picamera
import picamera.array
raspi = True
except:
raspi = False
class FrameInputPi(object):
__slots__ = ['raspi', 'input_width', 'input_height', 'thread',
'video_frame', 'exit_thread', 'cam', 'stream', 'foo',
'frame_width', 'frame_height', 'loop', 'cnf']
def __init__(self, video_src, input_width, input_height, cnf):
if raspi is False:
print('Picamera not found, exiting.')
sys.exit(0)
self.input_width = int(input_width)
self.input_height = int(input_height)
self.frame_width = self.input_width
self.frame_height = self.input_height
self.thread = None
self.loop = False
self.video_frame = None
self.exit_thread = False
self.cnf = cnf
if self.thread is None:
# start background frame thread
self.thread = threading.Thread(target=self.framegrabber_thread)
self.thread.start()
# wait until frames start to be available
while self.video_frame is None:
time.sleep(0)
def framegrabber_thread(self):
with picamera.PiCamera() as self.cam:
# camera setup
if self.input_width > 0 and self.input_height > 0:
self.cam.resolution = (self.input_width, self.input_height)
# let camera warm up
self.cam.start_preview()
time.sleep(2)
self.cam.stop_preview()
self.stream = picamera.array.PiRGBArray(self.cam)
for foo in self.cam.capture_continuous(self.stream, 'bgr',
use_video_port=True):
# store frame
self.stream.seek(0)
#self.video_frame = self.stream.read()
self.video_frame = self.stream.array
# reset stream for next frame
self.stream.seek(0)
self.stream.truncate()
if not self.cnf.proc_thread_run:
break
def grab_frame(self):
return self.video_frame
def error_handler(self):
print("No more frames or capture device down - exiting.",
file=sys.stderr)
sys.exit(0)
class FrameInput(object):
__slots__ = ['cnf', 'video_frame', 'exit_thread', 'loop', 'video_src', 'input_width',
'input_height', 'cam', 'frame_width', 'frame_height', 'ret', 'old_frame',
'img']
def __init__(self, video_src, input_width, input_height, cnf):
self.cnf = cnf
self.video_frame = None
self.exit_thread = False # needed for compatibility with videosource_pi.py
self.loop = False
self.video_src = video_src
self.input_width = int(input_width)
self.input_height = int(input_height)
self.cam = cv2.VideoCapture(self.video_src)
self.cam.set(6, cv2.VideoWriter_fourcc(
self.cnf.prop_fourcc[0], self.cnf.prop_fourcc[1], self.cnf.prop_fourcc[2],
self.cnf.prop_fourcc[3]))
if self.input_width > 0 and self.input_height > 0:
self.cam.set(self.cnf.cap_prop_frame_width,
float(self.input_width))
self.cam.set(self.cnf.cap_prop_frame_height,
float(self.input_height))
if self.cam is None or not self.cam.isOpened():
print("Warning: unable to open video source:" +
str(self.video_src), file=sys.stderr)
self.frame_width = int(self.cam.get(self.cnf.cap_prop_frame_width))
self.frame_height = int(self.cam.get(self.cnf.cap_prop_frame_height))
self.ret, self.img = self.cam.read()
if self.ret is False:
self.error_handler()
def grab_frame(self):
self.old_frame = self.video_frame
self.ret, self.video_frame = self.cam.read()
while self.ret is False:
if self.loop:
self.cam.set(self.cnf.cap_prop_pos_frames, 0)
self.ret, self.video_frame = self.cam.read()
else:
self.cnf.rootwidget.playmode_wid.text = '||'
self.ret = True
self.video_frame = self.old_frame
return self.video_frame
def error_handler(self):
print("No more frames or capture device down - exiting.",
file=sys.stderr)
sys.exit(0)