-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathE2.py
42 lines (35 loc) · 1.61 KB
/
E2.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
import numpy as np
def swap(coords: np.ndarray):
"""
This method will flip the x and y coordinates in the coords array.
:param coords: A numpy array of bounding box coordinates with shape [n,5] in format:
::
[[x11, y11, x12, y12, classid1],
[x21, y21, x22, y22, classid2],
...
[xn1, yn1, xn2, yn2, classid3]]
:return: The new numpy array where the x and y coordinates are flipped.
"""
# Create a copy to avoid modifying the original array
swapped_coords = coords.copy()
# Swap x11 with y11 and x12 with y12
swapped_coords[:, [0, 2]] = coords[:, [1, 3]]
swapped_coords[:, [1, 3]] = coords[:, [0, 2]]
return swapped_coords
# Example usage
coords = np.array([[10, 5, 15, 6, 0],
[11, 3, 13, 6, 0],
[5, 3, 13, 6, 1],
[4, 4, 13, 6, 1],
[6, 5, 13, 16, 1]])
swapped_coords = swap(coords)
print("Original Coordinates:")
print(coords)
print("Swapped Coordinates:")
print(swapped_coords)
# the obvious error is that coords is a pointer and if we change everything in place we get trouble in whole process
# Making a Copy: Using coords.copy() ensures that the original coords array remains unchanged, preventing unwanted side effects.
# Correct Swapping: I've correctly swapped the x and y coordinates using the correct numpy indexing.
# This is done by swapping the columns pairwise ([0, 2] with [1, 3]).
# This updated function will now correctly swap x and y coordinates in the bounding box array and
# prevent side-effects by not altering the original array directly.