-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathjpg_img_queue.py
64 lines (51 loc) · 2.1 KB
/
jpg_img_queue.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
import cv2
import time
import logging
import os
import numpy as np
from ethoscope.utils.debug import EthoscopeException
import multiprocessing
from scipy.misc import toimage, imread, imsave
from io import BytesIO
import io
from ethoscope.hardware.input.cameras import PiFrameGrabber, OurPiCameraAsync
class PiFrameGrabberJPEG(PiFrameGrabber):
def run(self):
try:
with PiCamera() as capture:
capture.resolution = self._target_resolution
capture.framerate = self._target_fps
raw_capture = PiRGBArray(capture, size=self._target_resolution)
for frame in capture.capture_continuous(raw_capture, format="bgr", use_video_port=True):
if not self._stop_queue.empty():
logging.info("The stop queue is not empty. Stop acquiring frames")
self._stop_queue.get()
self._stop_queue.task_done()
logging.info("Stop Task Done")
break
raw_capture.truncate(0)
# out = np.copy(frame.array)
out = BytesIO()
toimage(frame.array, cmin=0.0, cmax=255).save(out,"JPEG")
#fixme here we could actually pass a JPG compressed file object (http://docs.scipy.org/doc/scipy-0.16.0/reference/generated/scipy.misc.imsave.html)
# This way, we would manage to get faster FPS
self._queue.put(out)
finally:
self._stop_queue.close()
self._queue.close()
logging.info("Camera Frame grabber stopped acquisition cleanly")
class OurPiCameraAsyncJPEG(OurPiCameraAsync):
def _next_image(self):
try:
g = self._queue.get(timeout=30)
self._frame = imread(g)
return self._frame
except Exception as e:
raise EthoscopeException("Could not get frame from camera\n%s", str(e))
cam = OurPiCameraAsyncJPEG()
t0 = 0
for t,f in cam:
print("dt = ", t - t0)
print(np.sum(cam))
time.sleep(.3)
t0 = t