Spaces:
Paused
Paused
| # -*- 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() |