メモです

メモです

COCO FormatからPascal VOC Formatに変換する

import cv2
import re
from PIL import Image
import io
import json
import os
import numpy as np
from tqdm import tqdm
import requests

#画像データのディレクトリ
IMG_DIR = "./img/"
#ラベル画像のディレクトリ
LBL_DIR = "./lbl/"

with open('input_coco_format.json') as f:
     jsn = json.load(f)

#私の環境だと、file_nameにファイル名入りのURLが入っていたので正規表現を使っている
pattern = '[a-zA-Z0-9]+\.[a-zA-Z]+$'
repatter = re.compile(pattern)

for t in tqdm(jsn["annotations"]):
    sg = t["segmentation"]
    category = t["category_id"]
    #リストをforループで回すと遅いのでsetとかで書き直した方がいい
    for j in jsn["images"]:
        if j["id"] == t["image_id"]:
            width = j['width']
            height = j['height']
            m = repatter.search(j['file_name'])
            img = Image.open(io.BytesIO(requests.get(j['coco_url']).content))

            img.save(IMG_DIR + m.group())

    # working file
 # cv2だとy,x,cの順なので注意
    mask = np.zeros((int(height), int(width), 3))  # (y, x, c)

    # segmentation data
    sg = np.asarray(sg[0])
    poly_number = int(len(sg) / 2)
    poly = np.zeros((poly_number, 2))
    for i in range(poly_number):
        poly[i][0] = sg[(i * 2) + 0]  # x
        poly[i][1] = sg[(i * 2) + 1]  # y

 #category_idをそのまま使っている
    mask = cv2.fillConvexPoly(mask, np.array(
        poly, 'int32'), color=(category, category, category))

    # generate src_img and mask_image
    cv2.imwrite(LBL_DIR + m.group(), mask)


大変参考にさせていただいた
Pythonでポリゴン(多角形)の領域を抽出する方法 - Qiita

Labelboxの出力をCOCO Formatに変換する

LabelBoxのアウトプットが独自フォーマットになったのとCOCOで出力できなくなったのと公式レポジトリの変換ツールが使えないので作った。正直いろんなサイトのやつを見て作ったのでオリジナリティはない。

なお、for文でlabelboxのデータにアクセスするとたまにtimeoutになるので元の画像とアノテーション画像をローカルに保存してからやる。
あとLaeblboxはRLE形式では出力せずにどんなときもpolygon形式で出力していたのでそれに合わせた

import json
import io
import logging
import datetime as dt
import os
import numpy as np
from skimage import measure
from PIL import Image
from pycocotools import mask
from tqdm import tqdm

def main():
    with open('input.json') as f:
        jsn = json.load(f)

    coco = make_coco_metadata("train", "John Dow")

    for data in tqdm(jsn):
        convert_data(coco, data['ID'], data['External ID'],
                         data['Labeled Data'], data['Label']['objects'])

    with open('output.json', 'w') as f:
        json.dump(coco, f, indent=4)

def convert_data(coco, id, file_name, image_url, labels):
    image = {
        "id": id,
        "file_name": file_name,
        "license": None,
        "flickr_url": image_url,
        "coco_url": image_url,
        "date_captured": None,
    }

    file_path = file_name
    image['width'], image['height'] = Image.open(file_path).size
    coco['images'].append(image)

    # labelがない場合、処理をスキップ
    if labels == []:
        return

    category_id = None

    for label_data in labels:
        for c in coco['categories']:
            if c['name'] == label_data['title']:
                category_id = c['id']

        if category_id == None:
            category_id = len(coco['categories']) + 1
            category = {
                # supercategoryがある場合はここを修正する
                'supercategory': label_data['title'],
                'id': category_id,
                'name': label_data['title']
            }
            coco['categories'].append(category)

        #conert("L")でアルファチャンネルを消して、2D画像化
        binary_mask=np.array(Image.open(file_name).convert("L"))

        create_annotation_info(coco=coco, image_id=id, category_id=category_id, binary_mask=binary_mask)

