When performing image recognition / classification using machine learning such as DNN, a large amount of training images are required when executing learning. There are many cases where data sets (image sets) published on the web etc. are used. However, in the case of face image classification / recognition, there are few data sets that are open to the public because of problems such as portrait rights. As a result, many people are having a hard time collecting learning images. So here's one way to create your own face image dataset.
First, collect images that may show the target face (hereinafter referred to as candidate images). The following collection methods are possible.
This time, I would like to write about ** How to collect candidate images using WebAPI service **.
There are many Web API services that return image information published on the web or SNS as a response, but Here we use the ** Bing Image Search API **, which is provided free of charge at Microsoft Cognitive Services.
In this collection method using the Bing Image Search API,
Take two steps. Therefore, as mentioned above, OpenCV is required as a library to import.
Microsoft Cognitive Services registration and Bing Image Search API usage registration procedure Please see the separate article for more information. How to register Microsoft Cognitive Services
To use the API, you will need a subscription key that you can get when you complete the API registration.
Basically, like a general Web API, by throwing an HTTP method (get request this time) to the API endpoint, You can receive the response information (json format) that stores the image URL that is the search result of the query. The code example that realized this is as follows.
# _*_ coding: utf-8 _*_
import requests
import urllib.request
import os.path
import cv2
import numpy as np
#API End Point and Subscription Key
REQUEST_BASE_URL = 'https://bingapis.azure-api.net/api/v5/images/search'
SUBSCRIPTION_KEY = '********************'
def api_request(query, count=10):
#header and parameter settings
headers = { 'Ocp-Apim-Subscription-Key': SUBSCRIPTION_KEY, }
params = {
'q': str(query),
'count': int(count),
'mkt': 'ja-JP',
'offset': '0',
}
#API throw
response = requests.get(REQUEST_BASE_URL, headers=headers, params=params)
res_headers = response.headers
status_code = response.status_code
results = None
#Content setting to be returned to the caller according to the status code and header content
if status_code == 200:
if 'content-length' in res_headers and int(res_headers['content-length']) == 0:
results = None
elif 'content-type' in res_headers and isinstance(res_headers['content-type'], str):
if 'application/json' in res_headers['content-type'].lower():
results = response.json()['value']['contentUrl'] if response.content else None
else
results = None
else:
#Forced termination if status code is other than 200
print("Error! - status code : {0}".format(response.status_code))
print("Message : {0}".format(response.json()['error']['message']))
exit()
#Returns the image URL that is the search result
return results
Please refer to Official API Reference for detailed specifications of API request and response. Please give me. Here, only the specifications that are considered to be the minimum required at the time of request are described.
Information item | Contents |
---|---|
Ocp-Apim-Subscription-Key | Subscription key string |
Parameter item | Data type | Contents |
---|---|---|
q | String | Character string that becomes a search query |
count | UnsignedShort | Number of images in search results returned in response |
mkt | String | Request The name of the region where the request is being sent (specified by Market Code) |
This time, only the image URL that is the search result is obtained from the response information of the API. In addition, the following information can be obtained with the Bing Image Search API.
--Image pixel size --Image file size --URL of the page where the image is posted --Image title --URL / size of thumbnail image
After getting the image URL set of the search result from the above method, Create a new candidate image by using OpenCV from that URL.
def make_image(image_url):
#Load image information of URL
resp = urllib.request.urlopen(image_url)
#Rebuild image memory buffer so that it can be read by OpenCV
image = np.asarray(bytearray(resp.read()), dtype=np.uint8)
#Read image from memory buffer
image = cv2.imdecode(image, cv2.IMREAD_COLOR)
#Export as a new image
cv2.imwrite("hoge.jpg ", image)
When rebuilding the memory buffer, set the data type to uint8 type.
This is because the pixel values that make up an image generally take an integer value within the interval [0,255].
Also, by specifying cv2.IMREAD_COLOR
when reading an image with the imdecode method,
It is read as a color image.
Although it is easy, I explained an example of collecting candidate images using the Bing Image Search API.
Next time will write how to collect candidate images by frame analysis of videos with OpenCV.
Recommended Posts