Spaces:
Sleeping
Sleeping
import os | |
from pathlib import Path | |
from subprocess import run | |
import gradio as gr | |
yolo_input_size = 384 | |
versions = ('2_v108', '4_v109', '0_int6', '1_v110', '3_v111') | |
score_thr = 0.025 | |
zoom_score_thr = 0.35 | |
iou_thr = 0.6 | |
max_det = 1 | |
yolo_ens = 'fast' # fast, val, detect, detect_internal, all | |
output_size = (512, 512) | |
bs = 1 #128 if 'CUDA_VERSION' in os.environ else 16 | |
project_dir = None | |
working = Path(os.getcwd()) | |
modelbox = working / 'models' | |
checkpoint_files = [modelbox / f'yolov5_l6_{yolo_input_size}_fold{x}.pt' for x in versions] | |
image_root = working / 'images' / 'subdir' | |
image_urls = [ | |
# Negatives | |
'https://upload.wikimedia.org/wikipedia/commons/c/c5/Common_Dolphin.jpg', | |
'https://upload.wikimedia.org/wikipedia/commons/b/b8/Beluga847.jpg', | |
'https://upload.wikimedia.org/wikipedia/commons/e/ea/Beluga_1_1999-07-03.jpg', | |
'https://upload.wikimedia.org/wikipedia/commons/2/2b/Whale_Watching_in_Gloucester%2C_Massachusetts_5.jpg', | |
# Positives | |
'/kaggle/input/happy-whale-and-dolphin/test_images/00098d1376dab2.jpg', | |
] | |
yolo_source = f'{image_root}/negative001.jpg' | |
def pred_fn(image, fake=True): | |
if fake: | |
x0, x1 = (int(f * image.shape[0]) for f in (0.2, 0.8)) | |
y0, y1 = (int(f * image.shape[1]) for f in (0.2, 0.8)) | |
cropped_image = image[x0:x1, y0:y1, :] | |
response_str = f"This looks like a common dolphin, but I have not seen this individual before (0.834 confidence).\n" \ | |
"Go submit your photo on www.happywhale.com!" | |
return cropped_image, response_str | |
cropped_image, species = fast_yolo_crop(image) | |
test_embedding = get_test_embedding(embed_models, sizes) | |
cosine_similarity = np.dot(comp_embeddings, test_embedding[0]) / n_models | |
cosine_distances = 1 - cosine_similarity | |
normalized_distances = cosine_distances / max_distance | |
normalized_similarities = 1 - normalized_distances | |
min_similarity = normalized_similarities.min() | |
max_similarity = normalized_similarities.max() | |
confidence = get_confidence(max_similarity, threshold) | |
print(f"Similarities: {min_similarity:.4f} ... {max_similarity:.4f}") | |
print(f"Threshold:", threshold) | |
if max_similarity > threshold: | |
response_str = f"This looks like a {species} I have seen before ({confidence:.3f} confidence).\n" \ | |
"You might find its previous encounters on www.happywhale.com" | |
else: | |
response_str = f"This looks like a {species}, but I have not seen this individual before ({confidence:.3f} confidence).\n" \ | |
"Go submit your photo on www.happywhale.com!" | |
return cropped_image, response_str | |
examples = [str(image_root / f'negative{i:03d}') for i in range(3)] | |
demo = gr.Interface(fn=pred_fn, inputs="image", outputs=["image", "text"], | |
examples=examples) | |
demo.launch() |