def _cleanup(self): print('cleaning up') # When everything is done, release the capture if environment.is_mac(): self.video_capture.release() else: # raspberry pi self.video_stream.stop() cv2.destroyAllWindows()
def get_timing(self): if not environment.is_mac(): timing = self.tof.get_timing() if (timing < 20000): timing = 20000 return timing else: return 0
def __init__(self): if environment.is_mac(): self.video_capture = cv2.VideoCapture(0) self.video_capture.set(cv2.CAP_PROP_FRAME_WIDTH, 640) self.video_capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 480) else: # raspberry pi self.video_stream = PiVideoStream().start() self.face_cascade = cv2.CascadeClassifier(FACE_CASC_PATH) self.smile_cascade = cv2.CascadeClassifier(SMILE_CASC_PATH) # self.hand_cascade = cv2.CascadeClassifier(HAND_CASC_PATH) self._detect_face = False self._detect_smile = False atexit.register(self._cleanup)
def setup_dlopen(library, depends=[]): """Set the flags for a call to import a shared library such that all symbols are imported. On Linux this sets the flags for dlopen so that all symbols from the library are imported in to the global symbol table. Without this each shared library gets its own copy of any singleton, which is not the correct behaviour Args: library - The path to the library we are opening depends - A list of dependents to open (default=[]) Returns the original flags """ if environment.is_windows(): return None old_flags = sys.getdlopenflags() import _dlopen dlloader = _dlopen.loadlibrary import subprocess _bin = os.path.join(os.path.abspath(os.path.dirname(__file__)), '../../') def get_libpath(mainlib, dependency): if environment.is_linux(): cmd = 'ldd %s | grep %s' % (mainlib, dependency) subp = subprocess.Popen(cmd,stdout=subprocess.PIPE, stderr=subprocess.STDOUT,shell=True) out = subp.communicate()[0] # ldd produces a string that always has 4 columns. The full path # is in the 3rd column libpath = out.split()[2] else: libpath = os.path.join(_bin, dependency + '.dylib') return libpath library_var = "LD_LIBRARY_PATH" if environment.is_mac(): library_var = 'DY' + library_var ldpath = os.environ.get(library_var, "") ldpath += ":" + _bin os.environ[library_var] = ldpath pythonlib = library if environment.is_linux(): # stdc++ has to be loaded first or exceptions don't get translated # properly across bounadries # NeXus has to be loaded as well as there seems to be an issue with # the thread-local storage not being initialized properly unles # it is loaded before other libraries. dlloader(get_libpath(pythonlib, 'stdc++')) dlloader(get_libpath(pythonlib, 'libNeXus.so')) # Load the dependencies for dep in depends: dlloader(get_libpath(pythonlib, dep)) oldflags = sys.getdlopenflags() if environment.is_mac(): try: import dl RTLD_LOCAL = dl.RTLD_LOCAL RTLD_NOW = dl.RTLD_NOW except ImportError: RTLD_LOCAL = 0x4 RTLD_NOW = 0x2 sys.setdlopenflags(RTLD_LOCAL|RTLD_NOW) return old_flags
import sys import os import environment ####################################################################### # Ensure the correct Mantid shared libaries are found when loading the # python shared libraries ####################################################################### if environment.is_windows(): _var = 'PATH' _sep = ';' else: _sep = ':' if environment.is_linux(): _var = 'LD_LIBRARY_PATH' elif environment.is_mac(): _var = 'DYLD_LIBRARY_PATH' else: # Give it a go how it is _var = '' _sep = '' try: _oldpath = os.environ[_var] except: _oldpath = '' _sep = '' os.environ[_var] = os.environ['MANTIDPATH'] + _sep + _oldpath ####################################################################### # Public api #######################################################################
def detect(self): if not self._detect_face and self._detect_smile: raise "in order to detect smile, must detect face" _return = {} _return['face'] = False _return['smile'] = False # Capture frame-by-frame if environment.is_mac(): _, frame = self.video_capture.read() else: # raspberry pi frame = self.video_stream.read() if frame is None: return _return # camera might not be ready yet gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # hands = self.hand_cascade.detectMultiScale( # gray, # scaleFactor=1.3, # minNeighbors=5, # minSize=(30, 30), # flags=cv2.CASCADE_SCALE_IMAGE # ) # # # Draw a rectangle around the faces # for (x, y, w, h) in hands: # cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) if self._detect_face: faces = self.face_cascade.detectMultiScale( gray, scaleFactor=1.3, minNeighbors=5, minSize=(60, 60), flags=cv2.CASCADE_SCALE_IMAGE) # Draw a rectangle around the faces for (x, y, w, h) in faces: cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) # if face detected face_detected = hasattr(faces, 'shape') _return['face'] = face_detected if face_detected and self._detect_smile: # detect smile # pick largest face faces_area = faces[:, 2] * faces[:, 3] face = faces[np.argmax(faces_area)] # find smile within face x, y, w, h = face roi_gray = gray[y:y + h, x:x + w] roi_color = frame[y:y + h, x:x + w] smiles = self.smile_cascade.detectMultiScale( roi_gray, scaleFactor=1.3, minNeighbors=60, # the lower, the more sensitive minSize=(10, 10), maxSize=(120, 120), flags=cv2.CASCADE_SCALE_IMAGE) # Set region of interest for smiles for (x, y, w, h) in smiles: cv2.rectangle(roi_color, (x, y), (x + w, y + h), (0, 0, 255), 3) # if smile detected _return['smile'] = hasattr(smiles, 'shape') # Display the resulting frame cv2.imshow('Video', frame) if cv2.waitKey(1) & 0xFF == ord('q'): exit(0) return _return
# code adopted from here: # https://realpython.com/blog/python/face-detection-in-python-using-a-webcam/ import os import cv2 import sys import atexit import environment import numpy as np if not environment.is_mac(): # raspberry pi from picamera.array import PiRGBArray from picamera import PiCamera from camera import PiVideoStream dir = os.path.dirname(__file__) CASC_DIR = os.path.join(dir, 'cascades') FACE_CASC_PATH = os.path.join(CASC_DIR, 'haarcascade_frontalface_default.xml') SMILE_CASC_PATH = os.path.join(CASC_DIR, 'haarcascade_smile.xml') # HAND_CASC_PATH = '/Users/Keven/Downloads/fist2.xml' # HAND_CASC_PATH = '/Users/Keven/Downloads/hand4.xml' class Tracking(object): def __init__(self): if environment.is_mac(): self.video_capture = cv2.VideoCapture(0) self.video_capture.set(cv2.CAP_PROP_FRAME_WIDTH, 640) self.video_capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 480) else: # raspberry pi self.video_stream = PiVideoStream().start()
def __init__(self): if not environment.is_mac(): self.tof = VL53L0X.VL53L0X() self.tof.start_ranging(VL53L0X.VL53L0X_BETTER_ACCURACY_MODE)
def get_distance(self): if not environment.is_mac(): return self.tof.get_distance() else: return random.randint(300, 800)
import environment import random if not environment.is_mac(): import VL53L0X_rasp_python.python.VL53L0X as VL53L0X class Tof(object): def __init__(self): if not environment.is_mac(): self.tof = VL53L0X.VL53L0X() self.tof.start_ranging(VL53L0X.VL53L0X_BETTER_ACCURACY_MODE) def get_timing(self): if not environment.is_mac(): timing = self.tof.get_timing() if (timing < 20000): timing = 20000 return timing else: return 0 def get_distance(self): if not environment.is_mac(): return self.tof.get_distance() else: return random.randint(300, 800)