代码:
import cv2
def read_img():
file_names=[]
txt_path = r"WIDER_train\train_label.txt"
f = open(txt_path, 'r')
lines = f.readlines()
isFirst = True
labels = []
words = []
for line in lines:
line = line.rstrip()
if line.startswith('#'):
if isFirst is True:
isFirst = False
else:
labels_copy = labels.copy()
words.append(labels_copy)
labels.clear()
path = line[2:]
path = txt_path.replace('train_label.txt', 'images/') + path
file_names.append(path)
else:
line = line.split(' ')
label = [float(x) for x in line]
labels.append(label)
words.append(labels)
return file_names,words
if __name__ == '__main__':
file_names, words=read_img()
small_face = 100
max_face = 100
for index, file in enumerate(file_names):
img_raw=cv2.imread(file)
labels=words[index]
face_num=0
has_small=False
for label in labels:
x1=int(label[0])
y1=int(label[1])
x2=int(label[0] + label[2])
y2=int(label[1]+label[3])
if label[4]<0:
cv2.rectangle(img_raw, (x1, y1), (x2, y2), (0, 0, 255), 2)
else:
if max_face<label[2]*label[3]:
max_face=label[2]*label[3]
print(index, "max_face", label[2],label[3], file)
if label[2]*label[3]<small_face:
small_face= label[2]*label[3]
has_small=True
face_num+=1
cv2.rectangle(img_raw, (x1, y1), (x2, y2), (0, 255, 0), 2)
if has_small:
print(index,'small', small_face,img_raw.shape[0]*img_raw.shape[1],img_raw.shape[:2], file)
if face_num>12:
print(index,'many',face_num,file)
print(index, "maxx_face", max_face,"small_face",small_face)
# cv2.imshow("sdf", img_raw)
# cv2.waitKey()