def save_data_bbox(image_dbg, image_orig, fnum): global state class_num = state.class_num posi0 = state.posi0 posi1 = state.posi1 basedir = '../data4bbox/' fname_body = "image_%08d" % fnum # save image_orig (for Images/) fname = fname_body + ".jpg" basedir_images = basedir + "Images/%03d/" % (class_num) ofname_images = mkdir.get_filename_frame_simple(basedir_images, fname) print(ofname_images) cv2.imwrite(ofname_images, image_orig) # save image_dbg (for Debug/) fname = fname_body + ".jpg" basedir_dbg = basedir + "Debug/%03d/" % (class_num) ofname_dbg = mkdir.get_filename_frame_simple(basedir_dbg, fname) print(ofname_dbg) cv2.imwrite(ofname_dbg, image_dbg) # save labes fname = fname_body + ".txt" basedir_labels = basedir + "Labels/%03d/" % (class_num) ofname_labels = mkdir.get_filename_frame_simple(basedir_labels, fname) print(ofname_labels) bbox_posi = [] bbox_posi.append(min(posi0[0], posi1[0])) bbox_posi.append(min(posi0[1], posi1[1])) bbox_posi.append(max(posi0[0], posi1[0])) bbox_posi.append(max(posi0[1], posi1[1])) with open(ofname_labels, "w") as file: file.write("1\n") file.write("%d %d %d %d\n" % (tuple(bbox_posi)))
def save_data_darknet(image_dbg, image_orig, fnum): global state class_num = state.class_num posi0 = state.posi0 posi1 = state.posi1 basedir = '../data4darknet/' fname_body = "image_%08d" % fnum # save image_orig (for Images/) fname = fname_body + ".jpg" basedir_images = basedir + "images/%03d/" % (class_num) ofname_images = mkdir.get_filename_frame_simple(basedir_images, fname) #print (ofname_images) cv2.imwrite(ofname_images, image_orig) # save image_dbg (for Debug/) fname = fname_body + ".jpg" basedir_dbg = basedir + "debug/%03d/" % (class_num) ofname_dbg = mkdir.get_filename_frame_simple(basedir_dbg, fname) #print (ofname_dbg) cv2.imwrite(ofname_dbg, image_dbg) # save labes fname = fname_body + ".txt" basedir_labels = basedir + "labels/%03d/" % (class_num) ofname_labels = mkdir.get_filename_frame_simple(basedir_labels, fname) print(ofname_labels) bbox_posi = [] bbox_posi.append(min(posi0[0], posi1[0])) bbox_posi.append(min(posi0[1], posi1[1])) bbox_posi.append(max(posi0[0], posi1[0])) bbox_posi.append(max(posi0[1], posi1[1])) label_format = 'BBox' label_format = 'darknet' if label_format == 'BBox': with open(ofname_labels, "w") as file: file.write("1\n") file.write("%d %d %d %d\n" % (tuple(bbox_posi))) if label_format == 'darknet': img_h, img_w = image_orig.shape[:2] dh = float(img_h) dw = float(img_w) x0 = bbox_posi[0] y0 = bbox_posi[1] x1 = bbox_posi[2] y1 = bbox_posi[3] dnet_x = (x0 + x1) / (2.0 * dw) dnet_y = (y0 + y1) / (2.0 * dh) dnet_w = (x1 - x0) / dw dnet_h = (y1 - y0) / dh with open(ofname_labels, "w") as file: file.write("0 %1.17f %1.17f %1.17f %1.17f\n" % (dnet_x, dnet_y, dnet_w, dnet_h))