Upload inference.py with huggingface_hub
Browse files- inference.py +10 -3
inference.py
CHANGED
|
@@ -3,6 +3,7 @@ import argparse
|
|
| 3 |
import cv2
|
| 4 |
import os
|
| 5 |
import numpy as np
|
|
|
|
| 6 |
|
| 7 |
parser = argparse.ArgumentParser(description='Run model inference on images.')
|
| 8 |
parser.add_argument('input_dir', type=str, help='Directory containing images for inference.')
|
|
@@ -70,30 +71,36 @@ def crop_image_from_prediction(card, prediction):
|
|
| 70 |
|
| 71 |
return warped
|
| 72 |
|
|
|
|
| 73 |
for img_path in image_paths:
|
| 74 |
img = cv2.imread(img_path)
|
| 75 |
height, width = img.shape[:2]
|
| 76 |
-
card = np.ones((height+
|
| 77 |
card[50:50+height, 50:50+width] = img
|
| 78 |
cv2.imwrite(f"debug/{os.path.basename(img_path)}", card)
|
| 79 |
if img is None and verbose>=1:
|
| 80 |
print(f"Could not read image: {img_path}")
|
|
|
|
| 81 |
continue
|
| 82 |
results = model.predict(
|
| 83 |
source=img,
|
| 84 |
save=False,
|
| 85 |
conf=0.25,
|
| 86 |
-
imgsz=416
|
|
|
|
| 87 |
)
|
| 88 |
rotated = crop_image_from_prediction(card, results[0])
|
| 89 |
if rotated is None and verbose>=1:
|
| 90 |
print(f"Could not rotate image: {img_path}")
|
|
|
|
| 91 |
continue
|
| 92 |
try:
|
| 93 |
cv2.imwrite(f"{save_dir}/{os.path.basename(img_path)}", rotated)
|
| 94 |
except Exception as e:
|
| 95 |
if verbose>=1:
|
| 96 |
print(f"Error saving image {img_path} to {save_dir}: {e}")
|
|
|
|
| 97 |
continue
|
| 98 |
if verbose==2:
|
| 99 |
-
print(f"Processed {img_path}, saved to {save_dir}/{os.path.basename(img_path)}")
|
|
|
|
|
|
| 3 |
import cv2
|
| 4 |
import os
|
| 5 |
import numpy as np
|
| 6 |
+
from tqdm import tqdm
|
| 7 |
|
| 8 |
parser = argparse.ArgumentParser(description='Run model inference on images.')
|
| 9 |
parser.add_argument('input_dir', type=str, help='Directory containing images for inference.')
|
|
|
|
| 71 |
|
| 72 |
return warped
|
| 73 |
|
| 74 |
+
progress = tqdm(total=len(image_paths), desc="Processing images", unit="image")
|
| 75 |
for img_path in image_paths:
|
| 76 |
img = cv2.imread(img_path)
|
| 77 |
height, width = img.shape[:2]
|
| 78 |
+
card = np.ones((height+50, width+50, 3), dtype=np.uint8) * 255
|
| 79 |
card[50:50+height, 50:50+width] = img
|
| 80 |
cv2.imwrite(f"debug/{os.path.basename(img_path)}", card)
|
| 81 |
if img is None and verbose>=1:
|
| 82 |
print(f"Could not read image: {img_path}")
|
| 83 |
+
progress.update(1)
|
| 84 |
continue
|
| 85 |
results = model.predict(
|
| 86 |
source=img,
|
| 87 |
save=False,
|
| 88 |
conf=0.25,
|
| 89 |
+
imgsz=416,
|
| 90 |
+
verbose=verbose==2
|
| 91 |
)
|
| 92 |
rotated = crop_image_from_prediction(card, results[0])
|
| 93 |
if rotated is None and verbose>=1:
|
| 94 |
print(f"Could not rotate image: {img_path}")
|
| 95 |
+
progress.update(1)
|
| 96 |
continue
|
| 97 |
try:
|
| 98 |
cv2.imwrite(f"{save_dir}/{os.path.basename(img_path)}", rotated)
|
| 99 |
except Exception as e:
|
| 100 |
if verbose>=1:
|
| 101 |
print(f"Error saving image {img_path} to {save_dir}: {e}")
|
| 102 |
+
progress.update(1)
|
| 103 |
continue
|
| 104 |
if verbose==2:
|
| 105 |
+
print(f"Processed {img_path}, saved to {save_dir}/{os.path.basename(img_path)}")
|
| 106 |
+
progress.update(1)
|