From ca7a30c3738455a247a23c715b934a9c0f333848 Mon Sep 17 00:00:00 2001 From: furikake6000 Date: Tue, 10 Jul 2018 14:48:50 +0900 Subject: [PATCH 1/4] Added DS_Store to gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) 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 From 21c66a9ba46ba34d8c45dc9c27faefdcb074944c Mon Sep 17 00:00:00 2001 From: furikake6000 Date: Thu, 12 Jul 2018 15:08:30 +0900 Subject: [PATCH 2/4] FaceDetector no longer uses class val and method --- fr/modules/actions/moveToFace.py | 9 ++++++--- fr/modules/actions/searchFaces.py | 4 ++-- fr/modules/analyzers/faceXAxis.py | 4 +++- fr/modules/analyzers/faceYAxis.py | 4 +++- fr/modules/analyzers/facedetector.py | 29 ++++++++++++++-------------- fr/modules/analyzers/isFaceExist.py | 4 +++- 6 files changed, 31 insertions(+), 23 deletions(-) diff --git a/fr/modules/actions/moveToFace.py b/fr/modules/actions/moveToFace.py index 5e2da5e..77d4e7a 100644 --- a/fr/modules/actions/moveToFace.py +++ b/fr/modules/actions/moveToFace.py @@ -4,6 +4,9 @@ from ..analyzers.facedetector import FaceDetector class Noaction(object): + def __init__(self): + self.fd = FaceDetector() + def activate(self): # Called when action activated return {} @@ -14,10 +17,10 @@ def update(self): "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/searchFaces.py b/fr/modules/actions/searchFaces.py index 9af3727..384f955 100644 --- a/fr/modules/actions/searchFaces.py +++ b/fr/modules/actions/searchFaces.py @@ -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/faceXAxis.py b/fr/modules/analyzers/faceXAxis.py index 0cc7b16..049a0a9 100644 --- a/fr/modules/analyzers/faceXAxis.py +++ b/fr/modules/analyzers/faceXAxis.py @@ -3,7 +3,9 @@ from .facedetector import FaceDetector class FaceXAxis(object): + def __init__(self): + self.fd = FaceDetector() def analyze(self, observation): - x, _ = FaceDetector.biggestFaceRectPosNormalized(observation) + x, _ = self.fd.biggestFaceRectPosNormalized(observation) return x diff --git a/fr/modules/analyzers/faceYAxis.py b/fr/modules/analyzers/faceYAxis.py index 5fa35ed..e4ff472 100644 --- a/fr/modules/analyzers/faceYAxis.py +++ b/fr/modules/analyzers/faceYAxis.py @@ -3,7 +3,9 @@ from .facedetector import FaceDetector class FaceYAxis(object): + def __init__(self): + self.fd = FaceDetector() def analyze(self, observation): - _, y = FaceDetector.biggestFaceRectPosNormalized(observation) + _, y = self.fd.biggestFaceRectPosNormalized(observation) return y diff --git a/fr/modules/analyzers/facedetector.py b/fr/modules/analyzers/facedetector.py index f772217..a5cf8a2 100644 --- a/fr/modules/analyzers/facedetector.py +++ b/fr/modules/analyzers/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) diff --git a/fr/modules/analyzers/isFaceExist.py b/fr/modules/analyzers/isFaceExist.py index 2e19e56..544da3f 100644 --- a/fr/modules/analyzers/isFaceExist.py +++ b/fr/modules/analyzers/isFaceExist.py @@ -4,9 +4,11 @@ from .facedetector import FaceDetector class IsFaceExist(object): + def __init__(self): + self.fd = FaceDetector() def analyze(self, observation): - facerect = FaceDetector.getFacerect(observation) + facerect = self.fd.getFacerect(observation) if str(facerect) == "None": return if len(facerect) > 0: return 1.0 From 983a86305b074b5e333e3ae0d14b6bb0d33fc4a6 Mon Sep 17 00:00:00 2001 From: furikake6000 Date: Thu, 12 Jul 2018 15:30:11 +0900 Subject: [PATCH 3/4] Made submodules dir --- fr/modules/actions/moveToFace.py | 2 +- fr/modules/actions/searchFaces.py | 2 +- fr/modules/analyzers/faceXAxis.py | 2 +- fr/modules/analyzers/faceYAxis.py | 2 +- fr/modules/analyzers/isFaceExist.py | 2 +- fr/modules/{analyzers => submodules}/facedetector.py | 0 6 files changed, 5 insertions(+), 5 deletions(-) rename fr/modules/{analyzers => submodules}/facedetector.py (100%) diff --git a/fr/modules/actions/moveToFace.py b/fr/modules/actions/moveToFace.py index 77d4e7a..5796e51 100644 --- a/fr/modules/actions/moveToFace.py +++ b/fr/modules/actions/moveToFace.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- import cv2 import math -from ..analyzers.facedetector import FaceDetector +from ..submodules.facedetector import FaceDetector class Noaction(object): def __init__(self): diff --git a/fr/modules/actions/searchFaces.py b/fr/modules/actions/searchFaces.py index 384f955..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, diff --git a/fr/modules/analyzers/faceXAxis.py b/fr/modules/analyzers/faceXAxis.py index 049a0a9..951b24a 100644 --- a/fr/modules/analyzers/faceXAxis.py +++ b/fr/modules/analyzers/faceXAxis.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- import cv2 -from .facedetector import FaceDetector +from ..submodules.facedetector import FaceDetector class FaceXAxis(object): def __init__(self): diff --git a/fr/modules/analyzers/faceYAxis.py b/fr/modules/analyzers/faceYAxis.py index e4ff472..7e15f92 100644 --- a/fr/modules/analyzers/faceYAxis.py +++ b/fr/modules/analyzers/faceYAxis.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- import cv2 -from .facedetector import FaceDetector +from ..submodules.facedetector import FaceDetector class FaceYAxis(object): def __init__(self): diff --git a/fr/modules/analyzers/isFaceExist.py b/fr/modules/analyzers/isFaceExist.py index 544da3f..c597c75 100644 --- a/fr/modules/analyzers/isFaceExist.py +++ b/fr/modules/analyzers/isFaceExist.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- import cv2 import time -from .facedetector import FaceDetector +from ..submodules.facedetector import FaceDetector class IsFaceExist(object): def __init__(self): diff --git a/fr/modules/analyzers/facedetector.py b/fr/modules/submodules/facedetector.py similarity index 100% rename from fr/modules/analyzers/facedetector.py rename to fr/modules/submodules/facedetector.py From 2d5f8853671dbab166cebc167dcdeb7a83b1244f Mon Sep 17 00:00:00 2001 From: furikake6000 Date: Thu, 12 Jul 2018 15:32:32 +0900 Subject: [PATCH 4/4] Changed "update" and "analyze" to __call__ --- fr/modules/actions/action.py | 2 +- fr/modules/actions/moveToFace.py | 2 +- fr/modules/actions/noaction.py | 2 +- fr/modules/actions/raiseLeftHand.py | 2 +- fr/modules/actions/raiseRightHand.py | 2 +- fr/modules/actions/randomMove.py | 2 +- fr/modules/actions/rotateLeft.py | 2 +- fr/modules/actions/rotateRight.py | 2 +- fr/modules/analyzers/analyzer.py | 2 +- fr/modules/analyzers/faceXAxis.py | 2 +- fr/modules/analyzers/faceYAxis.py | 2 +- fr/modules/analyzers/isFaceExist.py | 2 +- 12 files changed, 12 insertions(+), 12 deletions(-) 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 5796e51..9c87bf4 100644 --- a/fr/modules/actions/moveToFace.py +++ b/fr/modules/actions/moveToFace.py @@ -11,7 +11,7 @@ def activate(self): # Called when action activated return {} - def update(self): + def __call__(self): act = { "wheelleft": 0.0, "wheelright": 0.0 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/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 951b24a..53e0c4f 100644 --- a/fr/modules/analyzers/faceXAxis.py +++ b/fr/modules/analyzers/faceXAxis.py @@ -6,6 +6,6 @@ class FaceXAxis(object): def __init__(self): self.fd = FaceDetector() - def analyze(self, 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 7e15f92..b04e8a3 100644 --- a/fr/modules/analyzers/faceYAxis.py +++ b/fr/modules/analyzers/faceYAxis.py @@ -6,6 +6,6 @@ class FaceYAxis(object): def __init__(self): self.fd = FaceDetector() - def analyze(self, 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 c597c75..6a2d7e2 100644 --- a/fr/modules/analyzers/isFaceExist.py +++ b/fr/modules/analyzers/isFaceExist.py @@ -7,7 +7,7 @@ class IsFaceExist(object): def __init__(self): self.fd = FaceDetector() - def analyze(self, observation): + def __call__(self, observation): facerect = self.fd.getFacerect(observation) if str(facerect) == "None": return if len(facerect) > 0: