File: classify_movidius_server.py

package info (click to toggle)
berrynet 3.10.2-2
  • links: PTS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,572 kB
  • sloc: python: 6,286; javascript: 2,373; xml: 1,094; sh: 738; makefile: 33
file content (201 lines) | stat: -rw-r--r-- 5,969 bytes parent folder | download | duplicates (2)
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# Copyright 2017 DT42
#
# This file is part of BerryNet.
#
# BerryNet 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 3 of the License, or
# (at your option) any later version.
#
# BerryNet 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 BerryNet.  If not, see <http://www.gnu.org/licenses/>.

"""Simple image classification server with Inception.

The server monitors image_dir and run inferences on new images added to the
directory. Every image file should come with another empty file with '.done'
suffix to signal readiness. Inference result of a image can be read from the
'.txt' file of that image after '.txt.done' is spotted.

This is an example the server expects clients to do. Note the order.

# cp cat.jpg /run/image_dir
# touch /run/image_dir/cat.jpg.done

Clients should wait for appearance of 'cat.jpg.txt.done' before getting
result from 'cat.jpg.txt'.
"""


from __future__ import print_function

import argparse
import json
import multiprocessing
import os
import signal
import sys
import threading
import time

import cv2
import movidius as mv
import numpy as np
import Queue

from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler


image_queue = Queue.Queue()
threads = []
mvng = None
graph_filepath = ''
labels_filepath = ''


def logging(*args):
    print("[%08.3f]" % time.time(), ' '.join(args))


def touch(fname, times=None):
    with open(fname, 'a'):
        os.utime(fname, times)


def generalize_inference_result(ncs_classifications):
    """Result format conversion from NCS classification to generic format.

    param list ncs_classifications: [(label, confidence), ...]
    return dict r: generic inference result format
    """
    r = {
        'annotations': []
    }
    conf_digits = 2
    for label, confidence in ncs_classifications:
        r['annotations'].append({
            'type': 'classification',
            'label': label,
            'confidence': round(float(confidence), conf_digits)
        })
    return r


def server():
    """Infinite loop serving inference requests"""

    global image_queue
    global mvng

    logging(threading.current_thread().getName(), "is running")

    while True:
        input_name = image_queue.get()
        image_data = cv2.imread(input_name).astype(np.float32)
        image_data = mv.process_inceptionv3_input(image_data)

        output = mvng.inference(image_data)
        inceptionv3_outputs = mv.process_inceptionv3_output(output,
                                                            mvng.get_labels())

        output_name = input_name + '.txt'
        output_done_name = output_name + '.done'
        inference_result = generalize_inference_result(inceptionv3_outputs)
        logging(json.dumps(inference_result, indent=4))
        with open(output_name, 'w') as f:
            json.dump(inference_result, f, indent=4)
        #with open(output_name, 'w') as f:
        #    for i in inceptionv3_outputs:
        #        print("%s (score = %.5f)" % (i[0], i[1]), file=f)
        touch(output_done_name)
        logging(input_name, " classified!")


class EventHandler(PatternMatchingEventHandler):
    def process(self, event):
        """
        event.event_type
            'modified' | 'created' | 'moved' | 'deleted'
        event.is_directory
            True | False
        event.src_path
            path/to/observed/file
        """
        # the file will be processed there
        global image_queue

        _msg = event.src_path
        image_queue.put(_msg.rstrip('.done'))
        os.remove(_msg)
        logging(_msg, event.event_type)

    # ignore all other types of events except 'modified'
    def on_created(self, event):
        self.process(event)


def main(args):
    global threads

    # Create a server thread for each CPU core
    cpu_count = multiprocessing.cpu_count()
    for i in xrange(cpu_count/4):
        threads.append(
            threading.Thread(target=server,
                             name='Server thread %d' % i))
    for t in threads:
        t.start()
    for t in threads:
        t.join()


def parse_args():
    ap = argparse.ArgumentParser()
    ap.add_argument('--model', required=True, help='Model file path')
    ap.add_argument('--label', required=True, help='Label file path')
    ap.add_argument('--image_dir', required=True, help='Path to image file')
    ap.add_argument('--num_top_predictions', default=5,
                    help='Display this many predictions')
    return vars(ap.parse_args())


if __name__ == '__main__':
    args = parse_args()

    mvng = mv.MovidiusNeuralGraph(args['model'], args['label'])

    pid = str(os.getpid())
    pidfile = "/tmp/classify_movidius_server.pid"

    if os.path.isfile(pidfile):
        logging("%s already exists, exiting" % pidfile)
        sys.exit(1)

    with open(pidfile, 'w') as f:
        f.write(pid)

    logging("model filepath: ", args['model'])
    logging("label filepath: ", args['label'])
    logging("image_dir: ", args['image_dir'])

    # workaround the issue that SIGINT cannot be received (fork a child to
    # avoid blocking the main process in Thread.join()
    child_pid = os.fork()
    if child_pid == 0:  # child
        # observer handles event in a different thread
        observer = Observer()
        observer.schedule(EventHandler(['*.jpg.done']), path=args['image_dir'])
        observer.start()
        main(args)
    else:  # parent
        try:
            os.wait()
        except KeyboardInterrupt:
            os.kill(child_pid, signal.SIGKILL)
            os.unlink(pidfile)