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 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. |
---|
49 | if not os.path.exists(RESULTS_DIR): |
---|
50 | print "Creating dir for collected fingerprints in %s" % RESULTS_DIR |
---|
51 | os.mkdir(RESULTS_DIR) |
---|
52 | |
---|
53 | print |
---|
54 | print "After entering a student ID, the fingerprint scanner GUI will" |
---|
55 | print "appear. Then, please do a scan ('enroll') of a finger via the GUI." |
---|
56 | print |
---|
57 | print "After scanning, please close the scanner programme." |
---|
58 | print |
---|
59 | |
---|
60 | if os.path.exists(PRINTS_DIR): |
---|
61 | shutil.rmtree(PRINTS_DIR) |
---|
62 | |
---|
63 | stud_id = get_stud_id() |
---|
64 | |
---|
65 | os.system(CMD) |
---|
66 | |
---|
67 | fp_file = get_first_dir(get_first_dir(get_first_dir(PRINTS_DIR))) |
---|
68 | if fp_file is None or not os.path.isfile(fp_file): |
---|
69 | print "ERROR: No fingerprint generated. Exiting." |
---|
70 | sys.exit(0) |
---|
71 | |
---|
72 | basename = os.path.basename(fp_file) |
---|
73 | |
---|
74 | student_path = os.path.join(RESULTS_DIR, stud_id.upper()) |
---|
75 | fp_target = os.path.join(student_path, basename) |
---|
76 | if not os.path.exists(student_path): |
---|
77 | # create a dir for each student |
---|
78 | os.mkdir(student_path) |
---|
79 | |
---|
80 | print "COPYING fingerprint over..." |
---|
81 | if os.path.isfile(fp_target): |
---|
82 | # such a fingerprint exists already (same finger, stud_id) |
---|
83 | os.unlink(fp_target) |
---|
84 | shutil.copy2(fp_file, fp_target) |
---|
85 | |
---|
86 | print "DONE. Thanks for using enroll_script.py." |
---|
87 | print "Collected prints are in %s" % RESULTS_DIR |
---|