<jupyter_start><jupyter_text>Run carpel detection on multiple x-ray images  Set directories<jupyter_code># make empty dataframe to store carpel area info
all_dfs = pd.DataFrame()

# set directory to folder with x-ray images. Remember to convert from tif to 8-bit RGB jpeg
# to-do: add batch tif to jpeg conversion
dataset_dir = (ROOT_DIR + "/datasets/21UM/gt1P")

# set save directory for mask arrays (if desired) and final csv
save_dir = (ROOT_DIR + "/results/21UM/gt1P")
os.mkdir(save_dir + "/mask_arrays")

dataset_all = os.listdir(dataset_dir)
# dataset = fnmatch.filter(dataset_all, '*08.jpg') + fnmatch.filter(dataset_all, '*10.jpg') # if desired, modify which x-ray image angles are included in the analysis
dataset = dataset_all
print("Measuring " + str(len(dataset)) + " images from " + dataset_dir)
print("Saving output in " + save_dir)<jupyter_output><empty_output><jupyter_text>Calculate carpel area<jupyter_code># detect carpels in each x-ray
# inputs: x-ray jpegs from dataset_dir
# outputs: dataframe all_dfs with all carpel area measurements. Written to carpel_area_results.csv in save_dir
for image_id in dataset:

    #generate temp dataframe for image_id
    df = pd.DataFrame(area_data, columns=['image_id', 'carpel_number', 'carpel_area'])
    df["image_id"] = str(image_id)
    
    #run detection
    print("Running detection on image " + str(dataset.index(image_id) + 1) + "/" + str(len(dataset)))
    image = skimage.io.imread(dataset_dir + '/' + image_id)
    r = model.detect([image], verbose=0)
    # np.save(save_dir + "/mask_arrays/" + str(image_id) + ".npy", r) # if desired, save numpy array
    r = r[0]
    
    # skip image if no carpels detected
    if np.size(r['masks']) == 0:
        print("No carpels detected.")
        continue
        
    #plot the detections
    ax = get_ax(1)
    display_carpel_instances(image, r['rois'], r['masks'], r['class_ids'], r['scores'], ax=ax, title= image_id + " Predictions")
    
    #if desired, save mask overlay plot of detected carpels
    #plt.savefig(save_dir + "/" + str(image_id) + '.masks.jpg')
    #plt.close()
    
    #measure carpel pixel area
    carpel_count = np.reshape(r['masks'], (-1, r['masks'].shape[-1])).astype(np.float32).shape
    print(str(carpel_count[1]) + " carpels detected.")
    area_data = np.ndarray(shape=(carpel_count[1],3), dtype=float, order='F')
    for x in range(0,(carpel_count[1])):
        area_sum = np.reshape(r['masks'], (-1, r['masks'].shape[-1])).astype(np.float32)[:,x].sum()
        print("Pixel area of carpel " + str(x) + ": " + str(area_sum))
        df["carpel_number"][x] = x
        df["carpel_area"][x] = area_sum
    
    #merge new dataframe with previous dataframe
    all_dfs = pd.concat([all_dfs, df])
    #write dataframe to csv at each step
    all_dfs.to_csv(save_dir + "/carpel_area_results.csv", header= True)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1421636615811373, 'fraction_numerical': 0.010055478502080445, 'mean_word_length': 4.034904013961605, 'pattern_counts': {'":': 1, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '642411', 'license': 'MIT', 'n_tokens_mistral': 1041, 'n_tokens_neox': 998, 'n_words': 252, 'path': 'carpel/carpel_detection.ipynb', 'repo_name': 'aednv/Mask_RCNN', 'size': '2884'}