Souheil-b commited on
Commit
5231df8
·
unverified ·
2 Parent(s): 6c49ce9 5dd4887

Merge pull request #2 from ia-labo/feat/prediction

Browse files

fix: replace absolute path with relative path and update JSON key fro…

Files changed (1) hide show
  1. src/predict.py +24 -12
src/predict.py CHANGED
@@ -1,6 +1,7 @@
1
  """Predict the class of a list of images, and return a json file with the prediction"""
2
 
3
  import json
 
4
  from ultralytics import YOLO
5
 
6
  # [{
@@ -16,15 +17,24 @@ from ultralytics import YOLO
16
 
17
 
18
  def add_prediction_to_json(predictions) -> None:
 
 
 
 
 
 
 
19
  formatted_predictions = []
20
  for result in predictions:
21
  for box in result.boxes:
22
- formatted_predictions.append({
23
- "class": int(box.cls[0].numpy().item()),
24
- "confidence": box.conf[0].numpy().item(),
25
- "box": box.xyxy[0].numpy().tolist()
26
- })
27
-
 
 
28
  return formatted_predictions
29
 
30
 
@@ -43,13 +53,15 @@ def get_prediction(list_folder, model_file, output_file) -> None:
43
  for folder in list_folder[:2]:
44
  # stream=True to get the prediction for each image
45
  # instead of trying to get all the predictions at once
46
- # show=False to not show the image with the prediction
47
  predictions = model(folder, stream=True, show=False)
48
  try:
49
- dict_predictions.append({
50
- "image": folder,
51
- "prediction": add_prediction_to_json(predictions)
52
- })
 
 
53
  except AttributeError as att_error:
54
  dict_predictions.append(
55
  {
@@ -63,7 +75,7 @@ def get_prediction(list_folder, model_file, output_file) -> None:
63
 
64
  if __name__ == "__main__":
65
  get_prediction(
66
- ["images/ChIJ_yEAweFlXj4Reo-x-AghMRM"],
67
  "models/yolov5s.pt",
68
  "./predictions.json",
69
  )
 
1
  """Predict the class of a list of images, and return a json file with the prediction"""
2
 
3
  import json
4
+
5
  from ultralytics import YOLO
6
 
7
  # [{
 
17
 
18
 
19
  def add_prediction_to_json(predictions) -> None:
20
+ """Add the prediction to the json file.
21
+
22
+ Args:
23
+ predictions (generator): generator of predictions from the YOLO model
24
+ Returns:
25
+ list: list of predictions to add to the json result
26
+ """
27
  formatted_predictions = []
28
  for result in predictions:
29
  for box in result.boxes:
30
+ formatted_predictions.append(
31
+ {
32
+ "image": result.path.split("/")[-1],
33
+ "class": int(box.cls[0].numpy().item()),
34
+ "confidence": box.conf[0].numpy().item(),
35
+ "box": box.xyxy[0].numpy().tolist(),
36
+ }
37
+ )
38
  return formatted_predictions
39
 
40
 
 
53
  for folder in list_folder[:2]:
54
  # stream=True to get the prediction for each image
55
  # instead of trying to get all the predictions at once
56
+ # show=False to not show the image when getting the prediction
57
  predictions = model(folder, stream=True, show=False)
58
  try:
59
+ dict_predictions.append(
60
+ {
61
+ "park": folder.split("/")[-1],
62
+ "prediction": add_prediction_to_json(predictions),
63
+ }
64
+ )
65
  except AttributeError as att_error:
66
  dict_predictions.append(
67
  {
 
75
 
76
  if __name__ == "__main__":
77
  get_prediction(
78
+ ["../images/Al Khaldiyah Park", "../images/Family Park"],
79
  "models/yolov5s.pt",
80
  "./predictions.json",
81
  )