0
点赞
收藏
分享

微信扫一扫

39、提取人头检测数据集和手机检测,并追加标注手机检测中人头数据


基本思想:需要一个人头检测和手机检测模型,然后部署硬件开发板上,然后开始搜集数据集和训练

第一步:下载“4.10_霍普金斯JHU-CHAND++大规模人群计数数据集及基准网络”数据集

链接:https://pan.baidu.com/s/1kwT6t88yXmIIcBlIrooChA 
提取码:o939

然后使用脚本提取一下数据集转成labelme数据集的格式

import glob
import cv2
import os
import json


dataset_path= r"F:\arXiv\jhu_crowd_v2.0\test\test_json"
if not os.path.exists(dataset_path):
os.mkdir(dataset_path)
txt_path = r"F:\arXiv\jhu_crowd_v2.0\test\gt"
images_path = r"F:\arXiv\jhu_crowd_v2.0\test\images"
txt_list=glob.glob(os.path.join(txt_path,"*.txt"))
images_list=glob.glob(os.path.join(txt_path,"*.jpg"))
for txt_item in txt_list:
print(txt_item)
(filepath, tempfilename) = os.path.split(txt_item)
(filename, extension) = os.path.splitext(tempfilename)
folder = "sxj731533730"
path = tempfilename
image_full_path=os.path.join(images_path,filename+".jpg")
image_full_dest=os.path.join(dataset_path,filename+".jpg")
img_cv=cv2.imread(image_full_path)
height,width,_=img_cv.shape
cv2.imwrite(image_full_dest, img_cv)
print(width, height)
data = {}
data['imagePath'] = filename+".jpg"
data['flags'] = {}
data['imageWidth'] = width
data['imageHeight'] = height
data['imageData'] = None
data['version'] = "5.0.1"
data["shapes"] = []
with open(txt_item, "r", encoding="utf-8") as f:
for line in f:
txt_data = line.strip(" ").split()
center_x= int(txt_data[0])
center_y=int(txt_data[1])
xmin = center_x-int(txt_data[2])/2
ymin = center_y-int(txt_data[3])/2
xmax = center_x+int(txt_data[2])/2
ymax = center_y+int(txt_data[3])/2
if xmin<0 or ymin<0:
continue
elif xmax>width or ymax>height:
continue
points = [[xmin, ymin], [xmax, ymax]]
itemData = {'points': []}
itemData['points'].extend(points)
name = "head"
itemData["flag"] = {}
itemData["group_id"] = None
itemData["shape_type"] = "rectangle"
itemData["label"] = name
data["shapes"].append(itemData)

jsonName = ".".join([filename, "json"])

print(dataset_path, jsonName)
jsonPath = os.path.join(dataset_path, jsonName)
with open(jsonPath, "w") as f:
json.dump(data, f)
print("加载入文件完成...")

分别提取了对应的训练集、测试集、训练集

coco的yolo数据集转lableme数据集,稍加修改提取手机的类别

import glob
import cv2
import os
import json
coco=["person", "bicycle", "car", "motorcycle", "airplane", "bus", "train", "truck", "boat", "traffic light",
"fire hydrant","stop sign", "parking meter", "bench", "bird", "cat", "dog", "horse", "sheep", "cow",
"elephant", "bear", "zebra", "giraffe","backpack", "umbrella","handbag", "tie", "suitcase", "frisbee",
"skis", "snowboard", "sports ball", "kite", "baseball bat", "baseball glove", "skateboard", "surfboard",
"tennis racket", "bottle", "wine glass", "cup", "fork", "knife", "spoon", "bowl", "banana", "apple",
"sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza", "donut", "cake", "chair", "couch",
"potted plant", "bed", "dining table","toilet", "tv", "laptop", "mouse", "remote", "keyboard", "cell phone",
"microwave", "oven", "toaster", "sink", "refrigerator", "book","clock", "vase", "scissors", "teddy bear",
"hair drier", "toothbrush"]

