0822更新代码逻辑,更新图片分类代码

V0.1.0
王莹
parent da397678d2
commit 410d033cfe

@ -2,7 +2,7 @@
from lxml.etree import Element, SubElement, tostring
def create_xml(boxs,img_shape,xml_path):
def create_xml(boxs, img_shape, xml_path):
"""
创建xml文件依次写入xml文件必备关键字
:param boxs: txt文件中的box
@ -22,7 +22,7 @@ def create_xml(boxs,img_shape,xml_path):
node_depth = SubElement(node_size, 'depth')
node_depth.text = str(img_shape[2])
if len(boxs)>=1: # 循环写入box
if len(boxs) >= 1: # 循环写入box
for box in boxs:
node_object = SubElement(node_root, 'object')
node_name = SubElement(node_object, 'name')
@ -45,14 +45,9 @@ def create_xml(boxs,img_shape,xml_path):
xml = tostring(node_root, pretty_print=True) # 格式化显示,该换行的换行
file_name = img_shape[3].split(".")[0]
# file_name = img_shape[3].split(".")[0]
# filename = xml_path+"/{}.xml".format(file_name)
f = open(xml_path, "wb")
f.write(xml)
f.close()

@ -51,12 +51,7 @@ class MediapipeProcess:
result_dict = {'hand_bbox':[right_hand_bbox,left_hand_bbox]}
# print('hand_bbox:',result_dict)
return result_dict
def point_to_bbox(result_list):
@ -78,29 +73,7 @@ class MediapipeProcess:
bbox_list = left_top + right_bottom
# print('bbox:',bbox)
# print('bbox_list:',bbox_list)
# bbox_list = []
# bbox_list.append(bbox[0][0])
# bbox_list.append(bbox[0][1])
# bbox_list.append(bbox[2][0])
# bbox_list.append(bbox[2][1])
return bbox_list
else:
pass
# if __name__ == '__main__':
# # media_holistic(video_file='E:/Bank_files/Bank_02/dataset/video_person/after_1/0711-1_199_0.avi',
# video_save_path='E:/Bank_files/Bank_02/videos_mediapipe/test_data/0725_test')

@ -12,142 +12,148 @@ import queue
class DetProcess():
def __init__(self,person_det_model,hand_det_model):
def __init__(self, person_det_model, hand_det_model):
self.person_det_model = person_det_model
self.hand_det_model = hand_det_model
def get_person_cut(self,frame,det_dict,imgsize):
def get_person_cut(self, frame, det_dict, imgsize):
# person_det_dict = [perdict for perdict in det_dict if list(perdict.keys())[0] == 'person']
# print('person_det_dict:',det_dict)
person_list = tools_function.get_dict_values(det_dict)
# 坐标参数修正
person_bbox_list = tools_function.para_list_correction(images_size=imgsize,bbox_list=person_list,dertpara=5)
person_bbox_list = tools_function.para_list_correction(
images_size=imgsize, bbox_list=person_list, dertpara=5)
frame_list = []
for per_bbox in person_bbox_list:
# 裁剪后人的图片
person_cut_frame = tools_function.img_cut(frame=frame,bbox=per_bbox)
person_cut_frame = tools_function.img_cut(
frame=frame, bbox=per_bbox)
frame_cut_dict = {tuple(per_bbox):person_cut_frame}
frame_cut_dict = {tuple(per_bbox): person_cut_frame}
frame_list.append(frame_cut_dict)
return frame_list
def hand_det(self,person_cut_frame,per_bbox):
# print('11111')
hands_result = MediapipeProcess.mediapipe_det(image=person_cut_frame,holistic=self.hand_det_model)
hands_result_dict = MediapipeProcess.get_analysis_result(image=person_cut_frame,results=hands_result)
def hand_det(self, person_cut_frame, per_bbox):
hands_result = MediapipeProcess.mediapipe_det(
image=person_cut_frame, holistic=self.hand_det_model)
hands_result_dict = MediapipeProcess.get_analysis_result(
image=person_cut_frame, results=hands_result)
hands_list = tools_function.select_list(hands_result_dict['hand_bbox'])
# print('hands_list:',hands_list)
imgsize2 = person_cut_frame.shape
# 手部坐标修正
hands_bbox_list = tools_function.para_list_correction(images_size=imgsize2,bbox_list=hands_list,dertpara=5)
# print('hands_bbox_list:',hands_bbox_list)
hands_bbox_list = tools_function.para_list_correction(
images_size=imgsize2, bbox_list=hands_list, dertpara=5)
hand_bbox_list = []
for hand in hands_bbox_list:
hands_result_list = tools_function.change_bbox(bbox_person=[per_bbox[0],per_bbox[1]],bbox_hand=hand)
# print('hands_result_list:',hands_result_list)
hands_result_list = tools_function.change_bbox(
bbox_person=[per_bbox[0], per_bbox[1]], bbox_hand=hand)
re_dict = {'hands':hands_result_list}
re_dict = {'hands': hands_result_list}
hand_bbox_list.append(re_dict)
# hands_result_original_dict = {'results':hand_bbox_list}
# print(hands_result_original_dict)
return hand_bbox_list
def save_annotations_xml(self,xml_save_file,save_infors,images):
def save_annotations_xml(self, xml_save_file, save_infors, images):
# images = save_infors['images']
results = save_infors
img = os.path.basename(images)
img = os.path.basename(images)
img_frame = cv2.imread(images)
xml_save_path = os.path.join(xml_save_file,img.split('.')[0] + '.xml')
w,h,d = img_frame.shape
img_shape = (w,h,d,img)
xml_save_path = os.path.join(xml_save_file, img.split('.')[0] + '.xml')
w, h, d = img_frame.shape
img_shape = (w, h, d, img)
if os.path.isfile(xml_save_path):
add_labels = add_xml(inforsDict=results,xmlFilePath=xml_save_path)
add_labels = add_xml(inforsDict=results, xmlFilePath=xml_save_path)
else:
create_new = create_xml(boxs=results,img_shape=img_shape,xml_path=xml_save_path)
def person_cut_process(self,images,img_save_files):
create_new = create_xml(
boxs=results, img_shape=img_shape, xml_path=xml_save_path)
def person_cut_process(self, images, img_save_files):
'''
使用模型将检测到的人从大图中裁剪出来
images图片全路径
img_save_files裁剪后保存小图的文件夹
通过设置labels_name_list列表中的标签名调整需要裁剪出来的目标
'''
frame = cv2.imread(images)
imgsize = frame.shape
labels_name_list = ['person']
per_det_dict = analysis_yolov8(frame=frame,
model_coco=self.person_det_model,
labels_names=labels_name_list,
confidence_set=0.2)
per_frame_cut = self.get_person_cut(frame=frame,det_dict=per_det_dict,imgsize=imgsize)
per_frame_list = [value for dictionary in per_frame_cut for value in dictionary.values()]
model_coco=self.person_det_model,
labels_names=labels_name_list,
confidence_set=0.2)
per_frame_cut = self.get_person_cut(
frame=frame, det_dict=per_det_dict, imgsize=imgsize)
per_frame_list = [
value for dictionary in per_frame_cut for value in dictionary.values()]
# print('per_frame_list:',per_frame_list)
for id_num,cut_frame in enumerate(per_frame_list):
for id_num, cut_frame in enumerate(per_frame_list):
cut_frame_save = tools_function.img_write(frame=cut_frame,img_file=images,id_num=id_num,save_file=img_save_files)
def hands_det_process(self,images,xml_save_file):
cut_frame_save = tools_function.img_write(
frame=cut_frame, img_file=images, id_num=id_num, save_file=img_save_files)
def hands_det_process(self, images, xml_save_file):
'''
使用目标检测模型检测到行人然后串联使用mediapipe模型检测到人的手部后将检测到的手部坐标框保存成xml标注文件
images检测图片的全路径
xml_save_file: 保存标注文件路径
'''
frame = cv2.imread(images)
imgsize = frame.shape
labels_name_list = ['person']
per_det_dict = analysis_yolov8(frame=frame,
model_coco=self.person_det_model,
labels_names=labels_name_list,
confidence_set=0.2)
per_frame_cut = self.get_person_cut(frame=frame,det_dict=per_det_dict,imgsize=imgsize)
model_coco=self.person_det_model,
labels_names=labels_name_list,
confidence_set=0.2)
per_frame_cut = self.get_person_cut(
frame=frame, det_dict=per_det_dict, imgsize=imgsize)
for frame_dict in per_frame_cut:
per_bbox = list(frame_dict.keys())[0]
person_cut_frame = list(frame_dict.values())[0]
hands_dict = self.hand_det(person_cut_frame=person_cut_frame,per_bbox=per_bbox)
self.save_annotations_xml(xml_save_file=xml_save_file,save_infors=hands_dict,images=images)
hands_dict = self.hand_det(
person_cut_frame=person_cut_frame, per_bbox=per_bbox)
self.save_annotations_xml(
xml_save_file=xml_save_file, save_infors=hands_dict, images=images)
# person_det = self.detect_yolo(images_path=images,labels_name_list=labels_name_list)
def det_process(self,images,xml_save_file):
def det_process(self, images, xml_save_file):
'''
检测指定目标并保存检测结果到xml文件中
通过设置labels_name_list列表中的标签名调整需要裁剪出来的目标
'''
frame = cv2.imread(images)
imgsize = frame.shape
labels_name_list = ['cell phone','mouse','keyboard']
labels_name_list = ['cell phone', 'mouse', 'keyboard']
per_det_dict = analysis_yolov8(frame=frame,
model_coco=self.person_det_model,
labels_names=labels_name_list,
confidence_set=0.2)
self.save_annotations_xml(xml_save_file=xml_save_file,save_infors=per_det_dict,images=images)
model_coco=self.person_det_model,
labels_names=labels_name_list,
confidence_set=0.2)
self.save_annotations_xml(
xml_save_file=xml_save_file, save_infors=per_det_dict, images=images)
if __name__ == '__main__':
@ -157,25 +163,18 @@ if __name__ == '__main__':
img_save_files = 'images_cut'
xml_save_file = 'annotations'
# 初始化目标检测
person_model = YOLO("model_files/yolov8x.pt")
# 初始化mediapipe
mp_holistic = mp.solutions.holistic
holistic = mp_holistic.Holistic(min_detection_confidence=0.1,min_tracking_confidence=0.1)
holistic = mp_holistic.Holistic(
min_detection_confidence=0.1, min_tracking_confidence=0.1)
Det = DetProcess(person_det_model=person_model,hand_det_model=holistic)
Det = DetProcess(person_det_model=person_model, hand_det_model=holistic)
for images in images_list:
# Det.person_cut_process(images=images,img_save_files=img_save_files)
Det.hands_det_process(images=images,xml_save_file=xml_save_file)
Det.det_process(images=images,xml_save_file=xml_save_file)
Det.person_cut_process(images=images, img_save_files=img_save_files)
Det.hands_det_process(images=images, xml_save_file=xml_save_file)
Det.det_process(images=images, xml_save_file=xml_save_file)

@ -1,43 +1,40 @@
def analysis_yolov8(frame, model_coco,labels_names,confidence_set):
def analysis_yolov8(frame, model_coco, labels_names, confidence_set):
# 第一步用COCO数据集推理
results_coco = model_coco(frame)
re_list = []
if results_coco:
for r in results_coco:
boxes = r.boxes
idx = 0
for box in boxes:
idx += 1
b = box.xyxy[0] # get box coordinates in (top, left, bottom, right) format
# get box coordinates in (top, left, bottom, right) format
b = box.xyxy[0]
c = box.cls
# 保存标签和坐标值作为返回结果
blist = b.tolist()
labels_name = model_coco.names[int(c)]
# print('labels_name:',labels_name)
confidence = float(box.conf)
confidence = round(confidence, 2)
# 过滤置信度以下目标
if confidence < confidence_set:
continue
if labels_name in labels_names:
# 一个结果字典
re_dict = {labels_name:blist}
# 一个结果字典
re_dict = {labels_name: blist}
re_list.append(re_dict)

@ -0,0 +1,48 @@
import os
import cv2
from tqdm import tqdm
#总数据集图像路径
imgway_1="E:/data-temporary/zhongtian_nianhou/shoes/del"
#符合要求图像的路径
imgway_2="E:/data-temporary/zhongtian_nianhou/shoes/0216_dataset/background"
#不符合要求图像的路径.
imgway_3="G:/zhongtian_second/hangche/images background"
#误触
imgway_7='G:/zhongtian_second/hangche/images background'
for filename in tqdm(os.listdir(r"%s" % imgway_1)):
img = cv2.imread("%s/%s"%(imgway_1,filename))
h, w, c = img.shape
resize_width = int(w * 2)
resize_height = int(h * 2)
# cv2.namedWindow(img, cv2.WINDOW_NORMAL)
# cv2.resizeWindow(img, resize_width, resize_height) #缩小显示
img = cv2.resize(img,(resize_width,resize_height)) #更改图片大小设定为指定大小o
# # h = 640
# # w = 300
# cv2.resizeWindow(img, resize_height, resize_width)
# cv2.namedWindow('datasets',0)
cv2.imshow('datasets', img)
k=cv2.waitKey(0)& 0xFF
if k == ord('o'):
cv2.imwrite('%s/%s' % (imgway_2, filename),img)
print('%s符合要求!' % filename)
cv2.destroyAllWindows()
elif k == ord('k'):
pass
# cv2.imwrite('%s/%s' % (imgway_3, filename),img)
# print('%s不符合要求' % filename)
# cv2.destroyAllWindows()
else:
cv2.imwrite('%s/%s' % (imgway_7, filename),img)
print('误触错误的键!背景图%s保存至mistake文件夹' % filename)
cv2.destroyAllWindows()

@ -78,8 +78,6 @@ def select_list(result_list):
if result == None:
pass
else:
# result_bbox = select_bbox(result)
result_only.append(result)
return result_only
@ -87,7 +85,6 @@ def select_list(result_list):
def para_list_correction(images_size, bbox_list, dertpara):
# bbox_list_iou = del_bbox(bbox_list)
updata_result_list = []

Loading…
Cancel
Save