File size: 1,509 Bytes
033be9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b505c3d
033be9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# -*- coding: utf-8 -*-
"""
Created on Fri Jun  2 11:24:19 2023

@author: admin
"""

import gradio as gr
import requests
import base64
import cv2


def face_detect(img):
    success,encoded_image = cv2.imencode(".jpg",img)
    img_test = base64.b64encode(encoded_image)
    request_url = "https://aip.baidubce.com/rest/2.0/face/v3/detect"
    params={'image':str(img_test,'utf-8'),'image_type':'BASE64','face_field':'age,beauty,faceshape,gender,glasses,landmark'}
    
    access_token = '24.c6d21fc24e374bc693685b79eb907c28.2592000.1688272635.282335-27897283'
    request_url = request_url + "?access_token=" + access_token
    headers ={'content-type':'application/json'}
    response =requests.post(request_url,data=params,headers=headers)
    # response =requests.post(request_url,data=params)
    content = response.json()
    
    left_top = (int(content['result']['face_list'][0]['location']['left']), int(content['result']['face_list'][0]['location']['top']))
    right_bottom = (int(left_top[0] + content['result']['face_list'][0]['location']['width']),int(left_top[1] + content['result']['face_list'][0]['location']['height']))
    cv2.rectangle(img, left_top, right_bottom, (255, 0, 0), 2)
    return img

with gr.Blocks() as demo:
    gr.Markdown("Face detect.")

    with gr.Row():
        image_input = gr.Image()
        image_output = gr.Image()
    image_button = gr.Button("Detect")

    image_button.click(face_detect, inputs=image_input, outputs=image_output)

gr.close_all()
demo.launch()