dataset_path = r"/home/ubuntu/Downloads/coco_yolo/images/val_json"
if not os.path.exists(dataset_path):
os.mkdir(dataset_path)
txt_path = r"/home/ubuntu/Downloads/coco_yolo/labels/val2017"
images_path = r"/home/ubuntu/Downloads/coco_yolo/images/val2017"
txt_list = glob.glob(os.path.join(txt_path, "*.txt"))
images_list = glob.glob(os.path.join(txt_path, "*.jpg"))
for txt_item in txt_list:
print(txt_item)
(filepath, tempfilename) = os.path.split(txt_item)
(filename, extension) = os.path.splitext(tempfilename)
folder = "sxj731533730"
path = tempfilename
image_full_path = os.path.join(images_path, filename + ".jpg")
image_full_dest = os.path.join(dataset_path, filename + ".jpg")
img_cv = cv2.imread(image_full_path)
height, width, _ = img_cv.shape
cv2.imwrite(image_full_dest, img_cv)
print(width, height)
data = {}
data['imagePath'] = filename + ".jpg"
data['flags'] = {}
data['imageWidth'] = width
data['imageHeight'] = height
data['imageData'] = None
data['version'] = "5.0.1"
data["shapes"] = []
with open(txt_item, "r", encoding="utf-8") as f:
for line in f:
txt_data = line.strip(" ").split()
X = eval(txt_data[1])*width*2
Y = eval(txt_data[2])*height*2
W = eval(txt_data[3])*width
H = eval(txt_data[4])*height
xmin=(X-W)/2
xmax=(X+W)/2
ymin=(Y-H)/2
ymax=(Y+H)/2
if xmin < 0 or ymin < 0:
continue
elif xmax > width or ymax > height:
continue
points = [[xmin, ymin], [xmax, ymax]]
itemData = {'points': []}
itemData['points'].extend(points)
name = coco[eval(txt_data[0])]
itemData["flag"] = {}
itemData["group_id"] = None
itemData["shape_type"] = "rectangle"
itemData["label"] = name
data["shapes"].append(itemData)

jsonName = ".".join([filename, "json"])

print(dataset_path, jsonName)
jsonPath = os.path.join(dataset_path, jsonName)
with open(jsonPath, "w") as f:
json.dump(data, f)
print("加载入文件完成...")

在上面筛选出手机的数据集基础上,嵌入了centerface在转xml中把含有人头的标签也打上了

# -*- coding: utf-8 -*-
import cv2
import json
import io
import os
from xml.dom.minidom import Document

path = '/home/ubuntu/cap_scrp'
destPath = "/home/ubuntu/cap_scrp"


def file_name(file_dir):
L = []
for root, dirs, files in os.walk(file_dir):
for file in files:
if os.path.splitext(file)[1] == '.json':
L.append(os.path.join(root, file))
return L


m_folder = os.path.basename(path)
print('m_folder=', m_folder)
m_database = 'Unknown'
print('m_database=', m_database)
m_depth = 3
print('m_depth=', m_depth)
m_segmented = 0
print('m_segmented=', m_segmented)

m_pose = 'Unspecified'
print('m_pose=', m_pose)
m_truncated = 0
print('m_truncated=', m_truncated)
m_difficult = 0
print('m_difficult=', m_difficult)
m_segmented = 0
print('m_segmented=', m_segmented)

path_list = file_name(path)
for name in enumerate(path_list):
m_path = name[1]
dir = os.path.dirname(m_path)
print('dir=', dir)

file_json = io.open(m_path, 'r', encoding='utf-8')
json_data = file_json.read()
data = json.loads(json_data)
m_filename = data['imagePath']
print('m_filename=', m_filename)
m_path = os.path.join(dir, m_filename)
print('m_path=', m_path)
m_width = data['imageWidth']
print('m_width=', m_width)
m_height = data['imageHeight']
print('m_height=', m_height)
object_name = os.path.splitext(m_filename)[0]
new_object_name = object_name + '.xml'
img_name=object_name+".jpg"



