Face++を使いフォルダ内の顔画像すべてに処理を行うプログラムを書いているのですが、その画像読み込み時に
1 2 detect2.py:52: DeprecationWarning: path should be string, bytes, or os.PathLike, not numpy.ndarray 'image_file': open(image, 'rb').read() Traceback (most recent call last): File "detect2.py", line 80, in <module> call_api() File "detect2.py", line 52, in call_api 'image_file': open(image, 'rb').read() UnicodeDecodeError: 'utf-8' codec can't decode byte 0xcf in position 0: invalid continuation byte
このようなエラーが起きてしまいます。
# coding: utf-8 import os import sys import csv import pandas as pd sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) import requests from config import API_KEY, API_SECRET, DETECT_PATH ,ANALYZE_PATH from example.common import get_input_file_path import cv2 import codecs return_landmark = 0 return_attributes = None #calculate_all = 0 face_rectangle = '' beauty_score_min = 0 beauty_score_max = 100 def call_api(): data = { 'api_key': API_KEY, 'api_secret': API_SECRET, 'return_landmark': 1, 'return_attributes': 'gender,age,emotion' #'calculate_all': calculate_all, #'beauty_score_min': beauty_score_min, #'beauty_score_max': beauty_score_max } data_dir_path = u"E:/facepp-python-demo-master/facepp-python-demo-master/example/detect/images/images01/" file_list = os.listdir(r'E:/facepp-python-demo-master/facepp-python-demo-master/example/detect/images/images01/') #この内部の処理がうまくいっていない ################################################## for f in file_list: root, ext = os.path.splitext(f) print(1) if ext in ('.png', '.jpeg', '.jpg'): abs_name = data_dir_path +'/' + f image = cv2.imread(abs_name) print(2) #以下各画像に対する処理を記載する if face_rectangle: print(3) data.update({'face_rectangle': face_rectangle}) input_file = get_input_file_path(os.path.abspath(os.path.dirname(__file__)), f) files = { 'image_file': open(image, 'rb').read() } resp = requests.post(DETECT_PATH, data=data, files=files).json() with open('emotion.csv','w') as csv_file: writer = csv.writer(csv_file) for key, value in resp.items(): writer.writerows(resp) print(4) (pd.DataFrame.from_dict(data=resp,orient='index').to_csv('emotion_csv',header=False)) print(5) osp = resp['faces'] df = pd.io.json.json_normalize(osp) print(df) df.to_csv('emotion2_csv') print(6) #input_file = get_input_file_path(os.path.abspath(os.path.dirname(__file__)), 'input') if not face_rectangle: print('请将input.png/input.jpg文件放在detect目录下') return if not ext in ('.png', '.jpeg', '.jpg'): print("終了") #(pd.DataFrame.from_dict(data=osp, orient='index').to_csv('emotion2_csv', header=False)) #print(resp['faces']) ################################################ if __name__ == "__main__": call_api() print(7)
よくあるエラー内容なので調べたのですが
ignore='utf-8'
'utf-8','ignore'
encoding='Shift-JIS'
などを
'image_file': open(image, 'rb',######).read()
の箇所に入れてみたのですがエラーが直らなかったので質問させていただきました。
あなたの回答
tips
プレビュー