Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- os.environ["CUDA_VISIBLE_DEVICES"]="-1"
- import tensorflow as tf
- import numpy as np
- import cv2
- import matplotlib.pyplot as plt
- #from PIL import Image
- #import time
- tf.compat.v1.enable_eager_execution()
- #config = tf.ConfigProto()
- #config.intra_op_parallelism_threads = 1
- #config.inter_op_parallelism_threads = 1
- #tf.keras.backend.set_session(tf.Session(config=config))
- IMG_SIZE = 250
- fashion_filepath = 'model/Fashion_niche_resnet50_model_wo_opt.hdf5'
- beauty_health_filepath = 'model/Beauty_Health_niche_resnet50_model_wo_opt.hdf5'
- food_beverage_filepath = 'model/Food_Beverage_niche_resnet50_model_wo_opt.hdf5'
- lifestyle_travel_filepath = 'model/Lifestyle_Travel_niche_resnet50_model_wo_opt.hdf5'
- technology_filepath = 'model/Technology_niche_resnet50_model_wo_opt.hdf5'
- Fashion_classifier = tf.keras.models.load_model(fashion_filepath, custom_objects=None, compile=False)
- Beauty_Health_classifier = tf.keras.models.load_model(beauty_health_filepath, custom_objects=None, compile=False)
- Food_Beverage_classifier = tf.keras.models.load_model(food_beverage_filepath, custom_objects=None, compile=False)
- Lifestyle_Travel_classifier = tf.keras.models.load_model(lifestyle_travel_filepath, custom_objects=None, compile=False)
- Technology_classifier = tf.keras.models.load_model(technology_filepath, custom_objects=None, compile=False)
- def preprocess_image(image):
- old_size = image.shape[:2] # old_size is in (height, width) format
- ratio = float(IMG_SIZE)/max(old_size)
- new_size = tuple([int(x*ratio) for x in old_size])
- # new_size should be in (width, height) format
- image = cv2.resize(image, dsize = (new_size[1], new_size[0]), interpolation=cv2.INTER_NEAREST)
- delta_w = IMG_SIZE - new_size[1]
- delta_h = IMG_SIZE - new_size[0]
- top, bottom = delta_h//2, delta_h-(delta_h//2)
- left, right = delta_w//2, delta_w-(delta_w//2)
- color = [0, 0, 0]
- image = cv2.copyMakeBorder(image, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color)
- #image = cv2.resize(image, dsize=(IMG_SIZE, IMG_SIZE ), interpolation=cv2.INTER_NEAREST)
- image = (image/255.0) # normalize to [0,1] range
- return image
- def output(Status, fashion_score, beauty_health_score, food_beverage_score, lifestyle_travel_score, technology_score, images_flag):
- return {'Status': Status, 'Niche_score': {'Fashion': fashion_score, 'Beauty & Health': beauty_health_score, 'Food & Beverage': food_beverage_score,
- 'Lifestyle & Travel': lifestyle_travel_score, 'Technology': technology_score}, 'Pic_flag' : images_flag}
- def calculate_score(folderpath):
- user_im = []
- success_image = []
- image_files = os.listdir(folderpath)
- for image_path in image_files:
- try:
- image = plt.imread('{}/{}'.format(folderpath, image_path), format='jpeg')
- #image = file.read()
- #image = Image.open(io.BytesIO(image))
- #image = plt.imread(file, format='jpeg')
- user_im.append(preprocess_image(image))
- success_image.append(image_path)
- except:
- pass
- if len(user_im) > 0:
- tensor_image = np.asanyarray(user_im)
- else:
- return output('Failed', 0, 0, 0, 0, 0, 'Empty')
- fashion_proba_image = Fashion_classifier.predict(tensor_image)
- beauty_health_proba_image = Beauty_Health_classifier.predict(tensor_image)
- food_beverage_proba_image = Food_Beverage_classifier.predict(tensor_image)
- lifestyle_travel_proba_image = Lifestyle_Travel_classifier.predict(tensor_image)
- technology_proba_image = Technology_classifier.predict(tensor_image)
- flag_fashion = np.argmax(fashion_proba_image, axis = 1)
- flag_beauty_health = np.argmax(beauty_health_proba_image, axis = 1)
- flag_food_beverage = np.argmax(food_beverage_proba_image, axis = 1)
- flag_lifestyle_travel = np.argmax(lifestyle_travel_proba_image, axis = 1)
- flag_technology = np.argmax(technology_proba_image, axis = 1)
- images_flag = {}
- for i in range(len(success_image)):
- value = []
- if flag_fashion[i]==1:
- value.append('Fashion')
- if flag_beauty_health[i]==1:
- value.append('Beauty and Health')
- if flag_food_beverage[i] == 1:
- value.append('Food and Beverage')
- if flag_lifestyle_travel[i] == 1:
- value.append('Lifestyle and Travel')
- if flag_technology[i] == 1:
- value.append('Technology')
- images_flag[image_files[i]] = value
- fashion_score = flag_fashion.sum()/len(fashion_proba_image)
- beauty_health_score = flag_beauty_health.sum()/len(beauty_health_proba_image)
- food_beverage_score = flag_food_beverage.sum()/len(food_beverage_proba_image)
- lifestyle_travel_score = flag_lifestyle_travel.sum()/len(lifestyle_travel_proba_image)
- technology_score = flag_technology.sum()/len(technology_proba_image)
- return output('Success', fashion_score, beauty_health_score, food_beverage_score, lifestyle_travel_score, technology_score, images_flag)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement