├── Imagesets ├── val.txt ├── test.txt └── train.txt ├── 1_bmp_to_jpg.py ├── readme.txt ├── 4_Imageset.py ├── wrape_white_pills.py ├── txt_to_dict.py ├── wrape_blue_pills.py ├── wrape_oill_pills.py ├── wrape_yellow_pills.py ├── 3_Annotation.py ├── dict_to_xml.py ├── txt_to_xml.py └── 2_标注.py /Imagesets/val.txt: -------------------------------------------------------------------------------- 1 | 000005 2 | -------------------------------------------------------------------------------- /Imagesets/test.txt: -------------------------------------------------------------------------------- 1 | 000004 2 | -------------------------------------------------------------------------------- /Imagesets/train.txt: -------------------------------------------------------------------------------- 1 | 000003 2 | 000002 3 | 000001 4 | -------------------------------------------------------------------------------- /1_bmp_to_jpg.py: -------------------------------------------------------------------------------- 1 | #-*- encoding=utf-8 2 | from wrape_white_pills import wrape_white_pills 3 | from wrape_blue_pills import wrape_blue_pills 4 | from wrape_yellow_pills import wrape_yellow_pills 5 | from wrape_oill_pills import wrape_oill_pills 6 | 7 | if __name__ == "__main__": 8 | wrape_white_pills() 9 | wrape_blue_pills() 10 | wrape_yellow_pills() 11 | wrape_oill_pills() 12 | print("bmp to jpg 完成!") -------------------------------------------------------------------------------- /readme.txt: -------------------------------------------------------------------------------- 1 | 注意:在wrape_***_pills.py文件中,从bmp到jpg的过程需要提前注意裁剪的大小!!! 2 | 3 | 1,有新图像数据,则请先存放于/data/origin_data/***_pills_1或***_pills_2/ 4 | 2,剪切部分数据到相应的/data/origin_data/***_pills/;并对其进行筛选!!!(此处很关键!!!)筛选依据:没有目标的图像, 5 | 或者目标都在边缘,不方便标注的图像,这些图像都需要删除! 6 | 3,然后按以下步骤进行: 7 | 1:运行1_bmp_to_jpg.py(注:该步骤不仅完成从bmp到jpg转化,同时完成排序工作,方便标注,还有 8 | 如果开始数据完成裁剪了,则修改代码不用再裁剪,若是未裁剪,则修改代码,找到合适的值,裁剪之!) 9 | 2:对/data/***_pills/图像进行标注(若2_标注可以使用的话,就用它吧,不行的话,请在网上找一下,有在win7下用opencv做的, 10 | 我的这个就是打算仿它来写,写好就可以用,写不好就呜呜~~~~(>_<)~~~~了!) 11 | 3:运行3_Annotation.py 12 | 4:运行4_Imageset.py 13 | 14 | 按阅读顺序进行操作后,则可得到质量较好的数据集!注:***表示可选(white, blue, oill, yellow),项目中未出现的文件夹请自行创建(如:/data/origin_data/)! 15 | -------------------------------------------------------------------------------- /4_Imageset.py: -------------------------------------------------------------------------------- 1 | # -*- encoding=utf-8 2 | import os 3 | import random 4 | 5 | if __name__ == "__main__": 6 | annotation_path = os.path.join(os.path.dirname(__file__), "Annotations") 7 | assert os.path.exists(annotation_path), "{}不存在!".format(annotation_path) 8 | JPEGImage_path = os.path.join(os.path.dirname(__file__), "JPEGImages") 9 | assert os.path.exists(JPEGImage_path), "{}不存在!".format(JPEGImage_path) 10 | Imageset_path = os.path.join(os.path.dirname(__file__), "Imagesets") 11 | assert os.path.exists(Imageset_path) 12 | 13 | list_a = os.listdir(annotation_path) 14 | list_j = os.listdir(JPEGImage_path) 15 | 16 | assert len(list_a) == len(list_j), "好好查查Annotations和JPEGImage文件夹下的数据是否对应!!!" 17 | 18 | l = [] 19 | for j in list_j: 20 | name = j.split(".")[0] 21 | l.append(name) 22 | 23 | #对l索引进行随机处理 24 | r = random.sample(l, len(l)) 25 | train_step = int(len(l) * 0.6) 26 | test_step = int(len(l) * 0.9) 27 | 28 | #train.txt 29 | tr = r[:train_step] 30 | with open(os.path.join(Imageset_path, "train.txt"), "w") as f_tr: 31 | for text in tr: 32 | f_tr.write(text + "\n") 33 | 34 | #test.txt 35 | te = r[train_step:test_step] 36 | with open(os.path.join(Imageset_path, "test.txt"), "w") as f_te: 37 | for text in te: 38 | f_te.write(text + "\n") 39 | 40 | #val.txt 41 | va = r[test_step:] 42 | with open(os.path.join(Imageset_path, "val.txt"), "w") as f_va: 43 | for text in va: 44 | f_va.write(text + "\n") 45 | 46 | print("Imageset完成!") -------------------------------------------------------------------------------- /wrape_white_pills.py: -------------------------------------------------------------------------------- 1 | # -*- encoding=utf-8 2 | import os 3 | import cv2 4 | 5 | 6 | def _get_write_from_path(): 7 | cur_dir = os.path.dirname(__file__) 8 | wp_path = os.path.join(cur_dir, "data", "origin_data", "white_pills") 9 | assert os.path.exists(wp_path), "{} not exists".format(wp_path) 10 | return wp_path 11 | 12 | def _get_write_to_path(): 13 | cur_dir = os.path.dirname(__file__) 14 | wt_path = os.path.join(cur_dir, "data", "white_pills") 15 | assert os.path.exists(wt_path), "{} not exists".format(wt_path) 16 | return wt_path 17 | 18 | def wrape_white_pills(): 19 | write_to_path = _get_write_to_path() 20 | write_from_path = _get_write_from_path() 21 | # 先判断是否完成标注转化,即有无ouput.txt文件 22 | output = os.path.join(write_to_path, "output.txt") 23 | if os.path.exists(output): 24 | print("{}存在,请先完成相应标注转化!".format(output)) 25 | return 26 | 27 | list_to_images = os.listdir(write_to_path) 28 | list_from_images = os.listdir(write_from_path) 29 | # 转化相应.bmp文件为.jpg 30 | print("此次从{}添加图像数量:{}".format(write_from_path, len(list_from_images))) 31 | for i in range(len(list_from_images)): 32 | # 读取源文件 33 | origin_image_ext = os.path.join(write_from_path, list_from_images[i]) 34 | origin_image = cv2.imread(origin_image_ext) 35 | # 写入目标文件 36 | target_image_ext = os.path.join(write_to_path, "{:06d}.jpg") 37 | target_image = origin_image[300:, 50:-50, 0:3] # 此处可以修改 38 | cv2.imwrite(target_image_ext.format(len(list_to_images) + i + 1), target_image) 39 | # 删除源文件 40 | os.remove(origin_image_ext) 41 | 42 | print("从{}转化完成!".format(write_from_path)) 43 | 44 | if __name__ == "__main__": 45 | pass 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /txt_to_dict.py: -------------------------------------------------------------------------------- 1 | #-*- encoding=utf-8 2 | import os 3 | import cv2 4 | 5 | def write_txt_to_dict(output_dir): 6 | output_path = os.path.join(output_dir, "output.txt") 7 | assert os.path.exists(output_path), "{}不存在!".format(output_path) 8 | 9 | all_dict = {} 10 | with open(output_path, "r") as f_txt: 11 | file_dict = {} 12 | for line in f_txt.readlines(): 13 | filename, checkwell, xmin, ymin, xmax, ymax = line.split() 14 | 15 | im_path = os.path.join(output_dir, filename) 16 | 17 | im = cv2.imread(im_path) 18 | im_height, im_width, im_depth = im.shape 19 | 20 | 21 | temp_dict = {} 22 | temp_dict["filename"] = filename 23 | temp_dict["size"] = {} 24 | temp_dict["size"]["width"] = im_width 25 | temp_dict["size"]["height"] = im_height 26 | temp_dict["size"]["depth"] = im_depth 27 | temp_dict["object"] = {} 28 | temp_dict["object"]["bndbox"] = [[xmin, ymin, xmax, ymax]] 29 | temp_dict["object"]["name"] = [output_dir.split("/")[-1].split("_")[0] + "_" + checkwell] 30 | temp_dict["object"]["difficult"] = [0] 31 | if not all_dict.has_key(filename): 32 | # 新建 33 | all_dict[filename] = temp_dict 34 | else: 35 | # 添加 36 | all_dict[filename]["object"]["bndbox"].append(temp_dict["object"]["bndbox"][0]) 37 | all_dict[filename]["object"]["name"].append(temp_dict["object"]["name"][0]) 38 | all_dict[filename]["object"]["difficult"].append(temp_dict["object"]["difficult"][0]) 39 | 40 | return all_dict -------------------------------------------------------------------------------- /wrape_blue_pills.py: -------------------------------------------------------------------------------- 1 | # -*- encoding=utf-8 2 | import os 3 | import cv2 4 | 5 | 6 | def _get_write_from_path(): 7 | cur_dir = os.path.dirname(__file__) 8 | wp_path = os.path.join(cur_dir, "data", "origin_data", "blue_pills") 9 | assert os.path.exists(wp_path), "{} not exists".format(wp_path) 10 | return wp_path 11 | 12 | 13 | def _get_write_to_path(): 14 | cur_dir = os.path.dirname(__file__) 15 | wt_path = os.path.join(cur_dir, "data", "blue_pills") 16 | assert os.path.exists(wt_path), "{} not exists".format(wt_path) 17 | return wt_path 18 | 19 | 20 | def wrape_blue_pills(): 21 | write_to_path = _get_write_to_path() 22 | write_from_path = _get_write_from_path() 23 | # 先判断是否完成标注转化,即有无ouput.txt文件 24 | output = os.path.join(write_to_path, "output.txt") 25 | if os.path.exists(output): 26 | print("{}存在,请先完成相应标注转化!".format(output)) 27 | return 28 | 29 | list_to_images = os.listdir(write_to_path) 30 | list_from_images = os.listdir(write_from_path) 31 | # 转化相应.bmp文件为.jpg 32 | print("此次从{}添加图像数量:{}".format(write_from_path, len(list_from_images))) 33 | for i in range(len(list_from_images)): 34 | # 读取源文件 35 | origin_image_ext = os.path.join(write_from_path, list_from_images[i]) 36 | origin_image = cv2.imread(origin_image_ext) 37 | # 写入目标文件 38 | target_image_ext = os.path.join(write_to_path, "{:06d}.jpg") 39 | target_image = origin_image[300:, 50:-50, 0:3] # 此处可以修改 40 | cv2.imwrite(target_image_ext.format(len(list_to_images) + i + 1), target_image) 41 | # 删除源文件 42 | os.remove(origin_image_ext) 43 | 44 | print("从{}转化完成!".format(write_from_path)) 45 | 46 | 47 | if __name__ == "__main__": 48 | pass 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /wrape_oill_pills.py: -------------------------------------------------------------------------------- 1 | # -*- encoding=utf-8 2 | import os 3 | import cv2 4 | 5 | 6 | def _get_write_from_path(): 7 | cur_dir = os.path.dirname(__file__) 8 | wp_path = os.path.join(cur_dir, "data", "origin_data", "oill_pills") 9 | assert os.path.exists(wp_path), "{} not exists".format(wp_path) 10 | return wp_path 11 | 12 | 13 | def _get_write_to_path(): 14 | cur_dir = os.path.dirname(__file__) 15 | wt_path = os.path.join(cur_dir, "data", "oill_pills") 16 | assert os.path.exists(wt_path), "{} not exists".format(wt_path) 17 | return wt_path 18 | 19 | 20 | def wrape_oill_pills(): 21 | write_to_path = _get_write_to_path() 22 | write_from_path = _get_write_from_path() 23 | # 先判断是否完成标注转化,即有无ouput.txt文件 24 | output = os.path.join(write_to_path, "output.txt") 25 | if os.path.exists(output): 26 | print("{}存在,请先完成相应标注转化!".format(output)) 27 | return 28 | 29 | list_to_images = os.listdir(write_to_path) 30 | list_from_images = os.listdir(write_from_path) 31 | # 转化相应.bmp文件为.jpg 32 | print("此次从{}添加图像数量:{}".format(write_from_path, len(list_from_images))) 33 | for i in range(len(list_from_images)): 34 | # 读取源文件 35 | origin_image_ext = os.path.join(write_from_path, list_from_images[i]) 36 | origin_image = cv2.imread(origin_image_ext) 37 | # 写入目标文件 38 | target_image_ext = os.path.join(write_to_path, "{:06d}.jpg") 39 | target_image = origin_image[300:, 50:-50, 0:3] # 此处可以修改 40 | cv2.imwrite(target_image_ext.format(len(list_to_images) + i + 1), target_image) 41 | # 删除源文件 42 | os.remove(origin_image_ext) 43 | 44 | print("从{}转化完成!".format(write_from_path)) 45 | 46 | 47 | if __name__ == "__main__": 48 | pass 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /wrape_yellow_pills.py: -------------------------------------------------------------------------------- 1 | # -*- encoding=utf-8 2 | import os 3 | import cv2 4 | 5 | 6 | def _get_write_from_path(): 7 | cur_dir = os.path.dirname(__file__) 8 | wp_path = os.path.join(cur_dir, "data", "origin_data", "yellow_pills") 9 | assert os.path.exists(wp_path), "{} not exists".format(wp_path) 10 | return wp_path 11 | 12 | 13 | def _get_write_to_path(): 14 | cur_dir = os.path.dirname(__file__) 15 | wt_path = os.path.join(cur_dir, "data", "yellow_pills") 16 | assert os.path.exists(wt_path), "{} not exists".format(wt_path) 17 | return wt_path 18 | 19 | 20 | def wrape_yellow_pills(): 21 | write_to_path = _get_write_to_path() 22 | write_from_path = _get_write_from_path() 23 | # 先判断是否完成标注转化,即有无ouput.txt文件 24 | output = os.path.join(write_to_path, "output.txt") 25 | if os.path.exists(output): 26 | print("{}存在,请先完成相应标注转化!".format(output)) 27 | return 28 | 29 | list_to_images = os.listdir(write_to_path) 30 | list_from_images = os.listdir(write_from_path) 31 | # 转化相应.bmp文件为.jpg 32 | print("此次从{}添加图像数量:{}".format(write_from_path, len(list_from_images))) 33 | for i in range(len(list_from_images)): 34 | # 读取源文件 35 | origin_image_ext = os.path.join(write_from_path, list_from_images[i]) 36 | origin_image = cv2.imread(origin_image_ext) 37 | # 写入目标文件 38 | target_image_ext = os.path.join(write_to_path, "{:06d}.jpg") 39 | target_image = origin_image[300:, 50:-50, 0:3] # 此处可以修改 40 | cv2.imwrite(target_image_ext.format(len(list_to_images) + i + 1), target_image) 41 | # 删除源文件 42 | os.remove(origin_image_ext) 43 | 44 | print("从{}转化完成!".format(write_from_path)) 45 | 46 | 47 | if __name__ == "__main__": 48 | pass 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /3_Annotation.py: -------------------------------------------------------------------------------- 1 | # -*- encoding=utf-8 2 | import os 3 | import wrape_white_pills as white 4 | import wrape_blue_pills as blue 5 | import wrape_yellow_pills as yellow 6 | import wrape_oill_pills as oill 7 | 8 | from txt_to_dict import write_txt_to_dict 9 | from dict_to_xml import write_dict_to_xml 10 | 11 | def process(path, annotation_path, JPEGImage_path): 12 | list_annotation = os.listdir(annotation_path) 13 | list_JPEGImage = os.listdir(JPEGImage_path) 14 | assert len(list_annotation) == len(list_JPEGImage),\ 15 | "查查{}和{}路径下的文件数是否相等!".format(annotation_path, JPEGImage_path) 16 | # annotation之前的文件数 17 | list_origin = os.listdir(path) 18 | origin_num_images = len(list_origin) - 1 19 | 20 | # 读取output.txt文件,变成dict字典 21 | all_dict = write_txt_to_dict(path) 22 | 23 | # 进行annotation和JPEGImage步骤 24 | for k, v in all_dict.items(): 25 | # 新加的文件序号得接在原来文件的序号后面 26 | k_int = int(k.split(".")[0]) + len(list_annotation) 27 | 28 | # 生成相应的xml文件到Imagesets目录 29 | filename_path = os.path.join(annotation_path, "{:06d}".format(k_int) + ".xml") 30 | v["filename"] = "{:06d}.jpg".format(k_int) 31 | write_dict_to_xml(v, filename_path) 32 | # 剪切相应的图像到JPEGImage目录,同时修改名字 33 | image_origin_filename = os.path.join(path, k) 34 | image_target_filename = os.path.join(JPEGImage_path, "{:06d}".format(k_int) + ".jpg") 35 | os.rename(image_origin_filename, image_target_filename) 36 | 37 | # 删除output.txt 38 | os.remove(os.path.join(path, "output.txt")) 39 | 40 | # 并将剩余图像重新排序 41 | # 注意 42 | list_last = os.listdir(path) 43 | last_num_images = len(list_last) 44 | 45 | cut_num_images = origin_num_images - last_num_images # 剪切走了的图像个数 46 | 47 | temp_list = [] 48 | for i in range(len(list_last)): 49 | temp_list.append("{:06d}.jpg".format(i+1+cut_num_images)) 50 | list_last = temp_list 51 | 52 | for i in range(len(list_last)): 53 | old_name_path = os.path.join(path, list_last[i]) 54 | new_name_path = os.path.join(path, "{:06d}.jpg".format(i+1)) 55 | os.rename(old_name_path, new_name_path) 56 | 57 | print("{} 注释完成!".format(path)) 58 | 59 | 60 | if __name__ == "__main__": 61 | #先判断annotation文件夹和JPFGImage文件夹下的文件数是否相等 62 | annotation_path = os.path.join(os.path.dirname(__file__), "Annotations") 63 | assert os.path.exists(annotation_path), "{}不存在!".format(annotation_path) 64 | JPEGImage_path = os.path.join(os.path.dirname(__file__), "JPEGImages") 65 | assert os.path.exists(JPEGImage_path), "{}不存在!".format(JPEGImage_path) 66 | 67 | 68 | 69 | # 获取相应的标注过的图像路径 70 | white_path = white._get_write_to_path() 71 | blue_path = blue._get_write_to_path() 72 | yellow_path = yellow._get_write_to_path() 73 | oill_path = oill._get_write_to_path() 74 | 75 | # 判断是否有需要annotation,即:有无output.txt文件 76 | if os.path.exists(os.path.join(white_path, "output.txt")): 77 | process(white_path, annotation_path, JPEGImage_path) 78 | else: 79 | print("{} 无标注文件!".format(white_path)) 80 | 81 | if os.path.exists(os.path.join(blue_path, "output.txt")): 82 | process(blue_path, annotation_path, JPEGImage_path) 83 | else: 84 | print("{} 无标注文件!".format(blue_path)) 85 | 86 | if os.path.exists(os.path.join(yellow_path, "output.txt")): 87 | process(yellow_path, annotation_path, JPEGImage_path) 88 | else: 89 | print("{} 无标注文件!".format(yellow_path)) 90 | 91 | if os.path.exists(os.path.join(oill_path, "output.txt")): 92 | process(oill_path, annotation_path, JPEGImage_path) 93 | else: 94 | print("{} 无标注文件!".format(oill_path)) 95 | 96 | print("annotation完成!") -------------------------------------------------------------------------------- /dict_to_xml.py: -------------------------------------------------------------------------------- 1 | # -*- encoding=utf-8 2 | from pprint import pprint 3 | from xml.dom.minidom import Document 4 | 5 | 6 | def write_dict_to_xml(mydict, filename): 7 | """ 8 | :param mydict: 需被转化的字典 9 | :param filename: 目标文件名 10 | :return: 11 | 用来完成从一个字典到.xml文件的转化,被调用时要注意配合使用,可修改! 12 | """ 13 | doc = Document() 14 | 15 | annotationlist = doc.createElement("annotation") 16 | 17 | doc.appendChild(annotationlist) 18 | 19 | for (k, v) in mydict.iteritems(): 20 | if k == "object": 21 | for i in range(len(v["bndbox"])): 22 | 23 | obj = doc.createElement("object") 24 | annotationlist.appendChild(obj) 25 | # *** 26 | bdb = doc.createElement("bndbox") 27 | obj.appendChild(bdb) 28 | 29 | x_min = doc.createElement("xmin") 30 | x_min_text = doc.createTextNode(str(v["bndbox"][i][0])) 31 | x_min.appendChild(x_min_text) 32 | bdb.appendChild(x_min) 33 | 34 | y_min = doc.createElement("ymin") 35 | y_min_text = doc.createTextNode(str(v["bndbox"][i][1])) 36 | y_min.appendChild(y_min_text) 37 | bdb.appendChild(y_min) 38 | 39 | x_max = doc.createElement("xmax") 40 | x_max_text = doc.createTextNode(str(v["bndbox"][i][2])) 41 | x_max.appendChild(x_max_text) 42 | bdb.appendChild(x_max) 43 | 44 | y_max = doc.createElement("ymax") 45 | y_max_text = doc.createTextNode(str(v["bndbox"][i][3])) 46 | y_max.appendChild(y_max_text) 47 | bdb.appendChild(y_max) 48 | # **** 49 | name = doc.createElement("name") 50 | name_text = doc.createTextNode(str(v["name"][i])) 51 | name.appendChild(name_text) 52 | obj.appendChild(name) 53 | 54 | diff = doc.createElement("difficult") 55 | diff_text = doc.createTextNode(str(0)) 56 | diff.appendChild(diff_text) 57 | obj.appendChild(diff) 58 | 59 | if k == "size": 60 | si = doc.createElement("size") 61 | annotationlist.appendChild(si) 62 | 63 | width = doc.createElement("width") 64 | width_text = doc.createTextNode(str(v["width"])) 65 | width.appendChild(width_text) 66 | si.appendChild(width) 67 | 68 | height = doc.createElement("height") 69 | height_text = doc.createTextNode(str(v["height"])) 70 | height.appendChild(height_text) 71 | si.appendChild(height) 72 | 73 | deep = doc.createElement("depth") 74 | deep_text = doc.createTextNode(str(v["depth"])) 75 | deep.appendChild(deep_text) 76 | si.appendChild(deep) 77 | 78 | if k=="filename": 79 | fn = doc.createElement("filename") 80 | fn_text = doc.createTextNode(str(v)) 81 | fn.appendChild(fn_text) 82 | annotationlist.appendChild(fn) 83 | 84 | with open(filename, "w") as f: 85 | f.write(doc.toprettyxml(indent="\t", encoding="utf-8")) 86 | 87 | if __name__ == "__main__": 88 | print("It's a test!") 89 | mydict = {"object":{"bndbox":[[1,2,4,3], [2,3,4,5]], 90 | "name":["chair", "horse"], 91 | "difficult":[0,0] 92 | }, 93 | "size":{"width":500, 94 | "height":375, 95 | "depth":3}, 96 | "owner":{"flickrid":"None", 97 | "name":"?"}, 98 | "source":{"database":"None", 99 | "annotation":"like_voc_2007"}, 100 | "filename":"00005.jpg" 101 | } 102 | pprint(mydict) 103 | write_dict_to_xml(mydict, "./001.xml") -------------------------------------------------------------------------------- /txt_to_xml.py: -------------------------------------------------------------------------------- 1 | # -*- encoding=utf-8 2 | import os 3 | import Tkinter 4 | import tkFileDialog 5 | import cv2 6 | import pprint 7 | from dict_to_xml import write_dict_to_xml 8 | 9 | def txt_to_xml(filename, target): 10 | target.delete(0.0, Tkinter.END) 11 | txt_dir = filename.get() 12 | if not os.path.exists(txt_dir): 13 | target.insert(0.0, "路径目录不存在:" + txt_dir) 14 | return 15 | 16 | xml_dir = os.path.join(txt_dir, "xml") 17 | if not os.path.exists(xml_dir): 18 | os.makedirs(xml_dir) 19 | 20 | target.insert(0.0, "正在转化...") 21 | #***************************************************************************** 22 | #此处根据情况修改txt到xml转化具体实现(取决于两者对接) 23 | all_dict = {} 24 | with open(txt_dir+"/output.txt", "r") as f_txt: 25 | file_dict = {} 26 | for line in f_txt.readlines(): 27 | filename, checkwell, xmin, ymin, xmax, ymax = line.split() 28 | 29 | im_path = os.path.join(txt_dir, filename) 30 | #print(im_path) 31 | im = cv2.imread(im_path) 32 | im_height, im_width, im_depth = im.shape 33 | #print(im.shape, checkwell, xmin, ymin, xmax, ymax) 34 | 35 | temp_dict = {} 36 | temp_dict["filename"] = filename 37 | temp_dict["size"] = {} 38 | temp_dict["size"]["width"] = im_width 39 | temp_dict["size"]["height"] = im_height 40 | temp_dict["size"]["depth"] = im_depth 41 | temp_dict["object"] = {} 42 | temp_dict["object"]["bndbox"] = [[xmin, ymin, xmax, ymax]] 43 | temp_dict["object"]["name"] = ["white_" + checkwell] 44 | temp_dict["object"]["difficult"] = [0] 45 | if not all_dict.has_key(filename): 46 | # 新建 47 | all_dict[filename] = temp_dict 48 | #print(temp_dict) 49 | else: 50 | #添加 51 | all_dict[filename]["object"]["bndbox"].append(temp_dict["object"]["bndbox"][0]) 52 | all_dict[filename]["object"]["name"].append(temp_dict["object"]["name"][0]) 53 | all_dict[filename]["object"]["difficult"].append(temp_dict["object"]["difficult"][0]) 54 | 55 | print(all_dict) 56 | for k, v in all_dict.items(): 57 | filenamepath = os.path.join(xml_dir, k.split(".")[0]+".xml") 58 | write_dict_to_xml(v, filenamepath) 59 | #************************************************************************** 60 | target.insert(1.0, "转化完成!") 61 | 62 | 63 | def xml_to_txt(filename, target): 64 | print(filename.get()) 65 | 66 | 67 | def check_dir(write_to, target): 68 | f = tkFileDialog.askdirectory() 69 | 70 | # 清空数据 71 | write_to.delete(0, Tkinter.END) 72 | target.delete(0.0, Tkinter.END) 73 | 74 | # 显示数据 75 | write_to.insert(0, f) 76 | target.insert(0.0, "readed dir: " + write_to.get()) 77 | 78 | 79 | 80 | if __name__ == "__main__": 81 | top = Tkinter.Tk() 82 | top.geometry("500x300") 83 | top.title("txt与xml转化demo") 84 | 85 | frame2 = Tkinter.Frame(top) 86 | multi_text = Tkinter.Text(frame2) 87 | multi_text.pack() 88 | frame2.grid(row=1, column=0) 89 | 90 | 91 | frame1 = Tkinter.Frame(top) 92 | 93 | frame_title1 = Tkinter.Frame(frame1) 94 | Tkinter.Label(frame_title1, text="txt所在目录").grid() 95 | Tkinter.Label(frame_title1, text="xml所在目录").grid() 96 | entry1 = Tkinter.Entry(frame_title1) 97 | entry2 = Tkinter.Entry(frame_title1) 98 | entry1.grid(row=0, column=1) 99 | entry2.grid(row=1, column=1) 100 | frame_title1.grid(row=0, column=0, sticky=Tkinter.E) 101 | 102 | frame_title2 = Tkinter.Frame(frame1) 103 | button_check1 = Tkinter.Button(frame_title2, text="浏览", command=lambda x=entry1, y=multi_text:check_dir(write_to=x, target=y)) 104 | button_check2 = Tkinter.Button(frame_title2, text="浏览", command=lambda x=entry2, y=multi_text:check_dir(write_to=x, target=y)) 105 | button_check1.grid(row=0, column=0) 106 | button_check2.grid(row=1, column=0) 107 | frame_title2.grid(row=0, column=1) 108 | 109 | frame_title3 = Tkinter.Frame(frame1) 110 | button_t_x = Tkinter.Button(frame_title3, text="txt to xml", command=lambda x=entry1, y=multi_text: txt_to_xml(filename=x, target=y)) 111 | button_x_t = Tkinter.Button(frame_title3, text="xml to txt", command=lambda x=entry2, y=multi_text: xml_to_txt(filename=x, target=y)) 112 | button_t_x.grid(row=0, column=0) 113 | button_x_t.grid(row=1, column=0) 114 | frame_title3.grid(row=0, column=2, sticky=Tkinter.W) 115 | 116 | frame1.grid(row=0, column=0) 117 | 118 | Tkinter.mainloop() -------------------------------------------------------------------------------- /2_标注.py: -------------------------------------------------------------------------------- 1 | # -*- encoding=utf-8 2 | import os 3 | import cv2 4 | import tkFileDialog 5 | import numpy as np 6 | 7 | g_bDrawingBox = False # 用来判断鼠标是否在按下时移动,从判断是否需要绘制矩形框 8 | g_mouse_event = False 9 | option = None # 标记退出选项 10 | my_dict = {} # 用于先存储数据 11 | temp_dict = {} 12 | box = [0, 0, 0, 0] 13 | image = None # 用于回调的图像 14 | 15 | 16 | def on_mouse_handle(event, x, y, flags, l_image): 17 | global g_bDrawingBox 18 | global g_mouse_event 19 | global box 20 | global image 21 | 22 | if event == cv2.EVENT_LBUTTONDOWN: 23 | g_mouse_event = True # 标记刚画完一个边框 24 | 25 | box[0] = x 26 | box[1] = y 27 | 28 | g_bDrawingBox = True 29 | #print("down:{},{}".format(box[0], box[1])) 30 | 31 | if event == cv2.EVENT_MOUSEMOVE: 32 | if g_bDrawingBox: 33 | # 用于显示绘图过程 34 | temp_image = image.copy() 35 | cv2.rectangle(temp_image, (box[0], box[1]), (x, y), (0, 0, 255), 2) 36 | cv2.imshow("image", temp_image) 37 | 38 | if event == cv2.EVENT_LBUTTONUP: 39 | g_bDrawingBox = False 40 | box[2] = x 41 | box[3] = y 42 | 43 | if box[0] < box[2] and box[1] > box[3]: 44 | box[1], box[3] = box[3], box[1] 45 | if box[0] > box[2] and box[1] < box[3]: 46 | box[0], box[2] = box[2], box[0] 47 | if box[0] > box[2] and box[1] > box[3]: 48 | box[0], box[2] = box[2], box[0] 49 | box[1], box[3] = box[3], box[1] 50 | 51 | if box[0] == box[2] or box[1] == box[3]: 52 | g_mouse_event = False 53 | return 54 | if (box[2]-box[0]) < 5 or (box[3]-box[1]) < 5: 55 | g_mouse_event = False 56 | return 57 | 58 | 59 | cv2.rectangle(image, (box[0], box[1]), (box[2], box[3]), (0, 255, 0), 2) 60 | #print("up:{},{}".format(box[2], box[3])) 61 | cv2.imshow("image", image) 62 | 63 | 64 | if __name__ == "__main__": 65 | # 选择要标注的图像路径 66 | image_path = tkFileDialog.askdirectory() 67 | try: 68 | image_path.split("/")[-1].split("_")[-1] == "pills" 69 | except: 70 | raise 71 | 72 | print(image_path) 73 | 74 | output_file = os.path.join(image_path, "output.txt") 75 | assert not os.path.exists(output_file), "请处理下output.txt,存在它说明你还没有进行Annotation!" 76 | list_files = os.listdir(image_path) 77 | 78 | # 显示图像,并标注 79 | image = None 80 | cv2.namedWindow("image", cv2.WINDOW_AUTOSIZE) # 按图像大小自动显示 81 | cv2.moveWindow("image", 100, 100) # 窗口显示在屏幕固定区域,方便标注 82 | cv2.setMouseCallback("image", on_mouse_handle, image) # 设置鼠标回调函数 83 | 84 | # 给图像标注有以下选项:1 n:下一张, ESC:退出, r:重绘 85 | # 这一步保证了图像是从000001索引按顺序开始进行标注的 86 | temp_list_files = [] 87 | for i in range(len(list_files)): 88 | temp_list_files.append("{:06d}.jpg".format(i+1)) 89 | list_files = temp_list_files[:] 90 | print(list_files) 91 | 92 | for i in range(len(list_files)): 93 | im_file = os.path.join(image_path, list_files[i]) 94 | im = cv2.imread(im_file) 95 | 96 | image = im.copy() # 复制一份图像 97 | cv2.imshow("image", image) 98 | 99 | while True: 100 | #该循环主要为标注矩形框负责,当绘制矩形框功能重置为False时,break 101 | if g_mouse_event == False: # 没开始画矩形框,或画完已正常处理了 102 | key = cv2.waitKey(1) 103 | if key == 27: 104 | option = "ESC" 105 | if temp_dict == {}: 106 | break 107 | else: 108 | my_dict[list_files[i]] = temp_dict[list_files[i]] 109 | temp_dict = {} 110 | break # 跳出while 111 | if key == ord("n"): 112 | if temp_dict == {}: 113 | option = "ESC" 114 | break 115 | else: 116 | my_dict[list_files[i]] = temp_dict[list_files[i]] 117 | temp_dict = {} 118 | break # 跳出while 119 | if key == ord("r"): 120 | temp_dict = {} 121 | image = im.copy() # 复制一份图像 122 | cv2.imshow("image", image) 123 | continue 124 | 125 | else: # 刚刚画完边框 126 | key = cv2.waitKey(0) # 画完边框则无限期等待处理,输入键值 127 | 128 | if key == ord("n"): 129 | print("else n") 130 | # 存入信息到my_dict 131 | if temp_dict == {}: 132 | option = "ESC" # 需要跳出for 133 | break 134 | else: 135 | my_dict[list_files[i]] = temp_dict[list_files[i]] 136 | temp_dict = {} 137 | break # 跳出while 138 | 139 | 140 | if key == ord("r"): 141 | temp_dict = {} 142 | image = im.copy() # 复制一份图像 143 | g_mouse_event = False 144 | print("else r") 145 | cv2.imshow("image", image) 146 | continue 147 | 148 | if key == 27: 149 | print("else Esc") 150 | if temp_dict == {}: 151 | option = "ESC" # 需要跳出for 152 | break 153 | else: 154 | my_dict[list_files[i]] = temp_dict[list_files[i]] 155 | temp_dict = {} 156 | option = "ESC" 157 | break # 跳出while 158 | 159 | # 输入有效键值了,存入信息到temp_dict 160 | if temp_dict == {}: 161 | temp_dict[list_files[i]] = [[chr(key), box[0], box[1], box[2], box[3]]] 162 | else: 163 | temp_dict[list_files[i]].append([chr(key), box[0], box[1], box[2], box[3]]) 164 | #print(key) 165 | # 打印当前图像名,边框名,边框 166 | print("{} {} [{:3d}, {:3d}, {:3d}, {:3d}]".format(list_files[i], chr(key), box[0], box[1], box[2], box[3])) 167 | 168 | g_mouse_event = False 169 | 170 | cv2.imshow("image", image) 171 | 172 | if option == "ESC": 173 | break 174 | 175 | 176 | with open(output_file, "w") as f: 177 | # 将my_dict中的数据写入到output.txt中 178 | for k, v in my_dict.items(): 179 | for l in range(len(v)): 180 | temp = "{} {} {} {} {} {}\n".format(k, v[l][0], v[l][1], v[l][2], v[l][3], v[l][4]) 181 | f.write(temp) 182 | 183 | if my_dict == {}: 184 | os.remove(output_file) 185 | 186 | --------------------------------------------------------------------------------