Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,37 +12,50 @@ def predict_image(image, crop=False, count=True):
|
|
| 12 |
Predict single image using YOLO model
|
| 13 |
"""
|
| 14 |
try:
|
|
|
|
| 15 |
r_image = yolo.detect_image(image, crop=crop, count=count)
|
|
|
|
| 16 |
return r_image
|
| 17 |
except Exception as e:
|
| 18 |
-
print(f"Error: {e}")
|
| 19 |
return None
|
| 20 |
|
| 21 |
def predict_directory(input_dir, output_dir, crop=False, count=True):
|
| 22 |
"""
|
| 23 |
Predict images in a directory using YOLO model and save results to another directory
|
| 24 |
"""
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
def inference(image, mode='predict', crop=False, count=True, input_dir=None, output_dir=None):
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
# Gradio interface setup
|
| 48 |
title = "YOLO Image Prediction"
|
|
@@ -95,4 +108,3 @@ with gr.Blocks() as demo:
|
|
| 95 |
)
|
| 96 |
|
| 97 |
demo.launch()
|
| 98 |
-
|
|
|
|
| 12 |
Predict single image using YOLO model
|
| 13 |
"""
|
| 14 |
try:
|
| 15 |
+
print("Starting image prediction...") # Debug log
|
| 16 |
r_image = yolo.detect_image(image, crop=crop, count=count)
|
| 17 |
+
print("Prediction completed.") # Debug log
|
| 18 |
return r_image
|
| 19 |
except Exception as e:
|
| 20 |
+
print(f"Error during image prediction: {e}") # Debug log
|
| 21 |
return None
|
| 22 |
|
| 23 |
def predict_directory(input_dir, output_dir, crop=False, count=True):
|
| 24 |
"""
|
| 25 |
Predict images in a directory using YOLO model and save results to another directory
|
| 26 |
"""
|
| 27 |
+
try:
|
| 28 |
+
print(f"Processing directory: {input_dir}") # Debug log
|
| 29 |
+
img_names = os.listdir(input_dir)
|
| 30 |
+
results = []
|
| 31 |
+
for img_name in tqdm(img_names):
|
| 32 |
+
if img_name.lower().endswith(('.bmp', '.dib', '.png', '.jpg', '.jpeg', '.pbm', '.pgm', '.ppm', '.tif', '.tiff')):
|
| 33 |
+
image_path = os.path.join(input_dir, img_name)
|
| 34 |
+
image = Image.open(image_path)
|
| 35 |
+
r_image = yolo.detect_image(image, crop=crop, count=count)
|
| 36 |
+
if not os.path.exists(output_dir):
|
| 37 |
+
os.makedirs(output_dir)
|
| 38 |
+
output_path = os.path.join(output_dir, img_name.replace(".jpg", ".png"))
|
| 39 |
+
r_image.save(output_path, quality=95, subsampling=0)
|
| 40 |
+
results.append((img_name, output_path))
|
| 41 |
+
print(f"Directory processing completed: {input_dir}") # Debug log
|
| 42 |
+
return results
|
| 43 |
+
except Exception as e:
|
| 44 |
+
print(f"Error during directory prediction: {e}") # Debug log
|
| 45 |
+
return []
|
| 46 |
|
| 47 |
def inference(image, mode='predict', crop=False, count=True, input_dir=None, output_dir=None):
|
| 48 |
+
try:
|
| 49 |
+
print(f"Received mode: {mode}, crop: {crop}, count: {count}") # Debug log
|
| 50 |
+
if mode == 'predict':
|
| 51 |
+
return predict_image(image, crop=crop, count=count)
|
| 52 |
+
elif mode == 'dir_predict' and input_dir and output_dir:
|
| 53 |
+
return predict_directory(input_dir, output_dir, crop=crop, count=count)
|
| 54 |
+
else:
|
| 55 |
+
raise ValueError("Invalid mode or missing directories for 'dir_predict' mode.")
|
| 56 |
+
except Exception as e:
|
| 57 |
+
print(f"Error in inference function: {e}") # Debug log
|
| 58 |
+
return None
|
| 59 |
|
| 60 |
# Gradio interface setup
|
| 61 |
title = "YOLO Image Prediction"
|
|
|
|
| 108 |
)
|
| 109 |
|
| 110 |
demo.launch()
|
|
|