parkpow / deep-license-plate-recognition

Automatic License Plate Recognition (ALPR) or Automatic Number Plate Recognition (ANPR) software that works with any camera.
https://platerecognizer.com/
MIT License
523 stars 122 forks source link

blur-benchmark #201

Closed danleyb2 closed 5 months ago

github-actions[bot] commented 6 months ago

Risk Level 2 - /home/runner/work/deep-license-plate-recognition/deep-license-plate-recognition/benchmark/benchmark_snapshot.py

The changes introduce a new feature for blurring images via an API call. However, there are a few concerns:

  1. The blur_api function raises a generic Exception which is not ideal for error handling. It's better to define a custom exception class.
  2. The response.status_code check could be simplified by using response.raise_for_status() which automatically raises an HTTPError if the HTTP request returned an unsuccessful status code.
  3. The blur_api function assumes the response will be JSON and have a blur key. It should handle the case where the response is not JSON or does not contain the expected key.

Suggested changes:

  1. Define a custom exception class for better error handling.
  2. Use response.raise_for_status() to simplify status code checks.
  3. Add error handling for JSON decoding and key presence.

Example:

class BlurAPIError(Exception):
    pass

# ... inside blur_api ...
response.raise_for_status()
try:
    blur_data = response.json().get(\"blur\")
    if blur_data is None:
        raise BlurAPIError(\"'blur' key not found in response.\")
except ValueError:
    raise BlurAPIError(\"Response content is not valid JSON.\")

Risk Level 3 - /home/runner/work/deep-license-plate-recognition/deep-license-plate-recognition/plate_recognition.py

The changes include a compatibility fix for Python 3.10+ regarding the MutableMapping import. However, there are some issues with the error handling and code structure:

  1. The recognition_api function prints the response text and exits directly on error, which is not a good practice for library functions. It should return an error or raise an exception instead.
  2. The recognition_api function has a retry loop for HTTP 429 status codes, but it does not handle other potential retry-able status codes or network issues.
  3. The recognition_api function uses a global _session variable which could lead to issues if the module is used concurrently in a multi-threaded environment.

Suggested changes:

  1. Replace print and exit with raising a custom exception.
  2. Expand the retry logic to handle other retry-able scenarios and network exceptions.
  3. Consider using thread-local storage for _session or refactor to avoid using a global variable.

Example:

class RecognitionAPIError(Exception):
    pass

# ... inside recognition_api ...
if response.status_code != 200:
    raise RecognitionAPIError(f\"API request failed with status {response.status_code}: {response.text}\")

# ... retry logic ...
except requests.exceptions.RequestException as e:
    time.sleep(1)
    # retry logic here

🔍🛠️🚫


Powered by Code Review GPT