0% found this document useful (0 votes)
11 views3 pages

Handwritten Digit Classifier API Guide

The document outlines the development of a Handwritten Digit Classifier using a REST API for modular interaction between a Tkinter frontend and a FastAPI backend. It details the project structure, image processing, API interaction, and configurable settings for both frontend and backend. The application allows users to draw digits, which are then processed and predicted by a pre-trained machine learning model.

Uploaded by

ch21b006
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

Handwritten Digit Classifier API Guide

The document outlines the development of a Handwritten Digit Classifier using a REST API for modular interaction between a Tkinter frontend and a FastAPI backend. It details the project structure, image processing, API interaction, and configurable settings for both frontend and backend. The application allows users to draw digits, which are then processed and predicted by a pre-trained machine learning model.

Uploaded by

ch21b006
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Handwritten Digit Classifier: REST API

Integration
Aditya Sharma (CH21B006)
February 20, 2025

1 Introduction
This project involves developing a digit classification application where users
can draw a number on a Tkinter canvas, and a backend machine learning model
predicts the digit. The objective was to decouple the UI and the model by using
a REST API for inference, making the system modular and flexible.

2 Project Structure
The project consists of two main components:

• Frontend ([Link]): A Tkinter-based UI where users can draw digits.


• Backend ([Link]): A FastAPI server that loads a pre-trained model
and serves predictions via an API.

The directory structure is as follows:

HandwrittenDigitClassifier/
[Link] # UI application
[Link] # FastAPI server
[Link] # Pre-trained MLP model
dense_neural_class.py # Model class definition
[Link] # Dependencies
[Link] # Project documentation

3 Image Processing and API Interaction


3.1 Image Representation
The drawn digit is captured as a 28x28 grayscale image using the PIL library
in [Link]. It is then flattened into a vector of 784 values before being sent to
the API as a JSON payload:

1
Listing 1: Image Processing in [Link]
i m a g e v e c t o r = np . a r r a y ( s e l f . image ) . r e s h a p e ( 1 , −1). t o l i s t ( )
r e s p o n s e = r e q u e s t s . p o s t (API URL , j s o n ={” image ” : i m a g e v e c t o r } )

3.2 API Endpoint


The FastAPI server receives the image, normalizes pixel values (0-255 scaled to
0-1), and reshapes it for model inference:
Listing 2: API Processing in [Link]
@app . p o s t ( ” / p r e d i c t ” )
async def p r e d i c t ( data : ImageData ) :
i m a g e d a t a = np . a r r a y ( data . image , dtype=np . f l o a t 3 2 )
image data = image data / 255.0
i m a g e d a t a = i m a g e d a t a . r e s h a p e ( 1 , −1)
p r e d i c t i o n = model . p r e d i c t ( i m a g e d a t a ) [ 0 ]
return {” p r e d i c t i o n ” : int ( p r e d i c t i o n ) }

4 Configurable API URL


The application supports a configurable API endpoint via command-line argu-
ments. This allows for flexible deployment across different servers and ports:

4.1 Frontend Configuration

Listing 3: Configuring API URL in [Link]


p a r s e r . add argument (
”−−api−u r l ” ,
type=str ,
d e f a u l t=” h t t p : / / 1 2 7 . 0 . 0 . 1 : 5 0 0 0 / p r e d i c t ” ,
help=”The API URL f o r t h e p r e d i c t i o n e n d p o i n t ”
)
args = parser . parse args ()
API URL = a r g s . a p i u r l

4.2 Backend Configuration

Listing 4: Configurable Host and Port in [Link]


p a r s e r . add argument ( ”−−h o s t ” , type=str , d e f a u l t=” 0 . 0 . 0 . 0 ”
)
p a r s e r . add argument ( ”−−p o r t ” , type=int , d e f a u l t =5000)
args = parser . parse args ()
u v i c o r n . run ( app , h o s t=a r g s . host , p o r t=a r g s . p o r t )

