def com2imgs(): if request.method == "POST": imgbase64_1 = request.form.get('imgbase64_1') imgbase64_1 = base64.b64decode(imgbase64_1) imgbase64_2 = request.form.get('imgbase64_2') imgbase64_2 = base64.b64decode(imgbase64_2) randname1 = getRandomSet(20) + '.jpg' randname2 = getRandomSet(20) + '.jpg' file = open(randname1, 'wb') file.write(imgbase64_1) file.close() file = open(randname2, 'wb') file.write(imgbase64_2) file.close() try: img1 = cv2.imread(randname1) img2 = cv2.imread(randname2) hash1 = fun_Hash(img1) hash2 = fun_Hash(img2) score = com2hashstr(hash1, hash2) os.remove(randname1) os.remove(randname2) return {'sign': 1, 'score': score} except: os.remove(randname1) os.remove(randname2) return {'sign': -1, 'score': None} else: return "<h1>compare img, please use post !</h1>"
def get_limit_imgpath(dstimgpath, _hash_strs, _imgpaths): limit = 85 dstimg = cv2.imread(dstimgpath) dstimg_2 = cv2.flip(dstimg, 1) # check overturn image on the same time dst_hash_str = fun_Hash(dstimg) dst_hash_str_2 = fun_Hash(dstimg_2) out_imgpaths = [] for n in range(len(_hash_strs)): temp_value = com2hashstr(dst_hash_str, _hash_strs[n]) temp_value_2 = com2hashstr(dst_hash_str_2, _hash_strs[n]) temp_value = max(temp_value, temp_value_2) # use max score print('5444646464646464', temp_value) if temp_value > limit: out_imgpaths.append(_imgpaths[n]) return out_imgpaths
def load_img(orgimgroot): hash_str_list = [] imgpath_list = [] orgimg_list = os.listdir(orgimgroot) cnt = 0 cnt_error = 0 for n in orgimg_list: imgpath = os.path.join(orgimgroot, n) print('>>>', cnt, imgpath) cnt = cnt + 1 try: img = cv2.imread(imgpath) img.shape except: print('read img error: ', imgpath) cnt_error = cnt_error + 1 continue img_hash = fun_Hash(img) hash_str_list.append(img_hash) # 拼接es数据库图像url,接下来仍然需要调整的 !!!!!!!!!! imgpath = [ "allimage", "https://filefusion.ccwb.cn/files/2020/12/images/" + n ] imgpath_list.append(imgpath) print('successful and error: ', cnt, cnt_error) return hash_str_list, imgpath_list
def load_img(imgs_record_txt): hash_str_list = [] img_from_id_list = [] # orgimg_list = os.listdir(orgimgroot) cnt = 0 cnt_error = 0 for n_ in open(imgs_record_txt): n = n_[:-1] imgpath = n.split(',')[0] print('>>>', cnt, imgpath) cnt = cnt + 1 try: img = cv2.imread(imgpath) img.shape except: print('read img error: ', imgpath) cnt_error = cnt_error + 1 continue img_hash = fun_Hash(img) hash_str_list.append(img_hash) # 拼接es数据库图像url,接下来仍然需要调整的 !!!!!!!!!! img_from_id = n.split(',')[1] img_from_id_list.append(img_from_id) print('successful and error: ', cnt, cnt_error) return hash_str_list, img_from_id_list # 图像hash和来源id对应
def get_limit_imgpath(dstimgpath, _hash_strs, _imgpaths, limit): limit = int(limit) dstimg = cv2.imread(dstimgpath) dstimg_2 = cv2.flip(dstimg, 1) # check overturn image on the same time dst_hash_str = fun_Hash(dstimg) dst_hash_str_2 = fun_Hash(dstimg_2) out_imgpaths = [] similar_scores = [] for n in range(len(_hash_strs)): temp_value = com2hashstr(dst_hash_str, _hash_strs[n]) temp_value_2 = com2hashstr(dst_hash_str_2, _hash_strs[n]) temp_value = max(temp_value, temp_value_2) # use max score if temp_value > limit: out_imgpaths.append(_imgpaths[n]) similar_scores.append(temp_value) # 查询es数据库,返回similar_infos similar_infos = [] for id in range(len(out_imgpaths)): id_img_str = out_imgpaths[id] str1 = id_img_str[0] str2 = id_img_str[1] # 根据图像URL(视为唯一id),模糊匹配title,在解析title信息 print('==========', str2) body = {"query": {"match": {str1: str2}}} info_org = es_db.search(index='fwnews', body=body) info_dst = info_org.get('hits').get('hits')[0].get('_source') img_url = str2 id_title = info_dst.get('id') all_imgs = info_dst.get('allimage') title_url = info_dst.get('source_url') web_source = info_dst.get('source') title = info_dst.get('title') img_similar_score = similar_scores[id] similar_infos.append({ 'img_url': img_url, 'title': title, 'title_url': title_url, 'web_source': web_source, 'img_similar_score': img_similar_score, 'id_title': id_title, 'all_imgs': all_imgs }) if len(similar_scores) == 0: return similar_infos, 0 else: return similar_infos, max(similar_scores)
def load_img(orgimgroot): hash_str_list = [] imgpath_list = [] orgimg_list = os.listdir(orgimgroot) for n in orgimg_list: imgpath = os.path.join(orgimgroot, n) try: img = cv2.imread(imgpath) except: print('read img error: ', imgpath) img = None img_hash = fun_Hash(img) hash_str_list.append(img_hash) imgpath_list.append(imgpath) return hash_str_list, imgpath_list
def upimgs(upimgroot, orgimgroot): _hash_strs = [] _imgpaths = [] upimg_list = os.listdir(upimgroot) for n in upimg_list: imgpath = os.path.join(upimgroot, n) copy_path = os.path.join(orgimgroot, n) shutil.copyfile(imgpath, copy_path) os.remove(imgpath) try: img = cv2.imread(copy_path) except: print('read img error:', copy_path) img = None img_hash = fun_Hash(img) _hash_strs.append(img_hash) _imgpaths.append(copy_path) print('adding: ', copy_path) return _hash_strs, _imgpaths