0
2

More than 1 year has passed since last update.

ポケモンとMTGのためのシンプルな画像付きトレーディングカード検索API

Posted at

はじめに

API: https://docs.ximilar.com

今回は、トレーディングカードの画像を自動認識・解析するAIサービス「ximilar」を試してみました。

これらのカードを分析し、識別することができます

  • マジック:ザ・ギャザリング - MTG
  • 遊☆戯☆王
  • ポケットモンスター

ezgif.com-webp-to-jpg.jpg

事前準備

APIを使用するには、有料のximilarアカウントが必要です。
app.ximilar.comから登録できます。

APIで遊んでみよう。

まず、API経由でポケモンカードを分析してみる。Pythonとrequestsライブラリを使います。

import requests
import sys
import pprint
    
url = "https://api.ximilar.com/collectibles/v2/card_id"
url_img_path = sys.argv[1]
api_auth_token = sys.argv[2]

payload = {"records": [{"_url": url_img_path}]}
headers = {
   "Authorization": "Token " + api_auth_token,
   "Content-Type": "application/json"
}
    
response = requests.request("POST", url, json=payload, headers=headers)
result = response.json()
pprint.pprint(result)

このスクリプトを次のように実行する:

python script.py https://i.ebayimg.com/images/g/Sn0AAOSwnSFjQPDa/s-l1600.jpg YOURAPITOKEN

結果

結果は簡単だ:

{
	"records": [
		{
			"_url": "https://i.ebayimg.com/images/g/Sn0AAOSwnSFjQPDa/s-l1600.jpg",
			"_status": {
				"code": 200,
				"text": "OK",
				"request_id": "3682e62c-a2f2-414e-a890-7ac4b355108f"
			},
			"_id": "dfb7ab80-6d59-455f-b13f-a1f44efabab2",
			"_width": 926,
			"_height": 1559,
			"_objects": [
				{
					"name": "Card",
					"id": "76fa9ec3-e0d9-408a-b582-55a1cd6712e0",
					"bound_box": [
						70,
						377,
						862,
						1482
					],
					"prob": 0.9816063642501831,
					"area": 0.6062201361286863,
					"Top Category": [
						{
							"id": "8ae26c4a-ae79-4c01-9b54-ac4e2b42e914",
							"name": "Card",
							"prob": 1.0
						}
					],
					"_tags": {
						"Category": [
							{
								"prob": 0.99817,
								"name": "Card/Trading Card Game",
								"id": "d44efa56-09c3-4829-b3e7-226fe9cfe798"
							}
						],
						"Side": [
							{
								"prob": 0.99074,
								"name": "front",
								"id": "b48a23fe-381e-4a67-b649-9839e1f7d5a7"
							}
						],
						"Subcategory": [
							{
								"prob": 0.99741,
								"name": "Pokemon",
								"id": "fdbb3942-b7e0-476a-8f13-af77aedd13e6"
							}
						],
						"Autograph": [
							{
								"prob": 0.89952,
								"name": "not autograph",
								"id": "5c1bb98b-cd69-42c9-ab2a-75e480ffa2b0"
							}
						],
						"Rotation": [
							{
								"prob": 0.98045,
								"name": "rotation_ok",
								"id": "2cfa3525-1dc6-440f-9b0d-efcb297ccf37"
							}
						],
						"Foil/Holo": [
							{
								"prob": 0.81857,
								"name": "foil/holo",
								"id": "8d0a5c67-eb08-4908-81c9-60e74d7028e7"
							}
						],
						"Alphabet": [
							{
								"prob": 0.9916,
								"name": "japanese",
								"id": "24620de3-93a6-4d69-9aed-3b0aaceb6f6e"
							}
						]
					},
					"_tags_simple": [
						"Card/Trading Card Game",
						"front",
						"Pokemon",
						"not autograph",
						"rotation_ok",
						"foil/holo",
						"japanese"
					],
					"_identification": {
						"best_match": {
							"name": "Blastoise",
							"full_name": "Blastoise Expansion Pack (EXP) #32",
							"set": "Expansion Pack",
							"set_code": "EXP",
							"series": "Original Series",
							"card_number": "32",
							"year": 1996,
							"subcategory": "Pokemon"
						}
					}
				}
			]
		}
	],
	"statistics": {
		"processing time": 2.187345027923584
	},
	"status": {
		"code": 200,
		"text": "OK",
		"request_id": "3682e62c-a2f2-414e-a890-7ac4b355108f",
		"proc_id": "aa73dfae-4c3a-49de-91d2-ecc125865a5c"
	}
}

最も重要な情報は "best_match "JSONフィールドにある:

"best_match": {
   "name": "Blastoise",
   "full_name": "Blastoise Expansion Pack (EXP) #32",
   "set": "Expansion Pack",
   "set_code": "EXP",
   "series": "Original Series",
   "card_number": "32",
   "year": 1996,
   "subcategory": "Pokemon"
}

終わりに

後はGUIDEの続きを読んで理解を深めるもよし、ライブラリのリファレンスを見ながら自分のサービスに組み込んで試してみるのもよしといったところだ。

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2