帮我改一下代码,如果是"error_msg":"pic not has face",帮我跳过这个文件的处理,import csv import os import json from IPython import display pic_folder_path = "./期末作业-面部数据/face_images/a4" output_list = [] no_result_list = [] for filename in os.listdir(pic_folder_path): if filename.endswith(".jpg"): pic_path = os.path.join(pic_folder_path, filename) result = None while not result or "result" not in result or "face_list" not in json.loads(result)["result"]: result = main(pic_path) # 调用主函数获取结果 result_dict = json.loads(result)["result"]["face_list"] if not result_dict: no_result_list.append(filename) for item in result_dict: output_list.append({ "filename": filename, "type": item["emotion"]["type"] }) # 输出未能识别结果的图片名称 print("无法识别结果的图片:", no_result_list) # 将结果保存为csv文件 with open("output.csv", "a", newline="") as f: writer = csv.DictWriter(f, fieldnames=["filename", "type"]) # writer.writeheader() writer.writerows(output_list)
时间: 2024-03-12 22:43:00 浏览: 120
可以在获取 result 后加上一行代码判断是否为"error_msg":"pic not has face",如果是的话就跳过这个文件的处理,代码如下:
```
import csv
import os
import json
from IPython import display
pic_folder_path = "./期末作业-面部数据/face_images/a4"
output_list = []
no_result_list = []
for filename in os.listdir(pic_folder_path):
if filename.endswith(".jpg"):
pic_path = os.path.join(pic_folder_path, filename)
result = None
while not result or "result" not in result or "face_list" not in json.loads(result)["result"]:
result = main(pic_path) # 调用主函数获取结果
result_dict = json.loads(result)["result"]
if "error_msg" in result_dict and result_dict["error_msg"] == "pic not has face":
continue
result_dict = result_dict["face_list"]
if not result_dict:
no_result_list.append(filename)
for item in result_dict:
output_list.append({
"filename": filename,
"type": item["emotion"]["type"]
})
# 输出未能识别结果的图片名称
print("无法识别结果的图片:", no_result_list)
# 将结果保存为csv文件
with open("output.csv", "a", newline="") as f:
writer = csv.DictWriter(f, fieldnames=["filename", "type"])
# writer.writeheader()
writer.writerows(output_list)
```
这样,程序会在获取到"error_msg":"pic not has face"时直接跳过该文件的处理,继续处理下一个文件。
阅读全文