File: AMC-decode.pl.in

package info (click to toggle)
auto-multiple-choice 1.7.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 92,612 kB
  • sloc: perl: 26,752; xml: 24,889; cpp: 1,997; python: 895; makefile: 569; sh: 233; ansic: 195
file content (155 lines) | stat: -rwxr-xr-x 4,379 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#! @/PERLPATH/@
#
# Copyright (C) 2019-2025 Alexis Bienvenüe <paamc@passoire.fr>
#
# This file is part of Auto-Multiple-Choice
#
# Auto-Multiple-Choice is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation, either version 2 of
# the License, or (at your option) any later version.
#
# Auto-Multiple-Choice is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Auto-Multiple-Choice.  If not, see
# <http://www.gnu.org/licenses/>.

use warnings;
use 5.012;

use Getopt::Long;

use AMC::Basic;
use AMC::Data;
use AMC::DataModule::capture qw/:zone/;
use AMC::DataModule::scoring qw/:direct/;
use AMC::Queue;
use AMC::Gui::Avancement;

use Module::Load;

my $zone_type    = ZONE_NAME;
my $tag          = 'names';
my $decoder_name = '';
my $all          = 0;
my $projects_dir = $ENV{HOME} . '/' . __("MC-Projects");
my $project_dir  = '';
my $cr_dir       = '';
my $data_dir     = '';
my $n_procs      = 0;
my $progress     = 0;
my $progress_id  = 0;

GetProjectOptions(
    ":cr:dir|cr=s"               => \$cr_dir,
    ":project_dir|project=s"     => \$project_dir,
    "projects-dir=s"             => \$projects_dir,
    ":data:dir|data=s"           => \$data_dir,
    "zone-type=s"                => \$zone_type,
    "tag=s"                      => \$tag,
    "all!"                       => \$all,
    ":name_field_type|decoder=s" => \$decoder_name,
    "n-procs=s"                  => \$n_procs,
    "progression=s"              => \$progress,
    "progression-id=s"           => \$progress_id,
);

$project_dir = $projects_dir . '/' . $project_dir if ( $project_dir !~ /\// );
$cr_dir      = $project_dir . "/cr"               if ( !$cr_dir );
$data_dir    = $project_dir . "/data"             if ( !$data_dir );

my $queue = '';

my $progress_h = AMC::Gui::Avancement::new( $progress, id => $progress_id );

sub catch_signal {
    my $signame = shift;
    $queue->killall() if ($queue);
    die "Killed";
}

$SIG{INT} = \&catch_signal;

my $data = AMC::Data->new($data_dir);

$data->require_module('capture');
$data->require_module('scoring');

$data->begin_transaction('Deco');
my @all_zones =
  @{ $data->module('capture')->zone_images_available($zone_type) };
my $last_decoded = $data->module('capture')->variable( 'last_decoded_' . $tag );
$last_decoded = 0 if ( !$last_decoded );

$data->module('scoring')->clear_code_direct(DIRECT_NAMEFIELD)
  if ($all);

if ( !$decoder_name ) {
    $data->end_transaction('Deco');
    debug("No decoder!");
    exit(0);
}

my $t = time();

load("AMC::Decoder::$decoder_name");
my $decoder = "AMC::Decoder::$decoder_name"->new();

$queue = AMC::Queue::new( 'max.procs', $n_procs );

my $delta;

sub decode_one {
    my ($z) = @_;
    my $path = $z->{image};
    $path = "$cr_dir/$path" if ($path);
    my $d = $decoder->decode_image( $path, $z->{imagedata} );
    debug("Zone $z->{zoneid}: $d->{ok} ($d->{status}) [$d->{value}]");
    print "Student $z->{student}/$z->{copy}: "
      . ( $d->{ok} ? "success" : "FAILED" )
      . " ($d->{status})"
      . ( $d->{ok} ? " -> $d->{value}" : "" ) . "\n";

    my $decode_data = AMC::Data->new($data_dir);
    my $scoring = $decode_data->module('scoring', version_checked=>1);
    $scoring->begin_transaction('DecS');
    $scoring->new_code(
        $z->{student}, $z->{copy}, "_namefield", $d->{value},
        DIRECT_NAMEFIELD
    );
    $scoring->end_transaction('DecS');
    $progress_h->progres($delta);
}

my $n_zones = 0;
for my $z (@all_zones) {
    debug( "$z->{zoneid} $z->{image} $z->{timestamp_auto}"
          . ( $z->{timestamp_auto} >= $last_decoded ? " [X]" : "" ) );

    if ( $all || $z->{timestamp_auto} >= $last_decoded ) {
        $n_zones += 1;
        $queue->add_process( \&decode_one, $z );
    }

}

$data->end_transaction('Deco');

$data->disconnect();

$delta = ( $n_zones > 1 ? 1 / $n_zones : 1 );

$queue->run();

$progress_h->fin();

debug("New last_decoded time for $tag: $t");

$data->connect;
my $capture = $data->module('capture');
$capture->variable_transaction( 'last_decoded_' . $tag, $t );