File size: 3,857 Bytes
03e275e |
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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
from tabulate import tabulate
from modules import visual_checks, text_checks, content_checks
def classify(image_path):
"""Perform complete classification with detailed results."""
# Components to check
components = [
visual_checks.image_quality,
visual_checks.ribbon,
text_checks.tagline,
text_checks.tooMuchText,
content_checks.theme,
content_checks.body,
text_checks.cta,
text_checks.tnc,
visual_checks.gnc
]
# Collect all results
all_results = {}
for component in components:
try:
results = component(image_path)
all_results.update(results)
except Exception as e:
print(f"Error in component {component.__name__}: {e}")
# Optionally set default values or log error
pass
# Calculate final classification
# Check if any result value is 1 (or starts with '1' for string results like "1 [Religious]")
final_classification = 0
for result in all_results.values():
if isinstance(result, int):
if result == 1:
final_classification = 1
break
elif isinstance(result, str):
if result.startswith('1'):
final_classification = 1
break
# Determine Pass or Fail
classification_result = "Fail" if final_classification == 1 else "Pass"
# Prepare the table data
table_data = []
labels = [
"Bad Image Quality", "No Ribbon", "Empty/Illegible/Black Tagline", "Multiple Taglines",
"Incomplete Tagline", "Hyperlink", "Price Tag", "Excessive Emojis", "Too Much Text",
"Inappropriate Content", "Religious Content", "High Risk Content",
"Illegal Content", "Competitor References", "Bad CTA", "Terms & Conditions",
"Visual Gesture or Icon"
]
# Collect labels responsible for failure
failure_labels = []
for label in labels:
result = all_results.get(label, 0)
is_fail = False
if isinstance(result, int) and result == 1:
is_fail = True
elif isinstance(result, str) and result.startswith('1'):
is_fail = True
if is_fail:
failure_labels.append(label)
table_data.append([label, result])
# Format the results as a table
result_table = tabulate(table_data, headers=["LABEL", "RESULT"], tablefmt="fancy_grid")
# Return the final classification, result table, and failure labels (if any)
return classification_result, result_table, failure_labels
# Dummy interface for testing (can be enabled if needed)
def classify_dummy(image_path):
import random
all_results = {
"Bad Image Quality": 0,
"No Ribbon": random.choice([0, 1]),
"Empty/Illegible/Black Tagline": 0,
"Multiple Taglines": 0,
"Incomplete Tagline": 0,
"Hyperlink": 0,
"Price Tag": 0,
"Excessive Emojis": 0,
"Too Much Text": 0,
"Inappropriate Content": 0,
"Religious Content": 0,
"High Risk Content": 0,
"Illegal Content": 0,
"Competitor References": 0,
"Bad CTA": 0,
"Terms & Conditions": 0,
"Visual Gesture or Icon": 0
}
final_classification = 1 if any(result == 1 for result in all_results.values()) else 0
classification_result = "Fail" if final_classification == 1 else "Pass"
table_data = []
labels = list(all_results.keys())
failure_labels = [label for label in labels if all_results[label] == 1]
for label in labels:
table_data.append([label, all_results[label]])
result_table = tabulate(table_data, headers=["LABEL", "RESULT"], tablefmt="fancy_grid")
return classification_result, result_table, failure_labels |