IceCTF2018-HotOrNot

Question

Solution

咁個標題都講左係要認熱狗同狗 , 咁我思路就係認晒圖入面咁多張 , 當最後個output係就白色 唔係就黑色咁先啦

首先切做均等大細先

1
2
3
4
5
6
7
8
9
10
11
12
from PIL import Image, ImageFont, ImageDraw

Image.MAX_IMAGE_PIXELS = 379782144

im = Image.open('hotornot.jpg')

index = 1
for i in range(0,87):
for j in range(0,87):
chim = im.crop((224*i, 224*j, 224*i+224, 224*j+224))
chim.save(str(index) + ".jpg")
index += 1

最後出左7569張圖

咁之後就要做認圖果part啦 , 因為我懶 , 咁是但上網搵個library用算啦
最後就搵到呢個 https://github.com/jramasani/hotdog-nothotdog

最後都係跟番佢

1
docker run -it   --publish 6006:6006   --volume ${HOME}/tf_files:/tf_files   --workdir /tf_files   tensorflow/tensorflow:latest-devel

然後改左改佢條script

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
import os, sys

import tensorflow as tf

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
files = os.listdir("./hotornot")
files = [file for file in os.listdir('./hotornot') if file.endswith('.jpg')]
files.sort(key= lambda x:int(x[:-4]))

index = 1
# change this as you see fit
image_path = sys.argv[1]

# Read in the image_data
image_data = tf.gfile.FastGFile(image_path, 'rb').read()

# Loads label file, strips off carriage return
label_lines = [line.rstrip() for line
in tf.gfile.GFile("/tf_files/retrained_labels.txt")]

# Unpersists graph from file
with tf.gfile.FastGFile("/tf_files/retrained_graph.pb", 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
tf.import_graph_def(graph_def, name='')

with tf.Session() as sess:
# Feed the image_data as input to the graph and get first prediction
softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')

predictions = sess.run(softmax_tensor, \
{'DecodeJpeg/contents:0': image_data})

# Sort to show labels of first prediction in order of confidence
top_k = predictions[0].argsort()[-len(predictions[0]):][::-1]

hotScore = 0
notHotScore = 0
for node_id in top_k:
human_string = label_lines[node_id]
score = predictions[0][node_id]
#print('%s (score = %.5f)' % (human_string, score))
if human_string == "hotdog":
hotScore = score
else:
notHotScore = score
if hotScore > notHotScore:
print "1"
else:
print "0"

自己用黎call 上面果條script既script

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import os
from subprocess import call

files = os.listdir("./hotornot")
files = [file for file in os.listdir('./hotornot') if file.endswith('.jpg')]
files.sort(key= lambda x:int(x[:-4]))
print files
result = ""
index = 1
for f in files:
result += os.popen("python label_hotnot.py ./hotornot/" + f).read().replace("\n","")
print "Status: " + str(index) + " / " + str(len(files))
index += 1
with open("result.txt", "wb") as f:
f.write(result)
f.close()

最後似係qrcode , 然後執左執