This repository has been archived by the owner on Nov 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_redis_stream.py
73 lines (59 loc) · 2.19 KB
/
test_redis_stream.py
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
import gc
import time
import argparse
import queue
import traceback
import redisstream
from pipes import Pipeline, Base64ToImagePipe, ImageToCV2Pipe, JsonParserPipe, DistributionToLabelPipe, CV2CropFacePipe, CV2ResizePipe, CV2ToTensorPipe
from model import Model
parser = argparse.ArgumentParser(description='Test network model from the Redis stream')
parser.add_argument('--size', type=int, help='The number of images for testing. It will end the execution after it.')
args = parser.parse_args()
stream = redisstream.RedisStream()
stream.startListening()
imagePipeline = Pipeline().pipe(Base64ToImagePipe) \
.pipe(ImageToCV2Pipe) \
.pipe(CV2CropFacePipe) \
.pipe(CV2ResizePipe) \
.pipe(CV2ToTensorPipe)
labelPipeline = Pipeline().pipe(JsonParserPipe) \
.pipe(DistributionToLabelPipe)
def main():
counter = 0
total = 0
while True:
try:
key = stream.queue.get_nowait()
data = stream.getKeyData(key)
image = imagePipeline.execute(data[0])
label = labelPipeline.execute(data[1].decode())
if image is None:
del label
gc.collect()
stream.deleteKeyData(key)
continue
if Model.test(image, label):
counter += 1
total += 1
del image
del label
gc.collect()
stream.deleteKeyData(key)
if args.size is not None:
if counter == args.size:
print(f'Total: {total}')
print(f'Correct images: {counter}')
print(f'Accuracy: {(counter/total) * 100}%')
raise Exception('Size reached')
elif counter != 0 and ((counter / args.size) * 100) % 10 == 0:
print(f'{((counter / args.size) * 100)}% done.')
except queue.Empty:
time.sleep(1)
if __name__ == '__main__':
print('Start listening...')
try:
main()
except:
print(traceback.format_exc())
print('Exit')
stream.stopListening()