from io import BytesIO from PIL import Image, UnidentifiedImageError import imagehash from Classifier.havoxClassifier import VoxClassifier from Classifier.wdClassifier import WDClassifier from Database.x_classes import ErrorID, HavoxLabel, PostRating from helpers import twos_complement async def classify_all(photo_bytes, wd_classifier: WDClassifier, vox_classifier : VoxClassifier): vox_label = await vox_classifier.classify_async(photo_bytes) is_filtered = vox_label == HavoxLabel.Rejected tags = [] filtered_tags = {} phash = None dhash = None error_id = ErrorID.SUCCESS if not is_filtered: rating, tags, filtered_tags = await wd_classifier.classify_async(photo_bytes, tag_threshold=0.6) tags = list(tags.keys()) try: with BytesIO(photo_bytes) as image_file: with Image.open(image_file) as imag: phash = twos_complement(str(imagehash.phash(imag)), 64) dhash = twos_complement(str(imagehash.dhash(imag)), 64) except UnidentifiedImageError: error_id = ErrorID.HASHING_Unidentified except FileNotFoundError: error_id = ErrorID.HASHING_FileNotFound except Exception as ex: error_id = ErrorID.HASHING_OTHER else: rating = PostRating.Filtered return vox_label, rating, tags, filtered_tags, phash, dhash, error_id