gpt4 book ai didi

python - Python:如果JSON响应为空,则无法使用

转载 作者:行者123 更新时间:2023-12-02 17:22:52 36 4
gpt4 key购买 nike

我只是尝试Platerecognizer.com API来检测矩形框并识别框(牌照)中的每个字符。但API无法读取视频文件,它们只接收jpeg / jpg文件,然后生成JSON。所以我将视频每一帧分开,并发送到API,而我已经完成了。

我有此代码:

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import glob
import argparse
import requests
from PIL import Image
import json
import time
import math
import cv2
import os
import json

def main():
cap = cv2.VideoCapture("assets/car-number-plate.mp4")
frameRate = cap.get(5)
print(frameRate)
seconds=2
multiplier=frameRate*seconds
x=1
while (cap.isOpened()):
frameId = int(round(cap.get(1)))
print(frameId)
ret, frame = cap.read()
if (ret != True):
break
result = []
if (frameId % multiplier == 0):
filename = 'output_frame/frameId' + str(int(x)) + ".jpg"
cv2.imwrite(filename, frame)
time.sleep(1)
path = "/home/mycomputer/Documents/platerecognizer-test/%s" % filename
with open(path, 'rb') as fp:
response = requests.post(
'https://api.platerecognizer.com/v1/plate-reader/',
files=dict(upload=fp),
headers={'Authorization': 'Token ' + 'MY_API_KEY'})
result.append(response.json())
print(json.dumps(result, indent=2));
with open('data.json', 'w') as outfile:
json.dump(result, outfile)
os.remove("%s" %filename)
x+=1
time.sleep(1)

resp_dict = json.loads(json.dumps(result, indent=2))
num=resp_dict[0]['results'][0]['plate']
boxs=resp_dict[0]['results'][0]['box']
xmins,ymins,ymaxs,xmaxs=boxs['xmin'],boxs['ymin'],boxs['ymax'],boxs['xmax']

cv2.rectangle(frame, (xmins, ymins), (xmaxs, ymaxs), (0, 255, 0), 2)

font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(frame,num,(xmins, ymins-10), font, 1,(0, 255, 0),2,cv2.LINE_AA)
cv2.imshow("plat nomor yang terdeteksi",frame)
cv2.waitKey(0)
cv2.destroyAllWindows()
print(f"mobil dengan plat nomor: {num}")
cap.release()
if __name__ == '__main__':
main()

上面的代码如何工作?因此,首先我要从目录加载视频,按帧拆分视频,将其发送到API,从API接收完整(非空)响应JSON,如下所示:
[
{
"processing_time": 102.465,
"results": [
{
"box": {
"xmin": 537,
"ymin": 346,
"xmax": 657,
"ymax": 407
},
"plate": "b1399ere",
"region": {
"score": 0.876,
"code": "id"
},
"vehicle": {
"score": 0.802,
"box": {
"xmin": 163,
"ymin": 12,
"xmax": 709,
"ymax": 472
},
"type": "Car"
},
"score": 0.898,
"candidates": [
{
"score": 0.898,
"plate": "b1399ere"
},
{
"score": 0.791,
"plate": "bi399ere"
},
{
"score": 0.79,
"plate": "81399ere"
},
{
"score": 0.683,
"plate": "8i399ere"
}
],
"dscore": 0.732
}
],
"filename": "0243_tzJBu_frameId7.jpg",
"version": 1,
"camera_id": null,
"timestamp": "2020-04-20T02:43:34.123444Z"
}
]

当他们从我们之前发送过图像的json API中找到结果时,代码运行良好,但是当代码找到具有空结果的JSON响应时,则代码如下:
[
{
"processing_time": 82.204,
"results": [],
"filename": "0243_aKa1u_frameId8.jpg",
"version": 1,
"camera_id": null,
"timestamp": "2020-04-20T02:43:38.881827Z"
}
]

我只是停留在此错误: IndexError: list index out of range
如果结果JSON为空,我想使此循环工作,然后返回以重新生成上面的下一个捕获帧,并获得具有完整结果的JSON响应。但是,该如何循环?

谢谢

最佳答案

可以将访问结果的代码放入条件中,以仅在结果包含元素时使用结果。

if resp_dict[0]['results']:
num=resp_dict[0]['results'][0]['plate']
boxs=resp_dict[0]['results'][0]['box']
xmins,ymins,ymaxs,xmaxs=boxs['xmin'],boxs['ymin'],boxs['ymax'],boxs['xmax']
cv2.rectangle(frame, (xmins, ymins), (xmaxs, ymaxs), (0, 255, 0), 2)
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(frame,num,(xmins, ymins-10), font, 1,(0, 255, 0),2,cv2.LINE_AA)
cv2.imshow("plat nomor yang terdeteksi",frame)
cv2.waitKey(0)
cv2.destroyAllWindows()
print(f"mobil dengan plat nomor: {num}")

关于python - Python:如果JSON响应为空,则无法使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61315134/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com