def identify_audio(filename):
	"""
	Identify a given song.
	You can provide a filename of some audio. 
	You must provide start time to end time for
	scanning.
	"""
	cmd = '{} "{}" > song.out'.format(EP_PATH, filename)
	os.system(cmd)
	with open('song.out', 'rb') as fp:
		data = fp.read()
		fp.close()
	data = json.loads(data.strip())[0]
	print json.dumps(data, indent=3)
	fp_code = data["code"]
	cmd = 'curl -S http://localhost:8080/query?fp_code={} > song.out'.format(fp_code)
	os.system(cmd)
	with open('song.out', 'rb') as fp:
		data = fp.read()
		fp.close()
	data = json.loads(data.strip())
	print json.dumps(data, indent=3)
	if data["match"]:
		print "SUCCESSFUL MATCH!"
		title = songMap[str(data["track_id"])]
		print "Matched to: {}".format(title)
		return {
			"success": True,
			"match": title,
			"score": data["score"]
		};
	else:
		return {
			"success": False
		};
def read_id():
	global lastId
	try:
		with open(ID_FNAME, 'rb') as fp:
			lastId = fp.read()
			fp.close()
	except IOError:
		lastId = "songid1"
		pass
    def do_GET(self):
    	print self.path
        if self.path == '/identify':
			self.send_response(200)
			self.send_header('Content-type', 'text/json')
			self.end_headers()
			self.wfile.write(json.dumps(identify_audio("file.mp3")))
		elif self.path == '/learn':
			self.send_response(200)
			self.send_header('Content-type', 'text/html')
			self.end_headers()
			with open("learn.title", "rb") as fp:
				title = fp.read()
				fp.close()
			learn_song("file.mp3", title)
			self.wfile.write('OK')
def learn_song(filename, description):
	global lastId
	cmd = '{} "{}" > song.out'.format(EP_PATH, filename)
	with open('song.out', 'rb') as fp:
		subprocess.call(cmd, shell=True, stdout=fp)
		os.system(cmd)
		data = fp.read()
		fp.close()
	data = json.loads(data.strip())[0]
	#print json.dumps(data, indent=3)
	try:
		fp_code = data["code"]
	except KeyError:
		print "Could not learn song with title: {}".format(description)
		return
	length = data["metadata"]["duration"]
	cmd = 'curl http://localhost:8080/ingest -d "fp_code={}&track_id={}&length={}&codever=4.12" > /dev/null'.format(fp_code, lastId, length)
	songMap[lastId] = description
	lastId = 'songid' + str(int(lastId.split('songid')[1]) + 1)
	save_id()
	save_map()
	os.system(cmd)
	print "Learned song with title: {}".format(description)
Exemple #5
0
def batch_test():
    # 参数表: 测试图片集路径,结果存储路径,发票类型
    curr = time.strftime('%Y.%m.%d', time.localtime(time.time()))

    do_ocr = "y"
    if len(sys.argv) == 1:
        images_dir = input("Please enter pictures dir:")
        result_save_path = input("Please enter result save path:")
        invoicetype = input(
            "Please enter the invoice type(special=01, normal=04):")
    elif len(sys.argv) == 4:
        images_dir = sys.argv[1]
        result_save_path = sys.argv[2]
        invoicetype = sys.argv[3]
    elif len(sys.argv) == 5:
        images_dir = sys.argv[1]
        result_save_path = sys.argv[2]
        invoicetype = sys.argv[3]
        do_ocr = sys.argv[4]
    else:
        print("Wrong input args.\n")
        return

    root_path = result_save_path
    result_save_path = os.path.join(result_save_path, curr)
    if not os.path.exists(result_save_path):
        os.makedirs(result_save_path)

    image_list = glob.glob(os.path.join(images_dir, "*.jpg"))
    if len(image_list) == 0:
        image_list = glob.glob(os.path.join(images_dir, "*.bmp"))
        if len(image_list) == 0:
            print("No pics in " + images_dir + " !\n")
            return
    num_of_imges = len(image_list)

    if do_ocr == "y" or do_ocr == "Y":
        # 是否执行ocr操作
        print("Now begin to ocr...")
        ocr_result = []
        ocr_erorr_list = []
        for img in image_list:
            try:
                tuple_info = json.loads(init(img))
                tuple_info['filename'] = img
                ocr_result.append(tuple_info)
            except Exception as _:
                ocr_erorr_list.append(img)
        print("ocr is complete, get %d / %d pics info." %
              (len(ocr_result), num_of_imges))

        file_ocr_result = os.path.join(result_save_path, "ocr_result.json")
        file_ocr_error_list = os.path.join(result_save_path,
                                           "ocr_error_files.txt")

        print("Now saving the result...")
        with open(file_ocr_result, 'w') as re, open(file_ocr_error_list,
                                                    'w') as er:
            re.write(json.dumps(ocr_result, indent=4, ensure_ascii=False))
            for i in ocr_erorr_list:
                er.writelines(i + '\n')
    else:
        # 不执行ocr直接用已有结果对比chinese_info.json
        file_ocr_result = os.path.join(result_save_path, "ocr_result.json")
        with open(file_ocr_result, "r", encoding="utf-8") as fp:
            content = fp.read()
            content = content.replace('¥', '¥')
            if content.startswith(u'\ufeff'):
                content = content.encode('utf8')[3:].decode('utf8')
            ocr_result = json.loads(content)

    print("Chinese infor query begin...")
    succeed, query_error = Get_Chinese_Info(ocr_result, result_save_path,
                                            invoicetype, root_path)

    file_result = str(succeed) + "(%d)_ocr_correct.txt" % num_of_imges
    file_result = os.path.join(result_save_path, file_result)

    try:
        with open(file_result, "w") as wp:
            wp.writelines(file_result + " %.2f " % (succeed / num_of_imges) +
                          '\n')
            wp.writelines("%d not find in web:没有查询到中文数据(不一定ocr错误)" %
                          (num_of_imges - succeed) + '\n')
            wp.writelines("%d query_error_pics:本地有中文数据,但五元组错误" %
                          (query_error) + '\n')
            # wp.writelines("%d ocr_error_files:ocr没有正确生成五元组"%(len(ocr_erorr_list))+'\n')
    except Exception as e:
        print(e)
    finally:
        print("Test end.")