-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataProcess.py
63 lines (54 loc) · 1.61 KB
/
dataProcess.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
from PIL import Image
import numpy as np
from os import path
import matplotlib.pyplot as plt
import cv2
import pickle
f = open("C:/Users/user/Desktop/CS464 Machine Learning/Project/MPHB-label-txt/train+val.txt", "r")
imgID = []
dataPtr = []
for imgNo in f:
imageDir = "C:/Users/user/Desktop/CS464 Machine Learning/Project/Human Body Image/" + imgNo[:-1] + ".jpg"
if path.isfile(imageDir):
dataPtr.append(imageDir)
imgID.append(imgNo)
f.close()
labelF = open("C:/Users/user/Desktop/CS464 Machine Learning/Project/MPHB-label-txt/MPHB-label.txt", "r")
trainImgNo = 0
label = []
for i, line in enumerate(labelF):
if line == ("idx: " + imgID[trainImgNo]):
bodyPos = []
labelF.readline()
while True:
posCandidate = labelF.readline().rstrip().split(" ")
if posCandidate[0][0] == "s":
break
else:
pos = [int(float(borders)) for borders in posCandidate]
bodyPos.append(pos)
label.append(bodyPos)
if trainImgNo < len(imgID) - 1:
trainImgNo += 1
labelF.close()
#take only images with single object
singleLabel = []
singlePtr = []
for (l, ptr) in zip(label, dataPtr):
if len(l) == 1:
singleLabel.append(l)
singlePtr.append(ptr)
#show example sample
#j = 25
#img = np.array(Image.open(dataPtr[j]))
#for body in label[j]:
# cv2.rectangle(img,(body[0],body[1]),(body[2],body[3]), (255, 0, 0), 2)
#plt.imshow(img)
#ones = 0
#for j in label:
# if len(j) == 1:
# ones +=1
with open("dataPtr", "wb") as f:
pickle.dump(singlePtr, f)
with open("label", "wb") as f:
pickle.dump(singleLabel, f)