WEBVTT

﻿1
00:00:01.334 --> 00:00:08.258
Today, we will be creating an API that detects fire in images using the YOLO model.  

2
00:00:08.800 --> 00:00:14.681
This lecture will focus on the `router.py` file, while `schema.py` defines the response data types. 

3
00:00:16.433 --> 00:00:21.187
First, the `router.py` file begins by importing the necessary modules.  

4
00:00:21.563 --> 00:00:25.400
For example, `uuid` is used for generating unique identifiers, 

5
00:00:25.734 --> 00:00:30.155
`FastAPI` for API routing and exception handling, 

6
00:00:30.655 --> 00:00:33.158
`ultralytics` for running the YOLO model, 

7
00:00:33.408 --> 00:00:35.160
`cv2` for image processing, 

8
00:00:35.326 --> 00:00:37.495
`pytz` for timezone handling, 

9
00:00:37.746 --> 00:00:39.914
and `logging` for recording logs.  

10
00:00:40.874 --> 00:00:43.752
The first step is file extension validation.  

11
00:00:44.586 --> 00:00:52.594
The `ALLOWED_EXTENSIONS` set defines allowed image file extensions such as `jpg`, `jpeg`, and `png`.  

12
00:00:53.762 --> 00:01:01.269
The `allowed_file` function checks whether the uploaded file's extension belongs to this set.  

13
00:01:02.729 --> 00:01:10.820
If the file has an invalid extension, an `HTTPException` is raised to notify the user of the error.

14
00:01:12.739 --> 00:01:17.702
The next step is file storage and temporary directory management.  

15
00:01:18.078 --> 00:01:27.629
The `generate_random_file_name` function generates a unique random file name while preserving the original file extension.  

16
00:01:28.588 --> 00:01:36.096
The uploaded file is then saved in a directory named `temp` using this generated file name.  

17
00:01:36.679 --> 00:01:45.438
If an issue occurs during file storage, an exception is handled to return an appropriate error message. 

18
00:01:47.232 --> 00:01:53.154
The most crucial part is loading the YOLO model and running predictions.

19
00:01:53.947 --> 00:01:58.952
The code loads a trained model from the path `assets/best.pt`.  

20
00:01:59.911 --> 00:02:06.334
Once the model is successfully loaded, it runs predictions on the stored temporary file.  

21
00:02:08.002 --> 00:02:18.471
The prediction results are returned as a list, providing bounding box information, class names, and confidence scores for each detected object.  

22
00:02:19.681 --> 00:02:22.809
Next, the prediction results are processed.  

23
00:02:23.268 --> 00:02:30.233
Each detected object is iterated through, extracting information to create `Detection` objects.  

24
00:02:30.859 --> 00:02:36.281
If an object of class `fire` is detected, the `fire_detected` flag is set.  

25
00:02:37.115 --> 00:02:43.621
A marked-up image with bounding boxes is also generated and saved in the `log` directory.  

26
00:02:44.330 --> 00:02:49.919
The current time is converted from UTC to Asia/Seoul timezone, 

27
00:02:50.044 --> 00:02:55.592
and all information is formatted according to `PredictFireSchema` for the response data.  

28
00:02:58.094 --> 00:03:01.347
Finally, we look at exception handling and logging.  

29
00:03:02.098 --> 00:03:11.274
A `try-except-finally` structure is used to catch errors that may occur during file storage, model loading, or prediction execution.

30
00:03:12.025 --> 00:03:15.069
Logs are recorded to ensure API stability. 

31
00:03:15.820 --> 00:03:18.656
Now, let's test this API using Postman.  

32
00:03:19.574 --> 00:03:22.702
Open Postman and create a new `POST` request.  

33
00:03:23.369 --> 00:03:28.499
Enter the server address and `/predict_fire` endpoint in the request URL.  

34
00:03:29.292 --> 00:03:36.007
In the `Body` tab, select `form-data`, add a key named `file`, and set its type to `File`.  

35
00:03:37.091 --> 00:03:40.970
Select an image file for testing and click the `Send` button.  

36
00:03:41.221 --> 00:03:47.644
The file is sent to the server, and the processing result is returned in JSON format.  

37
00:03:48.186 --> 00:03:56.027
If fire is detected, the `message` field will display `fire detected`; otherwise, it will show `safe`.  

38
00:03:56.361 --> 00:04:02.659
The response also includes the file name, detection results, result image key, and timestamp.  

39
00:04:03.159 --> 00:04:09.207
In fact, this implementation only utilizes a small portion of YOLO’s capabilities.  

40
00:04:09.791 --> 00:04:17.882
It is similar to turning a dial to operate a microwave—you don’t need to fully understand the internal mechanisms.  
