about the author
Yan Song, male, School of Electronic Information, Xi'an Polytechnic University, 2022 graduate student
Research direction: robot grasping detection
Email: [email protected]
Zhu Ying, female, School of Electronic Information, Xi'an Polytechnic University, 2022 graduate student
Research direction: contraband detection
Email: [email protected]
Wang Zeyu, male, School of Electronic Information, Xi'an Polytechnic University, 2022 graduate student, Zhang Hongwei Artificial Intelligence Research Group
Research direction: machine vision and artificial intelligence
Email: [email protected]
1. Baidu API to realize the logo trademark recognition interface introduction
This request is used to detect and identify logo information such as the Taiwan logo and brand logo in the picture. That is, for an input picture (which can be decoded normally and has an appropriate aspect ratio), output the name, position and confidence of the logo in the picture.
When in use, you can directly call the logo recognition-retrieval interface, which supports the identification of more than 20,000 types of logo names; when the effect is not good, you can build a sub-library (create an application in the console and apply for a library) and complete the self-sufficiency by calling the logo entry interface Define the logo into the library, and then call the logo recognition-retrieval interface, choose to search in the custom logo library to improve the recognition effect.
2. Call Baidu API to realize logo trademark recognition process
1. Register Baidu Smart Cloud and create an application
Link to Baidu Smart Cloud: (https://cloud.baidu.com/)
After registration, click on the console, first receive free resources, and then click Create Application. After creating the application, you can view the application, and you can see the API Key and Secret key.
Figure 1 Create an application
Figure 2 View application
2. Python code for debugging
Let’s take a look at the request parameters and return instructions for calling the API. The request parameters include three parameters, namely: image, url and custom_lib. The input method is to choose one of image and url. If the user needs to create his own sub-library, then custom_lib is set to true, otherwise it is false. The return parameters mainly include: problem location (log_id), number of returned results (result_num), returned result (result), name (name), confidence (probability) and so on.
Figure 3 request parameters
Figure 4 Return to Description
The following is the complete experimental code:
import base64
import urllib
import requests
API_KEY = ""
SECRET_KEY = ""
def main():
url = "https://aip.baidubce.com/rest/2.0/image-classify/v2/logo?access_token=" + get_access_token()
#payload =get_file_content_as_base64("E:\研一 课\AR\华为logo.jpg", True)
f = open('/data/Users/chenmd/myProject/csdn/百度API/Logo商标识别/hauwei.jpg', 'rb')
img = base64.b64encode(f.read()).decode("utf-8")
host = url + '?access_token=' +'access_token' # 我的access_token
params = {
'image': img}
#response = requests.post(host, data=params, headers=headers) # 注意用data传值,使用json会报错
#print("Status code:" + str(response.status_code) + " content:" + response.text)
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=params)
print(response.text)
def get_access_token():
"""
使用 AK,SK 生成鉴权签名(Access Token)
:return: access_token,或是None(如果错误)
"""
url = "https://aip.baidubce.com/oauth/2.0/token"
params = {
"grant_type": "client_credentials", "client_id": API_KEY, "client_secret": SECRET_KEY}
return str(requests.post(url, params=params).json().get("access_token"))
if __name__ == '__main__':
main()
3. Online debugging:
Enter the client_id, client_secret and the path of your own image in the debug bar, and then debug, you can see the debug results.
Figure 5 Debugging results
Return example:
1.1 Comparing messy logo debugging results
Figure 6 Messy logo
return results: You can see that a total of 5 sets of results are returned, and one set of "Zebra Office" does not meet expectations, which proves that the recognized logo should not have a complicated background as much as possible .
{
"result_num": 5,
"result": [
{
"name": "可口可乐",
"type": 1,
"probability": 0.95749333630437,
"location": {
"height": 123,
"left": 44,
"top": 22,
"width": 313
}
},
{
"name": "可口可乐",
"type": 0,
"probability": 0.97189044952393,
"location": {
"height": 123,
"left": 44,
"top": 22,
"width": 313
}
},
{
"name": "斑马办公",
"type": 1,
"probability": 0.33733308532021,
"location": {
"height": 57,
"left": 118,
"top": 127,
"width": 204
}
},
{
"name": "可口可乐",
"type": 0,
"probability": 0.83195910211337,
"location": {
"height": 226,
"left": 36,
"top": 0,
"width": 321
}
},
{
"name": "可口可乐",
"type": 1,
"probability": 0.32056304758245,
"location": {
"height": 226,
"left": 36,
"top": 0,
"width": 321
}
}
],
"log_id": 1657261019508606700
}
1.2 The logo recognition result of the complete compact bounding box image
Figure 7 The logo of the complete compact bounding box image
Returned results: You can see that a total of 4 sets of results have been returned, and all of them are in line with expectations
{
"result_num": 4,
"result": [
{
"name": "合生元",
"type": 1,
"probability": 0.96611329092496,
"location": {
"height": 34,
"top": 7,
"width": 121,
"left": 36
}
},
{
"name": "合生元",
"type": 0,
"probability": 0.98904836588892,
"location": {
"height": 34,
"top": 7,
"width": 121,
"left": 36
}
},
{
"name": "合生元",
"type": 1,
"probability": 0.5572005059984,
"location": {
"height": 32,
"top": 6,
"width": 64,
"left": 81
}
},
{
"name": "合生元",
"type": 1,
"probability": 0.39284993518483,
"location": {
"height": 36,
"top": 6,
"width": 63,
"left": 61
}
}
],
"log_id": 1657263068743726800
}
4. Summary
(1) When selecting a logo image for input, try to choose a logo that is clear and tightly surrounded, so that the recognition result is accurate and has a high degree of confidence; (2) If the user needs to
create his own sub-library, before cropping the image logo When cutting, try to cut it in blocks. For example, the logo of the Industrial and Commercial Bank of China is generally English + Chinese + small logo. When cutting, do not put three together to cut, but three separate cuts and put them in the sub-library , so that the recognition result will be more accurate.