def run(input_dir, output_dir, allow_api): """ Runs inference of the Scene Description Model on images located in the input_dir. Stores results in a .csv file located in the output_dir. If allowed, run() sends selected images to content moderation models of the Amazon Rekognition, Google Vision Cloud and Microsoft Computer Vision APIs. Images with the top prediction`s score below the API_THRESHOLD constant are sent to APIs. APIs` results are also stored in the output_dir. Parameters ---------- input_dir : str Path to the dir with images to run inference on. output_dir : str Path to the dir where all results will be stored. allow_api : bool If True selected images are sent to the APIs. """ print("[NSFW DETECTION] Loading model...") model = predict.load_model(MODEL_PATH) nsfw_classification = dict() imgs_for_api = set() try: nsfw_classification = predict.classify(model, input_dir, image_dim=NETWORK_INPUT_SIZE) except ValueError: print("[Error] Input directory does not contain any valid images") with open(os.path.join(output_dir, "moderate_image.csv"), "w") \ as output_file: for img in nsfw_classification.keys(): img_name = img[img.rfind('\\') + 1:] if (allow_api and max(nsfw_classification[img].values()) < API_THRESHOLD): imgs_for_api.add(img_name) img_name = img[img.rfind('\\') + 1:] output = f"{img_name}" for cat, score in nsfw_classification[img].items(): output += f",{cat}:{score:.2f}" output += "\n" output_file.write(output) print(f"[NSFW DETECTION] Analyzed {len(nsfw_classification)} images, " "results are stored in " f"{os.path.join(output_dir, 'moderate_image.csv')}") print(f"[NSFW DETECTION] Passing {len(imgs_for_api)} images for" " further analysis to APIs...") run_aws_api(imgs_for_api, input_dir, output_dir, "moderate_image") run_ms_api(imgs_for_api, input_dir, output_dir, "moderate_image") run_gg_api(imgs_for_api, input_dir, output_dir, "moderate_image") print("-------------------------------------------------------\n")
def guess_nsfw_info(image_path): """ Guess NSFW content from an image with nsfw_model. :param image_path """ try: model = predict.load_model(NSFW_MODEL) img_dict = predict.classify(model, image_path)[image_path] return ( float(img_dict["p**n"]), float(img_dict["hentai"]), float(img_dict["sexy"]), ) except Exception as error: logger.error(error, exc_info=True)
def image_data_convert(file_list, network, resize_method, include_top): print('画像から特徴量を抽出中…') output_features = [] if network == 'nsfw': nsfw_model = predict.load_model('./nsfw.299x299.h5') if network == 'inception_resnet_v2': img_list = [] for j in tqdm(range(len(file_list))): img_file = file_list[j] orig_img = Image.open('./thumbnails/' + img_file + '.jpg') resized_img = resize_image(orig_img, 299, 299, resize_method) img_list.append(resized_img) img_list = np.array(img_list) inputs = Input(shape=(299, 299, 3)) model = InceptionResNetV2(include_top=include_top, weights='imagenet', input_tensor=inputs) output_features = model.predict(img_list) print("画像から特徴量への変換終了") elif network == 'mobilenet_v2': img_list = [] for j in tqdm(range(len(file_list))): img_file = file_list[j] orig_img = Image.open('./thumbnails/' + img_file + '.jpg') resized_img = resize_image(orig_img, 224, 224, resize_method) img_list.append(resized_img) img_list = np.array(img_list) inputs = Input(shape=(224, 224, 3)) model = MobileNetV2(include_top=include_top, weights='imagenet', input_tensor=inputs) #一度include_topをtrueにしてテスト output_features = model.predict(img_list) print("画像から特徴量への変換終了") else: return None final_out = [] for i in range(len(output_features)): final_out.append(output_features[i].flatten()) final_out = np.array(final_out) return final_out
def check_nsfw(image_list): """ :param image_list: list of image paths """ # Painfully slow importing time from nsfw_detector import predict logger.info("Checking for NSFW content") for image in image_list: try: model = predict.load_model(NSFW_MODEL) img_dict = predict.classify(model, image)[image] nsfw_tuple = ( float(img_dict["p**n"]), float(img_dict["hentai"]), float(img_dict["sexy"]), ) except Exception as error: logger.error(error, exc_info=True) raise NSFWContent("Error guessing NSFW") logger.info(nsfw_tuple) if any(guessed > 0.2 for guessed in nsfw_tuple): raise NSFWContent("NSFW guessed from %s: %s", image, nsfw_tuple)
from nsfw_detector import predict import os, fcntl import time import shutil import random import requests from flask import Flask, render_template, request import base64 model = predict.load_model('/Imgfit/ImageReview-v3/nsfw_model/nsfw.299x299.h5') img_root = './images' def getRandomSet(bits): num_set = [chr(i) for i in range(48, 58)] char_set = [chr(i) for i in range(97, 123)] total_set = num_set + char_set value_set = "".join(random.sample(total_set, bits)) return value_set def predict_img(imgpath): name_rand = imgpath.split('/')[2].split('.')[0] + '.txt' print('outname', name_rand) try: print('start') time.sleep(float(random.randint(0, 5) / 10)) res = predict.classify(model, imgpath) print(res) # save res2 = res[imgpath]
def image_to_nsfw_score(file_list, batch_size=100, use_nsfw_score=True, meta_path='view_data', image_path='thumbnails'): output_features = [] pd_output_features = {} pd_output_features['nsfw_score_hentai'] = [] pd_output_features['nsfw_score_porn'] = [] pd_output_features['nsfw_score_sexy'] = [] if use_nsfw_score: nsfw_model = predict.load_model('./nsfw_mobilenet2.224x224.h5') for i in range(-(-len(file_list) // batch_size)): print('画像のNSFWスコアを計算中...{0}/{1}'.format( i + 1, -(-len(file_list) // batch_size))) if i == -(-len(file_list) // batch_size) - 1: batch = file_list[batch_size * i:] else: batch = file_list[batch_size * i:batch_size * i + batch_size] image_paths = [ image_path + '/' + video_id + '.jpg' for video_id in batch ] video_ids = [video_id for video_id in batch] #既にデータファイル側に記録されていればそれを使う data_used_nsfw = 0 for video_id in batch: with open(meta_path + '/{}'.format(video_id) + '.json', mode='r', encoding='utf-8') as f: json_data = json.load(f) if 'hentai' in json_data: data_used_nsfw += 1 if data_used_nsfw == len(batch): nsfw_score_dict = {} for video_id in batch: nsfw_score_dict[image_path + '/' + video_id + '.jpg'] = { 'hentai': json_data['hentai'], 'p**n': json_data['p**n'], 'sexy': json_data['sexy'] } else: nsfw_score_dict = predict.classify( nsfw_model, image_paths) #GantMan氏のモデルを使用。 one_batch_features = [] #時間だけが異なるデータが重複しているのでその対策 duplicating = {} for j in batch: duplicating[j] = batch.count(j) for key in nsfw_score_dict: video_id = video_ids[image_paths.index(key)] one_video_feature = [] #後で利用できるようにjsonファイルを更新しておく with open(meta_path + '/{}'.format(video_id) + '.json', mode='r', encoding='utf-8') as f: json_data = json.load(f) if 'hentai' not in json_data: json_data['hentai'] = nsfw_score_dict[key]['hentai'] if 'p**n' not in json_data: json_data['p**n'] = nsfw_score_dict[key]['p**n'] if 'sexy' not in json_data: json_data['sexy'] = nsfw_score_dict[key]['sexy'] with open(meta_path + '/{}'.format(video_id) + '.json', mode='w', encoding='utf-8') as f: json.dump(json_data, f, ensure_ascii=False, indent=4) #それぞれ0~1の間でその種類に属する確率を表している数値。 one_video_feature.append( nsfw_score_dict[key]['hentai']) #性的なイラスト one_video_feature.append(nsfw_score_dict[key]['p**n']) #実写のポルノ one_video_feature.append( nsfw_score_dict[key]['sexy']) #ポルノというほどではないがやや性的な画像 #そのほか、'drawings'(健全なアニメ等のイラスト), 'neutral'(普通の実写画像)というNSFWではない要素もあるが、 #今回は必要ないので無視。 for k in range(duplicating[video_id]): one_batch_features.append(one_video_feature) pd_output_features['nsfw_score_hentai'].append( nsfw_score_dict[key]['hentai']) pd_output_features['nsfw_score_porn'].append( nsfw_score_dict[key]['p**n']) pd_output_features['nsfw_score_sexy'].append( nsfw_score_dict[key]['sexy']) output_features.extend(one_batch_features) output_features = np.array(output_features) return output_features, pd_output_features