@@ -145,21 +145,28 @@ def classify(self, img_path, results):
145145 logger .info ("Running classification on %s image..." , img_path )
146146 img = Image .open (img_path ).convert ("RGB" )
147147
148- classified_animals = self .detection_pipeline .classify (
149- img , results , AiModel .classification_threshold
150- )
151-
152- for detection in classified_animals :
153- # Cropping detection result(s) from original image leveraging detected boxes
154- cropped_image = img .crop (detection ["xyxy" ])
155- cropped_image_path = os .path .join (
156- "classification_output" , f"{ detection ['id' ]} _cropped_image.jpg"
148+ classified_animals = None
149+ classified_img_path = None
150+ if not (
151+ classified_animals := self .detection_pipeline .classify (
152+ img , results , AiModel .classification_threshold
157153 )
158- cropped_image .save (cropped_image_path )
159- logger .debug ("Saved crop of image at %s." , cropped_image_path )
160-
161- img_path = self .build_classification_square (img , classified_animals , img_path )
162- return img_path , classified_animals
154+ ):
155+ logger .debug ("No classified animals, skipping img crops saving." )
156+ else :
157+ for classified_animal in classified_animals :
158+ # Cropping detection result(s) from original image leveraging detected boxes
159+ cropped_image = img .crop (classified_animal ["xyxy" ])
160+ cropped_image_path = os .path .join (
161+ "classification_output" , f"{ classified_animal ['id' ]} _cropped_image.jpg"
162+ )
163+ cropped_image .save (cropped_image_path )
164+ logger .debug ("Saved crop of image at %s." , cropped_image_path )
165+
166+ classified_img_path = self .build_classification_square (
167+ img , classified_animals , img_path
168+ )
169+ return classified_img_path , classified_animals
163170
164171 def build_classification_square (self , img , classified_animals , img_path ):
165172 """Build square on classified animals."""
0 commit comments