def create_annotation_info(coco, image_id, category_id, binary_mask, tolerance=2):
    binary_mask_encoded = mask.encode(
        np.asfortranarray(binary_mask.astype(np.uint8)))

    area = mask.area(binary_mask_encoded)
    bounding_box = mask.toBbox(binary_mask_encoded)
    polygons = get_polygons(binary_mask)

    #labelboxが出力するcocoはiscrowd:0で固定されていたのでこうしている
    annotation = {
        "id": len(coco['annotations']) + 1,
        "image_id": image_id,
        "category_id": category_id,
        "iscrowd": 0,
        "area": area.tolist(),
        "bbox": bounding_box.tolist(),
        "segmentation": polygons
    }

    coco['annotations'].append(annotation)

def get_polygons(binary_mask):
    polygons = []
    # pad mask to close contours of shapes which start and end at an edge
    padded_binary_mask = np.pad(
        binary_mask, pad_width=1, mode='constant', constant_values=0)
    contours = measure.find_contours(padded_binary_mask, 0.5)
    contours = np.subtract(contours, 1)
    for contour in contours:
        contour = close_contour(contour)
        contour = measure.approximate_polygon(contour, tolerance=5)
        if len(contour) < 3:
            continue
        contour = np.flip(contour, axis=1)
        segmentation = contour.ravel().tolist()
        # after padding and subtracting 1 we may get -0.5 points in our segmentation
        segmentation = [0 if i < 0 else i for i in segmentation]
        polygons.append(segmentation)

    return polygons

def close_contour(contour):
    if not np.array_equal(contour[0], contour[-1]):
        contour = np.vstack((contour, contour[0]))
    return contour

def make_coco_metadata(project_name, created_by):
    return {
        'info': {
            'year': dt.datetime.now(dt.timezone.utc).year,
            'version': None,
            'description': project_name,
            'contributor': created_by,
            'url': 'labelbox.com',
            'date_created': dt.datetime.now(dt.timezone.utc).isoformat()
        },
        'images': [],
        'annotations': [],
        'licenses': [],
        'categories': []
    }

if __name__ == '__main__':
    main()

参考:
Labelbox/coco_exporter.py at master · Labelbox/Labelbox · GitHub
pycococreator/pycococreatortools.py at master · waspinator/pycococreator · GitHub

tf-pose-estimationをGoogleColaboratoryでやる

準備

tf-pose-estimation/run.py at master · ildoonet/tf-pose-estimation · GitHub
をダウンロードする。ファイル名はrun.pyにする。
run.pyをエディタで開いて88行目付近を

plt.colorbar()
plt.savefig('output.png')
plt.show()

に編集して保存する。

実行

!git clone https://www.github.com/ildoonet/tf-pose-estimation
cd tf-pose-estimation
pip install tqdm==4.29.0
!pip install -r requirements.txt
!bash models/graph/cmu/download.sh
!sudo apt install swig
!swig -python -c++ tf_pose/pafprocess/pafprocess.i
cd tf_pose/pafprocess
!python setup.py build_ext --inplace
cd ../../
rm run.py
from google.colab import files
files.upload()
!python run.py --model=cmu --resize=432x368 --image=./images/p1.jpg
from IPython.display import Image,display_png
display_png(Image('output.png'))

uploadのところで準備で編集したrun.pyをアップロードする。

tf-pose-estimationの各座標の表示

tf-pose-estimationの各座標はrun.pyでcommon.CocoPart.Background.valueとhuman.body_partsをfor文でまわして取得する。一枚の写真に複数人いることを考慮した設計なので注意。

座標のリストは以下

list= ["鼻","首","右肩","右ひじ","右手首","左肩", "左ひじ", "左手首","左臀部","左ひざ","左足首","右臀部","右ひざ","左足首","右目","左目","右耳","左耳"]

素人が思いつく国債を減らす方法がダメな理由

現状での理解をまとめた。

提案1

日本銀行を大量に刷って借金を返す

基本的な知識

日本銀行券は日銀にとって借金である。いつでも貨幣に交換できることが国民に求められている。

日本銀行券が日銀の資産だとするとおかしくなる。サラ金で金を借りてそのまま自分の口座に預金したものを友達に見せて「俺、金持ってますよ」ということはなんかおかしい。

提案2

国債を全部日銀に買い取ってもらう。そして日銀はそれをずっと売らずにもっておく。

