Firstly, What is FastAPI ?
FastAPI is a modern, fast (high-performance), web framework for building APIs with Python based.
Some of the features FastAPI offers are :
- Fast: Very high performance, on par with NodeJS and Go (thanks to Starlette and Pydantic).
- Fast to code: Increase the speed to develop features by about 200% to 300%. *
- Fewer bugs
- Robust.
Let’s Look at some of Tech Companies using FastAPI:
- Microsoft
- Uber
- Netflix
Installation of FastAPI :
pip install fastapi
pip install “uvicorn[standard]”
NOTE : Python Version 3.6+ .
Running A FastAPI File:
uvicorn main:app — reload
NOTE : Name the API file main.py always or else the file wont run.
Simple Example of FastAPI [OCR_FASTAPI]
Python code for OCR
import cv2
import pytesseract
img = cv2.imread(‘7.png’)
cv2.imshow(‘sample image’ , img)
cv2.waitKey(0)
cv2.destroyAllWindows()
text = pytesseract.image_to_string(img)
print(text)
Now Let’s look at the FastAPI Code for this python code :
from typing import Optional
from fastapi import FastAPI
import pytesseract
import numpy as np
import cv2
import urllib.request
from pydantic import BaseModel
app = FastAPI()
class DataModel(BaseModel):
url:str
def url_to_image(url):
download the image, convert it to a NumPy array, and then read
it into OpenCV format
resp = urllib.request.urlopen(url)
image = np.asarray(bytearray(resp.read()), dtype=”uint8")
image = cv2.imdecode(image, cv2.IMREAD_COLOR)
return the image
return image
[@app](twitter.com/app "Twitter profile for @app").get(‘/’)
def serverstatus():
return {“status”: “Running”}
[@app](twitter.com/app "Twitter profile for @app").post(“/text/”)
def read_root(dm:DataModel):
baseurl = dm.url
image = url_to_image(baseurl)
text = pytesseract.image_to_string(image)
return { “result”: text}
One of the best and important features of FastAPI is you can test your API with FastAPI’S Software:
Usually Once you run the API : You will be run on your localhost. http://127.0.0.1:8000
Now to test the api you needn’t use POSTMAN Just add docs to the url
FastAPI Testing Interface
It as easy as this and effective . To Learn More about FastAPI refer to these websites below:
Some of the Other Helpful Products By tiangolo (FastAPI Creator):
Happy Reading :)