-
Notifications
You must be signed in to change notification settings - Fork 3
/
preprocessing.py
80 lines (68 loc) · 3.14 KB
/
preprocessing.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
78
79
80
import os
import shutil
def getAnomalyFrameRanges(fileName):
anomalyFrameRanges = []
with open(fileName) as f:
content = f.readlines()
content = [x.strip() for x in content]
for line in range(1, len(content)):
string = content[line]
start = string.find('[')
end = string.find(']', start)
anomalyFrameRanges.append(string[start:end + 1])
return anomalyFrameRanges
def renameAndCopyTrainDataSet(folder, subfolder):
mainDirName = "./" + folder + "/" + subfolder + "/Train/"
renameImage = ""
if(subfolder == "UCSDped1"):
renameImage = "p1_"
else:
renameImage = "p2_"
for dirname in os.listdir(mainDirName):
absolutePath = mainDirName + dirname + "/"
if os.path.isdir(absolutePath):
for i, filename in enumerate(os.listdir(absolutePath)):
#The train folder have data store files so ignore those files
if("Train" in dirname and "DS_Store" not in filename):
newImageName = renameImage + dirname + "_0_" + filename
destinationPath = "./Dataset/" + newImageName
print(destinationPath)
shutil.copy2(absolutePath+filename, destinationPath)
def renameAndCopyTestDataSet(folder, subfolder):
mainDirName = "./" + folder + "/" + subfolder + "/Test/"
renameImage = ""
pedFileName = ""
if(subfolder == "UCSDped1"):
renameImage = "p1_"
pedFileName = "UCSDped1.m"
else:
renameImage = "p2_"
pedFileName = "UCSDped2.m"
anomalyFrameRanges = getAnomalyFrameRanges(mainDirName + pedFileName)
dirCount = 0
for dirname in os.listdir(mainDirName):
if("Test" in dirname and "_gt" not in dirname):
absolutePath = mainDirName + dirname + "/"
if os.path.isdir(absolutePath):
frameRange = anomalyFrameRanges[dirCount]
frameRange = frameRange[1:len(frameRange)-1]
frameRangeList = frameRange.strip().split(",")
for i, filename in enumerate(os.listdir(absolutePath)):
label = 0
for frames in frameRangeList:
frames = frames.strip().split(":")
if(i >= int(frames[0]) and i <= int(frames[1])):
label = 1
break
#The train folder have data store files so ignore those files
if("DS_Store" not in filename):
pass
newImageName = renameImage + dirname + "_"+ str(label) +"_" + filename
destinationPath = "./Dataset/" + newImageName
print(destinationPath)
shutil.copy2(absolutePath+filename, destinationPath)
dirCount += 1
renameAndCopyTrainDataSet("UCSD_Anomaly_Dataset.v1p2", "UCSDped1")
renameAndCopyTrainDataSet("UCSD_Anomaly_Dataset.v1p2", "UCSDped2")
renameAndCopyTestDataSet("UCSD_Anomaly_Dataset.v1p2", "UCSDped1")
renameAndCopyTestDataSet("UCSD_Anomaly_Dataset.v1p2", "UCSDped2")