・国民の預金が死ぬ

・現状600兆もっててもいけるやん…

提案3

貨幣を発行して借金を返す

・インフレになって貨幣価値が下がる。

Mask R-CNNのDemoをGoogleColaboratoryでやった。

Mask_RCNN/demo.ipynb at master · matterport/Mask_RCNN · GitHub
をGoogleColaboratoryでやった。

※!を行頭につけるとLinuxのコマンドを実行できる
※%が行頭についているのはIPythonのマジックコマンド

Mask R-CNN(keras)で人物検出 on Colaboratory - Qiita
を参考にしてGoogleColaboratoryに入ってないやつを入れる。

!git clone https://github.com/matterport/Mask_RCNN.git
!cd Mask_RCNN
!pip install -r requirements.txt
%run -i setup.py install
!wget https://github.com/matterport/Mask_RCNN/releases/download/v2.0/mask_rcnn_coco.h5
!git clone https://github.com/waleedka/coco.git
!cd coco/PythonAPI
%run -i setup.py build_ext --inplace
%run -i setup.py build_ext install
#Mask_RCNNディレクトリまで戻る
cd ../../

matplotlibの設定

%matplotlib inline 
import os
import sys
import random
import math
import numpy as np
import skimage.io
import matplotlib
import matplotlib.pyplot as plt

# Root directory of the project
#GoogleColaboratoryのディレクトリに合わせる
ROOT_DIR = "/content/Mask_RCNN"

# Import Mask RCNN
sys.path.append(ROOT_DIR)  # To find local version of the library
from mrcnn import utils
import mrcnn.model as modellib
from mrcnn import visualize
# Import COCO config
sys.path.append(os.path.join(ROOT_DIR, "samples/coco/"))  # To find local version
import coco


# Directory to save logs and trained model
MODEL_DIR = os.path.join(ROOT_DIR, "logs")

# Local path to trained weights file
COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")
# Download COCO trained weights from Releases if needed
if not os.path.exists(COCO_MODEL_PATH):
    utils.download_trained_weights(COCO_MODEL_PATH)

# Directory of images to run detection on
IMAGE_DIR = os.path.join(ROOT_DIR, "images")

#エラーが出るので足す
from samples.coco import coco

class InferenceConfig(coco.CocoConfig):
    # Set batch size to 1 since we'll be running inference on
    # one image at a time. Batch size = GPU_COUNT * IMAGES_PER_GPU
    GPU_COUNT = 1
    IMAGES_PER_GPU = 1

config = InferenceConfig()
config.display()
# Create model object in inference mode.
model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config)

# Load weights trained on MS-COCO
model.load_weights(COCO_MODEL_PATH, by_name=True)
# COCO Class names
# Index of the class in the list is its ID. For example, to get ID of
# the teddy bear class, use: class_names.index('teddy bear')
#BGはBackgroundの略。常に0番目に来る。
class_names = ['BG', 'person', 'bicycle', 'car', 'motorcycle', 'airplane',
               'bus', 'train', 'truck', 'boat', 'traffic light',
               'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird',
               'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear',
               'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie',
               'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball',
               'kite', 'baseball bat', 'baseball glove', 'skateboard',
               'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup',
               'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple',
               'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza',
               'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed',
               'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote',
               'keyboard', 'cell phone', 'microwave', 'oven', 'toaster',
               'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors',
               'teddy bear', 'hair drier', 'toothbrush']
# Load a random image from the images folder
file_names = next(os.walk(IMAGE_DIR))[2]
image = skimage.io.imread(os.path.join(IMAGE_DIR, random.choice(file_names)))

をそのままやるとGoogleColaboratoryだとStopiterationのエラーが出るので

image = skimage.io.imread(os.path.join(IMAGE_DIR, "1045023827_4ec3e8ba5c_z.jpg"))

にした。(file_namesのところはMask_RCNN/imagesから適当に選ぶ)
後は

# Run detection
results = model.detect([image], verbose=1)

# Visualize results
r = results[0]
visualize.display_instances(image, r['rois'], r['masks'], r['class_ids'], 
                            class_names, r['scores'])

で表示する。