annaferrari02 commited on
Commit
1412d27
·
verified ·
1 Parent(s): 08dbc6f

Update script.py

Browse files
Files changed (1) hide show
  1. script.py +79 -79
script.py CHANGED
@@ -1,79 +1,79 @@
1
- import os
2
- import pandas as pd
3
- from ultralytics import YOLO
4
-
5
- def run_inference(model, image_path, conf_threshold, save_path):
6
- test_images = os.listdir(image_path)
7
- test_images.sort()
8
-
9
- bboxes = []
10
- category_ids = []
11
- test_images_names = []
12
-
13
- # Iterate through images for inference
14
- for image_name in test_images:
15
- # Skip any non-image files if they exist in the directory
16
- if not image_name.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp')):
17
- continue
18
-
19
- full_image_path = os.path.join(image_path, image_name)
20
-
21
- current_image_bboxes = []
22
- current_image_category_ids = []
23
-
24
- # Perform inference
25
- results = model(full_image_path)
26
-
27
- # Process results
28
- # results is a list of Results objects, one for each image
29
- # Since we pass one image at a time, results[0] is the relevant object
30
- for pred in results[0].boxes:
31
- # Bounding box in xyxy format, confidence, class_id
32
- xmin, ymin, xmax, ymax = pred.xyxy[0].tolist()
33
- conf = pred.conf.item()
34
- class_id = int(pred.cls.item())
35
-
36
- if conf >= conf_threshold:
37
- width = xmax - xmin
38
- height = ymax - ymin
39
-
40
- current_image_bboxes.append([xmin, ymin, width, height])
41
- current_image_category_ids.append(class_id)
42
-
43
- test_images_names.append(image_name)
44
- bboxes.append(current_image_bboxes)
45
- category_ids.append(current_image_category_ids)
46
-
47
- # Create DataFrame for predictions
48
- df_predictions = pd.DataFrame(columns=["file_name", "bbox", "category_id"])
49
-
50
- for i in range(len(test_images_names)):
51
- file_name = test_images_names[i]
52
- new_row = pd.DataFrame({"file_name": file_name,
53
- "bbox": str(bboxes[i]),
54
- "category_id": str(category_ids[i]),
55
- }, index=[0])
56
- df_predictions = pd.concat([df_predictions, new_row], ignore_index=True)
57
-
58
- # Ensure the save directory exists
59
- os.makedirs(os.path.dirname(save_path), exist_ok=True)
60
- df_predictions.to_csv(save_path, index=False)
61
- print(f"Inference results saved to: {save_path}")
62
-
63
-
64
- if __name__ == "__main__":
65
- # Define paths
66
- # You might need to change TEST_IMAGE_PATH to where your actual test images are stored
67
- current_directory = os.path.dirname(os.path.abspath(__file__))
68
- TEST_IMAGE_PATH = "/tmp/data/test_images"
69
- SUBMISSION_SAVE_PATH = os.path.join(current_directory, "submission.csv")"
70
-
71
- # Path to your trained model weights
72
- MODEL_WEIGHTS_PATH = os.path.join(current_directory, "best.pt")
73
- CONF_THRESHOLD = 0.30 # Confidence threshold for predictions
74
-
75
- # Load the YOLO model
76
- model = YOLO(MODEL_WEIGHTS_PATH) # Using ultralytics.YOLO for loading
77
-
78
- # Run inference
79
- run_inference(model, TEST_IMAGE_PATH, CONF_THRESHOLD, SUBMISSION_SAVE_PATH)
 
1
+ import os
2
+ import pandas as pd
3
+ from ultralytics import YOLO
4
+
5
+ def run_inference(model, image_path, conf_threshold, save_path):
6
+ test_images = os.listdir(image_path)
7
+ test_images.sort()
8
+
9
+ bboxes = []
10
+ category_ids = []
11
+ test_images_names = []
12
+
13
+ # Iterate through images for inference
14
+ for image_name in test_images:
15
+ # Skip any non-image files if they exist in the directory
16
+ if not image_name.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp')):
17
+ continue
18
+
19
+ full_image_path = os.path.join(image_path, image_name)
20
+
21
+ current_image_bboxes = []
22
+ current_image_category_ids = []
23
+
24
+ # Perform inference
25
+ results = model(full_image_path)
26
+
27
+ # Process results
28
+ # results is a list of Results objects, one for each image
29
+ # Since we pass one image at a time, results[0] is the relevant object
30
+ for pred in results[0].boxes:
31
+ # Bounding box in xyxy format, confidence, class_id
32
+ xmin, ymin, xmax, ymax = pred.xyxy[0].tolist()
33
+ conf = pred.conf.item()
34
+ class_id = int(pred.cls.item())
35
+
36
+ if conf >= conf_threshold:
37
+ width = xmax - xmin
38
+ height = ymax - ymin
39
+
40
+ current_image_bboxes.append([xmin, ymin, width, height])
41
+ current_image_category_ids.append(class_id)
42
+
43
+ test_images_names.append(image_name)
44
+ bboxes.append(current_image_bboxes)
45
+ category_ids.append(current_image_category_ids)
46
+
47
+ # Create DataFrame for predictions
48
+ df_predictions = pd.DataFrame(columns=["file_name", "bbox", "category_id"])
49
+
50
+ for i in range(len(test_images_names)):
51
+ file_name = test_images_names[i]
52
+ new_row = pd.DataFrame({"file_name": file_name,
53
+ "bbox": str(bboxes[i]),
54
+ "category_id": str(category_ids[i]),
55
+ }, index=[0])
56
+ df_predictions = pd.concat([df_predictions, new_row], ignore_index=True)
57
+
58
+ # Ensure the save directory exists
59
+ os.makedirs(os.path.dirname(save_path), exist_ok=True)
60
+ df_predictions.to_csv(save_path, index=False)
61
+ print(f"Inference results saved to: {save_path}")
62
+
63
+
64
+ if __name__ == "__main__":
65
+ # Define paths
66
+ # You might need to change TEST_IMAGE_PATH to where your actual test images are stored
67
+ current_directory = os.path.dirname(os.path.abspath(__file__))
68
+ TEST_IMAGE_PATH = "/tmp/data/test_images"
69
+ SUBMISSION_SAVE_PATH = os.path.join(current_directory, "submission.csv")
70
+
71
+ # Path to your trained model weights
72
+ MODEL_WEIGHTS_PATH = os.path.join(current_directory, "best.pt")
73
+ CONF_THRESHOLD = 0.30 # Confidence threshold for predictions
74
+
75
+ # Load the YOLO model
76
+ model = YOLO(MODEL_WEIGHTS_PATH) # Using ultralytics.YOLO for loading
77
+
78
+ # Run inference
79
+ run_inference(model, TEST_IMAGE_PATH, CONF_THRESHOLD, SUBMISSION_SAVE_PATH)