- 图像修复——图像去雾功能
- 图像修复——清晰度增强功能
- 实现上述UI界面
一、图像去雾功能
接口请求说明:
(一)接口请求:
HTTP 方法:POST
请求URL: https://aip.baidubce.com/rest/2.0/image-classify/v1/body_seg
URL参数:
access_token 通过API Key和Secret Key获取的access_token,参考“Access Token获取”
Header如下:
Content-Type application/x-www-form-urlencoded
Body中放置请求参数
(二)请求代码:
# 获取access_token
client_id = '----------------' # client_id 为官网获取的AK
client_secret = '-----------------------------' # client_secret 为官网获取的SK
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + client_id + '&client_secret=' + client_secret
response = requests.get(host)
if response:
print(response.json())
token = response.json()['access_token']
# 图像去雾
request_url = "https://aip.baidubce.com/rest/2.0/image-process/v1/dehaze"
# imgfile = 'F:/Documents/GitHub/mini_image/part4/images_col/test.jpg'
(三)功能实现
imgfile = instance.root_path+'/part4/images_mag/test.jpg'
imgfile = instance.magnify_original[0][0]
print(instance.magnify_original[0][0])
f = open(imgfile, 'rb') # 二进制方式打开图片文件
img = base64.b64encode(f.read())
params = {"image": img}
access_token = token
request_url = request_url + "?access_token=" + access_token
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
if response:
print(response.json())
img = base64.b64decode(response.json()['image'])
img_out= instance.root_path + '/part4/images_mag/result.jpg'
file = open(img_out, 'wb')
file.write(img)
res=img
# res=np.hstack([img_ori,img])
# img_array = np.frombuffer(img, np.uint8)
# 结果图转换成opencv可用格式
res_array=np.frombuffer(res, np.uint8)
# 原图转换
res=cv2.imdecode(res_array, cv2.COLOR_RGB2BGR)
cv2.imshow("Magnify_Result", res)
cv2.waitKey(0)
file.close
结果展示:
去雾前:
去雾后:
二、图像清晰度增强
# 图像清晰度增强
request_url = "https://aip.baidubce.com/rest/2.0/image-process/v1/image_definition_enhance"
# imgfile = 'F:/Documents/GitHub/mini_image/part4/images_col/test.jpg'
imgfile = instance.root_path+'/part4/images_cla/test.jpg'
print(imgfile)
print(instance.clarity_original[0][0])
imgfile = instance.clarity_original[0][0]
f = open(imgfile, 'rb') # 二进制方式打开图片文件
img = base64.b64encode(f.read())
img_ori=img
params = {"image": img}
access_token = token
request_url = request_url + "?access_token=" + access_token
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
if response:
print(response.json())
img = base64.b64decode(response.json()['image'])
img_out= instance.root_path + '/part4/images_cla/result.jpg'
file = open(img_out, 'wb')
file.write(img)
res=img
# res=np.hstack([img_ori,img])
# img_array = np.frombuffer(img, np.uint8)
# 结果图转换成opencv可用格式
res_array=np.frombuffer(res, np.uint8)
# 原图转换
res=cv2.imdecode(res_array, cv2.COLOR_RGB2BGR)
cv2.imshow("Clarity_Result", res)
cv2.waitKey(0)
file.close
结果展示:
修复前:
修复后: