做背景去除,自己的抠图一直不理想,于是想到用网上的api抠图,但是去除背景后的图片用cv2显示出来和原图是一样的,不知道怎么解决。
代码如下:
import requests, os
import json
import hashlib, base64, hmac
import sys
info = sys.version_info
if info[0] < 3:
is_python3 = False
else:
is_python3 = True
app_key = '204056920'
secret = '2APXsC7mz4GEEtVJu3FD4s0W7YfE4TWH'
file_name = './1.jpg'
import cv2
import numpy
cv2.imshow('img',cv2.imread(file_name))
cv2.waitKey(0)
stage = 'RELEASE' # 正式环境
#stage = 'TEST' # 测试环境
# 普通物品扣图接口
def params_of_object_matting(photo_base64):
print ('测试普通物品扣图接口 ...')
host = 'https://objseg.market.alicloudapi.com'
uri = '/commonseg/rgba'
return host, uri, {
'photo': photo_base64
}
def test_segment():
with open(file_name,'rb') as fp:
photo_base64 = base64.b64encode(fp.read())
if is_python3:
photo_base64 = photo_base64.decode('utf8')
_, photo_type = os.path.splitext(file_name)
photo_type = photo_type.lstrip('.')
# host, uri, body_json = params_of_portrait_matting(photo_base64, photo_type)
host, uri, body_json = params_of_object_matting(photo_base64)
# host, uri, body_json = params_of_idphoto(photo_base64, photo_type)
# host, uri, body_json = params_of_head(photo_base64, photo_type)
# host, uri, body_json = params_of_crop(photo_base64)
api = host + uri
body = json.dumps(body_json)
md5lib = hashlib.md5()
if is_python3:
md5lib.update(body.encode('utf8'))
else:
md5lib.update(body)
body_md5 = md5lib.digest()
body_md5 = base64.b64encode(body_md5)
if is_python3:
body_md5 = body_md5.decode('utf8')
method = 'POST'
accept = 'application/json'
content_type = 'application/octet-stream; charset=utf-8'
date_str = ''
headers = ''
string_to_sign = method + '\n' \
+ accept + '\n' \
+ body_md5 + '\n' \
+ content_type + '\n' \
+ date_str + '\n' \
+ headers \
+ uri
if is_python3:
signed = hmac.new(secret.encode('utf8'),
string_to_sign.encode('utf8'),
digestmod=hashlib.sha256).digest()
else:
signed = hmac.new(secret, string_to_sign, digestmod=hashlib.sha256).digest()
signed = base64.b64encode(signed)
if is_python3:
signed = signed.decode('utf8')
headers = {
'Accept': accept,
'Content-MD5': body_md5,
'Content-Type': content_type,
'X-Ca-Key': app_key,
'X-Ca-Stage': stage,
'X-Ca-Signature': signed
}
resp = requests.post(api, data=body, headers=headers)
try:
global res
res = resp.content
res = json.loads(res)
#print ('res:', res)
if str(res['status']) == '0':
print ('成功!')
else:
print ('失败!')
except:
print('failed parse:', resp)
if __name__ == "__main__":
test_segment()
img_src = res['data']['result']
import os,base64
import requests as req
from PIL import Image
from io import BytesIO
response = req.get(img_src)
# 内存中打开图片
image = Image.open(BytesIO(response.content))
# 图片的base64编码
ls_f = base64.b64encode(BytesIO(response.content).read())
# base64编码解码
imgdata = base64.b64decode(ls_f)
# 图片文件保存
file = open('test.png','wb')
file.write(imgdata)
file.close()
cv2.imshow('img',cv2.imread('./test.png'))
这是原图
一开始以为是imread()函数少读了一个通道,后来发现都是jpg格式,不存在alpha通道问题
请问怎样才能把去除背景的图用cv2读出来?
把16行的#cv2.waitKey(0) 注释掉,这句话会让代码一直停留在此处无法向下运行。运行结果保存在当前文件夹下.png