1 | #!/usr/bin/python |
---|
2 | import os |
---|
3 | import shutil |
---|
4 | import sys |
---|
5 | |
---|
6 | CMD = 'LD_LIBRARY_PATH=/usr/local/lib fprint_demo' |
---|
7 | PRINTS_DIR = os.path.join( |
---|
8 | os.path.expanduser('~'), '.fprint', 'prints') |
---|
9 | RESULTS_DIR = os.path.join( |
---|
10 | os.path.expanduser('~'), 'fingerprints') |
---|
11 | |
---|
12 | |
---|
13 | def 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 | |
---|
31 | def 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 not be |
---|
35 | empty. |
---|
36 | """ |
---|
37 | while True: |
---|
38 | stud_id = raw_input("Please ENTER the STUDENT ID: ") |
---|
39 | ctrl_id = raw_input("Please repeat : ") |
---|
40 | if not stud_id: |
---|
41 | print "Please enter a valid student ID!" |
---|
42 | stud_id = None |
---|
43 | if stud_id != ctrl_id: |
---|
44 | print "IDs do not match. We restart." |
---|
45 | if stud_id and stud_id == ctrl_id: |
---|
46 | return stud_id |
---|
47 | |
---|
48 | |
---|
49 | # create results dir if it does not exist already. |
---|
50 | if not os.path.exists(RESULTS_DIR): |
---|
51 | print "Creating dir for collected fingerprints in %s" % RESULTS_DIR |
---|
52 | os.mkdir(RESULTS_DIR) |
---|
53 | |
---|
54 | print |
---|
55 | print "After entering a student ID, the fingerprint scanner GUI will" |
---|
56 | print "appear. Then, please do a scan ('enroll') of a finger via the GUI." |
---|
57 | print |
---|
58 | print "After scanning, please close the scanner programme." |
---|
59 | print |
---|
60 | |
---|
61 | if os.path.exists(PRINTS_DIR): |
---|
62 | shutil.rmtree(PRINTS_DIR) |
---|
63 | |
---|
64 | stud_id = get_stud_id() |
---|
65 | |
---|
66 | os.system(CMD) |
---|
67 | |
---|
68 | fp_file = get_first_dir(get_first_dir(get_first_dir(PRINTS_DIR))) |
---|
69 | if fp_file is None or not os.path.isfile(fp_file): |
---|
70 | print "ERROR: No fingerprint generated. Exiting." |
---|
71 | sys.exit(0) |
---|
72 | |
---|
73 | basename = os.path.basename(fp_file) |
---|
74 | |
---|
75 | student_path = os.path.join(RESULTS_DIR, stud_id.upper()) |
---|
76 | fp_target = os.path.join(student_path, basename) |
---|
77 | if not os.path.exists(student_path): |
---|
78 | # create a dir for each student |
---|
79 | os.mkdir(student_path) |
---|
80 | |
---|
81 | print "COPYING fingerprint over..." |
---|
82 | if os.path.isfile(fp_target): |
---|
83 | # such a fingerprint exists already (same finger, stud_id) |
---|
84 | os.unlink(fp_target) |
---|
85 | shutil.copy2(fp_file, fp_target) |
---|
86 | |
---|
87 | print "DONE. Thanks for using enroll_script.py." |
---|
88 | print "Collected prints are in %s" % RESULTS_DIR |
---|