File size: 1,923 Bytes
38fd181
 
 
 
 
da7dbd0
 
 
 
 
38fd181
 
 
da7dbd0
 
38fd181
da7dbd0
 
 
504f37b
 
 
 
da7dbd0
 
 
 
38fd181
 
 
 
da7dbd0
 
 
 
 
 
 
 
38fd181
 
da7dbd0
 
 
 
38fd181
da7dbd0
38fd181
 
 
da7dbd0
 
 
 
 
 
 
 
 
 
38fd181
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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