161 lines
8.3 KiB
Python
Executable File
161 lines
8.3 KiB
Python
Executable File
'''
|
||
author : Hatcher W
|
||
Email: hcwang@semi.ac.cn
|
||
use: 用于检测voc数据集是否正确,
|
||
input:(图像的文件夹路径,图像标签文件夹路径)
|
||
output:check路径,所有的可视化结果都在里面
|
||
'''
|
||
import os
|
||
import cv2
|
||
import xml.dom.minidom
|
||
from tqdm import tqdm
|
||
from detection import fine_class_names
|
||
from Y5.utils.plots import plot_one_box
|
||
import numpy as np
|
||
from imatovideo import images_to_video,images_to_video_bigtosmall
|
||
from cv2AddChineseText import cv2AddChineseText
|
||
#BHQ_name = ['Grus_vipio','Grus_leucogeranus','Grus_monacha']
|
||
BHQ_name = ["Mallard", "Anas_poecilorhyncha", "Greylag_Goose", "Swan_Goose", "Domestic_goose","Podiceps_ruficollis"]
|
||
# CUI BHQ_chinese = ['绿头鸭', '斑嘴鸭', '灰雁', '鸿雁', '家鹅', '小鷿鷈']
|
||
BHQ_chinese = ["Mallard", "Anas_poecilorhyncha", "Greylag_Goose", "Swan_Goose", "Domestic_goose","Podiceps_ruficollis"]
|
||
#BHQ_chinese = ['白枕鹤', '白鹤', '白头鹤']
|
||
#BHQ_chinese = ['White-naped Crane', 'Siberian_crane', 'Hooded_Crane']
|
||
image_path="/data1/Hatcher/model/data_MPKFNet/result_raw/hiv00024/"
|
||
annotation_path="/data1/Hatcher/model/data_MPKFNet/result_xml/hiv00024Detection/"
|
||
save_path ="/data1/Hatcher/model/data_MPKFNet/result/hiv00024Detection-English/"
|
||
if not os.path.exists(save_path):
|
||
os.makedirs(save_path)
|
||
def _check_voc(image_path,annotation_path,save_path):
|
||
files_name = os.listdir(image_path)
|
||
files_name.sort()
|
||
num_image=0
|
||
num_count_old = 0
|
||
for filename_ in tqdm(files_name):
|
||
filename, extension= os.path.splitext(filename_)
|
||
#filename=filename.split('__')[0]
|
||
img_path =image_path+filename+'.jpg'
|
||
xml_path =annotation_path+filename+'.xml'
|
||
img = cv2.imread(img_path)
|
||
width_or = img.shape[1]
|
||
if img is None:
|
||
pass
|
||
try:
|
||
dom = xml.dom.minidom.parse(xml_path)
|
||
except:
|
||
#os.remove(img_path)
|
||
continue
|
||
root = dom.documentElement
|
||
objects=dom.getElementsByTagName("object")
|
||
num_detect = 0
|
||
spiec_number = [0 for x in range(0,len(BHQ_name))]
|
||
for object in objects:
|
||
bndbox = object.getElementsByTagName('bndbox')[0]
|
||
xmin = bndbox.getElementsByTagName('xmin')[0]
|
||
ymin = bndbox.getElementsByTagName('ymin')[0]
|
||
xmax = bndbox.getElementsByTagName('xmax')[0]
|
||
ymax = bndbox.getElementsByTagName('ymax')[0]
|
||
#print(xmin.childNodes[0].data)
|
||
#print(type(xmin.childNodes[0].data))
|
||
xmin_data=int(xmin.childNodes[0].data)
|
||
ymin_data=int(ymin.childNodes[0].data)
|
||
xmax_data=int(xmax.childNodes[0].data)
|
||
ymax_data=int(ymax.childNodes[0].data)
|
||
xyxy = [xmin_data,ymin_data,xmax_data,ymax_data]
|
||
label_name=object.getElementsByTagName('name')[0].childNodes[0].data
|
||
if int(label_name) == 99999:
|
||
label = f'{int(label_name)}'
|
||
else:
|
||
label = f'{fine_class_names[int(label_name)]}'
|
||
|
||
if label in BHQ_name:
|
||
label = BHQ_chinese[BHQ_name.index(label)]
|
||
spiec_number[BHQ_chinese.index(label)] += 1
|
||
else:
|
||
label = None
|
||
label_name = 0
|
||
palette = (2 ** 11 - 1, 2 ** 15 - 1, 2 ** 20 - 1)
|
||
colors = [int((p * (int(label_name) ** 2 - int(label_name) + 1)) % 255) for p in palette]
|
||
|
||
|
||
img = plot_one_box(xyxy, img, label=label, color=colors, line_thickness=2)
|
||
num_detect += 1
|
||
flag=0
|
||
if num_image % 10 == 0:
|
||
num_count_old = num_detect
|
||
spiec_number_old = spiec_number
|
||
# if width_or == 2560:
|
||
# img = cv2AddChineseText(img, "识别总数: {}只".format(str(num_count_old)), (50, 1200), textColor=(255, 255, 255),
|
||
# textSize=40)
|
||
# img = cv2AddChineseText(img, "分类识别:", (50, 1250), textColor=(255, 255, 255),
|
||
# textSize=40)
|
||
# for i in range(len(spiec_number)):
|
||
# img = cv2AddChineseText(img, "{}数量:{}只".format(str(BHQ_chinese[i]), str(spiec_number_old[i])),
|
||
# (250 + i * 300, 1250), textColor=(255, 255, 255),
|
||
# textSize=40)
|
||
# if width_or == 1920:
|
||
# radio_w = 1920/2560.0
|
||
# radio_h = 1080/1440.0
|
||
# img = cv2AddChineseText(img, "识别总数: {}只".format(str(num_count_old)), (int(50*radio_w), int(1200*radio_h)), textColor=(255, 255, 255),
|
||
# textSize=40)
|
||
# img = cv2AddChineseText(img, "分类识别:", (int(50*radio_w), int(1250*radio_h)), textColor=(255, 255, 255),
|
||
# textSize=40)
|
||
# for i in range(len(spiec_number)):
|
||
# img = cv2AddChineseText(img, "{}数量:{}只".format(str(BHQ_chinese[i]), str(spiec_number_old[i])),
|
||
# (int(250) + i * int(300), int(1250*radio_h)), textColor=(255, 255, 255),
|
||
# textSize=40)
|
||
#yingwen dibu
|
||
# if width_or == 2560:
|
||
# img = cv2AddChineseText(img, "The total number of: {}".format(str(num_count_old)), (50, 1200), textColor=(255, 255, 255),
|
||
# textSize=40)
|
||
# img = cv2AddChineseText(img, "Number of different spieces:", (50, 1250), textColor=(255, 255, 255),
|
||
# textSize=40)
|
||
# for i in range(len(spiec_number)):
|
||
# img = cv2AddChineseText(img, "{} number:{}".format(str(BHQ_chinese[i]), str(spiec_number_old[i])),
|
||
# (250 + i * 300, 1250), textColor=(255, 255, 255),
|
||
# textSize=40)
|
||
# if width_or == 1920:
|
||
# radio_w = 1920/2560.0
|
||
# radio_h = 1080/1440.0
|
||
# img = cv2AddChineseText(img, "The total number of: {}".format(str(num_count_old)), (int(50*radio_w), int(1200*radio_h)), textColor=(255, 255, 255),
|
||
# textSize=40)
|
||
#
|
||
# for i in range(len(spiec_number)):
|
||
# img = cv2AddChineseText(img, "{}:{}".format(str(BHQ_chinese[i]), str(spiec_number_old[i])),
|
||
# (int(50*radio_w) + i * int(400), int(1250*radio_h)), textColor=(255, 255, 255),
|
||
# textSize=40)
|
||
if width_or == 2560:
|
||
img = cv2AddChineseText(img, "The total number of: {}".format(str(num_count_old)), (50, 1200), textColor=(255, 255, 255),
|
||
textSize=40)
|
||
temp_pix = 0
|
||
for i in range(len(spiec_number)):
|
||
|
||
img = cv2AddChineseText(img, "{}:{}".format(str(BHQ_chinese[i]), str(spiec_number_old[i])),
|
||
(50 + temp_pix, 1250), textColor=(255, 255, 255),
|
||
textSize=40)
|
||
|
||
|
||
temp_pix += len(str(BHQ_chinese[i])) * 27
|
||
if i == 0:
|
||
temp_pix+=20
|
||
if i==1:
|
||
temp_pix-=25
|
||
if width_or == 1920:
|
||
radio_w = 1920/2560.0
|
||
radio_h = 1080/1440.0
|
||
img = cv2AddChineseText(img, "The total number of: {}".format(str(num_count_old)), (int(50*radio_w), int(1200*radio_h)), textColor=(255, 255, 255),
|
||
textSize=40)
|
||
|
||
for i in range(len(spiec_number)):
|
||
img = cv2AddChineseText(img, "{}:{}".format(str(BHQ_chinese[i]), str(spiec_number_old[i])),
|
||
(int(50*radio_w) + i * int(400), int(1250*radio_h)), textColor=(255, 255, 255),
|
||
textSize=40)
|
||
flag=cv2.imwrite(save_path+"{}.jpg".format(filename),img)
|
||
if not (flag):
|
||
print(filename,"error")
|
||
num_image+=1
|
||
print("all done ====================================")
|
||
CombineVideoRawImgPath = "../data_MPKFNet/result/" + "hiv00024" + "Detection-English"
|
||
CombineVideoResultPath = '../data_MPKFNet/result_vedio/' + "hiv00024" + "DetectionEnglish.avi"
|
||
_check_voc(image_path,annotation_path,save_path)
|
||
images_to_video(CombineVideoRawImgPath, CombineVideoResultPath)
|
||
#(int((xmin_data+xmax_data/2)),int((ymin_data+ymax_data)/2)) |