Skip to content

Commit

Permalink
some cleanup and bug fixing
Browse files Browse the repository at this point in the history
  • Loading branch information
pyskell committed Oct 30, 2015
1 parent 7aadb3b commit 6f00785
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ You need to install the following system-wide:

* Python 2.7
* [Qt4](http://doc.qt.io/qt-4.8/installation.html) and [PyQt4](http://pyqt.sourceforge.net/Docs/PyQt4/installation.html)
* [OpenCV](http://docs.opencv.org/doc/tutorials/introduction/table_of_content_introduction/table_of_content_introduction.html)
* [OpenCV 2 or 3](http://docs.opencv.org/doc/tutorials/introduction/table_of_content_introduction/table_of_content_introduction.html)
* [OpenCV-Python](https://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_setup/py_table_of_contents_setup/py_table_of_contents_setup.html#py-table-of-content-setup)

## Debian Dependencies
Expand Down
7 changes: 3 additions & 4 deletions arg.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
from argparse import *

from argparse import ArgumentParser

def get_arguments():
parser = ArgumentParser(description = 'Slouchy uses your webcam t'
Expand All @@ -18,7 +17,7 @@ def get_arguments():
# Settings overrides
parser.add_argument('--config-file', '-s', type = str,
help = 'The location of a config file for slouchy.')
parser.add_argument('--video-device', '-d', type = int,
parser.add_argument('--video-device', '-d', type = str,
help = 'Value for specifying the camera to use.')
parser.add_argument('--poll-rate', '-p', type = int,
help = 'Time to wait between checking user posture.')
Expand All @@ -30,7 +29,7 @@ def get_arguments():
'ts spinal mobility, this is used as a proxy for d'
'etecting slouching.')
parser.add_argument('--thoracolumbar-tolerance', '-l', type = float,
help = 'The ammount of deviation from the reference which'
help = 'The amount of deviation from the reference which'
' will be tolerated before registering the lower a'
'nd mid back of the subject is slouching.')
parser.add_argument('--cervical-tolerance', '-c', type = float,
Expand Down
20 changes: 13 additions & 7 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,33 @@
# and numeric/string values for selectively overiding the slouchy.ini settings
args = get_arguments()

# Determin if the user wants status output on the command line
# Determine if the user wants status output on the command line
text_mode = args.text_mode

# Load settings from the config file (default to slouchy.ini)
config = ConfigObj(args.config_file) if args.config_file\
else ConfigObj('slouchy.ini')
if args.config_file:
config_file = ConfigObj(args.config_file)
else:
config_file = ConfigObj('slouchy.ini')

# Dict-ize args (for looping)
args = vars(args)

# Overide config file settings per the command line
for key, val in args.iteritems():
if key in config['MAIN'].keys():
globals()[key] = args[key] if args[key] else config['MAIN'][key]
if key in config_file['MAIN'].keys():
globals()[key] = args[key] if args[key] else config_file['MAIN'][key]

# Some settings need to be floats (not strings)
for i in ['distance_reference', 'thoracolumbar_tolerance',\
'cervical_tolerance', 'camera_warm_up']:
globals()[i] = float(globals()[i])
'cervical_tolerance', 'camera_warm_up']:
globals()[i] = float(globals()[i])

# poll_rate needs to be an int
globals()['poll_rate'] = int(globals()['poll_rate'])

# video_device can be either an int or str, so try int but fall back on str
video_device = globals()['video_device']
try:
video_device = int(video_device)
except ValueError:
Expand Down
4 changes: 0 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import config

from collections import namedtuple
from configobj import ConfigObj
from math import atan, sqrt

"""
Expand Down Expand Up @@ -91,7 +90,6 @@ def determine_distance(MaybeFace):
print(' Position: x = {:d}, y = {:d}'.format(x, y))
print(' Dimensions: w = {:d}, h = {:d}'.format(w, h))

# distance = (y**2 + w**2)**0.5
distance = sqrt(y**2 + w**2)

return Maybe(True, distance)
Expand Down Expand Up @@ -226,7 +224,6 @@ def find_head_tilt(face):

if classifier.empty():
return Maybe(False, "Empty classifier")
# return 0 # Don't complain, gracefully continue without this function

eyes = classifier.detectMultiScale(face)

Expand All @@ -247,7 +244,6 @@ def find_head_tilt(face):
return Maybe(True, angle)

return Maybe(False, "No eyes found")
# return 0 # If both eyes couldn't be found, assume a level head


def detect_slouching(MaybePos):
Expand Down
11 changes: 6 additions & 5 deletions slouchy.ini
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[MAIN]
# distance_reference
# Determined from running Setup, should be a 6 digit number.
distance_reference = 228.449119062
distance_reference = 249.110417285

# thoracolumar_tolerance
# thoracolumar_tolerance (Back)
# Use to adjust the sensitivity of slouch detection.
# Tweak until the program correctly detects you slouching/upright.
# You should need small values (0.1 or so)
Expand All @@ -13,13 +13,13 @@ distance_reference = 228.449119062
# Note: The closer you get to the camera the more error there is in measurement.
thoracolumbar_tolerance = 0.05

# lat_cerv_tol
# cervical_tolerance (Neck)
# Set the tolerance for the lateral cervical angle (in radians).
# Probably good values: 0.3 (~17 deg), 0.35 (~20 deg), 0.4 (~23 deg)
cervical_tolerance = 0.3

# face_cascade_path
# Path to the haarscascade xml file, feel free to try other face cascades as well.
# face_cascade_path / eye_cascade_path
# Path to the haarscascade xml files, feel free to try other face cascades as well.
# If you find good results with one let me know.
face_cascade_path = haarcascade_frontalface_default.xml

Expand All @@ -38,6 +38,7 @@ poll_rate = 60

# camera_delay
# Some cameras require time to warm up before taking a picture
# If you're using a Mac you probably want to set this to a few seconds.
# If slouchy isn't detecting faces, and you're in a well-lit area
# then specify a delay (in seconds) to allow your camera to warm up first.
camera_warm_up = 0
Expand Down
21 changes: 14 additions & 7 deletions slouchy.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python2
# -*- coding: utf-8 -*-

import sys
import signal
import time
Expand All @@ -8,8 +9,7 @@

# Local imports
import config
from main import take_picture, detect_face, determine_posture, config
from main import config as config_file
from main import take_picture, determine_posture

# This fixes an UnboundLocalError / referenced before assignment error...
# Directly importing slouching_results doesn't work?
Expand All @@ -19,14 +19,21 @@

check_frequency = config.poll_rate


# Set initial values
# Set initial values to slouchy.ini
def setup():
maybe_image = take_picture(config.video_device)
maybe_face = detect_face(maybe_image)
maybe_current_posture = determine_posture(maybe_face)
maybe_current_posture = determine_posture(maybe_image)

if maybe_current_posture.success:
distance_reference = str(maybe_current_posture.result.get('distance'))
config.config_file['MAIN']['distance_reference'] = distance_reference
print("Reference value detected as:", maybe_current_posture.result)

else:
print("Error:", maybe_current_posture.result)
return maybe_current_posture

config_file.write()
config.config_file.write()


class TrayIcon(QtGui.QSystemTrayIcon):
Expand Down

0 comments on commit 6f00785

Please sign in to comment.