olive100 commited on
Commit
ad50d90
1 Parent(s): 188dc3d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -31
app.py CHANGED
@@ -13,53 +13,60 @@ def get_token(client_id, client_secret):
13
  resultJson = response.json()
14
  return resultJson['access_token']
15
 
16
- def face_merge(image_template,image_target):
17
  success,encode_image_temp = cv2.imencode(".jpg",image_template) #注意这句编码,否则图像质量不合格
18
  image_temp = base64.b64encode(encode_image_temp)
19
  success,encode_image_targ = cv2.imencode(".jpg",image_target) #注意这句编码,否则图像质量不合格
20
  image_targ = base64.b64encode(encode_image_targ)
21
  request_url = 'https://aip.baidubce.com/rest/2.0/face/v1/merge'
22
- params = {
23
- "image_template": { # 将其中一张图片设置为模板(相当于底层图片)
24
- "image": str(image_temp,'utf-8'),
25
- "image_type": "BASE64",
26
- "quality_control": "NORMAL"
27
- },
28
- "image_target": { # 将其中一张图片设置为目标(相当于人脸信息“叠加”到模板上)
29
- "image": str(image_targ,'utf-8'),
30
- "image_type": "BASE64",
31
- "quality_control": "NORMAL"
32
- },
33
- "merge_degree": "HIGH", # 融合程度
34
- "position": 2, #左上角水印
35
- "language": 1, #英语水印
36
- }
37
- params=json.dumps(params)
38
  access_token = get_token('gZBF8TaiLfewLu4XdWIVe5H1', 'O7zkY3cc1OYu2GHwqvsyxI4YaCgdRn7A')
39
  request_url = request_url + "?access_token=" + access_token
40
  headers ={'content-type':'application/json'}
41
- result = requests.post(request_url, data=params, headers=headers).json() # 经过调用百度云接口服务器返回的内容(融合结果)
42
- if result['error_code'] == 0:
43
- res = result["result"]["merge_image"]
44
- res = base64.b64decode(res)
45
- nparr = np.frombuffer(res,np.uint8)
46
- img_np = cv2.imdecode(nparr,cv2.IMREAD_COLOR)
47
- return img_np
48
- else:
49
- print(str(result['error_code']) + result['error_msg'])
50
- return result['error_code']
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
 
53
  with gr.Blocks() as demo:
54
  gr.Markdown("Face merge.")
55
 
56
  with gr.Row():
57
- image_template = gr.Image()
58
- image_target = gr.Image()
59
- image_output = gr.Image()
 
 
60
  image_button = gr.Button("Merge")
61
 
62
- image_button.click(face_merge, inputs=[image_template,image_target], outputs=image_output)
63
 
64
  gr.close_all()
 
65
  demo.launch()
 
13
  resultJson = response.json()
14
  return resultJson['access_token']
15
 
16
+ def face_merge(image_template,image_target,steps):
17
  success,encode_image_temp = cv2.imencode(".jpg",image_template) #注意这句编码,否则图像质量不合格
18
  image_temp = base64.b64encode(encode_image_temp)
19
  success,encode_image_targ = cv2.imencode(".jpg",image_target) #注意这句编码,否则图像质量不合格
20
  image_targ = base64.b64encode(encode_image_targ)
21
  request_url = 'https://aip.baidubce.com/rest/2.0/face/v1/merge'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  access_token = get_token('gZBF8TaiLfewLu4XdWIVe5H1', 'O7zkY3cc1OYu2GHwqvsyxI4YaCgdRn7A')
23
  request_url = request_url + "?access_token=" + access_token
24
  headers ={'content-type':'application/json'}
25
+
26
+ for i in range(steps,-1,-1):
27
+ params = {
28
+ "image_template": { # 将其中一张图片设置为模板(相当于底层图片)
29
+ "image": str(image_temp,'utf-8'),
30
+ "image_type": "BASE64",
31
+ "quality_control": "NORMAL"
32
+ },
33
+ "image_target": { # 将其中一张图片设置为目标(相当于人脸信息“叠加”到模板上)
34
+ "image": str(image_targ,'utf-8'),
35
+ "image_type": "BASE64",
36
+ "quality_control": "NORMAL"
37
+ },
38
+ "merge_degree": "HIGH", # 融合程度
39
+ "position": 2, #左上角水印
40
+ "language": 1, #英语水印
41
+ "version": 4.0,
42
+ "alpha": i/steps,
43
+ }
44
+ params=json.dumps(params)
45
+ result = requests.post(request_url, data=params, headers=headers).json() # 经过调用百度云接口服务器返回的内容(融合结果)
46
+ if result['error_code'] == 0:
47
+ res = result["result"]["merge_image"]
48
+ res = base64.b64decode(res)
49
+ nparr = np.frombuffer(res,np.uint8)
50
+ img_np = cv2.imdecode(nparr,cv2.IMREAD_COLOR)
51
+ yield img_np
52
+ else:
53
+ print(str(result['error_code']) + result['error_msg'])
54
+ return result['error_code']
55
 
56
 
57
  with gr.Blocks() as demo:
58
  gr.Markdown("Face merge.")
59
 
60
  with gr.Row():
61
+ image_template = gr.Image(label="模板图片")
62
+ image_target = gr.Image(label="目标图片")
63
+ with gr.Row():
64
+ steps = gr.Slider(1, 10, value=1, step=1, label="变化步数")
65
+ image_output = gr.Image(label="融合结果")
66
  image_button = gr.Button("Merge")
67
 
68
+ image_button.click(face_merge, inputs=[image_template,image_target,steps], outputs=image_output)
69
 
70
  gr.close_all()
71
+ demo.queue()
72
  demo.launch()