forked from qlxu0906/ECCVC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson2jpg.py
67 lines (52 loc) · 1.73 KB
/
json2jpg.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
# -----------------------------------------------------
# Analyse Annotations of the Dataset
#
# Author: Qiling Xu
# Creating Date: May 25, 2018
# Latest rectifying: May 25, 2018
# -----------------------------------------------------
import json
import os
import os.path
from PIL import Image
def crop(images, name):
for i in images.keys():
print(i)
for j in range(len(images[i]["candidates"])):
fn = images[i]["candidates"][j]["img"]
cropmake(fn, images[i]['candidates'][j]['bbox'],
images[i]['candidates'][j]['label'], name)
def cropmake(fn, bbox, label, name):
loc = 'E:/study/ECCVchallenge/person_search_trainval/'
newfn = fn.replace('.jpg', '_' + label + '.jpg')
im = Image.open(loc + '/' + name + '/' + fn)
left = bbox[0]
top = bbox[1]
width = bbox[2]
height = bbox[3]
bbox = (left, top, left + width, top + height)
bbox = tuple(bbox)
try:
newim = im.crop(bbox)
newloc = loc + 'newImage/' + name + '/'
crop_fn = newloc + newfn
File_Path = os.path.dirname(crop_fn)
if not os.path.exists(File_Path):
os.makedirs(File_Path)
newim.save(crop_fn)
except SystemError:
print("Error: error finding or failure cutting")
def main():
path = 'E:/study/ECCVchallenge/person_search_trainval/'
fullpath_t = path + 'train.json'
fp_t = open(fullpath_t, 'r')
images_t = json.load(fp_t)
fp_t.close()
crop(images_t, 'train')
fullpath_v = path + 'val.json'
fp_v = open(fullpath_v, 'r')
images_v = json.load(fp_v)
fp_v.close()
crop(images_v, 'val')
if __name__ == '__main__':
main()