def _resize_sample(self, image, landmarks): image = resize_image(image, self.size) resized_landmarks = [] for l in landmarks: l = resize_image(l, self.size) resized_landmarks.append(l) landmarks = np.array(resized_landmarks) return image, landmarks
def convert_image_to_bcolz(pair_filename, image_dir, save_dir, input_size=[112, 112]): from torchvision import transforms as trans import bcolz transform = trans.Compose( [trans.ToTensor(), trans.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])]) if not os.path.exists(save_dir): os.makedirs(save_dir) faces_list1, faces_list2, issames_data = read_pair_data(pair_filename) print("have {} pair".format(len(issames_data))) print("have {} pair".format(len(faces_list1))) issames_data = np.array(issames_data) issames_data = np.where(issames_data > 0, True, False) data = bcolz.fill(shape=[ len(faces_list1) + len(faces_list2), 3, input_size[0], input_size[1] ], dtype=np.float32, rootdir=save_dir, mode='w') for i, (face1_path, face2_path, issame) in enumerate(zip(faces_list1, faces_list2, issames_data)): # pred_id, pred_scores = faceRec.predict(faces) # 或者使用get_faces_embedding()获得embedding,再比较compare_embedding() if image_dir: face1_path = os.path.join(image_dir, face1_path) face2_path = os.path.join(image_dir, face2_path) face1 = image_processing.read_image_gbk(face1_path, colorSpace="BGR") face2 = image_processing.read_image_gbk(face2_path, colorSpace="BGR") face1 = image_processing.resize_image(face1, resize_height=input_size[0], resize_width=input_size[1]) face2 = image_processing.resize_image(face2, resize_height=input_size[0], resize_width=input_size[1]) # img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) # image_processing.cv_show_image("image_dict",img) face1 = Image.fromarray(face1.astype(np.uint8)) face2 = Image.fromarray(face2.astype(np.uint8)) data[i * 2, ...] = transform(face1) data[i * 2 + 1, ...] = transform(face2) if i % 100 == 0: print('loading bin', i) print(data.shape) np.save(str(save_dir) + '_list', issames_data)
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 resize_heatmaps(heatmaps, size): resized_landmarks = [] for l in heatmaps: l = resize_image(l, size) resized_landmarks.append(l) heatmaps = np.array(resized_landmarks) return heatmaps
def transform_in_dir(self, img_dir, dest_dir, max_images=0, img_size=(512, 512)): ''' Read all images from img_dir, transform and write the result to dest_dir ''' os.makedirs(dest_dir, exist_ok=True) files = os.listdir(img_dir) files = [f for f in files if self.is_valid_file(f)] print(f'Found {len(files)} images in {img_dir}') if max_images: files = files[:max_images] for fname in tqdm(files): image = cv2.imread(os.path.join(img_dir, fname))[:, :, ::-1] image = resize_image(image) anime_img = self.transform(image)[0] ext = fname.split('.')[-1] fname = fname.replace(f'.{ext}', '') anime_img = denormalize_input(anime_img, dtype=np.int16) cv2.imwrite(os.path.join(dest_dir, f'{fname}_anime.jpg'), anime_img[..., ::-1])
def save_videos(self, color_img, align_color_img, depth_img, ir_frame, bboxes): """ (424, 512, 4) :param color_img: :param align_color_img: :param depth_img: :param ir_frame: :param bboxes: :return: """ if self.save_video and len(bboxes) > 0: color_img = image_processing.resize_image( color_img, resize_height=self.depth_height * self.scale) # <class 'tuple'>: (424, 753, 4) # <class 'tuple'>: (848, 1024, 3) color_img = image_processing.center_crop( color_img, crop_size=[ self.depth_height * self.scale, self.depth_width * self.scale ]) # image_processing.cv_show_image("color_img", color_img, waitKey=1) color_img = cv2.cvtColor(color_img, cv2.COLOR_RGBA2RGB) # 将BGR转为RGB self.video_writer.write(color_img)
def predict(self, image, model='Hayao'): transformer = Transformer(model) img = read_image(str(image)) anime_img = transformer.transform(resize_image(img))[0] anime_img = denormalize_input(anime_img, dtype=np.int16) out_path = Path(tempfile.mkdtemp()) / "out.png" cv2.imwrite(str(out_path), anime_img[..., ::-1]) return out_path
def predict_crf(model, model_path, image_dir, out_csv_path): resize_height = 50 resize_width = 50 threshold = 0.5 isShow = False # model = UNet(n_channels=3, n_classes=1).to(device) model.load_state_dict(torch.load(model_path)) model.to(device) model.eval() # image = Image.open(image_path) # test_transform = transforms.Compose([ # transforms.Resize(size=(resize_height, resize_width)), # transforms.ToTensor(), # 吧shape=(H,W,C)->换成shape=(C,H,W),并且归一化到[0.0, 1.0]的torch.FloatTensor类型 # ]) test_transform = transforms.Compose([ transforms.Resize(size=(resize_height, resize_width)), transforms.ToTensor(), # transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5]) ]) normalize_transform = transforms.Compose([ transforms.Normalize( (0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) # 给定均值(R,G,B) 方差(R,G,B),将会把Tensor正则化 ]) images_list = file_processing.get_images_list(image_dir, ['*.jpg', '*.png']) mask_code_list = [] for image_path in images_list: print("processing image:{}".format(image_path)) src_image = Image.open(image_path).convert('RGB') src_image = normalize_transform(src_image) re_image = src_image.resize((resize_width, resize_height)) # re_image=src_image src_image = np.asarray(src_image) pre_mask = predict_img(net=model, full_img=re_image, scale_factor=1.0, out_threshold=0.5, use_dense_crf=True, use_gpu=True) pre_mask = np.asarray(pre_mask + 0, dtype=np.float32) pre_mask = pre_mask.transpose(1, 0) #转换通道顺序 pre_mask = image_processing.resize_image(pre_mask, 101, 101) pre_mask = np.where(pre_mask > threshold, 1, 0) mask_code = get_rle_encode(pre_mask) # 编码 mask_code_list.append(mask_code) if isShow: mask_image = get_rle_decode(mask_code) # 解码显示 image_processing.show_image("src", src_image) image_processing.show_image("mask", pre_mask) # image_processing.show_image("mask_image", mask_image) save_submit(images_list, mask_code_list, out_csv_path)
def __call__(self, sample): """ :param image: numpy image :param annotations: (x1, x2, y1, y2, label) unscaled """ if isinstance(sample, dict): image = sample['image'] if 'annotations' in sample: annotations = sample['annotations'] d = max(image.shape) / self.size annotations[:, :4] /= d annotations = annotations.astype(int) sample['annotations'] = annotations image = resize_image(image, self.size) sample['image'] = image else: sample = resize_image(sample, self.size) return sample
def do_something(self, frame): frame = np.rot90(frame, -1) h, w, d = frame.shape resize_height = int(h / 2) frame = image_processing.resize_image(frame, resize_height=resize_height) bboxes_list = [[0, 150, w, h]] frame = image_processing.get_bboxes_image(frame, bboxes_list)[0] out_frame = np.asarray(frame) cv2.imshow("image", out_frame) # cv2.imwrite("image.png",out_frame) cv2.waitKey(3) return out_frame
def transform_file(self, file_path, save_path): if not save_path.endswith('png'): raise ValueError(f"{save_path} should be png format") image = read_image(file_path) if image is None: raise ValueError(f"Could not get image from {file_path}") anime_img = self.transform(resize_image(image))[0] anime_img = denormalize_input(anime_img, dtype=np.int16) cv2.imwrite(save_path, anime_img[..., ::-1]) print(f"Anime image saved to {save_path}")
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 predict(model, model_path, image_dir, out_csv_path): resize_height = 50 resize_width = 50 threshold = 0.5 isShow = False # model = UNet(n_channels=3, n_classes=1).to(device) model.load_state_dict(torch.load(model_path)) model.to(device) model.eval() # image = Image.open(image_path) # test_transform = transforms.Compose([ # transforms.Resize(size=(resize_height, resize_width)), # transforms.ToTensor(), # 吧shape=(H,W,C)->换成shape=(C,H,W),并且归一化到[0.0, 1.0]的torch.FloatTensor类型 # ]) test_transform = transforms.Compose([ transforms.Resize(size=(resize_height, resize_width)), transforms.ToTensor(), # transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) # 给定均值(R,G,B) 方差(R,G,B),将会把Tensor正则化 ]) images_list = file_processing.get_images_list(image_dir, ['*.jpg', '*.png']) mask_code_list = [] for image_path in images_list: print("processing image:{}".format(image_path)) image = Image.open(image_path).convert('RGB') image_tensor = test_transform(image).float() # Add an extra batch dimension since pytorch treats all images as batches image_tensor = image_tensor.unsqueeze_(0) image_tensor = image_tensor.to(device) # Turn the input into a Variable input = Variable(image_tensor) # Predict the class of the image output = model(input) output = output.cpu().data.numpy() #gpu:output.data.numpy() pre_mask = output[0, :] src_image = np.asarray(image) pre_mask = np.squeeze(pre_mask) pre_mask = pre_mask.transpose(1, 0) #转换通道顺序 pre_mask = image_processing.resize_image(pre_mask, 101, 101) pre_mask = np.where(pre_mask > threshold, 1, 0) mask_code = get_rle_encode(pre_mask) # 编码 mask_code_list.append(mask_code) if isShow: mask_image = get_rle_decode(mask_code) # 解码显示 image_processing.show_image("src", src_image) image_processing.show_image("mask", pre_mask) # image_processing.show_image("mask_image",mask_image) save_submit(images_list, mask_code_list, out_csv_path)
def extension_img(self, bg_list=None): person = Person() euc_dists = [] cos_dists = [] image_list = [] print('数据处理中,请稍等...') for image in bg_list: [w, h, c] = image.shape for scale in np.arange(0.4, 1.7, 0.6): bg_image = image_processing.resize_image( image, int(w * scale), int(h * scale)) for angle in np.arange(-30, 31, 15): rotate_bg_image = tools.rotate_bound(bg_image, angle) bboxes, landmarks = self.face_rect(rotate_bg_image) if len(bboxes) == 0: print("-----no face") else: new_images = image_processing.get_bboxes_image( rotate_bg_image, bboxes, landmarks, resize_width, resize_height) # for clipLimit in np.arange(0.5, 3, 0.5): new_image, bilateral_image = self.CLAHE(new_images[0], clipLimit=2) image_list.append(new_image) image_list.append(bilateral_image) new_clahe_image, clahe_bilateral_image = self.CLAHE( np.fliplr(new_images[0])) image_list.append(new_clahe_image) image_list.append(clahe_bilateral_image) cv2.imshow("789", clahe_bilateral_image) cv2.waitKey(1) image_emb = self.face_512_vector(image_list) face_data = image_emb.tolist() person.face_data = [{ 'yaw': '{}'.format(0), 'pitch': '{}'.format(0), 'face_data': face_data }] person.euc_dists = euc_dists person.cos_dists = cos_dists print('模型生成中,请稍等...') return person
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 detect_face(self, image, front_tf=0.1): image = cv2.cvtColor(image, cv2.COLOR_RGBA2RGB) bboxes, scores, landms = self.detector.detect(image) front = False for s in scores: if s > front_tf: front = True else: front = False break image = image_processing.draw_image_bboxes_text(image, bboxes, scores, color=(0, 0, 255)) image = image_processing.resize_image(image, resize_height=600) cv2.imshow("Det", image) cv2.waitKey(5) return front
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 load_bin(path, image_size=[112, 112]): ''' 加载人脸bin文件数据,bin_files = ['agedb_30', 'cfp_fp', 'lfw', 'calfw', 'cfp_ff', 'cplfw', 'vgg2_fp'] :param path: :param image_size: :return: ''' bins, issame_list = pickle.load(open(path, 'rb'), encoding='bytes') data = [] for i in range(len(bins)): _bin = bins[i] img = cv2.imdecode(_bin, -1) # opencv image is bgr img = image_processing.resize_image(img, resize_height=image_size[0], resize_width=image_size[1]) # image_processing.show_image("src",img) data.append(img) i += 1 if i % 1000 == 0: print('loading bin', i) data = np.array(data) issames = np.array(issame_list) return data, issames
def face_landmarks_forward(self, faces): input_tensor=[] for face in faces: height, width, depths = np.shape(face) xmin, ymin = 0, 0 # resize image for net inputs input_face = image_processing.resize_image(face, 48, 48) input_face = box_utils._preprocess(input_face) input_face = torch.FloatTensor(input_face) input_tensor.append(input_face) input_tensor = torch.cat(input_tensor) input_tensor = torch.FloatTensor(input_tensor).to(self.device) output = self.onet(input_tensor) landmarks = output[0].cpu().data.numpy() # shape [n_boxes, 10] # compute landmark points in src face landmarks[:, 0:5] = np.expand_dims(xmin, 1) + np.expand_dims(width, 1) * landmarks[:, 0:5] landmarks[:, 5:10] = np.expand_dims(ymin, 1) + np.expand_dims(height, 1) * landmarks[:, 5:10] # adapter landmarks landmarks_list = [] for landmark in landmarks: face_landmarks = [[landmark[j], landmark[j + 5]] for j in range(5)] landmarks_list.append(face_landmarks) return landmarks_list
while True: # Grab a single frame of video ret, frame = video_capture.read() # Only process every other frame of video to save time # if process_this_frame: hits = face_identifier.identify(frame) cache = unite_dicts(cache, hits) # Display the results for name, faces in cache.items(): if len(faces) == 5: locs, frames = zip(*faces) frames = np.array([pad_image(resize_image(f, 128), (128, 128))[0] for f in frames], dtype='uint8') result = liveness_detector.predict(frames) color = (0, 255, 0) if result[0] > 0.95 else (0, 0, 255) top, right, bottom, left = locs[-1] # Draw a box around the face cv2.rectangle(frame, (left, top), (right, bottom), color, 2) # Draw a label with a name below the face cv2.rectangle(frame, (left, bottom - 35), (right, bottom), color, cv2.FILLED) font = cv2.FONT_HERSHEY_DUPLEX cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1) cache[name].pop(0) # Display the resulting image cv2.imshow('Video', frame)
def process_image(image, target_size): image = image_processing.resize_image(image, target_size[0]) image = image_processing.pad_image(image, target_size)[0] image = image_processing.normalize_image(image) image = image.transpose([2, 0, 1]) return image
def random_resize(image, range_size): # random.seed(100) r = int(random.uniform(range_size[0], range_size[1])) # size = (r, r) image = image_processing.resize_image(image, resize_height=r, resize_width=r) return image
def _resize_sample(self, image, landmarks): image = resize_image(image, self.size) heatmaps = resize_heatmaps(landmarks, self.size) return image, heatmaps