0

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

5
  • 1
    You need to clear results=[] within your loop, not outside, otherwise you are appending new results to the list, but only displaying the first, at [0] Commented Jun 22, 2020 at 7:59
  • how to clear results=[ ] ? Commented Jun 24, 2020 at 4:12
  • Yes, put results=[] inside your for file in files block. Commented Jun 24, 2020 at 4:19
  • yea, it's same result like stackoverflow.com/a/62509874/8361239 answer, it doesn't work with multiple image in file. how to do this? Commented Jun 24, 2020 at 4:42
  • Are you trying to separate multiple images in a single JPEG file? I don't know that much about graphic file formats to know if they naturally support multiple images internally, or you have to cut them apart digitally with image processing software. Commented Jun 28, 2020 at 2:16

1 Answer 1

1

you keep appending to your results but you always look at resp_dict[0] so you always look at the same item. instead look at resp_dict[-1] so you will look at the NEWEST item

so this:

if resp_dict[0]['results']:
                        num = resp_dict[0]['results'][0]['plate']
                        print(f"detected number:  {num}")

should be this:

if resp_dict[-1]['results']:
                        num = resp_dict[-1]['results'][0]['plate']
                        print(f"detected number:  {num}")
Sign up to request clarification or add additional context in comments.

9 Comments

ah i see, it works with only one image input, but does't works with many inputs
it not that it doesnt work, it just that you read the response of the first one you tried instead of the last one you tried
how to make it works with multiple inputs? @Nullman
how do you mean? multiple inputs where?
i'm sorry, i mean how to make it works with many images in folder? @Nullman
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.