@ the Author: Runsen
This project focuses on image recognition. By invoking Baidu API interface, many humanized functions can be realized, such as gesture recognition, portrait segmentation and appearance level scoring.
This time Gitchat paid for articles, but the subscription was too low. So can only regard as the public number article hair.
This project combines artificial intelligence and GUI graphic interface development to meet users’ needs for image recognition.
Main technical points used in this project:
- Baidu API interface call
- Tkinker
- Opencv calls USB camera
The functional requirements
The main functions of this Python project are to call the computer camera to take photos, and use Baidu API interface to achieve appearance level scoring, gesture recognition and matting.
API documentation for functional requirements:
Appearance rating: https://ai.baidu.com/ai-doc/FACE/yk37c1u4t
Gesture recognition: https://ai.baidu.com/ai-doc/BODY/4k3cpywrv
Portrait segmentation: https://ai.baidu.com/ai-doc/BODY/Fk3cpyxua
function | The implementation process | Interface API |
---|---|---|
Appearance level score (FACE_detect) | The camera is called for photo recognition, and the recognition results include age, gender, race and appearance level score | Aip.baidubce.com/rest/2.0/fa… |
Gesture Recognition | The camera is called for photo recognition, and the recognition result is the Chinese and English general idea of the gesture | Aip.baidubce.com/rest/2.0/im… |
Portrait Segmentation (body_seg) | Call the camera for photo identification, figure matting | Aip.baidubce.com/rest/2.0/im… |
Obtain the Access token value
This project adopts the method of encapsulating classes to write the code, and it is necessary to encapsulate the Baidu_API into classes.
The appearance level was rated
Identification results include age, sex, race, appearance level score (0-100)
The result of the above image is 47.34 22 male yellow
Gesture recognition
The recognition results include the Chinese and English meanings of the gestures
The result of the image above: Five, Five
Portrait segmentation matting
The result is a matting of the person
The complete code
"' @ Author: Runsen @ WeChat: RunsenLiu @ WeChat public number: the king of the Python @ CSDN: https://blog.csdn.net/weixin_44510615 @ making: https://github.com/MaoliRUNsen @ Date: 2020/11/29 "'
import requests
import base64
class Baidu_API() :
def __init__(self) :
self.headers={'Content-Type': 'application/json; charset=UTF-8'}
self.AppID = '23061934' # Baidu application account ID
self.APIKey = '0EGiM1wDAH5kmFAvhYVLYCbs' # Authorization for interface access
self.SecretKey = 'KVugHxxu4uq203b111SwVY2w98Cd9D70' # key
Get the access token value
def get_access_token(self) :
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id= & client_secret = {} {}'.format(self.APIKey,self.SecretKey)
response = requests.get(host,self.headers)
if response:
access_token=response.json()['access_token']
return access_token
else:
print("Failed to obtain access_token")
Get base64 image data
def get_img_base64(self,img_path) :
Read the image in binary format and convert it to base64 format (use the binary to base64 function)
with open(img_path,'rb')as f:
# base64 encoding
img_data = base64.b64encode(f.read())
return img_data
# Function 1. Score your appearance level
def face_detect(self,img_path) :
try:
# Access the face detection API
base_url = "https://aip.baidubce.com/rest/2.0/face/v3/detect"
# Basic parameters
request_url = base_url + "? access_token=" + self.get_access_token()
params = {"image": self.get_img_base64(img_path),
"image_type": "BASE64"."face_field": "faceshape,facetype,beauty,age,beauty,glasses,gender,race"}
# start access
response = requests.post(url=request_url,
data=params,
headers=self.headers)
re = response.json()
score = re["result"] ["face_list"] [0] ['beauty']
age = re["result"] ["face_list"] [0] ['age']
gender = re["result"] ["face_list"] [0] ['gender'] ['type']
race = re["result"] ["face_list"] [0] ['race'] ['type']
# return data
print(score,age,gender,race)
return score,age,gender,race
except:
return 'Not correctly recognized, please try again'
# Feature 2. Gesture recognition
def gesture(self,img_path) :
try:
base_url = 'https://aip.baidubce.com/rest/2.0/image-classify/v1/gesture'
# Basic parameters
request_url = base_url + "? access_token=" + self.get_access_token()
params = {"image": self.get_img_base64(img_path),
"image_type": "BASE64"}
# start access
response = requests.post(url=request_url,
data=params,
headers=self.headers) # <class 'requests.models.Response'>
re = response.json() # <class 'dict'>
classname_en = re["result"] [0] ['classname']
classname_zh = self.translate(classname_en)
print(classname_en,classname_zh)
return classname_en,classname_zh
except:
return 'Not correctly identified, please try again'
# Function 3. Portrait segmentation matting
def body_seg(self,img_path, out_path) :
try:
base_url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/body_seg"
request_url = base_url + "? access_token=" + self.get_access_token()
params = {"image": self.get_img_base64(img_path)}
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
labelmap = base64.b64decode(response.json()['foreground'])
with open(out_path, 'wb') as fp:
fp.write(labelmap)
except:
return 'Not correctly recognized, please try again'
# Translation APi interface
def translate(self, query) :
url = 'http://fanyi.youdao.com/translate?&doctype=json&type=AUTO&i={}'.format(query)
r = requests.post(url=url)
return r.json()['translateResult'] [0] [0] ['tgt'].strip('the')
if __name__ == '__main__':
Api = Baidu_API()
Api.face_detect('me.jpg')
Api.gesture('gesture.jpg')
Api.body_seg('me.jpg'.'me.png')
Copy the code
GitHub, portal ~, GitHub, portal ~, GitHub, Portal ~