-
Notifications
You must be signed in to change notification settings - Fork 0
/
detection_data_prep.py
52 lines (38 loc) · 1.21 KB
/
detection_data_prep.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
import os
import cv2
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
def convert(xmin, w, ymin, h, w_img, h_img):
xcenter = (xmin + w/2) / w_img
ycenter = (ymin + h/2) / h_img
w = w / w_img
h = h / h_img
return xcenter, ycenter, w, h
def reverse_convert(xcenter, ycenter, w, h, w_img, h_img):
h = h * h_img
w = w * w_img
x = (xcenter * w_img) - w/2
y = (ycenter * h_img) - h/2
return x, y, w + x, y + h
def yolobbox2bbox(x,y,w,h):
x1, y1 = x-w/2, y-h/2
x2, y2 = x+w/2, y+h/2
return x1, y1, x2, y2
def bbox1(img):
a = np.where(img != 0)
bbox = np.min(a[1]), np.max(a[1]), np.min(a[0]), np.max(a[0])
return bbox
def find_bbox(img:np.array) -> list:
"find bbox using masks"
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# threshold
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY)[1]
# get contour bounding boxes and draw on copy of input
contours = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
contours = contours[0] if len(contours) == 2 else contours[1]
bboxes = []
for c in contours:
x,y,w,h = cv2.boundingRect(c)
bboxes.append([x,y,w,h])
return bboxes