-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: tzzcl <zclnjucs@gmail.com>
- Loading branch information
Showing
9 changed files
with
668 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import numpy as np | ||
import xml.etree.ElementTree as ET | ||
|
||
def get_gt_boxes(xmlfile): | ||
'''get ground-truth bbox from VOC xml file''' | ||
tree = ET.parse(xmlfile) | ||
objs = tree.findall('object') | ||
num_objs = len(objs) | ||
gt_boxes = [] | ||
for obj in objs: | ||
bbox = obj.find('bndbox') | ||
x1 = float(bbox.find('xmin').text)-1 | ||
y1 = float(bbox.find('ymin').text)-1 | ||
x2 = float(bbox.find('xmax').text)-1 | ||
y2 = float(bbox.find('ymax').text)-1 | ||
|
||
gt_boxes.append((x1, y1, x2, y2)) | ||
return gt_boxes | ||
|
||
def get_cls_gt_boxes(xmlfile, cls): | ||
'''get ground-truth bbox from VOC xml file''' | ||
tree = ET.parse(xmlfile) | ||
objs = tree.findall('object') | ||
num_objs = len(objs) | ||
gt_boxes = [] | ||
for obj in objs: | ||
bbox = obj.find('bndbox') | ||
cls_name = obj.find('name').text | ||
#print(cls_name, cls) | ||
if cls_name != cls: | ||
continue | ||
x1 = float(bbox.find('xmin').text)-1 | ||
y1 = float(bbox.find('ymin').text)-1 | ||
x2 = float(bbox.find('xmax').text)-1 | ||
y2 = float(bbox.find('ymax').text)-1 | ||
|
||
gt_boxes.append((x1, y1, x2, y2)) | ||
if len(gt_boxes)==0: | ||
pass | ||
#print('%s bbox = 0'%cls) | ||
|
||
return gt_boxes | ||
|
||
def get_cls_and_gt_boxes(xmlfile, cls,class_to_idx): | ||
'''get ground-truth bbox from VOC xml file''' | ||
tree = ET.parse(xmlfile) | ||
objs = tree.findall('object') | ||
num_objs = len(objs) | ||
gt_boxes = [] | ||
for obj in objs: | ||
bbox = obj.find('bndbox') | ||
cls_name = obj.find('name').text | ||
#print(cls_name, cls) | ||
if cls_name != cls: | ||
continue | ||
x1 = float(bbox.find('xmin').text)-1 | ||
y1 = float(bbox.find('ymin').text)-1 | ||
x2 = float(bbox.find('xmax').text)-1 | ||
y2 = float(bbox.find('ymax').text)-1 | ||
|
||
gt_boxes.append((class_to_idx[cls_name],[x1, y1, x2-x1, y2-y1])) | ||
if len(gt_boxes)==0: | ||
pass | ||
#print('%s bbox = 0'%cls) | ||
|
||
return gt_boxes | ||
def convert_boxes(boxes): | ||
''' convert the bbox to the format (x1, y1, x2, y2) where x1,y1<x2,y2''' | ||
converted_boxes = [] | ||
for bbox in boxes: | ||
(x1, y1, x2, y2) = bbox | ||
converted_boxes.append((min(x1, x2), min(y1, y2), max(x1, x2), max(y1, y2))) | ||
return converted_boxes | ||
|
||
def IoU(a, b): | ||
#print(a, b) | ||
x1 = max(a[0], b[0]) | ||
y1 = max(a[1], b[1]) | ||
x2 = min(a[2], b[2]) | ||
y2 = min(a[3], b[3]) | ||
|
||
def compute_area(box): | ||
dx = max(0, box[2]-box[0]) | ||
dy = max(0, box[3]-box[1]) | ||
dx = float(dx) | ||
dy = float(dy) | ||
return dx*dy | ||
|
||
#print(x1, y1, x2, y2) | ||
w = max(0, x2-x1+1) | ||
h = max(0, y2-y1+1) | ||
#inter = w*h | ||
#aarea = (a[2]-a[0]+1)*(a[3]-a[1]+1) | ||
#barea = (b[2]-b[0]+1)*(b[3]-b[1]+1) | ||
inter = compute_area([x1, y1, x2, y2]) | ||
aarea = compute_area(a) | ||
barea = compute_area(b) | ||
|
||
#assert aarea+barea-inter>0 | ||
if aarea + barea - inter <=0: | ||
print(a) | ||
print(b) | ||
o = inter / (aarea+barea-inter) | ||
#if w<=0 or h<=0: | ||
# o = 0 | ||
return o |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
|
||
|
||
def count_max(x): | ||
count_dict = {} | ||
for xlist in x: | ||
for item in xlist: | ||
if item==0: | ||
continue | ||
if item not in count_dict.keys(): | ||
count_dict[item] = 0 | ||
count_dict[item] += 1 | ||
if count_dict == {}: | ||
return -1 | ||
count_dict = sorted(count_dict.items(), key=lambda d:d[1], reverse=True) | ||
return count_dict[0][0] | ||
|
||
|
||
def sk_pca(X, k): | ||
from sklearn.decomposition import PCA | ||
pca = PCA(k) | ||
pca.fit(X) | ||
vec = pca.components_ | ||
#print(vec.shape) | ||
return vec | ||
|
||
def fld(x1, x2): | ||
x1, x2 = np.mat(x1), np.mat(x2) | ||
n1 = x1.shape[0] | ||
n2 = x2.shape[0] | ||
k = x1.shape[1] | ||
|
||
m1 = np.mean(x1, axis=0) | ||
m2 = np.mean(x2, axis=0) | ||
m = np.mean(np.concatenate((x1, x2), axis=0), axis=0) | ||
print(x1.shape, m1.shape) | ||
|
||
|
||
c1 = np.cov(x1.T) | ||
s1 = c1*(n1-1) | ||
c2 = np.cov(x2.T) | ||
s2 = c2*(n2-1) | ||
Sw = s1/n1 + s2/n2 | ||
print(Sw.shape) | ||
W = np.dot(np.linalg.inv(Sw), (m1-m2).T) | ||
print(W.shape) | ||
W = W / np.linalg.norm(W, 2) | ||
return np.mean(np.dot(x1, W)), np.mean(np.dot(x2, W)), W | ||
|
||
def pca(X, k): | ||
n, m = X.shape | ||
mean = np.mean(X, 0) | ||
#print(mean.shape) | ||
temp = X - mean | ||
conv = np.cov(X.T) | ||
#print(conv.shape) | ||
conv1 = np.cov(temp.T) | ||
#print(conv-conv1) | ||
|
||
w, v = np.linalg.eig(conv) | ||
#print(w.shape) | ||
#print(v.shape) | ||
index = np.argsort(-w) | ||
vec = np.matrix(v.T[index[:k]]) | ||
#print(vec.shape) | ||
|
||
recon = (temp * vec.T)*vec+mean | ||
|
||
#print(X-recon) | ||
return vec | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import numpy as np | ||
|
||
def nms(boxes, scores, thresh): | ||
if len(boxes)==0: | ||
return [] | ||
boxes = np.array(boxes) | ||
#scores = np.array(scores) | ||
x1 = boxes[:, 0] | ||
y1 = boxes[:, 1] | ||
x2 = boxes[:, 2] | ||
y2 = boxes[:, 3] | ||
|
||
areas = (x2-x1+1)*(y2-y1+1) | ||
|
||
scores = np.array(scores) | ||
|
||
order = np.argsort(scores)[-100:] | ||
keep_boxes = [] | ||
while order.size > 0: | ||
i = order[-1] | ||
keep_boxes.append(boxes[i]) | ||
|
||
xx1 = np.maximum(x1[i], x1[order[:-1]]) | ||
yy1 = np.maximum(y1[i], y1[order[:-1]]) | ||
xx2 = np.minimum(x2[i], x2[order[:-1]]) | ||
yy2 = np.minimum(y2[i], y2[order[:-1]]) | ||
|
||
w = np.maximum(0.0, xx2-xx1+1) | ||
h = np.maximum(0.0, yy2-yy1+1) | ||
inter = w*h | ||
|
||
ovr = inter / (areas[i] + areas[order[:-1]] - inter) | ||
inds = np.where(ovr <= thresh) | ||
order = order[inds] | ||
|
||
return keep_boxes |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from __future__ import print_function | ||
|
||
import cv2 | ||
import numpy as np | ||
import os | ||
|
||
_GREEN = (18, 217, 15) | ||
_RED = (15, 18, 217) | ||
|
||
def vis_bbox(img, bbox, color=_GREEN, thick=1): | ||
'''Visualize a bounding box''' | ||
img = img.astype(np.uint8) | ||
(x0, y0, x1, y1) = bbox | ||
cv2.rectangle(img, (int(x0), int(y0)), (int(x1), int(y1)), color, thickness=thick) | ||
return img | ||
|
||
def vis_one_image(img, boxes, color=_GREEN): | ||
for bbox in boxes: | ||
img = vis_bbox(img, (bbox[0], bbox[1], bbox[2], bbox[3]), color) | ||
return img | ||
|
||
|