def proc_label(): label_list = file_io.get_listfile(ucsd_label_path, label_ext) for l_name in label_list: mat = sio.loadmat(l_name) desmap = mat["gtDensities"] desmap = desmap.astype(np.float32) new_label_name = l_name.replace(label_ext, new_label_ext) desmap.tofile(new_label_name)
def sample(avid_dir_list): avi_dir_list = file_io.get_dir_list(avid_dir_list) for avi_dir in avi_dir_list: avi_file_list = file_io.get_listfile(avi_dir, ".avi") avi_file_list.sort() for avi in avi_file_list: image_dir = avi.replace(".avi", "") command = "ffmpeg -i " + avi + " " + image_dir + "/%06d.jpg" os.system(command)
if __name__ == "__main__": if not len(sys.argv) == 3: print("Usage: label_preprocess.py data_dir size_len") exit(1) data_dir = sys.argv[1] size_len = int(sys.argv[2]) global dsize dsize = (size_len, size_len) mask_dir_list = file_io.get_dir_list(data_dir) for mask_dir in mask_dir_list: mask_list = file_io.get_listfile(mask_dir, ".msk") for mask in mask_list: image_dir_name = mask.replace(".msk", "") image_list = file_io.get_listfile(image_dir_name, "jpg") mask_name = mask.replace(".msk", ".png") mask_bbox = load_mask(mask_name) # if mask == "../data/data_new/Training_Data/Cam181/01.msk": # mask_bin = cv2.imread(mask_dir + "/01_mask.jpg") # mask_bin = mask_bin[:,:,1] # mask_bin /= 255 # else: # try: # mask_pts = load_mask(mask) # mask_bin = gen_mask_image(mask_pts) # except: # mask_pts = load_mask(mask)
im_color = cv2.applyColorMap(desmap, cv2.COLORMAP_JET) return im_color def cen_crop(input_image, dsize): h, w, c = input_image.shape offset_h = int((h - dsize[0]) / 2) offset_w = int((w - dsize[1]) / 2) input_image = input_image[offset_h:offset_h + dsize[0], offset_w:offset_w + dsize[1], :] return input_image ucsd_dir = "/media/dog/data/UCSD/images/" desmap_list = file_io.get_listfile(ucsd_dir, "infer_desmap") mask_name = "/media/dog/data/UCSD/Mask/mask227.npy" mask = np.fromfile(mask_name, np.float32) mask = np.expand_dims(np.reshape(mask, (227, 227)), 2) mask = cen_crop(mask, (224, 224)) mask = np.squeeze(mask) for des_name in desmap_list: desmap = opencv_plot(des_name, mask) img_name = des_name.replace(".infer_desmap", ".jpg") img = cv2.imread(img_name) img = cen_crop(img, (224, 224)) combine_img = np.hstack((img, desmap)) save_name = img_name.replace(".jpg", "_combine.png") cv2.imwrite(save_name, combine_img)
def cen_crop(input_image, dsize): h, w, c = input_image.shape offset_h = int((h - dsize[0])/2) offset_w = int((w - dsize[1])/2) input_image = input_image[offset_h:offset_h + dsize[0], offset_w:offset_w+dsize[1], :] return input_image desmap_dir = "/media/dog/data/WebCamT_60000" cam_list = file_io.get_dir_list(desmap_dir) for cam_dir in cam_list: #cam_dir = "/Users/Geoff/Documents/my_git/data/desmap/253/" video_list = file_io.get_dir_list(cam_dir) for video in video_list: img_list = file_io.get_listfile(video, "_256.jpg") mask_name = video+ "_msk_256.npy" if not os.path.exists(mask_name): continue mask = np.fromfile(mask_name, np.float32) mask = np.expand_dims(np.reshape(mask, (256, 256)), 2) mask = cen_crop(mask, (227,227)) mask = np.squeeze(mask) #mask = np.tile(mask, (1,1,3)) for img_name in img_list: desmap_name = img_name.replace(".jpg", ".infer_desmap") if not os.path.exists(desmap_name): continue desmap_name = img_name.replace(".jpg", ".desmap") desmap = opencv_plot(desmap_name, mask)
def proc_image(): image_list = file_io.get_listfile(ucsd_image_path, image_ext) for img_name in image_list: img = cv2.imread(img_name) new_image_name = img_name.replace(image_ext, ".jpg") cv2.imwrite(new_image_name, img)
print("Usage: label_preprocess.py data_dir size_len") exit(1) data_dir = sys.argv[1] size_len = int(sys.argv[2]) dsize = (size_len, size_len) data_ext = "_%d.jpg" % size_len label_ext = "_%d.desmap" % size_len seg_ext = "_%d.segmap" % size_len cam_dir_list = file_io.get_dir_list(data_dir) train_list = list() test_list = list() full_file_list = list() for cam_dir in cam_dir_list: video_list = file_io.get_listfile(cam_dir, ".avi") for file_name in video_list: data_dir_name = file_name.replace(".avi", "") curr_data_list = file_io.get_listfile(data_dir_name, label_ext) for desmap_filename in curr_data_list: desmap = np.fromfile(desmap_filename, np.float32) desmap = np.reshape(desmap, dsize) segmap = desmap > 0 segmap = segmap.astype(np.float32) segmap_filename = desmap_filename.replace(label_ext, seg_ext) segmap.tofile(segmap_filename) #cv2.imshow("image", segmap) #cv2.waitKey(0)
import numpy as np import cv2 from TensorflowToolbox.utility import file_io desmap_dir = "/media/dog/data/UCSD/gtDensities/" desmap_list = file_io.get_listfile(desmap_dir, "desmap") new_list = list() for f in desmap_list: num = np.sum(np.fromfile(f, np.float32)) format_num = "%.2f" % num new_list.append(f + " " + format_num) new_list.sort() file_io.save_file(new_list, "ucsd_file_label_num.txt")
return reverse_image_list if __name__ == "__main__": image_ext = ".jpg" desmap_ext = ".desmap" mask_ext = "_mask.npy" file_list_dir = "../file_list/" unroll_num = 5 mul_mask_name = " ".join([mask_file_name] * 5) save_train_file_name = "ucsd_train_list1.txt" save_test_file_name = "ucsd_test_list1.txt" image_list = file_io.get_listfile(ucsd_image_path, image_ext) image_list.sort() image_list = file_list_to_list(image_list, unroll_num) reverse_list = reverse_copy_list(image_list) file_list = file_list_to_train_list(image_list) reverse_file_list = file_list_to_train_list(reverse_list) #train_list = reverse_file_list[600:1400] train_list = file_list[600:1400] #+ reverse_file_list[600:1400] test_list = file_list[0:600] + file_list[1400:] file_io.save_file(train_list, file_list_dir + save_train_file_name, True)
img = cv2.fillPoly(img, [pts], (255, 255, 255)) # cv2.imshow("image", img / 255) # cv2.waitKey(0) cv2.imwrite(pic_name, img) if __name__ == "__main__": if len(sys.argv) > 1: data_dir = sys.argv[1] else: data_dir = "../data" cam_list = file_io.get_dir_list(data_dir) for cam in cam_list: print(cam) msk_list = file_io.get_listfile(cam, "msk") for msk in msk_list: pic_name = msk.replace(".msk", ".png") f = open(msk, "r") lines = f.readlines() f.close() points = [] for line in lines: if "[" in line and "," in line and "]" in line: a = line.replace("[", "").replace("]", "").replace( "\n", "").replace("\r", "").split(",") x = int(a[0]) y = int(a[1]) if x <= 2: x = 0 if x >= 349:
if __name__ == "__main__": if not len(sys.argv) == 3: print("Usage: msk_resize.py data_dir size_len") exit(1) data_dir = sys.argv[1] size_len = int(sys.argv[2]) dsize = (size_len, size_len) #if len(sys.argv) > 1: # data_dir = sys.argv[1] #else: # data_dir = "../data" mask_dir_list = file_io.get_dir_list(data_dir) for mask_dir in mask_dir_list: mask_list = file_io.get_listfile(mask_dir, ".msk") for mask in mask_list: mask_img_name = mask.replace("msk", "png") mask_img = cv2.imread(mask_img_name, 0) bbox = image_utility_func.get_bbox(mask_img, 127) mask_img = mask_img[bbox[1]: bbox[1] + bbox[3], bbox[0]: bbox[0] + bbox[2]] mask_img = cv2.resize(mask_img, dsize) mask_img = mask_img / 255 mask_img = mask_img.astype(np.float32) mask_img[mask_img > 0] = 1.0 np_name = mask_img_name.replace(".png", "_msk_%d.npy"%size_len) mask_img.tofile(np_name)
label_path = "/media/dog/data/WorldExpo/train_label/" mask_dir = "/media/dog/data/WorldExpo/mask/" desmap_dir = "/media/dog/data/WorldExpo/gtDensity/gtDensity/" img_dir = "/media/dog/data/WorldExpo/train_frame/" else: label_path = "/media/dog/data/WorldExpo/test_label/" mask_dir = "/media/dog/data/WorldExpo/test_mask/" desmap_dir = "/media/dog/data/WorldExpo/test_gtDensity/test_gtDensity/" img_dir = "/media/dog/data/WorldExpo/test_frame/" if i == 0: file_list = train_list else: file_list = test_list img_list = file_io.get_listfile(img_dir, ".jpg") scale_str = str(new_dsize[0]) for img_name in img_list: if not img_name.endswith("_" + scale_str + ".jpg"): continue label_name = img_name.replace(img_dir, desmap_dir).replace(".jpg", ".npy") mask_name = mask_dir + img_name.split("/")[-1][:6] \ + "_mask_" + scale_str + ".npy" if not os.path.exists(label_name): print(label_name, "is not exist") exit(1) if not os.path.exists(mask_name):