Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,6 @@ ENV/

# mypy
.mypy_cache/

# configure for MacOS
*.DS_Store
2 changes: 1 addition & 1 deletion fr/modules/actions/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 8 additions & 5 deletions fr/modules/actions/moveToFace.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion fr/modules/actions/noaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}

Expand Down
2 changes: 1 addition & 1 deletion fr/modules/actions/raiseLeftHand.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion fr/modules/actions/raiseRightHand.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion fr/modules/actions/randomMove.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion fr/modules/actions/rotateLeft.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion fr/modules/actions/rotateRight.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions fr/modules/actions/searchFaces.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -12,15 +12,15 @@

class SearchFaces(object):
def __init__(self):
pass
self.fd = FaceDetector()

def activate(self):
# Called when action activated
return {}

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
Expand Down
2 changes: 1 addition & 1 deletion fr/modules/analyzers/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 5 additions & 3 deletions fr/modules/analyzers/faceXAxis.py
Original file line number Diff line number Diff line change
@@ -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
8 changes: 5 additions & 3 deletions fr/modules/analyzers/faceYAxis.py
Original file line number Diff line number Diff line change
@@ -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
8 changes: 5 additions & 3 deletions fr/modules/analyzers/isFaceExist.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,32 +26,29 @@ 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,
minSize = (30, 30)
)

# 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)
Expand Down