#!/usr/bin/python3 # -*- coding: utf-8 -*- import argparse import base64 from datetime import datetime import json import os from pathlib import Path import sys pwd = os.path.abspath(os.path.dirname(__file__)) sys.path.append(os.path.join(pwd, "../../")) import pandas as pd import requests from tqdm import tqdm from project_settings import project_path def get_args(): parser = argparse.ArgumentParser() parser.add_argument("--host", default="127.0.0.1", type=str) parser.add_argument("--port", default=2080, type=int) parser.add_argument( "--wav_dir", default=(project_path / "data/wav/65").as_posix(), type=str ) parser.add_argument("--country_code", default=65, type=int) args = parser.parse_args() return args # 1 label_map_1 = { "bell": "bell", "caller_id_blocked": "caller_id_blocked", "can_not_be_completed": "can_not_be_completed", "disconnected": "disconnected", "disconnected_or_out_of_service_or_invalid_number": "disconnected_or_out_of_service_or_invalid_number", "mute": "mute", "not_accept_calls": "not_accept_calls", "not_available": "not_available", "out_of_service": "out_of_service", "restricted_or_unavailable": "restricted_or_unavailable", } # 52 label_map_52 = { "bell": "bell", "mute": "mute", "music": "music", "voice": "music", "arrears": "out_of_service", "busy": "busy", "invalid_number": "invalid_number", "not_available": "not_available", "not_available_or_out_of_service": "not_available_or_out_of_service", "number_paused": "number_paused", "out_of_service": "out_of_service", "restricted": "out_of_service", "power_off_or_out_of_service": "power_off_or_out_of_service", "voicemail": "voicemail", "voicemail_is_full": "voicemail_is_full", } # 63 label_map_63 = { "bell": "bell", "mute": "mute", "music": "music", "voice": "music", "noise": "music", "busy": "busy", "call_forwarding": "call_forwarding", "can_not_be_completed": "can_not_be_completed", "invalid_number": "invalid_number", "no_route_found": "no_route_found", "not_reachable": "not_reachable", "out_of_service": "out_of_service", "unattended": "unattended", "unattended_or_out_of_coverage_area": "unattended_or_out_of_coverage_area", } # 65 label_map_65 = { "bell": "bell", "mute": "mute", "music": "music", "voice": "music", "noise": "music", "busy": "busy", "can_not_be_completed": "can_not_be_completed", "invalid_number": "invalid_number", "not_available": "not_available", "not_responding": "not_responding", "out_of_service": "out_of_service", } # 66 label_map_66 = { "bell": "bell", "mute": "mute", "music": "music", "voice": "music", "noise": "music", "invalid_number": "invalid_number", "no_answer": "no_answer", "not_reachable": "not_reachable", "out_of_service": "out_of_service", "voicemail": "voicemail", } # 91 label_map_91 = { "bell": "bell", "mute": "mute", "music": "music", "voice": "music", "noise": "music", "noise_music": "music", "noise_mute": "music", "noise_voice": "music", "busy": "busy", "can_not_connect": "not_reachable", "forwarded": "forwarded", "line_busy": "line_busy", "not_available": "not_available", "not_reachable": "not_reachable", "invalid_number": "invalid_number", "out_of_service": "out_of_service", "out_of_service_area": "out_of_service_area", "power_off": "power_off", "unknown": "music", } # 234 label_map_234 = { "bell": "bell", "mute": "mute", "music": "music", "voice": "music", "other": "music", "busy": "busy", "on_another_call": "busy", "can_not_be_reached": "power_off", "invalid_number": "invalid_number", "not_available": "not_available", "power_off": "power_off", "power_off_or_out_of_service": "power_off", "not_reachable": "power_off", } # 254 label_map_254 = { "bell": "bell", "mute": "mute", "music": "music", "voice": "music", "busy": "busy", "invalid_number": "invalid_number", "not_available": "not_available", "not_reachable": "power_off", "out_of_service": "out_of_service", "power_off": "power_off", } # 255 label_map_255 = { "bell": "bell", "mute": "mute", "busy": "busy", "can_not_be_connected": "can_not_be_connected", "invalid_number": "invalid_number", "no_answer": "no_answer", "not_available": "not_available", "not_reachable": "not_available", "restricted": "restricted", "switched_off_or_not_available": "switched_off_or_not_available", } # 886 label_map_886 = { "bell": "bell", "mute": "mute", "music": "music", "voice": "music", "卡线": "music", "busy": "busy", "invalid_number": "invalid_number", "not_available": "not_available", "number_paused": "number_paused", "power_off": "power_off", "voicemail": "voicemail", } area_code2label_map = { 1: label_map_1, 52: label_map_52, 63: label_map_63, 65: label_map_65, 66: label_map_66, 91: label_map_91, 234: label_map_234, 254: label_map_254, 255: label_map_255, 886: label_map_886, } def main(): args = get_args() wav_dir = Path(args.wav_dir) url = "http://{host}:{port}/call_status".format( host=args.host, port=args.port, ) headers = { "Content-Type": "application/json" } label_map = area_code2label_map[args.country_code] result = list() for filename in tqdm(wav_dir.glob("*/*.wav")): label = filename.parts[-2] if label not in label_map.keys(): continue label = label_map[label] with open(filename, "rb") as f: data = f.read() base64string = base64.b64encode(data).decode("utf-8") data = { "country": args.country_code, "record": base64string } resp = requests.post(url, headers=headers, data=json.dumps(data)) if resp.status_code != 200: print("request failed, status_code: {}, text: {}.".format(resp.status_code, resp.text)) continue js = resp.json() predict = js["result"]["label_id"] result.append({ "filename": filename, "target": label, "predict": predict, "correct": 1 if label == predict else 0, }) version = datetime.now().strftime("%Y%m%d_%H%M") result = pd.DataFrame(result) result.to_excel("evaluation_{}_{}.xlsx".format(args.country_code, version), index=False, encoding="utf_8_sig") return if __name__ == "__main__": main()