diff --git a/README.md b/README.md index 66fd75c..bf46a95 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/arg.py b/arg.py index 9d2eff7..507a179 100644 --- a/arg.py +++ b/arg.py @@ -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' @@ -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.') @@ -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, diff --git a/config.py b/config.py index b8e4fd4..a12fee9 100644 --- a/config.py +++ b/config.py @@ -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: diff --git a/main.py b/main.py index d0f719b..854b880 100644 --- a/main.py +++ b/main.py @@ -4,7 +4,6 @@ import config from collections import namedtuple -from configobj import ConfigObj from math import atan, sqrt """ @@ -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) @@ -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) @@ -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): diff --git a/slouchy.ini b/slouchy.ini index 2602f0c..cc7b375 100644 --- a/slouchy.ini +++ b/slouchy.ini @@ -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) @@ -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 @@ -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 diff --git a/slouchy.py b/slouchy.py index 56f6cf6..162f987 100755 --- a/slouchy.py +++ b/slouchy.py @@ -1,5 +1,6 @@ #!/usr/bin/env python2 # -*- coding: utf-8 -*- + import sys import signal import time @@ -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? @@ -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):