Face++のdetectAPIを使ってフォルダ内の画像すべてに処理を行い。その結果を1つのcsvファイルに保存するプログラムを完成させたいです。
公式ドキュメントでは画像一枚だけに処理を行うコードが書かれているのですがそれを改変して書こうとしています。
機械学習用の画像データセットを作ろうと思い、
①画像をスクレイピングにて一括取得。
②取得した画像をカスケードファイルにて顔部分だけにリサイズ
まで行いました。
detect2.py
1# coding: utf-8 2import os 3import sys 4import csv 5import pandas as pd 6sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) 7import requests 8from config import API_KEY, API_SECRET, DETECT_PATH ,ANALYZE_PATH 9from example.common import get_input_file_path 10 11return_landmark = 0 12return_attributes = None 13#calculate_all = 0 14face_rectangle = '' 15beauty_score_min = 0 16beauty_score_max = 100 17 18def call_api(): 19 data = { 20 'api_key': API_KEY, 21 'api_secret': API_SECRET, 22 'return_landmark': 1, 23 'return_attributes': 'gender,age,emotion' 24 #'calculate_all': calculate_all, 25 #'beauty_score_min': beauty_score_min, 26 #'beauty_score_max': beauty_score_max 27 } 28 29 data_dir_path = u"E:/facepp-python-demo-master/facepp-python-demo-master/example/detect/images/images01/" 30 file_list = os.listdir(r'E:/facepp-python-demo-master/facepp-python-demo-master/example/detect/images/images01') 31 32 33 34#この内部の処理がうまくいっていない 35 ################################################## 36 if face_rectangle: 37 data.update({'face_rectangle': face_rectangle}) 38 print(1) 39 for f in file_list: 40 root, ext = os.path.splitext(f) 41 print(2) 42 if ext == u'.png' or u'.jpeg' or u'.jpg': 43 abs_name = data_dir_path + '/' + f 44 image = cv.imread(abs_name) 45 print(3) 46 #以下各画像に対する処理を記載する 47 files = { 48 'image_file': open(image, 'rb').read() 49 } 50 resp = requests.post(DETECT_PATH, data=data, files=files).json() 51 52 with open('emotion.csv','w') as csv_file: 53 writer = csv.writer(csv_file) 54 for key, value in resp.items(): 55 writer.writerows(resp) 56 57 (pd.DataFrame.from_dict(data=resp,orient='index').to_csv('emotion_csv',header=False)) 58 print(4) 59 osp = resp['faces'] 60 df = pd.io.json.json_normalize(osp) 61 print(df) 62 df.to_csv('emotion2_csv') 63 #input_file = get_input_file_path(os.path.abspath(os.path.dirname(__file__)), 'input') 64 if not f: 65 print('请将input.png/input.jpg文件放在detect目录下') 66 return 67 68 #(pd.DataFrame.from_dict(data=osp, orient='index').to_csv('emotion2_csv', header=False)) 69 #print(resp['faces']) 70################################################ 71 72 73 74 75 76 77 78if __name__ == "__main__": 79 call_api() 80 81
書く箇所にプリント文を挟んでみたところ、どこがうまくいっていないのかは理解できたのですが具体的にどうすればうまく処理が進むのかがわからず投稿させていただきました。
あなたの回答
tips
プレビュー