-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrank_images.py
77 lines (56 loc) · 1.77 KB
/
rank_images.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
74
75
76
77
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# rank_images.py -- Creating ranking image list
#
# Usage: python3 rank_images.py InputFile(.csv)
#
import subprocess
import csv
import sys
import numpy as np
args = sys.argv
InputCSV = args[1]
OutputPNG = InputCSV.replace ('.csv', '')
cnv_options = " -resize 128x128 -gravity center -background 'rgb(255,255,255)' -extent 128x128 -bordercolor 'rgb({})' -border 2x2 "
def get_class ( fname ):
import os
basename = os.path.basename(fname)
ret = basename.split('_')[0]
return ret
num = 0
with open( InputCSV, 'rt' ) as fin:
cin = csv.reader(fin)
rank_num = 0
q_class = ""
cmd = "convert "
for line in cin:
#### ブロックの切れ目:コマンドを発行して,次のクエリを読み込む
if line == ['\n'] :
cmd += " +append tmp.png"
subprocess.run(cmd,shell=True)
if num % 10 == 0 :
subprocess.call ( "convert tmp.png {}_{:03}.png".format(OutputPNG,num//10), shell=True )
else:
subprocess.call ( "convert {}_{:03}.png tmp.png -append {}_{:03}.png".format(OutputPNG,num//10,OutputPNG,num//10), shell=True )
num = num+1
rank_num = 0
cmd = "convert "
#### クエリ画像
elif len(line) == 1 :
sub_cmd = "convert " +line[0] + cnv_options.format("0,0,255") + " -gravity northwest -extent 138x134 query.png"
subprocess.run(sub_cmd, shell=True)
q_class = get_class ( line[0] )
print ( "query: {}".format(line[0]) )
cmd += "query.png "
#### ランキング画像
else:
r_class = get_class ( line[0] )
if q_class == r_class :
col = "0,0,255"
else:
col = "255,0,0"
sub_cmd = "convert " +line[0] + cnv_options.format(col) + " rank_{}.png".format(rank_num)
subprocess.run(sub_cmd, shell=True)
cmd += "rank_{}.png ".format(rank_num)
rank_num += 1