def convert_facebody_to_textdataset(image_list, annotations_dir, label_out_dir, class_names, coordinatesType, show=False): ''' label data format: SSD = [label_id,x,y,w,h] YOLO = [label_id,x_center/img_width ,y_center/img_height ,width/img_width ,height/img_height] MMDET= [img_width,img_height,label_id,x,y,w,h] :param image_list: 图片列表 :param annotations_dir: 图片对应annotations所在目录 :param label_out_dir: label输出目录 :param class_names: :param coordinatesType: 坐标类型:SSD,YOLO,MMDET格式 :param show: 显示 :return: ''' if not os.path.exists(label_out_dir): os.makedirs(label_out_dir) name_id_list = [] nums = len(image_list) for i, image_path in enumerate(image_list): name_id = os.path.basename(image_path)[:-len(".jpg")] ann_name = name_id + '.json' annotations_file = os.path.join(annotations_dir, ann_name) if not os.path.exists(image_path): print("no image_dict:{}".format(image_path)) continue if not os.path.exists(annotations_file): print("no annotations:{}".format(annotations_file)) continue out_file = os.path.join(label_out_dir, name_id + ".txt") # rects, class_name, class_id = pascal_voc.get_annotation(annotations_file, class_names, coordinatesType) image = image_processing.read_image(image_path) image_shape = image.shape rects, class_name, class_id = face_body.get_annotation( annotations_file, class_names, image_shape, coordinatesType) if len(rects) == 0 or len(class_name) == 0 or len(class_id) == 0: print("no class in annotations:{}".format(annotations_file)) continue content_list = [[c] + r for c, r in zip(class_id, rects)] name_id_list.append(name_id) file_processing.write_data(out_file, content_list, mode='w') if show: image = image_processing.read_image(image_path) image_processing.show_image_rects_text("image_dict", image, rects, class_name) if i % 10 == 0 or i == len(image_list) - 1: print("processing image_dict:{}/{}".format(i, len(image_list) - 1)) return name_id_list
def create_face(images_dir, out_face_dir): ''' 生成人脸数据图库,保存在out_face_dir中,这些数据库将用于生成embedding数据库 :param images_dir: :param out_face_dir: :return: ''' # image_list=file_processing.get_files_list(images_dir, postfix='jpg') image_list, names_list = file_processing.gen_files_labels(images_dir, postfix='jpg') face_detect = face_recognition.Facedetection() for image_path, name in zip(image_list, names_list): image = image_processing.read_image(image_path, resize_height=0, resize_width=0, normalization=False) # 获取 判断标识 bounding_box crop_image bounding_box, points = face_detect.detect_face(image) bounding_box = bounding_box[:, 0:4].astype(int) bounding_box = bounding_box[0, :] print("face box:{}".format(bounding_box)) face_image = image_processing.crop_image(image, bounding_box) # image_processing.show_image("face", face_image) # image_processing.show_image_box("face",image,bounding_box) out_path = os.path.join(out_face_dir, name) face_image = image_processing.resize_image(face_image, resize_height, resize_width) if not os.path.exists(out_path): os.mkdir(out_path) basename = os.path.basename(image_path) out_path = os.path.join(out_path, basename) image_processing.save_image(out_path, face_image)
def demo_for_human36m(): joint_world = [[-91.679, 154.404, 907.261], [-223.23566, 163.80551, 890.5342], [-188.4703, 14.077106, 475.1688], [-261.84055, 186.55286, 61.438915], [39.877888, 145.00247, 923.98785], [-11.675994, 160.89919, 484.39148], [-51.550297, 220.14624, 35.834396], [-132.34781, 215.73018, 1128.8396], [-97.1674, 202.34435, 1383.1466], [-112.97073, 127.96946, 1477.4457], [-120.03289, 190.96477, 1573.4], [25.895456, 192.35947, 1296.1571], [107.10581, 116.050285, 1040.5062], [129.8381, -48.024918, 850.94806], [-230.36955, 203.17923, 1311.9639], [-315.40536, 164.55284, 1049.1747], [-350.77136, 43.442127, 831.3473], [-102.237045, 197.76935, 1304.0605]] joint_world = np.asarray(joint_world) # 关节点连接线 kps_lines = ((0, 7), (7, 8), (8, 9), (9, 10), (8, 11), (11, 12), (12, 13), (8, 14), (14, 15), (15, 16), (0, 1), (1, 2), (2, 3), (0, 4), (4, 5), (5, 6)) # show in 世界坐标系 vis.vis_3d(joint_world, kps_lines, coordinate="WC", title="WC", set_lim=True, isshow=True) kp_vis = CameraTools() # show in 相机坐标系 joint_cam = kp_vis.convert_wc_to_cc(joint_world) vis.vis_3d(joint_cam, kps_lines, coordinate="CC", title="CC", set_lim=True, isshow=True) joint_img = kp_vis.convert_cc_to_ic(joint_cam) joint_world1 = kp_vis.convert_cc_to_wc(joint_cam) vis.vis_3d(joint_world1, kps_lines, coordinate="WC", title="WC", set_lim=True, isshow=True) # show in 像素坐标系 kpt_2d = joint_img[:, 0:2] image_path = "./data/s_01_act_02_subact_01_ca_02_000001.jpg" image = image_processing.read_image(image_path) image = image_processing.draw_key_point_in_image(image, key_points=[kpt_2d], pointline=kps_lines) image_processing.cv_show_image("image_dict", image)
def face_body_test(annotations_dir, image_dir, class_names, show=True): ''' :param annotations_dir: :param image_dir: :param class_names: :param show: :return: ''' annotations_list = file_processing.get_files_list(annotations_dir, postfix=["*.json"]) print("have {} annotations files".format(len(annotations_list))) for i, annotations_file in enumerate(annotations_list): name_id = os.path.basename(annotations_file)[:-len(".json")] image_name = name_id + ".jpg" image_path = os.path.join(image_dir, image_name) if not os.path.exists(image_path): print("no image_dict:{}".format(image_path)) continue if not os.path.exists(annotations_file): print("no annotations:{}".format(annotations_file)) continue boxList = face_body.get_annotation(annotations_file, class_names) if not boxList: print("no class in annotations:{}".format(annotations_file)) continue if show: image = image_processing.read_image(image_path) # image_processing.show_image_rects_text("image_dict", image_dict, rects, class_name) image_processing.show_boxList("image_dict", boxList, image)
def convert2voc(data, image_dir, dst_dir): for index in range(data.shape[0]): print(index) for i in range(9): image_dir_list = os.path.join( image_dir + "0%d" % i, data.iloc[index, :]["ImageID"] + ".jpg") if os.path.isfile(image_dir_list): break print(image_dir_list) # if not os.path.isfile(os.path.join(image_dir ,data.iloc[index, :]["ImageID"] + ".jpg")): # print(" not exist!" ) # continue if not os.path.isfile(image_dir_list): continue # image_dict = image_processing.read_image(os.path.join(image_dir, data.iloc[index,:]["ImageID"]+".jpg")) image = image_processing.read_image(image_dir_list) print(data.iloc[index, :]["ImageID"]) x, y, w, h = (data.iloc[index, :]["x"] * image.shape[1], data.iloc[index, :]["y"] * image.shape[0], data.iloc[index, :]["width"] * image.shape[1], data.iloc[index, :]["height"] * image.shape[0]) label_path = os.path.join(label_dir, data.iloc[index, :]["ImageID"] + ".txt") if w * h < 1500: continue # with open(label_path, "a+") as f: f.write("2") f.write(" ") f.write("{:<.0f} {:<.0f} {:<.0f} {:<.0f}".format(x, y, w, h)) f.write("\n") # copy_file(data.iloc[index, :]["ImageID"]+".jpg", image_dir, dst_dir) copy_file(data.iloc[index, :]["ImageID"] + ".jpg", image_dir + "0%d" % i, dst_dir)
def pascal_voc_test(annotations_dir, image_dir, class_names, coordinatesType="SSD", show=True): ''' :param annotations_dir: :param image_dir: :param class_names: :param coordinatesType: :param show: :return: ''' annotations_list = file_processing.get_files_list(annotations_dir, postfix=["*.xml"]) print("have {} annotations files".format(len(annotations_list))) for i, annotations_file in enumerate(annotations_list): name_id = os.path.basename(annotations_file)[:-len(".xml")] image_name = name_id + ".jpg" image_path = os.path.join(image_dir, image_name) if not os.path.exists(image_path): print("no image_dict:{}".format(image_path)) continue if not os.path.exists(annotations_file): print("no annotations:{}".format(annotations_file)) continue rects, class_name, class_id = pascal_voc.get_annotation( annotations_file, class_names, coordinatesType) if len(rects) == 0 or len(class_name) == 0 or len(class_id) == 0: print("no class in annotations:{}".format(annotations_file)) if show: image = image_processing.read_image(image_path) image_processing.show_image_rects_text("image_dict", image, rects, class_name)
def face_recognition_image(model_path, dataset_path, filename, image_path): # 加载数据库的数据 dataset_emb, names_list = load_dataset(dataset_path, filename) # 初始化mtcnn人脸检测 face_detect = face_recognition.Facedetection() # 初始化facenet face_net = face_recognition.facenetEmbedding(model_path) image = image_processing.read_image(image_path) # 进行人脸检测,获得bounding_box bounding_box, points = face_detect.detect_face(image) bounding_box = bounding_box[:, 0:4].astype(int) # 获得人脸区域 face_images = image_processing.get_crop_images(image, bounding_box, resize_height, resize_width, whiten=True) # image_processing.show_image("face", face_images[0,:,:,:]) pred_emb = face_net.get_embedding(face_images) pred_name = compare_embadding(pred_emb, dataset_emb, names_list) # 在图像上绘制人脸边框和识别的结果 bgr_image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) image_processing.cv_show_image_text("face_recognition", bgr_image, bounding_box, pred_name) cv2.waitKey(0)
def get_train_generator(self): category_ids = np.pad(self.train_category_ids, [ 0, math.ceil(len(self.train_category_ids) / self.category_per_batch) * self.category_per_batch - len(self.train_category_ids) ], mode='wrap') while True: for i in range(0, len(category_ids), self.category_per_batch): annotations = [] start, end = i, i + self.category_per_batch for cat_id in category_ids[start:end]: num_samples = math.ceil(self.config.batch_size / self.category_per_batch) num_samples = num_samples if num_samples <= len( self.cat2ann[cat_id]) else len(self.cat2ann[cat_id]) annotations.extend( np.random.choice(self.cat2ann[cat_id], num_samples, replace=False)) images, labels = [], [] for ann in annotations[:self.config.batch_size]: image = read_image( os.path.join(self.config.image_dir, ann['image_file'])) image = self.augment_image(image) image = self.process_image(image) label = ann['category_id'] images.append(image) labels.append(label) yield np.array(images), np.array(labels) np.random.shuffle(category_ids)
def caffe2_predictor_v3(init_net_path, predict_net_path, image_dir, labels_filename): ''' https://github.com/caffe2/tutorials/blob/master/Loading_Pretrained_Models.ipynb :param init_net_path: :param predict_net_path: :param image_dir: :param labels_filename: :return: ''' resize_height = 224 resize_width = 224 labels = np.loadtxt(labels_filename, str, delimiter='\t') test_transform = transforms.Compose([ transforms.Resize(size=(resize_height, resize_width)), transforms.ToTensor(), # transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) ]) # Read the contents of the input protobufs into local variables with open(init_net_path, "rb") as f: init_net = f.read() with open(predict_net_path, "rb") as f: predict_net = f.read() predict_def = caffe2_pb2.NetDef() predict_def.ParseFromString(predict_net) print(net_printer.to_string(predict_def)) # 加载图像 # workspace.RunNetOnce(init_net) # workspace.CreateNet(predict_net) p = workspace.Predictor(init_net, predict_net) images_list = glob.glob(os.path.join(image_dir, '*.jpg')) for image_path in images_list: print("--------------------------------------") image = image_processing.read_image(image_path, resize_height, resize_width, normalization=True) # Add an extra batch dimension since pytorch treats all images as batches image = image.transpose(2, 0, 1) # 通道由[h,w,c]->[c,h,w] input = image[np.newaxis, :] print("input.shape:{}".format(input.shape)) output = p.run([input]) output = np.asarray(output) output = np.squeeze(output, axis=(0, )) print(output) # print("output shape: ", output.shape) pre_score = fun.softmax(output, axis=1) pre_index = np.argmax(pre_score, axis=1) max_score = pre_score[:, pre_index] pre_label = labels[pre_index] print("{} is: pre labels:{},name:{} score: {}".format( image_path, pre_index, pre_label, max_score))
def convert_voc_to_linedataset(annotations_dir, image_list, class_names, out_filename, coordinatesType, show=True): ''' label data format: SSD = [label_id,x,y,w,h] YOLO = [label_id,x_center/img_width ,y_center/img_height ,width/img_width ,height/img_height] MMDET= [img_width,img_height,label_id,x,y,w,h] line_image_label:[image_path,boxes_nums,x1, y1, w, h, label_id,x1, y1, w, h, label_id,...] :param annotations_dir: 图片对应annotations所在目录 :param image_list: 图片列表 :param class_names: :param out_filename :param coordinatesType: 坐标类型:SSD,YOLO,MMDET格式 :param show: 显示 :return: ''' with open(out_filename, mode='w', encoding='utf-8') as f: for i, image_path in enumerate(image_list): image_name = os.path.basename(image_path) name_id = image_name[:-len(".jpg")] ann_name = name_id + '.xml' annotations_file = os.path.join(annotations_dir, ann_name) if not os.path.exists(image_path): print("no image_dict:{}".format(image_path)) continue if not os.path.exists(annotations_file): print("no annotations:{}".format(annotations_file)) continue rects, class_name, class_id = pascal_voc.get_annotation( annotations_file, class_names, minAreaTH=0, coordinatesType=coordinatesType) if len(rects) == 0 or len(class_name) == 0 or len(class_id) == 0: print("no class in annotations:{}".format(annotations_file)) continue line_image_label = pascal_voc.convert_to_linedataset( image_name, rects, class_id) # contents = face_body_tools.convert_pyramidbox_data(image_path, boxList, classes) contents_line = " ".join('%s' % id for id in line_image_label) f.write(contents_line + "\n") if show: image = image_processing.read_image(image_path) image_processing.show_image_rects_text("image_dict", image, rects, class_name) if i % 10 == 0 or i == len(image_list) - 1: print("processing image_dict:{}/{}".format( i, len(image_list) - 1))
def label_test(image_dir, filename, class_names): basename = os.path.basename(filename)[:-len('.txt')] + ".jpg" image_path = os.path.join(image_dir, basename) image = image_processing.read_image(image_path) data = file_processing.read_data(filename, split=" ") label_list, rect_list = file_processing.split_list(data, split_index=1) label_list = [l[0] for l in label_list] name_list = file_processing.decode_label(label_list, class_names) image_processing.show_image_rects_text("object2", image, rect_list, name_list)
def convert_images_format(image_dir, out_dir): image_list = file_processing.get_files_list(image_dir, postfix=["*.bmp"]) for image_path in image_list: image = image_processing.read_image(image_path) basename = os.path.basename(image_path).replace("bmp", "jpg") # dest_path=os.path.join(out_dir,basename) dest_path = file_processing.create_dir(out_dir, dir1=None, filename=basename) image_processing.save_image(dest_path, image, toUINT8=False)
def predict(models_path, image_dir, image_height, image_width, depth, char_set, captcha_size): ''' :param models_path: :param image_dir: :param image_height: :param image_width: :param depth: :param char_set: :param captcha_size: :return: ''' char_set_len = len(char_set) #字符种类个数 X = tf.placeholder(tf.float32, [None, image_height, image_width, depth]) keep_prob = 1.0 reg = None # output = net.simple_net(inputs=X, captcha_size=captcha_size, char_set_len=char_set_len,keep_prob=keep_prob)#learning_rate=0.001#学习率 output, end_points = net.multilabel_nets(inputs=X, captcha_size=captcha_size, char_set_len=char_set_len, keep_prob=keep_prob, is_training=False, reg=reg) print("output.shape={}".format(output.get_shape())) sess = tf.InteractiveSession() sess.run(tf.global_variables_initializer()) saver = tf.train.Saver() saver.restore(sess, models_path) images_list = glob.glob(os.path.join(image_dir, '*.jpg')) for image_path in images_list: im = image_processing.read_image(image_path, image_height, image_width, normalization=True) im = im[np.newaxis, :] # pred = sess.run(f_cls, feed_dict={x:im, keep_prob:1.0}) pre_output = sess.run([output], feed_dict={X: im}) predict = np.reshape(pre_output, newshape=[-1, captcha_size, char_set_len]) predict = softmax(predict, axis=1) # max_score:[[0.999864 0.5114571 0.9802696 0.4393546]] max_idx_pre = np.argmax(predict, 2) max_idx_pre = np.squeeze(max_idx_pre) label_name = [] for index in max_idx_pre: label_name += char_set[index] max_score = np.max(predict, axis=2) # print("{}:predict:{},max_idx_p:{},max_score:{},label_name:{}".format(image_path, predict,max_idx_pre,max_score,label_name)) print("{},max_idx_p:{},max_score:{},label_name:{}".format( image_path, max_idx_pre, max_score, label_name)) sess.close()
def random_resize_image_list(image_dir, out_dir, range_size): image_list, label_list = file_processing.get_files_labels(image_dir, postfix=['*.jpg']) print("hace {} images".format(len(image_list))) for image_path, lable in zip(image_list, label_list): basename = os.path.basename(image_path) image = image_processing.read_image(image_path) re_image = random_resize(image, range_size) print("image_dict shape:{}".format(re_image.shape)) # out_path = os.path.join(out_dir, lable, basename) out_path = file_processing.create_dir(out_dir, lable, basename) image_processing.save_image(out_path, re_image)
def fun_test(images_list): time.sleep(2) print(images_list) images = [] for filename in images_list: image = image_processing.read_image(filename, resize_height=224, resize_width=224, normalization=False) images.append(image) return images
def load_data(self, path, resize_height, resize_width, normalization): ''' 加载数据 :param path: :param resize_height: :param resize_width: :param normalization: 是否归一化 :return: ''' image = image_processing.read_image(path, resize_height, resize_width, normalization) return image
def get_reference_data(self): images, labels = [], [] for category in self.categories: image = read_image( os.path.join(self.config.image_dir, category['reference_image'])) image = self.process_image(image) label = category['id'] images.append(image) labels.append(label) return np.array(images), np.array(labels)
def resize_image_list(image_dir, out_dir, resize_height=None, resize_width=None): image_list, label_list = file_processing.get_files_labels(image_dir, postfix=['*.jpg',"*.JPG"]) print("hace {} images".format(len(image_list))) for image_path, lable in zip(image_list, label_list): basename = os.path.basename(image_path) image = image_processing.read_image(image_path, resize_height=resize_height, resize_width=resize_width) print("image_dict shape:{}".format(image.shape)) # out_path = os.path.join(out_dir, lable, basename) out_path = file_processing.create_dir(out_dir, lable, basename) image_processing.save_image(out_path, image)
def face_recognition_image(face_detect, face_net, compared_face_path, idcard_face_path): # 初始化mtcnn人脸检测 # face_detect = face_recognition.Facedetection() # 初始化facenet # face_net = face_recognition.facenetEmbedding(model_path) # 人脸数据是否有效 valid = False compared_face = image_processing.read_image(compared_face_path) # 获取 判断标识 bounding_box crop_image bboxes, landmarks = face_detect.detect_face(compared_face) bboxes, landmarks = face_detect.get_square_bboxes(bboxes, landmarks, fixed="height") if bboxes == [] or landmarks == []: return valid, '未检测到人脸' elif len(bboxes) > 1: return valid, '检测到多张人脸' compared_face_images = image_processing.get_bboxes_image(compared_face, bboxes, resize_height, resize_width) compared_face_images = image_processing.get_prewhiten_images(compared_face_images) compared_face_emb = face_net.get_embedding(compared_face_images) idcard_face = image_processing.read_image(idcard_face_path) # 获取 判断标识 bounding_box crop_image bboxes, landmarks = face_detect.detect_face(idcard_face) bboxes, landmarks = face_detect.get_square_bboxes(bboxes, landmarks, fixed="height") if bboxes == [] or landmarks == []: return valid, '证件人脸识别错误,请重新上传' elif len(bboxes) > 1: return valid, '证件人脸识别错误,请重新上传' valid = True idcard_face_images = image_processing.get_bboxes_image(idcard_face, bboxes, resize_height, resize_width) idcard_face_images = image_processing.get_prewhiten_images(idcard_face_images) idcard_face_emb = face_net.get_embedding(idcard_face_images) compare_res = compare_embadding(compared_face_emb, idcard_face_emb) return valid, compare_res
def convert_images_dirs_format(src_image_dir, out_dir): image_list, image_label = file_processing.get_files_labels( src_image_dir, postfix=["*.bmp"]) for i, (image_path, label) in enumerate(zip(image_list, image_label)): image = image_processing.read_image(image_path) basename = os.path.basename(image_path).replace("bmp", "jpg") # dest_path=os.path.join(out_dir,basename) dest_path = file_processing.create_dir(out_dir, dir1=label, filename=basename) image_processing.save_image(dest_path, image, toUINT8=False) if i % 100 == 0 or i == len(image_list) - 1: print("processing:{}/{}".format(i, len(image_list)))
def create_face(images_dir, out_face_dir): print(images_dir) ''' 生成人脸数据图库,保存在out_face_dir中,这些数据库将用于生成embedding数据库 :param images_dir: :param out_face_dir: :return: ''' # image_list=file_processing.get_files_list(images_dir, postfix='jpg') image_list, names_list = file_processing.gen_files_labels(images_dir) face_detect = face_recognition.Facedetection() for image_path, name in zip(image_list, names_list): print("图片的路径", str(image_path)) if image_path == 'dataset/images/.DS_Store': continue if image_path == 'dataset/images/face1/.DS_Store': continue if image_path == 'dataset/images/face2/.DS_Store': continue if image_path == 'dataset/images/face3/.DS_Store': continue if image_path == 'dataset/images/face4/.DS_Store': continue if image_path == './dataset/emb_face/.DS_Store': continue image = image_processing.read_image(image_path, resize_height=0, resize_width=0, normalization=False) # 获取 判断标识 bounding_box crop_image bounding_box, points = face_detect.detect_face(image) bounding_box = bounding_box[:, 0:4].astype(int) if (len(bounding_box) < 1): continue bounding_box = bounding_box[0, :] print("face box:{}".format(bounding_box)) face_image = image_processing.crop_image(image, bounding_box) # image_processing.show_image("face", face_image) # image_processing.show_image_box("face",image,bounding_box) out_path = os.path.join(out_face_dir, name) if (len(face_image) > 0): face_image = image_processing.resize_image(face_image, resize_height, resize_width) else: continue if not os.path.exists(out_path): os.mkdir(out_path) basename = os.path.basename(image_path) out_path = os.path.join(out_path, basename) image_processing.save_image(out_path, face_image)
def create_face(images_dir, out_face_dir): ''' 生成人脸数据图库,保存在out_face_dir中,这些数据库将用于生成embedding数据库 ''' #此处引用自定义函数 : file_processing.gen_files_labels(images_dir,postfix='jpg') #返回值: 图像数组: images_dir下所有文件(是的,你没想错!包括子目录下的所有文件)的路径(Path)会被存储到image_list # 标签数组: names_list就是相应的的"文件名或子目录名",在本项目中,子目录名 将作为 作为样本的标签(即ID,e.g.姓名 或 学号之类的) print('#2nd--Mtcnn人脸库') image_list,names_list=file_processing.gen_files_labels(images_dir,postfix='jpg') face_detect=face_recognition.Facedetection() for image_path ,name in zip(image_list,names_list): # try: image=image_processing.read_image(image_path, resize_height=0, resize_width=0, normalization=False) # 获取 判断标识 bounding_box crop_image bounding_box, points = face_detect.detect_face(image) bounding_box = bounding_box[:,0:4].astype(int) # try: bounding_box=bounding_box[0,:] # except: # print("跳过当前图片") # continue print("矩形框坐标为:{}".format(bounding_box)) face_image = image_processing.crop_image(image,bounding_box) # except: # print("当前图像帧格式非法,将被忽略") out_path=os.path.join(out_face_dir,name) face_image=image_processing.resize_image(face_image, resize_height, resize_width) if not os.path.exists(out_path): os.mkdir(out_path) basename=os.path.basename(image_path) out_path=os.path.join(out_path,basename) image_processing.save_image(out_path,face_image)
def convert_annotation_list(annotations_list, image_dir, label_out_dir, class_names, image_type='.jpg', show=True): ''' :param annotations_list:annotations列表 :param image_dir:图片所在路径 :param label_out_dir:输出label目录 :param class_names: :param image_type:图片的类型,如.jpg ,.png :param show: :return: ''' if not os.path.exists(label_out_dir): os.makedirs(label_out_dir) name_id_list = [] nums = len(annotations_list) for i, annotations_file in enumerate(annotations_list): name_id = os.path.basename(annotations_file)[:-len(".xml")] image_name = name_id + image_type image_path = os.path.join(image_dir, image_name) if not os.path.exists(image_path): print("no image:{}".format(image_path)) continue if not os.path.exists(annotations_file): print("no annotations:{}".format(annotations_file)) continue out_file = os.path.join(label_out_dir, name_id + ".txt") rects, class_name, class_id = pascal_voc.get_annotation( annotations_file, class_names) content_list = [[c] + r for c, r in zip(class_id, rects)] name_id_list.append(name_id) file_processing.write_data(out_file, content_list, mode='w') if show: image = image_processing.read_image(image_path) image_processing.show_image_rects_text("image", image, rects, class_name) if i % 100 == 0 or i == nums - 1: print("processing {}/{}".format(i + 1, nums)) return name_id_list
def linedataset_test(filename, image_dir=None, show=True): ''' label_data:[image_path,boxes_nums,x1, y1, w, h, label_id,x1, y1, w, h, label_id] :param filename: :param image_dir: :param show: :return: ''' with open(filename) as f: lines = f.readlines() for line in lines: image_id, box, label = face_body.read_line_image_label(line) if show: if image_dir: image_path = os.path.join(image_dir, image_id) else: image_path = image_id image = image_processing.read_image(image_path) image_processing.show_image_bboxes_text( "image_dict", image, box, label)
def label_test(image_dir, filename, class_names=None): basename = os.path.basename(filename)[:-len('.txt')] + ".jpg" image_path = os.path.join(image_dir, basename) image = image_processing.read_image(image_path) data = file_processing.read_data(filename, split=" ") label_list, rect_list = file_processing.split_list(data, split_index=1) label_list = [l[0] for l in label_list] if class_names: name_list = file_processing.decode_label(label_list, class_names) else: name_list = label_list show_info = ["id:" + str(n) for n in name_list] rgb_image = image_processing.show_image_rects_text("object2", image, rect_list, show_info, color=(0, 0, 255), drawType="custom", waitKey=1) rgb_image = image_processing.resize_image(rgb_image, 900) image_processing.cv_show_image("object2", rgb_image)
def decode_voc(self, vis=True): """ :return: """ for xml_file in self.xml_list: anns = CustomVoc.get_annotation(xml_file) filename = anns["image"] object = anns["object"] keypoints = [] bboxes = [] class_name = [] for item in object: joint = item["keypoints"] joint = np.asarray(joint).reshape(17, 3) joint = joint[:, 0:2] keypoints.append(joint.tolist()) bboxes.append(item["bbox"]) class_name.append(item["class_name"]) image_path = os.path.join(self.image_dir, filename) image = image_processing.read_image(image_path) self.show(filename, image, keypoints, bboxes, class_name, vis=True)
def get_val_generator(self): annotations = [ ann for ann in self.annotations if ann['category_id'] in self.val_category_ids ] while True: for i in range(0, len(annotations), self.config.batch_size): try: batch = annotations[i:i + self.config.batch_size] except IndexError: batch = annotations[i:] images, labels = [], [] for ann in batch: image = read_image( os.path.join(self.config.image_dir, ann['image_file'])) image = self.process_image(image) label = ann['category_id'] images.append(image) labels.append(label) yield np.array(images), np.array(labels)
def demo_for_human36m(): from modules.utils_3d.data import human36m_data # x,y,z # joint_world = human36m_data.data0 joint_world = human36m_data.data2 * 1000 # joint_world = human36m_data.data1*1000 joint_world = np.asarray(joint_world) kps_lines = human36m_data.kps_lines # show in 世界坐标系 vis.vis_3d(joint_world, kps_lines, coordinate="WC", title="WC", set_lim=True) kp_vis = KeyPointsVisual() # show in 相机坐标系 joint_cam = kp_vis.convert_wc_to_cc(joint_world) vis.vis_3d(joint_cam, kps_lines, coordinate="CC", title="CC", set_lim=True) joint_img = kp_vis.convert_cc_to_ic(joint_cam) # show in 像素坐标系 kpt_2d = joint_img[:, 0:2] image_path = "/media/dm/dm1/git/python-learning-notes/modules/utils_3d/data/s_01_act_02_subact_01_ca_02_000001.jpg" image = image_processing.read_image(image_path) image = image_processing.draw_key_point_in_image(image, key_points=[kpt_2d], pointline=kps_lines) image_processing.cv_show_image("image_dict", image)
def deme_test(self, out_voc_ann=None, vis=True): joints_bbox = [] for lines in self.boxes_label_lists: # lines = ['lexue_teacher_LHui211_20190921145000_20190921172000_40_000143.jpg', 543.053254438, 527.147928994, # 456.710059172, 548.733727811, 'person'] image_name = lines[0] rect = [lines[1], lines[2], lines[3], lines[4]] label = [lines[5]] image_path = os.path.join(self.image_dir, image_name) json_file = image_name[:-len(".jpg")] + ".json" json_path = os.path.join(self.json_dir, json_file) if not os.path.exists(image_path): print("Error:no path: {}".format(json_file)) continue if not os.path.exists(json_path): print("Error:no path: {}".format(json_file)) continue anno = CustomDataset.get_anno(json_path) if not anno: print("Error:empty path: {}".format(json_file)) continue keypoints = self.get_keypoints(anno) sum = np.sum(np.abs(np.asarray(keypoints))) if keypoints == [] or sum == 0: print("Error:empty path: {}".format(json_file)) continue image = image_processing.read_image(image_path) if out_voc_ann: try: bboxes = image_processing.rects2bboxes([rect]) joints_bbox = self.convert_voc_dataset( image_name, image_shape=image.shape, bboxes=bboxes, labels=label, keypoints=keypoints, out_voc_ann=out_voc_ann) except Exception as e: print("Error:empty path: {}".format(json_file)) raise Exception("lines: {}".format(lines)) save_image = True for i, joints in enumerate(keypoints): if np.sum(np.asarray(joints[5])) == 0 or np.sum(np.asarray(joints[6])) == 0 or \ np.sum(np.asarray(joints[11])) == 0 or np.sum(np.asarray(joints[12]))==0: save_image = False else: save_image = True chest_joint = (np.asarray(joints[5]) + np.asarray(joints[6])) / 2 hip_joint = (np.asarray(joints[11]) + np.asarray(joints[12])) / 2 keypoints[i].append(chest_joint.tolist()) keypoints[i].append(hip_joint.tolist()) if vis: image_processing.show_image_rects(None, image, [rect]) # image_processing.show_image_boxes(None, image, joints_bbox, color=(255, 0, 0)) image = image_processing.draw_key_point_in_image( image, keypoints, pointline=skeleton) image_processing.cv_show_image("Det", image, waitKey=1) if save_image: out_dir = "/media/dm/dm2/project/dataset/COCO/HumanPose/LeXue_teacher/Posture/tmp1" out_dir = file_processing.create_dir(out_dir) out_image_path = os.path.join(out_dir, image_name) image_processing.save_image(out_image_path, image)
landmarks_list = [] for landmark in landmarks: face_landmarks = [[landmark[j], landmark[j + 5]] for j in range(5)] landmarks_list.append(face_landmarks) landmarks = np.asarray(landmarks_list) return landmarks if __name__ == "__main__": # img = Image.open('some_img.jpg') # modify the image path to yours # bounding_boxes, landmarks = detect_faces(img) # detect bboxes and landmarks for all faces in the image # show_results(img, bounding_boxes, landmarks) # visualize the results # image_path = "/media/dm/dm/project/dataset/face_recognition/NVR/JPEGImages/2000.jpg" image_path = "/media/dm/dm1/FaceRecognition/torch-Face-Recognize-Pipeline/data/dataset2/zhoujielun/zhoujielun_1.jpg" image = image_processing.read_image(image_path, colorSpace="RGB") mt = MTCNN() bbox_score, landmarks = mt.detect(image) bboxes, scores, landmarks = mt.adapter_bbox_score_landmarks(bbox_score, landmarks) # image_processing.show_image_boxes("image",image,bboxes) # image_processing.show_landmark_boxes("image", image, landmarks, bboxes) # image_processing.show_landmark_boxes("image2", image, landmarks, bboxes) faces = image_processing.get_bboxes_image(image, bboxes) # landmarks2 = mt.landmarks_forward(bbox_score, image) # bboxes, scores, landmarks2 = mt.adapter_bbox_score_landmarks(bbox_score, landmarks2) # image_processing.show_landmark_boxes("image2", image, landmarks2, bboxes) for face in faces: image_processing.cv_show_image("face", face) image_processing.show_landmark_boxes("image", image, landmarks, bboxes) landmarks = mt.face_landmarks_forward([face])