Spaces:
Sleeping
Sleeping
from src.application.image.image_comparison import ( | |
compare_images, | |
get_image_from_file, | |
get_image_from_url, | |
) | |
from src.application.image.model_detection import image_generation_detection | |
from src.application.image.search_yandex import yandex_reverse_image_search | |
def compare_list_of_images(news_image_path, img_urls): | |
news_image = get_image_from_file( | |
news_image_path, | |
) # TODO: news_image_path is arrays | |
if news_image is None: | |
return None, -1 | |
matched_url = "" | |
max_similarity = 0 | |
for url in img_urls: | |
if "ichef.bbci.co.uk" in url and " " in url: | |
url_list = url.split(",") | |
if len(url_list) > 0: | |
url = url_list[0].split(" ")[0] | |
print(f"\t{url}") | |
referred_image = get_image_from_url(url) | |
if referred_image is None: | |
continue | |
distance = compare_images( | |
news_image, | |
referred_image, | |
) # Hamming algorithm | |
similarity = max(100 - distance, 0) | |
if similarity > max_similarity: | |
max_similarity = similarity | |
matched_url = url | |
if max_similarity > 90: | |
return matched_url, max_similarity | |
return None, -1 | |
def detect_image_from_news_image(news_image_path, image_urls): | |
print("\tFrom news:") | |
return compare_list_of_images(news_image_path, image_urls) | |
def detect_image_by_reverse_search(news_image_path): | |
image_urls = yandex_reverse_image_search( | |
news_image_path, | |
) # url or file_path | |
print("\tFrom search engine:") | |
for url in image_urls: | |
print(f"\t\t{url}") | |
return compare_list_of_images(news_image_path, image_urls) | |
def detect_image_by_ai_model(news_image_path): | |
print("\tFrom AI model:") | |
image_prediction_label, image_confidence = image_generation_detection( | |
news_image_path, | |
) | |
return image_prediction_label, image_confidence | |