diff --git a/.gitignore b/.gitignore index 7bbc71c..7c04a2a 100644 --- a/.gitignore +++ b/.gitignore @@ -99,3 +99,6 @@ ENV/ # mypy .mypy_cache/ + +# configure for MacOS +*.DS_Store diff --git a/fr/modules/actions/action.py b/fr/modules/actions/action.py index e51be9a..1329cd3 100644 --- a/fr/modules/actions/action.py +++ b/fr/modules/actions/action.py @@ -5,7 +5,7 @@ def activate(self): # Called when action activated pass - def update(self): + def __call__(self): # Called every frame while action is activated pass diff --git a/fr/modules/actions/moveToFace.py b/fr/modules/actions/moveToFace.py index 5e2da5e..9c87bf4 100644 --- a/fr/modules/actions/moveToFace.py +++ b/fr/modules/actions/moveToFace.py @@ -1,23 +1,26 @@ # -*- coding: utf-8 -*- import cv2 import math -from ..analyzers.facedetector import FaceDetector +from ..submodules.facedetector import FaceDetector class Noaction(object): + def __init__(self): + self.fd = FaceDetector() + def activate(self): # Called when action activated return {} - def update(self): + def __call__(self): act = { "wheelleft": 0.0, "wheelright": 0.0 } # Called every frame while action is activated - if str(FaceDetector.biggestFaceRect) != "None": + if str(self.fd.biggestFaceRect) != "None": # If face exists - x, _ = FaceDetector.biggestFaceRectPosNormalized(None) - size = FaceDetector.biggestFaceSizeNormalized(None) + x, _ = self.fd.biggestFaceRectPosNormalized(None) + size = self.fd.biggestFaceSizeNormalized(None) if abs(x) > 0.3: if x > 0: # Rotate to left diff --git a/fr/modules/actions/noaction.py b/fr/modules/actions/noaction.py index b4de192..1054a60 100644 --- a/fr/modules/actions/noaction.py +++ b/fr/modules/actions/noaction.py @@ -5,7 +5,7 @@ def activate(self): # Called when action activated return {} - def update(self): + def __call__(self): # Called every frame while action is activated return {} diff --git a/fr/modules/actions/raiseLeftHand.py b/fr/modules/actions/raiseLeftHand.py index f97f68c..2ee5c78 100644 --- a/fr/modules/actions/raiseLeftHand.py +++ b/fr/modules/actions/raiseLeftHand.py @@ -20,7 +20,7 @@ def activate(self): act["armleft"] = 1.0 return act - def update(self): + def __call__(self): # Called every frame while action is activated pass diff --git a/fr/modules/actions/raiseRightHand.py b/fr/modules/actions/raiseRightHand.py index 833b054..1140d63 100644 --- a/fr/modules/actions/raiseRightHand.py +++ b/fr/modules/actions/raiseRightHand.py @@ -20,7 +20,7 @@ def activate(self): act["armright"] = 1.0 return act - def update(self): + def __call__(self): # Called every frame while action is activated pass diff --git a/fr/modules/actions/randomMove.py b/fr/modules/actions/randomMove.py index 76057b6..f4b5651 100644 --- a/fr/modules/actions/randomMove.py +++ b/fr/modules/actions/randomMove.py @@ -21,7 +21,7 @@ def activate(self): act["armright"] = 1.0 return act - def update(self): + def __call__(self): # Called every frame while action is activated act = BASE_ACTION act["armleft"] = math.sin(time.time() * 2) diff --git a/fr/modules/actions/rotateLeft.py b/fr/modules/actions/rotateLeft.py index 624dda6..959dcce 100644 --- a/fr/modules/actions/rotateLeft.py +++ b/fr/modules/actions/rotateLeft.py @@ -15,7 +15,7 @@ def activate(self): # Called when action activated return {} - def update(self): + def __call__(self): # Called every frame while action is activated act = BASE_ACTION act["wheelleft"] = -0.1 diff --git a/fr/modules/actions/rotateRight.py b/fr/modules/actions/rotateRight.py index fe98458..a6a5108 100644 --- a/fr/modules/actions/rotateRight.py +++ b/fr/modules/actions/rotateRight.py @@ -15,7 +15,7 @@ def activate(self): # Called when action activated return {} - def update(self): + def __call__(self): # Called every frame while action is activated act = BASE_ACTION act["wheelleft"] = 0.1 diff --git a/fr/modules/actions/searchFaces.py b/fr/modules/actions/searchFaces.py index 9af3727..b3b0866 100644 --- a/fr/modules/actions/searchFaces.py +++ b/fr/modules/actions/searchFaces.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- import time import math -from ..analyzers.facedetector import FaceDetector +from ..submodules.facedetector import FaceDetector BASE_ACTION = { "armleft": 0.0, @@ -12,7 +12,7 @@ class SearchFaces(object): def __init__(self): - pass + self.fd = FaceDetector() def activate(self): # Called when action activated @@ -20,7 +20,7 @@ def activate(self): def update(self): # Called every frame while action is activated - facerect = FaceDetector.getFacerect(None) + facerect = self.fd.getFacerect(None) act = BASE_ACTION if str(facerect) != "None" and len(facerect) > 0: act["wheelleft"] = 0.0 diff --git a/fr/modules/analyzers/analyzer.py b/fr/modules/analyzers/analyzer.py index 7edaa4d..d4b5675 100644 --- a/fr/modules/analyzers/analyzer.py +++ b/fr/modules/analyzers/analyzer.py @@ -4,6 +4,6 @@ class Analyzer(object): def __init__(self): pass - def analyze(self, observation): + def __call__(self, observation): # Return scholar (not vector or dict) pass \ No newline at end of file diff --git a/fr/modules/analyzers/faceXAxis.py b/fr/modules/analyzers/faceXAxis.py index 0cc7b16..53e0c4f 100644 --- a/fr/modules/analyzers/faceXAxis.py +++ b/fr/modules/analyzers/faceXAxis.py @@ -1,9 +1,11 @@ # -*- coding: utf-8 -*- import cv2 -from .facedetector import FaceDetector +from ..submodules.facedetector import FaceDetector class FaceXAxis(object): + def __init__(self): + self.fd = FaceDetector() - def analyze(self, observation): - x, _ = FaceDetector.biggestFaceRectPosNormalized(observation) + def __call__(self, observation): + x, _ = self.fd.biggestFaceRectPosNormalized(observation) return x diff --git a/fr/modules/analyzers/faceYAxis.py b/fr/modules/analyzers/faceYAxis.py index 5fa35ed..b04e8a3 100644 --- a/fr/modules/analyzers/faceYAxis.py +++ b/fr/modules/analyzers/faceYAxis.py @@ -1,9 +1,11 @@ # -*- coding: utf-8 -*- import cv2 -from .facedetector import FaceDetector +from ..submodules.facedetector import FaceDetector class FaceYAxis(object): + def __init__(self): + self.fd = FaceDetector() - def analyze(self, observation): - _, y = FaceDetector.biggestFaceRectPosNormalized(observation) + def __call__(self, observation): + _, y = self.fd.biggestFaceRectPosNormalized(observation) return y diff --git a/fr/modules/analyzers/isFaceExist.py b/fr/modules/analyzers/isFaceExist.py index 2e19e56..6a2d7e2 100644 --- a/fr/modules/analyzers/isFaceExist.py +++ b/fr/modules/analyzers/isFaceExist.py @@ -1,12 +1,14 @@ # -*- coding: utf-8 -*- import cv2 import time -from .facedetector import FaceDetector +from ..submodules.facedetector import FaceDetector class IsFaceExist(object): + def __init__(self): + self.fd = FaceDetector() - def analyze(self, observation): - facerect = FaceDetector.getFacerect(observation) + def __call__(self, observation): + facerect = self.fd.getFacerect(observation) if str(facerect) == "None": return if len(facerect) > 0: return 1.0 diff --git a/fr/modules/analyzers/facedetector.py b/fr/modules/submodules/facedetector.py similarity index 72% rename from fr/modules/analyzers/facedetector.py rename to fr/modules/submodules/facedetector.py index f772217..a5cf8a2 100644 --- a/fr/modules/analyzers/facedetector.py +++ b/fr/modules/submodules/facedetector.py @@ -2,19 +2,21 @@ import cv2 import time +# Constants # cascade_path = "./haarcascades/haarcascade_frontalface_default.xml" cascade_path = "/var/opencv/haarcascades/haarcascade_frontalface_default.xml" +# refresh rate +refreshRate = 0.1 # sec class FaceDetector(object): - facerect = None - refreshRate = 0.1 # sec - lastRefreshed = 0.0 # sec + def __init__(self): + self.facerect = None + self.lastRefreshed = 0.0 # sec - @classmethod def getFacerect(self, observation): # Return cached data or not - if (time.time() - FaceDetector.lastRefreshed) < FaceDetector.refreshRate or observation == None: - return FaceDetector.facerect + if (time.time() - self.lastRefreshed) < refreshRate or observation == None: + return self.facerect # Engray image if str(observation["image"]) == "None": return @@ -24,7 +26,7 @@ def getFacerect(self, observation): cascade = cv2.CascadeClassifier(cascade_path) # Get facerect - FaceDetector.facerect = cascade.detectMultiScale( + self.facerect = cascade.detectMultiScale( image_gray, scaleFactor = 1.1, minNeighbors = 2, @@ -32,24 +34,21 @@ def getFacerect(self, observation): ) # Update cache time - FaceDetector.lastRefreshed = time.time() + self.lastRefreshed = time.time() - return FaceDetector.facerect + return self.facerect - @classmethod def biggestFaceRect(self, observation): - facerect = self.getFacerect(observation) - if str(facerect) == "None": return - return max(facerect, key= (lambda r: r[2] * r[3])) + self.facerect = self.getFacerect(observation) + if str(self.facerect) == "None": return + return max(self.facerect, key= (lambda r: r[2] * r[3])) - @classmethod def biggestFaceSizeNormalized(self, observation): width, height, _ = observation["image"].shape biggestface = self.biggestFaceRect(observation) if str(biggestface) == "None": return 0.0 return biggestface[2] * biggestface[3] / width / height - @classmethod def biggestFaceRectPosNormalized(self, observation): width, height, _ = observation["image"].shape biggestface = self.biggestFaceRect(observation)