def OnFdTextChanged(root, pgbar, pglabel, btn, entry, ft_label): fd_name = entry.get() file_io = FileIO() ft_existed = False if file_io.check_obj("save"): pic_dataset_paths = file_io.load_obj("save") for path in pic_dataset_paths: if path == fd_name: ft_existed = True break if ft_existed: pgbar["value"] = 100 pglabel.configure(text="100.00%") btn["text"] = "重新提取特征" if getattr(sys, 'frozen', False): application_path = os.path.dirname(sys.executable) elif __file__: application_path = os.path.dirname(__file__) ft_name = "feature_" + hashlib.md5(fd_name.encode()).hexdigest() + ".h5" ft_label["text"] = os.path.abspath(os.path.join(application_path, ft_name)) else: pgbar["value"] = 0 pglabel.configure(text="0.00%") btn["text"] = "提取特征" ft_label["text"] = "当前文件夹没有特征被提取" root.update()
def ShowOriginalImage(event): img_no = int(str(event.widget).split(".")[-1]) file_io = FileIO() imlist = file_io.load_obj("imlist") ipath = imlist[img_no] img = mpimg.imread(ipath) imgplot = plt.imshow(img) plt.show()
def GetFoldernameFromSelector(root, e): root.update() filename = askdirectory() if filename != '': file_io = FileIO() if file_io.check_obj("save"): pic_dataset_paths = file_io.load_obj("save") ft_existed = 0 for path in pic_dataset_paths: if path == filename: break ft_existed += 1 if ft_existed >= len(pic_dataset_paths): pic_dataset_paths.append(filename) else: tmp = pic_dataset_paths[ft_existed] pic_dataset_paths[ft_existed] = pic_dataset_paths[-1] pic_dataset_paths[-1] = tmp else: pic_dataset_paths = [filename] e["values"] = pic_dataset_paths e.current(len(pic_dataset_paths) - 1) root.update()
def GenerateFeatureDatabase(fd_entry, ft_label, pgbar, pglabel, root): ft_name = "feature" fd_name = fd_entry.get() root.update() if os.path.exists(fd_name) and os.path.isdir(fd_name): pass else: messagebox.showerror(title="文件夹读取错误", message="无法读取文件或文件夹不存在") return file_handler = FileHandler() # img_list = file_handler.get_imlist(fd_name) img_list = [os.path.join(dp, f) for dp, dn, filenames in os.walk(fd_name) for f in filenames if os.path.splitext(f)[1] == '.jpg' or os.path.splitext(f)[1] == '.jpeg'] # print(img_list) print("--------------------------------------------------") print(" feature extraction starts") print("--------------------------------------------------") feats = [] names = [] model = VGGNet() ite_no = 0 ite_to = len(img_list) error_occured = False if getattr(sys, 'frozen', False): application_path = os.path.dirname(sys.executable) elif __file__: application_path = os.path.dirname(__file__) for i, img_path in enumerate(img_list): try: ite_no += 1 norm_feat = model.extract_feat(img_path) img_name = img_path feats.append(norm_feat) names.append(img_name.encode('utf-8')) print("extracting feature from image No. %d , %d images in total" %((i+1), len(img_list))) pgbar['value'] = (100 * ite_no) / ite_to print(pgbar['value']) label_content="{0:0.2f}%".format(pgbar['value']) pglabel.configure(text=label_content) root.update() except Exception as e: error_occured = True logger_fname = os.path.join(application_path, 'error.log') now = datetime.now() dt_string = now.strftime("%d/%m/%Y %H:%M:%S") with open(logger_fname, "a+", encoding='utf-8') as f: f.write('[Error] ' + dt_string + "\n") f.write('[Trace Back] ' + str(e) + "\n") f.write('[Detail] ' + img_path + "\n") feats = np.array(feats) print("--------------------------------------------------") print(" writing feature extraction results ...") print("--------------------------------------------------") if error_occured: messagebox.showerror(title="提取特征时发生错误", message="提取特征时发生错误,程序跳过了错误文件完成特征提取,请将error.log文件,以及后台截图发给开发者。") output = ft_name + "_" + hashlib.md5(fd_name.encode()).hexdigest() + ".h5" output = os.path.join(application_path, output) h5f = h5py.File(output, 'w') h5f.create_dataset('dataset_1', data = feats) h5f.create_dataset('dataset_2', data = np.string_(names)) h5f.close() ft_label.configure(text=os.path.abspath(output)) # Load array from file and update the file path array file_io = FileIO() if file_io.check_obj("save"): pic_dataset_paths = file_io.load_obj("save") ft_existed = 0 for path in pic_dataset_paths: if path == fd_name: break ft_existed += 1 if ft_existed >= len(pic_dataset_paths): pic_dataset_paths.append(fd_name) else: tmp = pic_dataset_paths[ft_existed] pic_dataset_paths[ft_existed] = pic_dataset_paths[-1] pic_dataset_paths[-1] = tmp print(pic_dataset_paths) else: pic_dataset_paths = [fd_name] file_io.save_obj(pic_dataset_paths,"save") fd_entry["values"] = pic_dataset_paths fd_entry.current(len(pic_dataset_paths) - 1)
feature_text = "暂时没有特征被提取" label_feature_name = Label(root, text=feature_text) label_feature_name.grid(row=1, column=1, sticky=W) # Setup progress bar for feature extraction pgbar = ttk.Progressbar(root, orient=HORIZONTAL, length=550, mode='determinate') pgbar.grid(row=2, column=1, sticky=W) l_pg = Label(root, text="0.00%") l_pg.grid(row=2, column=1, sticky=E) # Create database and extract features Label(root, text="图片库路径:").grid(row=0, column=0, sticky=W) file_io = FileIO() dbFolder_text = [""] if file_io.check_obj('save'): dbFolder_text = file_io.load_obj('save') e_dbFolder = ttk.Combobox(root, values=dbFolder_text, width=80) e_dbFolder.grid(row=0, column=1, sticky=W) Button(root, text="浏览文件夹", command=lambda: GetFoldernameFromSelector(root, e_dbFolder)).grid(row=0, column=1, sticky=E) ft_extract_btn = Button(root, text="提取特征", command=lambda: GenerateFeatureDatabase(e_dbFolder, label_feature_name, pgbar, l_pg, root)) ft_extract_btn.grid(row=1, column=1, sticky=E) e_var = StringVar() e_var.trace("w", lambda a, b, c: OnFdTextChanged(root, pgbar, l_pg, ft_extract_btn, e_dbFolder, label_feature_name)) e_dbFolder["textvar"] = e_var # Setup the maximum number of similar images Label(root, text="最大图片显示数量:").grid(row=4, column=0, sticky=W) e_maxRsltNo = Entry(root, width=20) e_maxRsltNo.grid(row=4, column=1, sticky=W) e_maxRsltNo.insert(0, "3")