def gen_positive_list_voc_format(): ''' out_file = "/ssd/xulifeng/workspace/hd_densebox/train_data/fid_fullframe_lst.txt" dataset_list = ['/ssd/xulifeng/train_data/head_detection/fid/HeadVocFormat/FID_DID_HEAD_CLEAN_0', '/ssd/xulifeng/train_data/head_detection/fid/HeadVocFormat/FID_DID_HEAD_CLEAN_1', '/ssd/xulifeng/train_data/head_detection/fid/HeadVocFormat/FID_DID_HEAD_CLEAN_2', '/ssd/xulifeng/train_data/head_detection/fid/HeadVocFormat/HeadBoxDataFidChecked2'] ''' out_file = "/ssd/xulifeng/workspace/hd_densebox/train_data/nav_fullframe_lst.txt" dataset_list = [ '/ssd/xieqiang/Data/HeadVocFormat/NavTrackingOffice_20190703', '/ssd/xieqiang/Data/HeadVocFormat/NavTrackingAirport_20190704', '/ssd/xieqiang/Data/HeadVocFormat/NavTrackingAirport_20190705', '/ssd/xieqiang/Data/HeadVocFormat/NavTrackingAirport_20190706', '/ssd/xieqiang/Data/HeadVocFormat/NavTrackingAirport_20190707', '/ssd/xieqiang/Data/HeadVocFormat/NavTrackingAirport_20190708', '/ssd/xieqiang/Data/HeadVocFormat/NavTrackingAirport_20190709', '/ssd/xieqiang/Data/HeadVocFormat/NavTrackingAirport_20190710', '/ssd/xieqiang/Data/HeadVocFormat/NavTrackingAirport_20190711', '/ssd/xieqiang/Data/HeadVocFormat/NavTrackingAirport_20190712', '/ssd/xieqiang/Data/HeadVocFormat/NavTrackingAirport_20190713', '/ssd/xieqiang/Data/HeadVocFormat/NavTrackingAirport_20190714', ] anno_type = 1 # fully labelled fout = open(out_file, "wt") for data_folder in dataset_list: ori_anns_folder = os.path.join(data_folder, "Annotations") ori_imgs_folder = os.path.join(data_folder, "JPEGImages") imgs = list_all_files(ori_imgs_folder, exts=["jpg"]) for i, img_path in enumerate(imgs): img_base_name = os.path.basename(img_path) xml_base_name = os.path.splitext(img_base_name)[0] + ".xml" ori_xml = os.path.join(ori_anns_folder, xml_base_name) pascal_voc_ann = PascalVocAnn(xml=ori_xml) boxes = pascal_voc_ann.get_boxes() if len(boxes) == 0: continue vec = [] for b in boxes: xmin, ymin, xmax, ymax = b[1:5] w = xmax - xmin + 1 h = ymax - ymin + 1 sz = int(max(w, h) * 0.62) x = int(xmin + (w - sz) * 0.5) y = int(ymin + h - sz) vec.extend([x, y, sz, sz]) line = img_path + "," + str(anno_type) + "," + ",".join( map(str, vec)) + "\n" fout.write(line) fout.close()
def get_max_bbox_size(dataset_list): res_max_size = 0 for data_folder in dataset_list: ori_anns_folder = os.path.join(data_folder, "Annotations") xmls = list_all_files(ori_anns_folder, exts=["xml"]) for i, xml_path in enumerate(xmls): pascal_voc_ann = PascalVocAnn(xml=xml_path) bboxes = pascal_voc_ann.get_boxes() vec = [] for i, b in enumerate(bboxes): xmin, ymin, xmax, ymax = b[1:5] max_size = max(xmax - xmin, ymax - ymin) if res_max_size < max_size: res_max_size = max_size return res_max_size
def draw_by_imgxml(imgpath, xmlpath, img_out): _img_ph = imgpath _xml_ph = xmlpath print("imgpath:%s, xmlpath:%s" % (imgpath, xmlpath)) if not os.path.exists(_img_ph): raise Exception("%s not exists" % (_img_ph)) if not os.path.exists(_xml_ph): raise Exception("%s not exists" % (_xml_ph)) #img_ph = "example/originpic/ch00007_20190319_ch00007_20190319151500.mp4.cut.mp4_003000.jpg" #xml_ph = "example/originpic/ch00007_20190319_ch00007_20190319151500.mp4.cut.mp4_003000.xml" pascal_voc_ann = PascalVocAnn(xml=_xml_ph) gtboxes = pascal_voc_ann.get_boxes() print("basename:", os.path.basename(_img_ph)) print("splitext:", os.path.splitext(_img_ph)) image_basename = os.path.basename(_img_ph) img = cv2.imread(_img_ph) for bx in gtboxes: img = cv2.rectangle(img, (int(bx[1]), int(bx[2])), (int(bx[3]), int(bx[4])), (0, 0, 255), 1) rect_img_savepath = img_out + "/" + os.path.splitext( image_basename)[0] + "_gt.jpg" cv2.imwrite(rect_img_savepath, img)
jpgs = list_all_files(imglist_prefix + "/JPEGImages/", exts=["jpg"]) label_dict = {} dt_dict = {} gtdict = {} pred_dict = {} save_dstpath = imglist_prefix + "/facessdITERATION_conf%s_iou%s_%s/" % ( input_conf, input_iouthd, save_timestamp) if not os.path.exists(save_dstpath): os.mkdir(save_dstpath) for jpg_img in jpgs: sample_basename = os.path.basename(jpg_img) xml_temp = os.path.splitext(sample_basename)[0] xml_sample_path = imglist_prefix + "/Annotations/" + xml_temp + ".xml" pascal_voc_ann = PascalVocAnn(xml=xml_sample_path) bboxes = pascal_voc_ann.get_boxes() image = caffe.io.load_image(jpg_img) image_basename = os.path.basename(jpg_img) transformed_image = transformer.preprocess('data', image) net.blobs['data'].data[...] = transformed_image start = time.time() # Forward pass. detections = net.forward()['detection_out'] end = time.time() logging.warning('Running time: {} Seconds'.format(end - start)) # Parse the outputs. logging.info(detections) det_label = detections[0, 0, :, 1]
def draw_bbox_on_image(nxmin,nymin,nxmax,nymax, img_filepath): img = cv2.imread(img_filepath) region_rect = cv2.rectangle(img, (int(nxmin),int(nymin)), (int(nxmax),int(nymax)), (0,0,255), 2) rect_img_savepath = "example/gt_and_pred/"+os.path.basename(img_filepath)+"gt_and_pred.jpg" cv2.imwrite(rect_img_savepath, region_rect) if __name__ =='__main__': parse = argparse.ArgumentParser() parse.add_argument('--img_ph', type=str) parse.add_argument('--xml_ph', type=str) parse.add_argument('--env', type=str) _args = parse.parse_args() _img_ph= _args.img_ph; _xml_ph= _args.xml_ph; #img_ph = "example/originpic/ch00007_20190319_ch00007_20190319151500.mp4.cut.mp4_003000.jpg" #xml_ph = "example/originpic/ch00007_20190319_ch00007_20190319151500.mp4.cut.mp4_003000.xml" pascal_voc_ann = PascalVocAnn(xml=_xml_ph) gtboxes = pascal_voc_ann.get_boxes() print("basename:", os.path.basename(_img_ph)) print("splitext:", os.path.splitext(_img_ph)) image_basename = os.path.basename(_img_ph) img = cv2.imread(_img_ph) for bx in gtboxes: img = cv2.rectangle(img, (int(bx[1]),int(bx[2])), (int(bx[3]),int(bx[4])), (0,0,255), 2) rect_img_savepath = "./"+os.path.splitext(image_basename)[0]+"_gt.jpg" cv2.imwrite(rect_img_savepath, img)
def crop_and_resize(img_path, ori_xml, newvocdir): if not os.path.exists(newvocdir): os.mkdir(newvocdir) njpegpth = newvocdir + "/JPEGImages" if not os.path.exists(njpegpth): os.mkdir(njpegpth) nannopth = newvocdir + "/Annotations" if not os.path.exists(nannopth): os.mkdir(nannopth) img_filepath = img_path lb_filepath = ori_xml img = cv2.imread(img_filepath) pascal_voc_ann = PascalVocAnn(xml=lb_filepath) bboxes = pascal_voc_ann.get_boxes() vec = [] for i, b in enumerate(bboxes): print('reshape oribbox to retangle ...') xmin, ymin, xmax, ymax = b[1:5] w = xmax - xmin + 1 h = ymax - ymin + 1 sz = int(max(w, h) * 0.62) x = int(xmin + (w - sz) * 0.5) y = int(ymin + h - sz) vec.extend([x, y, sz, sz]) xc = x + sz / 2 yc = y + sz / 2 print('crop img with 300x300 ...') img_xmn = xc - IM_RUS if xc - IM_RUS >= 0 else 0 img_ymn = yc - IM_RUS if yc - IM_RUS >= 0 else 0 img_xmx = xc + IM_RUS if xc + IM_RUS < img.shape[ 1] else img.shape[1] - 1 img_ymx = yc + IM_RUS if yc - IM_RUS < img.shape[ 0] else img.shape[0] - 1 ioregion = img[img_ymn:img_ymx, img_xmn:img_xmx] #print(img_filepath.split('.')) #print(os.path.splitext(img_filepath)) new_anns_folder = os.path.join(newvocdir, "Annotations") new_imgs_folder = os.path.join(newvocdir, "JPEGImages") crop_img_name = os.path.splitext( img_filepath)[0] + "_crop_%d.jpg" % (i) crop_img_savepath = new_imgs_folder + "/" + os.path.basename( crop_img_name) cv2.imwrite(crop_img_savepath, ioregion) print('rebase ori bbox pos ...') #print("crop_img_savepath:%s", crop_img_savepath) nxmin = x - img_xmn nymin = y - img_ymn nxmax = x + sz - 1 - img_xmn nymax = y + sz - 1 - img_ymn crop_xml_name = os.path.splitext( os.path.basename(crop_img_savepath))[0] + ".xml" crop_xml_savepath = new_anns_folder + "/" + crop_xml_name newpascal_ann = PascalVocAnn(image_name=crop_img_savepath) newpascal_ann.set_filename(file_name=crop_img_savepath) newpascal_ann.set_size(size=[REQ_IMGSIZE, REQ_IMGSIZE, img.shape[2]]) newpascal_ann.add_object(object_class="head", xmin=nxmin, ymin=nymin, xmax=nxmax, ymax=nymax) newpascal_ann.write_xml(crop_xml_savepath) print('... done')
def crop_and_resize(img_path, ori_xml, newvocdir, gen_gt_rect=False): if not os.path.exists(newvocdir): os.mkdir(newvocdir) njpegpth = newvocdir + "/JPEGImages" if not os.path.exists(njpegpth): os.mkdir(njpegpth) nannopth = newvocdir + "/Annotations" if not os.path.exists(nannopth): os.mkdir(nannopth) if gen_gt_rect: rectpth = newvocdir + "/RECT_JPEGImages" if not os.path.exists(rectpth): os.mkdir(rectpth) img_filepath = img_path lb_filepath = ori_xml img = cv2.imread(img_filepath) pascal_voc_ann = PascalVocAnn(xml=lb_filepath) bboxes = pascal_voc_ann.get_boxes() h, w, c = pascal_voc_ann.get_size() img_output_size = 72 rectangle_boxes = {} rect_idx = 0 #print("raw bbox",bboxes) for (i, b) in enumerate(bboxes): xmin, ymin, xmax, ymax = b[1:5] #print("raw xmin,ymin:(%d,%d), xmax,ymax:(%d,%d)"%(xmin,ymin,xmax,ymax)) w_raw = xmax - xmin + 1 h_raw = ymax - ymin + 1 sz = int(max(w_raw, h_raw) * 0.62) x = int(xmin + (w_raw - sz) * 0.5) y = int(ymin + h_raw - sz) new_xmin = x new_ymin = y new_xmax = x + sz - 1 new_ymax = y + sz - 1 #print("x,y,sz:(%d,%d,%d)",x,y,sz) new_xmin = new_xmin if new_xmin >= 0 else 0 new_ymin = new_ymin if new_ymin >= 0 else 0 new_xmax = new_xmax if new_xmax < w else w - 1 new_ymax = new_ymax if new_ymax < h else h - 1 #print("new xmin,ymin:(%d,%d), xmax,ymax:(%d,%d)"%(new_xmin,new_ymin,new_xmax,new_ymax)) rectangle_boxes[i] = [new_xmin, new_ymin, new_xmax, new_ymax, sz] rect_idx = i for (i, boxlist) in rectangle_boxes.items(): [xmin, ymin, xmax, ymax, sz] = boxlist xc = xmin + sz / 2 yc = ymin + sz / 2 print('get crop info ...') #print("xmin,ymin:(%d,%d), xmax,ymax:(%d,%d)"%(xmin,ymin,xmax,ymax)) rus = get_rus_by_head_box(box=[xc, yc, sz], headrawsz=sz, head_out_base_sz=20, scale=0.2, outputsize=72) rus = int(rus) rus = rus / 2 #print("xc:%d, yc:%d, xmin:%d, ymin:%d, rus:%d"%(xc,yc,xmin,ymin,rus)) img_xmn = xc - rus if xc - rus >= 0 else 0 img_ymn = yc - rus if yc - rus >= 0 else 0 img_xmx = xc + rus if xc + rus < img.shape[1] else img.shape[1] - 1 img_ymx = yc + rus if yc - rus < img.shape[0] else img.shape[0] - 1 #rectangle_boxes[rect_idx+1] = [img_xmn, img_ymn, img_xmx, img_ymx] need_box = get_related_bboxes(rectangle_boxes, [img_xmn, img_ymn, img_xmx, img_ymx]) ioregion_croped = img[img_ymn:img_ymx, img_xmn:img_xmx] w_croped = ioregion_croped.shape[1] h_croped = ioregion_croped.shape[0] print('resize img from %sx%s to %sx%s ...' % (rus, rus, img_output_size, img_output_size)) ioregion = cv2.resize(ioregion_croped, (img_output_size, img_output_size), interpolation=cv2.INTER_CUBIC) print('... done') new_anns_folder = os.path.join(newvocdir, "Annotations") new_imgs_folder = os.path.join(newvocdir, "JPEGImages") crop_img_name = os.path.splitext( img_filepath)[0] + "_crop_%d.jpg" % (i) crop_img_savepath = new_imgs_folder + "/" + os.path.basename( crop_img_name) cv2.imwrite(crop_img_savepath, ioregion) print('rebase ori bbox pos ...') rebased_need_box = [] #print("need_box",need_box) for boxlist in need_box: [box_xmin, box_ymin, box_xmax, box_ymax] = boxlist reb_xmin = box_xmin - img_xmn reb_ymin = box_ymin - img_ymn reb_xmax = box_xmax - img_xmn reb_ymax = box_ymax - img_ymn reb_xmin = int(float(img_output_size) / w_croped * reb_xmin) reb_ymin = int(float(img_output_size) / h_croped * reb_ymin) reb_xmax = int(float(img_output_size) / w_croped * reb_xmax) reb_ymax = int(float(img_output_size) / h_croped * reb_ymax) rebased_need_box.append([reb_xmin, reb_ymin, reb_xmax, reb_ymax]) print('... done') if gen_gt_rect: print('draw rectangle ...') region_rect = cv2.rectangle(ioregion, (int(nxmin), int(nymin)), (int(nxmax), int(nymax)), (0, 255, 0), 2) rect_imgs_folder = os.path.join(newvocdir, "RECT_JPEGImages") rect_img_name = os.path.splitext( img_filepath)[0] + "_rect_%d.jpg" % (i) rect_img_savepath = rect_imgs_folder + "/" + os.path.basename( rect_img_name) cv2.imwrite(rect_img_savepath, region_rect) crop_xml_name = os.path.splitext( os.path.basename(crop_img_savepath))[0] + ".xml" crop_xml_savepath = new_anns_folder + "/" + crop_xml_name newpascal_ann = PascalVocAnn(image_name=crop_img_savepath) newpascal_ann.set_filename(file_name=crop_img_savepath) newpascal_ann.set_size( size=[img_output_size, img_output_size, img.shape[2]]) for reb_box in rebased_need_box: [reb_xmin, reb_ymin, reb_xmax, reb_ymax] = reb_box newpascal_ann.add_object(object_class="head", xmin=reb_xmin, ymin=reb_ymin, xmax=reb_xmax, ymax=reb_ymax) newpascal_ann.check_boxes() newpascal_ann.write_xml(crop_xml_savepath) print('... done')
def crop_and_resize(img_path, ori_xml, newvocdir, gen_gt_rect=False): if not os.path.exists(newvocdir): os.mkdir(newvocdir) njpegpth = newvocdir + "/JPEGImages" if not os.path.exists(njpegpth): os.mkdir(njpegpth) nannopth = newvocdir + "/Annotations" if not os.path.exists(nannopth): os.mkdir(nannopth) if gen_gt_rect: rectpth = newvocdir + "/RECT_JPEGImages" if not os.path.exists(rectpth): os.mkdir(rectpth) img_filepath = img_path lb_filepath = ori_xml img = cv2.imread(img_filepath) pascal_voc_ann = PascalVocAnn(xml=lb_filepath) bboxes = pascal_voc_ann.get_boxes() vec = [] print("Operating at ", img_filepath) for i, b in enumerate(bboxes): print('reshape oribbox to retangle ...') xmin, ymin, xmax, ymax = b[1:5] w = xmax - xmin + 1 h = ymax - ymin + 1 sz = int(max(w, h) * 0.62) x = int(xmin + (w - sz) * 0.5) y = int(ymin + h - sz) vec.extend([x, y, sz, sz]) xc = x + sz / 2 yc = y + sz / 2 print('crop img with 300x300 ...') print('input gtbox:', b) img_xmn = xc - IM_RUS if xc - IM_RUS >= 0 else 0 img_ymn = yc - IM_RUS if yc - IM_RUS >= 0 else 0 img_xmx = xc + IM_RUS if xc + IM_RUS < img.shape[ 1] else img.shape[1] - 1 img_ymx = yc + IM_RUS if yc - IM_RUS < img.shape[ 0] else img.shape[0] - 1 ioregion_300 = img[img_ymn:img_ymx, img_xmn:img_xmx] w300 = ioregion_300.shape[1] h300 = ioregion_300.shape[0] print('resize img from 300x300 to 128x128 ...') ioregion = cv2.resize(ioregion_300, (128, 128), interpolation=cv2.INTER_CUBIC) print('... done') #print(img_filepath.split('.')) #print(os.path.splitext(img_filepath)) new_anns_folder = os.path.join(newvocdir, "Annotations") new_imgs_folder = os.path.join(newvocdir, "JPEGImages") crop_img_name = os.path.splitext( img_filepath)[0] + "_crop_%d.jpg" % (i) crop_img_savepath = new_imgs_folder + "/" + os.path.basename( crop_img_name) cv2.imwrite(crop_img_savepath, ioregion) print('rebase ori bbox pos ...') #print("crop_img_savepath:%s", crop_img_savepath) nxmin = x - img_xmn nymin = y - img_ymn nxmax = x + sz - 1 - img_xmn nymax = y + sz - 1 - img_ymn print('... done') print('resize bbox pos ...') nxmin = int(128.0 / w300 * nxmin) nymin = int(128.0 / h300 * nymin) nxmax = int(128.0 / w300 * nxmax) nymax = int(128.0 / h300 * nymax) print('... done') if gen_gt_rect: print('draw rectangle ...') region_rect = cv2.rectangle(ioregion, (int(nxmin), int(nymin)), (int(nxmax), int(nymax)), (0, 255, 0), 2) rect_imgs_folder = os.path.join(newvocdir, "RECT_JPEGImages") rect_img_name = os.path.splitext( img_filepath)[0] + "_rect_%d.jpg" % (i) rect_img_savepath = rect_imgs_folder + "/" + os.path.basename( rect_img_name) cv2.imwrite(rect_img_savepath, region_rect) crop_xml_name = os.path.splitext( os.path.basename(crop_img_savepath))[0] + ".xml" crop_xml_savepath = new_anns_folder + "/" + crop_xml_name newpascal_ann = PascalVocAnn(image_name=crop_img_savepath) newpascal_ann.set_filename(file_name=crop_img_savepath) newpascal_ann.set_size(size=[128, 128, img.shape[2]]) newpascal_ann.add_object(object_class="head", xmin=nxmin, ymin=nymin, xmax=nxmax, ymax=nymax) newpascal_ann.check_boxes() newpascal_ann.write_xml(crop_xml_savepath) print('... done')
def crop_and_resize(src_filename, dst_filename): #img_filepath = 'ch01014_20190308_ch01014_20190308084110.mp4.cut.mp4_009000.jpg' #lb_filepath = 'ch01014_20190308_ch01014_20190308084110.mp4.cut.mp4_009000.xml' img_filepath = os.path.splitext(src_filename)[0] + '.jpg' lb_filepath = os.path.splitext(src_filename)[0] + '.xml' img = cv2.imread(img_filepath) pascal_voc_ann = PascalVocAnn(xml=lb_filepath) bboxes = pascal_voc_ann.get_boxes() vec = [] for i, b in enumerate(bboxes): xmin, ymin, xmax, ymax = b[1:5] w = xmax - xmin + 1 h = ymax - ymin + 1 sz = int(max(w, h) * 0.62) x = int(xmin + (w - sz) * 0.5) y = int(ymin + h - sz) vec.extend([x, y, sz, sz]) xc = x + sz / 2 yc = y + sz / 2 img_xmn = xc - IM_RUS if xc - IM_RUS >= 0 else 0 img_ymn = yc - IM_RUS if yc - IM_RUS >= 0 else 0 img_xmx = xc + IM_RUS if xc + IM_RUS < img.shape[ 1] else img.shape[1] - 1 img_ymx = yc + IM_RUS if yc - IM_RUS < img.shape[ 0] else img.shape[0] - 1 ioregion = img[img_ymn:img_ymx, img_xmn:img_xmx] #print(img_filepath.split('.')) #print(os.path.splitext(img_filepath)) crop_img_name = os.path.splitext( img_filepath)[0] + "_crop_%d.jpg" % (i) cv2.imwrite(crop_img_name, ioregion) nxmin = x - img_xmn nymin = y - img_ymn nxmax = x + sz - 1 - img_xmn nymax = y + sz - 1 - img_ymn crop_xml_name = os.path.splitext(crop_img_name)[0] + ".xml" newpascal_ann = PascalVocAnn(image_name=crop_img_name) newpascal_ann.set_filename(file_name=crop_img_name) newpascal_ann.set_size(size=[REQ_IMGSIZE, REQ_IMGSIZE, img.shape[2]]) newpascal_ann.add_object(object_class="head", xmin=nxmin, ymin=nymin, xmax=nxmax, ymax=nymax) newpascal_ann.write_xml(crop_xml_name)
def gen_pascal_xml(image_path, size, box, xml_dst_path): newpascal_ann = PascalVocAnn(image_name=image_path) newpascal_ann.set_filename(file_name=image_path) newpascal_ann.set_size(size=size) nxmin, nymin, nxmax, nymax = box newpascal_ann.add_object(object_class="head", xmin=nxmin, ymin=nymin, xmax=nxmax, ymax=nymax) newpascal_ann.check_boxes() newpascal_ann.write_xml(xml_dst_path)