print(new_object_name)
doc = Document() # 创建DOM文档对象
DOCUMENT = doc.createElement('annotation') # 创建根元素

folder = doc.createElement('folder')
folder_text = doc.createTextNode(m_folder)
folder.appendChild(folder_text)
DOCUMENT.appendChild(folder)
doc.appendChild(DOCUMENT)

filename = doc.createElement('filename')
filename_text = doc.createTextNode(m_filename)
filename.appendChild(filename_text)
DOCUMENT.appendChild(filename)
doc.appendChild(DOCUMENT)

path = doc.createElement('path')
path_text = doc.createTextNode(m_filename)
path.appendChild(path_text)
DOCUMENT.appendChild(path)
doc.appendChild(DOCUMENT)

source = doc.createElement('source')
database = doc.createElement('database')
database_text = doc.createTextNode(m_database) # 元素内容写入
database.appendChild(database_text)
source.appendChild(database)
DOCUMENT.appendChild(source)
doc.appendChild(DOCUMENT)

size = doc.createElement('size')
width = doc.createElement('width')
width_text = doc.createTextNode(str(m_width)) # 元素内容写入
width.appendChild(width_text)
size.appendChild(width)

height = doc.createElement('height')
height_text = doc.createTextNode(str(m_height))
height.appendChild(height_text)
size.appendChild(height)

depth = doc.createElement('depth')
depth_text = doc.createTextNode(str(m_depth))
depth.appendChild(depth_text)
size.appendChild(depth)

DOCUMENT.appendChild(size)

segmented = doc.createElement('segmented')
segmented_text = doc.createTextNode(str(m_segmented))
segmented.appendChild(segmented_text)
DOCUMENT.appendChild(segmented)
doc.appendChild(DOCUMENT)
for i in range(len(data['shapes'])):
m_xmin_0 = (data['shapes'][i]['points'][0][0] if (
data['shapes'][i]['points'][0][0] < data['shapes'][i]['points'][1][0]) else
data['shapes'][i]['points'][1][0])
print('m_xmin_0=', m_xmin_0)
m_ymin_0 = (data['shapes'][i]['points'][0][1] if (
data['shapes'][i]['points'][0][1] < data['shapes'][i]['points'][1][1]) else
data['shapes'][i]['points'][1][1])
print('m_ymin_0=', m_ymin_0)
m_xmax_0 = (data['shapes'][i]['points'][1][0] if (
data['shapes'][i]['points'][0][0] < data['shapes'][i]['points'][1][0]) else
data['shapes'][i]['points'][0][0])
print('m_xmax_0=', m_xmax_0)
m_ymax_0 = (data['shapes'][i]['points'][1][1] if (
data['shapes'][i]['points'][0][1] < data['shapes'][i]['points'][1][1]) else
data['shapes'][i]['points'][0][1])
print('m_ymax_0=', m_ymax_0)
m_name_0 = data['shapes'][i]['label']
print('m_name_0=', m_name_0)
object = doc.createElement('object')
name = doc.createElement('name')
name_text = doc.createTextNode(m_name_0)
name.appendChild(name_text)
object.appendChild(name)

pose = doc.createElement('pose')
pose_text = doc.createTextNode(m_pose)
pose.appendChild(pose_text)
object.appendChild(pose)

truncated = doc.createElement('truncated')
truncated_text = doc.createTextNode(str(m_truncated))
truncated.appendChild(truncated_text)
object.appendChild(truncated)

difficult = doc.createElement('difficult')
difficult_text = doc.createTextNode(str(m_difficult))
difficult.appendChild(difficult_text)
object.appendChild(difficult)

bndbox = doc.createElement('bndbox')
xmin = doc.createElement('xmin')
xmin_text = doc.createTextNode(str(int(m_xmin_0)))
xmin.appendChild(xmin_text)
bndbox.appendChild(xmin)

ymin = doc.createElement('ymin')
ymin_text = doc.createTextNode(str(int(m_ymin_0)))
ymin.appendChild(ymin_text)
bndbox.appendChild(ymin)

