i wrote simple python script to read letters and numbers in licenseplate in image, to read licenseplate, i'd send it to image recognition API and they will send back JSON response that i use.
this is full code:
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()
when i run this code, it will show response on terminal like this:
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...
i think it's works fine, but the problem is why detected number print same number on different image with different number? i don't know what's wrong is it.
any helps will be appriciated! thankyou
results=[]within your loop, not outside, otherwise you are appending new results to the list, but only displaying the first, at[0]results=[ ]?results=[]inside yourfor file in filesblock.