1+ import math
2+ import os
3+
4+ import pandas as pd
5+ import csv
6+ import cv2
7+ import argparse
8+
9+ def yolo_to_starfile (yolo_coords , image_width , image_height ,diameters ):
10+ x_center = math .ceil (yolo_coords ['x_centre' ] * image_width )
11+ y_center = math .ceil (yolo_coords ['y_centre' ] * image_height )
12+ width = yolo_coords ['width' ] * image_width
13+ height = yolo_coords ['height' ] * image_height
14+
15+ # Calculate diameter
16+ diameter = math .ceil (max (width , height ))
17+
18+ diameters .append (diameter )
19+
20+ # Return x_center, y_center, diameter
21+ return x_center , y_center , diameter
22+
23+
24+ # Modify the generate_output function to accept DynamicDet DataFrame
25+ def generate_output (labels , filename , star_writer ,diameters ,img_width ,img_height ):
26+ for index , row in labels .iterrows ():
27+ # x_centre = row['X-Coordinate']
28+ # y_centre = row['Y-Coordinate']
29+ # diameter = row['Diameter']
30+
31+ x_centre , y_centre , diameter = yolo_to_starfile (row , img_width , img_height ,diameters )
32+ # print(x_centre, y_centre, diameter)
33+
34+ star_writer .writerow ([filename , x_centre , y_centre , diameter ])
35+
36+ def main (labels_path ,images_path ,star_out_path ,conf_thresh ):
37+ # # Read DynamicDet output from txt file
38+ # labels_path = "path/to/labels"
39+ # images_path = "path/to/denoised_images"
40+ # star_out_path = "output/path/file.star"
41+ # conf_thresh = 0.5
42+
43+ diameters = list ()
44+
45+ with open (star_out_path , "w" ) as star_file :
46+ star_writer = csv .writer (star_file , delimiter = ' ' )
47+ star_writer .writerow ([])
48+ star_writer .writerow (["data_" ])
49+ star_writer .writerow ([])
50+ star_writer .writerow (["loop_" ])
51+ star_writer .writerow (["_rlnMicrographName" , "#1" ])
52+ star_writer .writerow (["_rlnCoordinateX" , "#2" ])
53+ star_writer .writerow (["_rlnCoordinateY" , "#3" ])
54+ star_writer .writerow (["_rlnDiameter" , "#4" ])
55+
56+ i = 1
57+ for image in os .listdir (images_path ):
58+ filename = image .split ("/" )[- 1 ][:- 4 ]
59+
60+ print (i )
61+
62+ i += 1
63+
64+ label_file_path = os .path .join (labels_path , str (filename + '.txt' ))
65+
66+ if not os .path .exists (label_file_path ):
67+ continue
68+
69+ # label_file_path = os.path.join(labels_path, str(filename + '.csv'))
70+ # labels = pd.read_csv(label_file_path)
71+
72+ image = cv2 .imread (os .path .join (images_path , image ))
73+ img_width = image .shape [1 ]
74+ img_height = image .shape [0 ]
75+
76+
77+ custom_headers = ['class' , 'x_centre' , 'y_centre' , 'width' , 'height' , 'conf' ]
78+
79+ # Read the CSV file with custom headers
80+ labels = pd .read_csv (label_file_path , header = None , names = custom_headers , sep = ' ' )
81+
82+ labels = labels [labels ['conf' ] > conf_thresh ]
83+
84+ # print(labels.head())
85+
86+ # label_file_path = os.path.join(labels_path, str(filename + '.csv'))
87+ # labels = pd.read_csv(label_file_path)
88+
89+ star_filename = str (filename + '.mrc' )
90+
91+ generate_output (labels , star_filename , star_writer ,diameters ,img_width ,img_height )
92+
93+ def parse_args ():
94+ parser = argparse .ArgumentParser (description = "Generate STAR file from PartiNet predictions" )
95+ parser .add_argument ("--labels" , required = True , help = "Path to the labels directory" )
96+ parser .add_argument ("--images" , required = True , help = "Path to the images directory" )
97+ parser .add_argument ("--output" , required = True , help = "Path to the output STAR file" )
98+ parser .add_argument ("--conf" , required = True , help = "Minimum confidence threshold for predictions" )
99+
100+ return parser .parse_args ()
101+
102+ if __name__ == "__main__" :
103+ args = parse_args ()
104+ main (args .labels , args .images , args .output , args .conf )
0 commit comments