-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetImage_Classify.py
More file actions
65 lines (56 loc) · 2.04 KB
/
Copy pathgetImage_Classify.py
File metadata and controls
65 lines (56 loc) · 2.04 KB
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
# coding:utf-8
import requests
import Image
import numpy as np
import os
import caffe
import shutil
if not os.path.exists("image"):
os.mkdir("image")
table = [i if i < 140 else 255 for i in xrange(256)]
labels = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ"
for i in labels:
if not os.path.exists("image/"+i):
os.mkdir("image/"+i)
caffe.set_mode_gpu()
model_def = 'caffeconfig/deploy.prototxt'
model_weights = "caffeconfig/misustc_iter_1900.caffemodel"
net = caffe.Net(model_def, model_weights, caffe.TEST) # 定义模型结构,包含了模型的训练权值,使用测试模式(不执行dropout)
net.blobs['data'].reshape(4,1,20,20)
net.reshape()
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
transformer.set_transpose('data', (2, 0, 1))
def getImage():
url = "http://mis.teach.ustc.edu.cn/randomImage.do?data=\'" + str(np.random.randint(1000000, 9999999)) + "\'"
req = requests.get(url)
try:
with open("image/tmp.jpg", 'wb') as file:
file.write(req.content)
file.close()
except IOError:
print "IOError"
req.close()
img = Image.open("image/tmp.jpg").convert('L').point(table)
img.crop((00, 0, 20, 20)).save("image/tmp0.jpg")
img.crop((20, 0, 40, 20)).save("image/tmp1.jpg")
img.crop((40, 0, 60, 20)).save("image/tmp2.jpg")
img.crop((60, 0, 80, 20)).save("image/tmp3.jpg")
def classify():
global index
for i in range(4):
filename = "image/tmp"+str(i)+".jpg"
im = caffe.io.load_image(filename, False)
transformed_image = transformer.preprocess('data', im)
net.blobs['data'].data[...] = transformed_image
output = net.forward()
output_prob = output['prob'][0]
result = labels[output_prob.argmax()]
shutil.move(filename, "image/"+result+"/"+str(index)+".jpg")
index+=1
index = 4000
while index<16000:
getImage()
classify()
if index%400==0:
print "------index:"+str(index)+"------"
os.remove("image/tmp.jpg")