Python:无法打印来自 API 的不同 JSON 响应
Python: can't print different JSON response from API
我写了简单的 python 脚本来读取图像中车牌中的字母和数字,读取车牌,我将它发送到图像识别 API 他们会发回 JSON 我使用的响应。
这是完整代码:
import glob
import requests
import json
import time
import os
import cv2
import numpy as np
def main():
result = []
regions = ['id']
time_to_wait = np.inf
time_counter = 0
while True:
files = glob.glob(os.path.join("./path_to_imagedir/*.jpg"))
files.sort(key=os.path.getmtime)
for file in files:
if os.path.isfile(file):
with open(file, 'rb') as fp:
response = requests.post(
'https://MY_API/',
data=dict(regions=regions),
files=dict(upload=fp),
headers={'Authorization': 'Token ' + 'XXX'})
result.append(response.json())
resp_dict = json.loads(json.dumps(result, indent=2))
if resp_dict[0]['results']:
num = resp_dict[0]['results'][0]['plate']
print(f"detected number: {num}")
os.remove(file)
time.sleep(1)
time_counter += 1
if time_counter > time_to_wait: break
print("waiting for file... ")
if __name__ == '__main__':
main()
当我 运行 这段代码时,它会在终端上显示这样的响应:
waiting for file...
waiting for file...
waiting for file...
detected number: b1962ub
waiting for file...
waiting for file...
waiting for file...
waiting for file...
detected number: b1962ub
waiting for file...
waiting for file...
waiting for file...
waiting for file...
我认为它工作正常,但问题是为什么 detected number
在不同的图像上用不同的数字打印相同的数字?我不知道这是怎么回事。
如有帮助,我们将不胜感激!
谢谢
你一直追加到你的 results
但你总是看 resp_dict[0]
所以你总是看同一个项目。而是查看 resp_dict[-1]
,这样您就会看到最新的项目
所以这样:
if resp_dict[0]['results']:
num = resp_dict[0]['results'][0]['plate']
print(f"detected number: {num}")
应该是这样的:
if resp_dict[-1]['results']:
num = resp_dict[-1]['results'][0]['plate']
print(f"detected number: {num}")
我写了简单的 python 脚本来读取图像中车牌中的字母和数字,读取车牌,我将它发送到图像识别 API 他们会发回 JSON 我使用的响应。
这是完整代码:
import glob
import requests
import json
import time
import os
import cv2
import numpy as np
def main():
result = []
regions = ['id']
time_to_wait = np.inf
time_counter = 0
while True:
files = glob.glob(os.path.join("./path_to_imagedir/*.jpg"))
files.sort(key=os.path.getmtime)
for file in files:
if os.path.isfile(file):
with open(file, 'rb') as fp:
response = requests.post(
'https://MY_API/',
data=dict(regions=regions),
files=dict(upload=fp),
headers={'Authorization': 'Token ' + 'XXX'})
result.append(response.json())
resp_dict = json.loads(json.dumps(result, indent=2))
if resp_dict[0]['results']:
num = resp_dict[0]['results'][0]['plate']
print(f"detected number: {num}")
os.remove(file)
time.sleep(1)
time_counter += 1
if time_counter > time_to_wait: break
print("waiting for file... ")
if __name__ == '__main__':
main()
当我 运行 这段代码时,它会在终端上显示这样的响应:
waiting for file...
waiting for file...
waiting for file...
detected number: b1962ub
waiting for file...
waiting for file...
waiting for file...
waiting for file...
detected number: b1962ub
waiting for file...
waiting for file...
waiting for file...
waiting for file...
我认为它工作正常,但问题是为什么 detected number
在不同的图像上用不同的数字打印相同的数字?我不知道这是怎么回事。
如有帮助,我们将不胜感激! 谢谢
你一直追加到你的 results
但你总是看 resp_dict[0]
所以你总是看同一个项目。而是查看 resp_dict[-1]
,这样您就会看到最新的项目
所以这样:
if resp_dict[0]['results']:
num = resp_dict[0]['results'][0]['plate']
print(f"detected number: {num}")
应该是这样的:
if resp_dict[-1]['results']:
num = resp_dict[-1]['results'][0]['plate']
print(f"detected number: {num}")