Today, we will be creating an API that detects fire in images using the YOLO model.
This lecture will focus on the `router.py` file, while `schema.py` defines the response data types.
First, the `router.py` file begins by importing the necessary modules.
For example, `uuid` is used for generating unique identifiers,
`FastAPI` for API routing and exception handling,
`ultralytics` for running the YOLO model,
`cv2` for image processing,
`pytz` for timezone handling,
and `logging` for recording logs.
The first step is file extension validation.
The `ALLOWED_EXTENSIONS` set defines allowed image file extensions such as `jpg`, `jpeg`, and `png`.
The `allowed_file` function checks whether the uploaded file's extension belongs to this set.
If the file has an invalid extension, an `HTTPException` is raised to notify the user of the error.
The next step is file storage and temporary directory management.
The `generate_random_file_name` function generates a unique random file name while preserving the original file extension.
The uploaded file is then saved in a directory named `temp` using this generated file name.
If an issue occurs during file storage, an exception is handled to return an appropriate error message.
The most crucial part is loading the YOLO model and running predictions.
The code loads a trained model from the path `assets/best.pt`.
Once the model is successfully loaded, it runs predictions on the stored temporary file.
The prediction results are returned as a list, providing bounding box information, class names, and confidence scores for each detected object.
Next, the prediction results are processed.
Each detected object is iterated through, extracting information to create `Detection` objects.
If an object of class `fire` is detected, the `fire_detected` flag is set.
A marked-up image with bounding boxes is also generated and saved in the `log` directory.
The current time is converted from UTC to Asia/Seoul timezone,
and all information is formatted according to `PredictFireSchema` for the response data.
Finally, we look at exception handling and logging.
A `try-except-finally` structure is used to catch errors that may occur during file storage, model loading, or prediction execution.
Logs are recorded to ensure API stability.
Now, let's test this API using Postman.
Open Postman and create a new `POST` request.
Enter the server address and `/predict_fire` endpoint in the request URL.
In the `Body` tab, select `form-data`, add a key named `file`, and set its type to `File`.
Select an image file for testing and click the `Send` button.
The file is sent to the server, and the processing result is returned in JSON format.
If fire is detected, the `message` field will display `fire detected`; otherwise, it will show `safe`.
The response also includes the file name, detection results, result image key, and timestamp.
In fact, this implementation only utilizes a small portion of YOLO’s capabilities.
It is similar to turning a dial to operate a microwave—you don’t need to fully understand the internal mechanisms.