以下のようなjsonファイルに格納されている値をpythonで読み込み、値に代入したいです。
OpenPoseで出力したjsonファイルを読み込むために以下のようなコードを書きました。
実行時の引数でファイル名とファイルの種類を与えています。
エラーが出たのでjson読み込み以降の処理は書いていません。
import sys import subprocess import os.path import json #import csv args = sys.argv oppdir = "C:\openpose\bin\OpenPoseDemo.exe" #path:拡張子のないファイル名 ext:ドット付き拡張子 path, ext = os.path.splitext(os.path.basename(args[1])) #ファイルをOpenPoseにかけてjsonを出力 if len(args)>=3: if args[2]=="image": subprocess.run([oppdir,"--video",args[1],"--write_json",".\json"]) elif args[2]=="video": subprocess.run([oppdir,"--video",args[1],"--write_json",".\json"]) elif args[2]=="folder" : subprocess.run([oppdir,"--image_dir",args[1],"--write_json",".\json"]) else: print("input file type[image/video/folder]") else: print("imput file path and type[image/video/folder].") #json読み込み,値の取得 f = open('.\json\'+path+'_000000000000_keypoints.json','r') jsonData = json.load(f) for n in jsonData['people']: if int(dict(jsonData['people'][n]["pose_keypoints_2d"][2]))!=0: centerX = float(dict(jsonData['people'][n]["pose_keypoints_2d"][0])) centerY = float(dict(jsonData['people'][n]["pose_keypoints_2d"][1])) radius = abs(float(dict(jsonData['people'][n]["pose_keypoints_2d"][4]-jsonData['people'][n]["pose_keypoints_2d"][1]))) print(centerX,centerY,radius) else: continue
実行したところ、
File ".\facemosjson.py", line 30, in <module>
if int(dict(jsonData['people'][n]["pose_keypoints_2d"][2]))!=0:
TypeError: list indices must be integers or slices, not dict
と返されました。
ここでのリストとはどれのことでしょうか。jsonのデータのことでしょうか。
int型でないといけないということですが、元からintでない値でもintに変換しろということでしょうか。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。