xmax = doc.createElement('xmax')
xmax_text = doc.createTextNode(str(int(m_xmax_0)))
xmax.appendChild(xmax_text)
bndbox.appendChild(xmax)

ymax = doc.createElement('ymax')
ymax_text = doc.createTextNode(str(int(m_ymax_0)))
ymax.appendChild(ymax_text)
bndbox.appendChild(ymax)
object.appendChild(bndbox)

DOCUMENT.appendChild(object)
import demo

list_info = demo.test_image(cv2.imread(os.path.join(dir, img_name)))
for item in list_info:
m_xmin_0 = item[0][0]
m_ymin_0 = item[0][1]
m_xmax_0 = item[1][0]
m_ymax_0 = item[1][1]
m_name_0 = "head"
print('m_name_0=', m_name_0)
object = doc.createElement('object')
name = doc.createElement('name')
name_text = doc.createTextNode(m_name_0)
name.appendChild(name_text)
object.appendChild(name)

pose = doc.createElement('pose')
pose_text = doc.createTextNode(m_pose)
pose.appendChild(pose_text)
object.appendChild(pose)

truncated = doc.createElement('truncated')
truncated_text = doc.createTextNode(str(m_truncated))
truncated.appendChild(truncated_text)
object.appendChild(truncated)

difficult = doc.createElement('difficult')
difficult_text = doc.createTextNode(str(m_difficult))
difficult.appendChild(difficult_text)
object.appendChild(difficult)

bndbox = doc.createElement('bndbox')
xmin = doc.createElement('xmin')
xmin_text = doc.createTextNode(str(int(m_xmin_0)))
xmin.appendChild(xmin_text)
bndbox.appendChild(xmin)

ymin = doc.createElement('ymin')
ymin_text = doc.createTextNode(str(int(m_ymin_0)))
ymin.appendChild(ymin_text)
bndbox.appendChild(ymin)

xmax = doc.createElement('xmax')
xmax_text = doc.createTextNode(str(int(m_xmax_0)))
xmax.appendChild(xmax_text)
bndbox.appendChild(xmax)

ymax = doc.createElement('ymax')
ymax_text = doc.createTextNode(str(int(m_ymax_0)))
ymax.appendChild(ymax_text)
bndbox.appendChild(ymax)
object.appendChild(bndbox)

DOCUMENT.appendChild(object)
new_path_filename = os.path.join(destPath, new_object_name)
print('new_path_filename=', new_path_filename)
f = open(new_path_filename, 'w')

doc.writexml(f, indent='\t', newl='\n', addindent='\t', encoding='utf-8')
f.close()

修改centerface源码为 ​​GitHub - Star-Clouds/CenterFace: face detection​​

def test_image(frame):
h, w = frame.shape[:2]
landmarks = True
centerface = CenterFace(landmarks=landmarks)
if landmarks:
dets, lms = centerface(frame, h, w, threshold=0.35)
else:
dets = centerface(frame, h, w, threshold=0.35)
rect_list=[]
for det in dets:
boxes, score = det[:4], det[4]
cv2.rectangle(frame, (int(boxes[0]), int(boxes[1])), (int(boxes[2]), int(boxes[3])), (2, 255, 0), 1)
rect_list.append([(int(boxes[0]), int(boxes[1])), (int(boxes[2]), int(boxes[3]))])
if landmarks:
for lm in lms:
for i in range(0, 5):
cv2.circle(frame, (int(lm[i * 2]), int(lm[i * 2 + 1])), 2, (0, 0, 255), -1)
return rect_list

这样就会在原来提取的手机数据集上叠加人脸的标注

39、提取人头检测数据集和手机检测,并追加标注手机检测中人头数据_计算机视觉

训练的结果图

39、提取人头检测数据集和手机检测,并追加标注手机检测中人头数据_opencv_02

39、提取人头检测数据集和手机检测,并追加标注手机检测中人头数据_opencv_03

 

 

举报

相关推荐

【行人检测数据集】

0 条评论