Spaces:
Paused
Paused
Upload face_detect.py
Browse files- face_detect.py +44 -0
face_detect.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""
|
| 3 |
+
Created on Fri Jun 2 11:24:19 2023
|
| 4 |
+
|
| 5 |
+
@author: admin
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
import gradio as gr
|
| 9 |
+
import requests
|
| 10 |
+
import base64
|
| 11 |
+
import cv2
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def face_detect(img):
|
| 15 |
+
success,encoded_image = cv2.imencode(".jpg",img)
|
| 16 |
+
img_test = encoded_image.tostring()
|
| 17 |
+
img_test = base64.b64encode(img_test)
|
| 18 |
+
request_url = "https://aip.baidubce.com/rest/2.0/face/v3/detect"
|
| 19 |
+
params={'image':str(img_test,'utf-8'),'image_type':'BASE64','face_field':'age,beauty,faceshape,gender,glasses,landmark'}
|
| 20 |
+
|
| 21 |
+
access_token = '24.c6d21fc24e374bc693685b79eb907c28.2592000.1688272635.282335-27897283'
|
| 22 |
+
request_url = request_url + "?access_token=" + access_token
|
| 23 |
+
headers ={'content-type':'application/json'}
|
| 24 |
+
response =requests.post(request_url,data=params,headers=headers)
|
| 25 |
+
# response =requests.post(request_url,data=params)
|
| 26 |
+
content = response.json()
|
| 27 |
+
|
| 28 |
+
left_top = (int(content['result']['face_list'][0]['location']['left']), int(content['result']['face_list'][0]['location']['top']))
|
| 29 |
+
right_bottom = (int(left_top[0] + content['result']['face_list'][0]['location']['width']),int(left_top[1] + content['result']['face_list'][0]['location']['height']))
|
| 30 |
+
cv2.rectangle(img, left_top, right_bottom, (255, 0, 0), 2)
|
| 31 |
+
return img
|
| 32 |
+
|
| 33 |
+
with gr.Blocks() as demo:
|
| 34 |
+
gr.Markdown("Face detect.")
|
| 35 |
+
|
| 36 |
+
with gr.Row():
|
| 37 |
+
image_input = gr.Image()
|
| 38 |
+
image_output = gr.Image()
|
| 39 |
+
image_button = gr.Button("Detect")
|
| 40 |
+
|
| 41 |
+
image_button.click(face_detect, inputs=image_input, outputs=image_output)
|
| 42 |
+
|
| 43 |
+
gr.close_all()
|
| 44 |
+
demo.launch()
|