source: main/pyfprint/trunk/enroll-script.py @ 16417

Last change on this file since 16417 was 11548, checked in by Henrik Bettermann, 11 years ago

Check length of student id.

  • Property svn:executable set to *
File size: 2.4 KB
Line 
1#!/usr/bin/python
2import os
3import shutil
4import sys
5
6CMD = 'LD_LIBRARY_PATH=/usr/local/lib fprint_demo'
7PRINTS_DIR = os.path.join(
8    os.path.expanduser('~'), '.fprint', 'prints')
9RESULTS_DIR = os.path.join(
10    os.path.expanduser('~'), 'fingerprints')
11
12
13def get_first_dir(path):
14    """Get first available path in `path`.
15
16    For instance if `path` is the directory ``/a/b/`` and contains the
17    files/directories ``c``, ``d``, ``e``, then this function will
18    return ``/a/b/c``.
19    """
20    name = ''
21    if path is None or not os.path.exists(path):
22        print "Doesn't exist: ", path
23        return None
24    try:
25        name = sorted(os.listdir(path))[0]
26    except:
27        return None
28    return os.path.join(path, name)
29
30
31def get_stud_id():
32    """Get a student id via input from keyboard.
33
34    We require the id two times (for safety). The id must have length 8.
35    """
36    while True:
37        stud_id = raw_input("Please ENTER the STUDENT ID: ")
38        if len(stud_id) != 8:
39            print "Please enter a valid student ID!"
40        else:
41            ctrl_id = raw_input("Please repeat              : ")
42            if stud_id != ctrl_id:
43                print "IDs do not match. We restart."
44            else:
45                return stud_id
46
47
48# create results dir if it does not exist already.
49if not os.path.exists(RESULTS_DIR):
50    print "Creating dir for collected fingerprints in %s" % RESULTS_DIR
51    os.mkdir(RESULTS_DIR)
52
53print
54print "After entering a student ID, the fingerprint scanner GUI will"
55print "appear. Then, please do a scan ('enroll') of a finger via the GUI."
56print
57print "After scanning, please close the scanner programme."
58print
59
60if os.path.exists(PRINTS_DIR):
61    shutil.rmtree(PRINTS_DIR)
62
63stud_id = get_stud_id()
64
65os.system(CMD)
66
67fp_file = get_first_dir(get_first_dir(get_first_dir(PRINTS_DIR)))
68if fp_file is None or not os.path.isfile(fp_file):
69    print "ERROR: No fingerprint generated. Exiting."
70    sys.exit(0)
71
72basename = os.path.basename(fp_file)
73
74student_path = os.path.join(RESULTS_DIR, stud_id.upper())
75fp_target = os.path.join(student_path, basename)
76if not os.path.exists(student_path):
77    # create a dir for each student
78    os.mkdir(student_path)
79
80print "COPYING fingerprint over..."
81if os.path.isfile(fp_target):
82    # such a fingerprint exists already (same finger, stud_id)
83    os.unlink(fp_target)
84shutil.copy2(fp_file, fp_target)
85
86print "DONE. Thanks for using enroll_script.py."
87print "Collected prints are in %s" % RESULTS_DIR
Note: See TracBrowser for help on using the repository browser.