This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#adjust_boxes(converted_boxes) | |
num=0 | |
for i in adjust_boxes(converted_boxes): | |
image = cv2.rectangle(img, (int(i[0]), int(i[1])), (int(i[2]), int(i[3])), (255,0,255), 1) | |
cv2.putText(img, "{}".format(num+1), (int(i[0]+30), int(i[1])+30), cv2.FONT_HERSHEY_SIMPLEX , .7, | |
(0, 0, 255), 2); | |
num+=1 | |
cv2.imwrite('result.jpg',img) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def adjust_boxes(boxes): | |
for i in range(len(boxes)): | |
for j in range(i+1, len(boxes)): | |
if j >= len(boxes): | |
break | |
# Check if the boxes intersect | |
if intersect(boxes[i], boxes[j]): | |
# Determine the overlap distance | |
dx = min(boxes[i][2], boxes[j][2]) - max(boxes[i][0], boxes[j][0]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
converted_boxes = [] | |
for box in df.values.tolist(): | |
x, y, w, h, x2 = box | |
y2 = y + h | |
converted_boxes.append([x, y, x2, y2]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# re-order the bounding boxes by their position | |
for i in range(len(bboxes)-1): | |
for ind in range(len(df)-1): | |
if df.iloc[ind][4] > df.iloc[ind+1][0] and df.iloc[ind][1] > df.iloc[ind+1][1]: | |
#print(df.iloc[ind][4] , df.iloc[ind+1][0] , df.iloc[ind][1] , df.iloc[ind+1][1]) | |
df.iloc[ind], df.iloc[ind+1] = df.iloc[ind+1].copy(), df.iloc[ind].copy() | |
#print(df.iloc[ind], df.iloc[ind+1],'') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# sort the bounding boxes by their x-coordinate, y-coordinate, and x2-coordinate | |
df = df.sort_values(["x","y", "x2"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# convert bounding boxes to [x, y, w, h] format | |
bboxes = [[box[0], box[1], box[2]-box[0], box[3]-box[1]] for box in bboxes] | |
# create a pandas dataframe from the bounding boxes | |
df = pd.DataFrame(bboxes, columns=['x','y','w', 'h'], dtype=int) | |
# add a column for the x-coordinate on the right side of the bounding box | |
df["x2"] = df["x"]+df["w"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import cv2 | |
img = cv2.imread("test.jpg") | |
# generate some bounding boxes (x1, y1, x2, y2) | |
bboxes = [[0.0, 11.934242248535156, 1772.6680908203125, 122.33704376220703], | |
[20.308576583862305, 10.841922760009766, 738.1071166992188, 125.6768798828125], | |
[27.024972915649414, 141.59010314941406, 672.558349609375, 497.9985656738281], | |
[650.5659790039062, 152.5384979248047, 890.4132690429688, 487.71356201171875], | |
[890.1557006835938, 135.5287322998047, 1202.565185546875, 374.8603820800781], |