2
Users can specify different host URLs and ports when launching the backend:
python [Link] --host [Link] --port 8000

Common questions

Powered by AI

When designing the image to JSON conversion process, considerations include ensuring efficient and accurate conversion from the image to a numerical format suitable for the model. This involves addressing how images are resized, ensuring they maintain the necessary level of detail for accurate predictions. Another consideration is minimizing latency in converting and transmitting data, which involves optimizing the data serialization and network request processes. Additionally, ensuring that the chosen data structure for the JSON payload is compatible with the format expected by the backend is crucial for seamless integration .

FastAPI offers several benefits for the handwritten digit classifier project, such as high performance thanks to its asynchronous request handling capabilities, which is suitable for model inference tasks. Additionally, FastAPI provides automatic generation of interactive documentation and type validation, which aids in efficient development and debugging. These features contribute to building a robust and responsive REST API that supports the project's modular architecture .

Scalability of a REST API-based application is directly influenced by network settings and environment configurations. Choosing optimal server settings, such as host address and port numbers, can significantly affect how well the application handles increasing loads. Using scalable server environments that can allocate resources dynamically ensures that as user demand grows, the application can scale accordingly. Furthermore, implementing load balancing and efficient routing can distribute incoming requests evenly across available resources, preventing any single server from becoming a bottleneck. These choices ultimately impact the performance, reliability, and user satisfaction of the application .

Using command-line arguments for configuring server settings such as host and port enhances deployment flexibility by allowing users to easily set these parameters at runtime. This means the backend can be adjusted to different network configurations and environments without altering the codebase, facilitating ease of deployment across different machines or cloud instances .

Normalization is a crucial preprocessing step that involves scaling pixel values of the image from a range of 0-255 to 0-1. This transformation ensures that the input data to the model is on a consistent scale, which can improve the convergence speed and stability of the learning algorithm. Without normalization, variations in pixel values could lead to poor model predictions due to differences in magnitude that are irrelevant to the model's performance .

The purpose of having a configurable API URL is to enable flexible deployment of the application across different server environments. By allowing the API URL to be set via command-line arguments, users can easily change the server address or port without modifying the source code. This capability is particularly useful when deploying the application on various networks or cloud environments, ensuring that the system can adapt to different contexts and configurations .

Decoupling the UI from the model aligns with the software engineering principle of separation of concerns. By isolating the concerns of user interaction (UI) and data processing (model), each component can be developed, tested, and maintained independently, reducing the complexity of the system. This approach also adheres to the principles of modularity and reusability, allowing each part to be easily replaced or updated without impacting the entire system. Furthermore, the use of REST APIs as a means of communication between the UI and model promotes interoperability and scalability .

First, the user draws a digit on the Tkinter canvas. This digit is captured as a grayscale image of size 28x28 using the PIL library. The image is then flattened into a vector of 784 values. This vector is sent as a JSON payload to the FastAPI server via a POST request. At the server, the image data is converted into a numpy array, normalized from the range 0-255 to 0-1, reshaped for model inference, and fed into the pre-trained model. The model predicts the digit, which is then returned as a JSON response .

Using a pre-trained machine learning model can significantly enhance performance by enabling quick deployment without the need for extensive training. Such models bring the benefits of being well-tuned and reliable, leading to high prediction accuracy. However, the trade-off comes in terms of customization; pre-trained models may not be easily modified to incorporate new patterns or data specific to users' needs. This could limit the system's adaptability to unique or evolving input characteristics unless further customization or retraining is applied .

The integration of a REST API allows for a clear separation between the frontend UI and the backend machine learning model, thereby enhancing modularity. This separation means that changes can be made independently to either the UI or the model without affecting the other component. Additionally, the REST API provides flexibility by enabling the model to be accessed remotely, allowing for deployment on different servers and configurations, as indicated by the configurable API endpoint and server settings .

You might also like