-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPiHDRVideoStream.py
103 lines (83 loc) · 3.19 KB
/
PiHDRVideoStream.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
# import the necessary packages
from picamera.array import PiRGBArray
from picamera import PiCamera
from threading import Thread
import cv2
from Queue import Queue
import time
from copy import copy
class PiHDRVideoStream:
def __init__(self,
resolution=(320, 240),
framerate=62,
expseq=[0,15000,30000],
isoseq=[100,400,800]):
if len(expseq) != len(isoseq):
raise ValueError("len(expseq) != len(isoseq)")
# initialize the camera and stream
self.camera = PiCamera()
self.camera.resolution = resolution
self.camera.framerate = framerate
# fix camera values
iso=100
time.sleep(2.0)
self.exposure_mode = 'off'
self.shutter_speed = 0
gains = self.camera.awb_gains
self.camera.awb_mode = 'off'
self.camera.awb_gains = gains
self.frameseq = []
for _ in range(len(expseq)):
self.frameseq.append(PiRGBArray(self.camera, size=resolution))
self.zeroCap= PiRGBArray(self.camera, size=resolution)
self.rawCapture= PiRGBArray(self.camera, size=resolution)
self.stream = self.camera.capture_continuous(self.rawCapture,
format="bgr", use_video_port=True)
# initialize the frame and the variable used to indicate
# if the thread should be stopped
#self.frame = None
self.stopped = False
self.frameready = True
self.que = Queue(maxsize=3)
self.expseq = expseq
self.isoseq = isoseq
self.idx = 0
def start(self):
# start the thread to read frames from the video stream
Thread(target=self.update, args=()).start()
return self
def update(self):
try:
# keep looping infinitely until the thread is stopped
for f in self.stream:
self.frameseq[self.idx] = f.array
if self.idx == len(self.expseq)-1:
calibrate = cv2.createCalibrateDebevec()
response = calibrate.process(self.frameseq, self.expseq)
merge_debevec = cv2.createMergeDebevec()
hdr= merge_debevec.process(
self.frameseq,
self.expseq,
response)
self.que.put(hdr.array)
self.rawCapture.truncate(0)
# if the thread indicator variable is set, stop the thread
# and resource camera resources
if self.stopped:
break
self.idx += 1
self.idx = self.idx % len(self.expseq)
self.camera.iso = self.isoseq[self.idx]
self.camera.shutter_speed = self.expseq[self.idx]
print(self.idx, self.camera.exposure_speed, self.camera.iso)
finally:
self.stream.close()
self.rawCapture.close()
self.camera.close()
def read(self):
# return the frame most recently read
self.frameready = False
return self.que.get()
def stop(self):
# indicate that the thread should be stopped
self.stopped = True