✨
This commit is contained in:
@ -1,9 +1,40 @@
|
||||
# Arlo Robot Controller
|
||||
|
||||
import ctypes
|
||||
import io
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
from contextlib import contextmanager
|
||||
from time import sleep
|
||||
import serial
|
||||
import cv2
|
||||
|
||||
libc = ctypes.CDLL(None)
|
||||
c_stderr = ctypes.c_void_p.in_dll(libc, 'stderr')
|
||||
|
||||
@contextmanager
|
||||
def stderr_redirector(stream):
|
||||
original_stderr_fd = sys.stderr.fileno()
|
||||
|
||||
def _redirect_stderr(to_fd):
|
||||
libc.fflush(c_stderr)
|
||||
sys.stderr.close()
|
||||
os.dup2(to_fd, original_stderr_fd)
|
||||
sys.stderr = io.TextIOWrapper(os.fdopen(original_stderr_fd, 'wb'))
|
||||
|
||||
saved_stderr_fd = os.dup(original_stderr_fd)
|
||||
try:
|
||||
tfile = tempfile.TemporaryFile(mode='w+b')
|
||||
_redirect_stderr(tfile.fileno())
|
||||
yield
|
||||
_redirect_stderr(saved_stderr_fd)
|
||||
tfile.flush()
|
||||
tfile.seek(0, io.SEEK_SET)
|
||||
stream.write(tfile.read().decode())
|
||||
finally:
|
||||
tfile.close()
|
||||
os.close(saved_stderr_fd)
|
||||
|
||||
class Robot(object):
|
||||
"""
|
||||
Defines the Arlo robot API
|
||||
@ -77,6 +108,7 @@ class Robot(object):
|
||||
)
|
||||
|
||||
def take_photo(self):
|
||||
with stderr_redirector(io.StringIO()):
|
||||
cap = self._init_camera()
|
||||
_, photo = cap.read()
|
||||
cap.release()
|
||||
|
Reference in New Issue
Block a user