Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Building Data Science Applications with FastAPI

You're reading from  Building Data Science Applications with FastAPI

Product type Book
Published in Oct 2021
Publisher Packt
ISBN-13 9781801079211
Pages 426 pages
Edition 1st Edition
Languages
Author (1):
François Voron François Voron
Profile icon François Voron

Table of Contents (19) Chapters

Preface Section 1: Introduction to Python and FastAPI
Chapter 1: Python Development Environment Setup Chapter 2: Python Programming Specificities Chapter 3: Developing a RESTful API with FastAPI Chapter 4: Managing Pydantic Data Models in FastAPI Chapter 5: Dependency Injections in FastAPI Section 2: Build and Deploy a Complete Web Backend with FastAPI
Chapter 6: Databases and Asynchronous ORMs Chapter 7: Managing Authentication and Security in FastAPI Chapter 8: Defining WebSockets for Two-Way Interactive Communication in FastAPI Chapter 9: Testing an API Asynchronously with pytest and HTTPX Chapter 10: Deploying a FastAPI Project Section 3: Build a Data Science API with Python and FastAPI
Chapter 11: Introduction to NumPy and pandas Chapter 12: Training Machine Learning Models with scikit-learn Chapter 13: Creating an Efficient Prediction API Endpoint with FastAPI Chapter 14: Implement a Real-Time Face Detection System Using WebSockets with FastAPI and OpenCV Other Books You May Enjoy

Implementing an HTTP endpoint to perform face detection on a single image

Before working with WebSockets, we'll start simple and implement, using FastAPI, a classic HTTP endpoint for accepting image uploads and performing face detection on them. As you'll see, the main difference from the previous example is in how we acquire the image: instead of streaming it from the webcam, we get it from a file upload that we have to convert into an OpenCV image object.

You can see the whole implementation in the following code:

chapter14_api.py

from typing import List, Tuple
import cv2
import numpy as np
from fastapi import FastAPI, File, UploadFile
from pydantic import BaseModel
app = FastAPI()
cascade_classifier = cv2.CascadeClassifier()
class Faces(BaseModel):
    faces: List[Tuple[int, int, int, int]]
@app.post("/face-detection", response_model=Faces)
async def face_detection(image: UploadFile = File(...)) -> Faces:
    ...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}