深度学习AI构图智能裁图显著性检测美感质量评价
Posted 踟蹰横渡口,彳亍上滩舟。
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了深度学习AI构图智能裁图显著性检测美感质量评价相关的知识,希望对你有一定的参考价值。
深度学习、AI构图、智能裁图、显著性检测、美感质量评价
基于美感数据集和改进的Alexnet-SPP的AI构图智能裁图
基于美感数据集和改进的Alexnet-SPP的显著性检测
部分代码下载地址:下载地址
实现思路和流程图
1. 基于显著性数据集,利用Alexnet-SPP回归出图像的显著性区域;
2. 基于显著性区域生成4x4或者5x5的候选框;
3. 利用美感质量数据集训练度量学习Siamese Network,对显著性候选框进行打分。
4. 对打分后的候选框进行排序,进行智能裁图。
效果展示:
测试代码:
import tensorflow as tf
import numpy as np
import cv2
import itertools
tf.reset_default_graph()
def gen_datalabel(filePath):
f = open(filePath)
lines = f.readlines()
lenNum = len(lines)
Num = 0
imPath =[]
imgs = []
for line in lines:
Num = Num + 1
result = line.split(',')
im = result[0]
print im
imPath.append(im)
print (Num,'--',lenNum, im)
img = cv2.imread(im)
img = cv2.resize(img, (640, 480))
imgs.append(img)
return imPath, np.asarray(imgs, np.float32)
def generate_box(x, y, z, w, img, imgPath):
box_centerx = int((x + z) / 2.0)
box_centery = int((y + w) / 2.0)
ratiosx = [1.0, 1.05, 1.1, 1.2, 1.3, 1.5]
ratiosy = [1.0, 1.05, 1.1, 1.2, 1.3, 1.5]
# ratiosx = [0.9, 0.95, 1.0, 1.05, 1.1, 1.15, 1.2, 1.3, 1.5]
# ratiosy = [0.9, 0.95, 1.0, 1.05, 1.1, 1.15, 1.2, 1.3, 1.5]
bbox_result = []
bbox_result.append(imgPath)
print (imgPath)
for ratiox, ratioy in itertools.product(ratiosx, ratiosy):
boxw = int((z - x) * ratiox)
boxh = int((w - y) * ratioy)
gen_boxx = max(box_centerx - (boxw / 2), 0)
gen_boxy = max(box_centery - (boxh / 2), 0)
gen_boxz = min(box_centerx + (boxw / 2), img2.shape[1])
gen_boxw = min(box_centery + (boxh / 2), img2.shape[0])
bbox_result.append(gen_boxx)
bbox_result.append(gen_boxy)
bbox_result.append(gen_boxz)
bbox_result.append(gen_boxw)
print(gen_boxx, gen_boxy, gen_boxz, gen_boxw)
cv2.rectangle(img, (gen_boxx, gen_boxy), (gen_boxz, gen_boxw), (0, 255, 0), 5)
return img, bbox_result
filePath = 'data_lab.txt'
imPath, data = gen_datalabel(filePath)
bbox_file = open('bbox_file_lab.txt', 'a')
W_image = 640
H_image = 480
with tf.Session() as sess:
saver = tf.train.import_meta_graph('./model/model.ckpt.meta')
saver.restore(sess, tf.train.latest_checkpoint('./model/'))
graph = tf.get_default_graph()
x_input = graph.get_tensor_by_name("x_input:0")
keep_prob = graph.get_tensor_by_name("Placeholder_1:0")#attention to modify
feed_dict = {x_input: data, keep_prob: 0.5}
logits = graph.get_tensor_by_name("logits_eval:0")
cls_result = sess.run(logits, feed_dict)
for i in range(len(imPath)):
# result_path1 = './lab_data/' + (imPath[i].split('/'))[2]
# result_path2 = './lab_crop/' + (imPath[i].split('/'))[2]
result_path3 = './lab_candidate/' + (imPath[i].split('/'))[2]
img2 = cv2.imread(imPath[i])
retxx = float(img2.shape[1]) / W_image
retyy = float(img2.shape[0]) / H_image
# cv2.rectangle(img2, ( max(int(retxx * cls_result[i][0]), 0), max(int(retyy * cls_result[i][1]), 0)),
# ( min(int(retxx * cls_result[i][2]), img2.shape[1]), min(int(retyy * cls_result[i][3]), img2.shape[0])), (0, 0, 0), 5)
#cv2.imwrite(result_path2, img2)
xx, yy, ww, hh = int( max(retxx * cls_result[i][0], 0)), int( max(retyy * cls_result[i][1], 0)), int( min(retxx * cls_result[i][2], img2.shape[1])), int( min(retyy * cls_result[i][3], img2.shape[0]))
img3, bbox_data= generate_box(xx, yy, ww, hh, img2, imPath[i])
cv2.rectangle(img3, (xx, yy), (ww, hh),(0, 0, 0), 25)
cv2.imwrite(result_path3, img3)
for j in range(len(bbox_data)):
bbox_file.write(str(bbox_data[j]) + ',')
if j % 4 == 0:
bbox_file.write('bbox' + str(j / 4) + ',')
bbox_file.write('\\n')
bbox_file.close()
以上是关于深度学习AI构图智能裁图显著性检测美感质量评价的主要内容,如果未能解决你的问题,请参考以下文章