Repository navigation
Expand file tree
/
Copy pathpreprocessing.py
More file actions
83 lines (63 loc) · 2.48 KB
/
Copy pathpreprocessing.py
File metadata and controls
83 lines (63 loc) · 2.48 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import numpy as np
from PIL import Image
def LoadImageFromPath(path):
return Image.open(path)
def LoadImages(paths):
return {id : LoadImageFromPath(paths[id]) for id in paths}
def GetIDFromImage(file_name, json_data):
image_list = json_data["images"]
for image in image_list:
if image["file_name"] == file_name:
return image["id"]
def GetBoundingBoxFromID(ID, json_data):
pass
def GetCleanedTrainPaths(paths, prefix):
return [path.replace(f"CocoDataSet/{prefix}\\", "") for path in paths]
def CountSingleCrows(json_data):
number_of_images = len(json_data['images'])
count = 0
for i in range(number_of_images+1):
number_of_crows = 0
for datapoint in json_data['annotations']:
if datapoint['image_id'] == i:
number_of_crows+=1
if number_of_crows ==1:
count+=1
def GetSingleCrowsIDs(json_data):
number_of_images = len(json_data['images'])
IDs = []
for id in range(number_of_images + 1):
number_of_crows = 0
for datapoint in json_data['annotations']:
if datapoint['image_id'] == id:
number_of_crows += 1
if number_of_crows == 1:
IDs.append(id)
return IDs
def GetListOfPaths(IDs, json_data, prefix):
paths = {}
for image in json_data['images']:
for ID in IDs:
if image['id'] == ID:
paths[ID] = (f"{prefix}{image['file_name']}")
break
return paths
def GetBoundingBoxByID(ID, json_data):
for annotation in json_data['annotations']:
if annotation['image_id'] == ID:
return annotation['bbox']
def GetBoundingBoxesByIDs(IDs, json_data):
return {id:GetBoundingBoxByID(id, json_data) for id in IDs}
def PillowImageArrayToNumpyArray(listOfImages, normalize = False):
# Convert list of PIL Images to a single NumPy array
numpy_train_images = np.array([np.array(image) for image in listOfImages])
# Normalize pixel values if your model expects values between 0 and 1
if normalize:
numpy_train_images = numpy_train_images.astype('float32') / 255.0
return numpy_train_images
def BoundingBoxesToNumpyArray(boundingBoxes):
return np.array(boundingBoxes)
def AppendImageToNumpyArray(numpyArray, image):
return np.append(numpyArray, np.array(image))
def AppenBoundingBoxToNumpyArray(numpyArray, boundingBox):
return np.append(numpyArray, boundingBox)