johnwesley756/instance-segmentation
0
1---2title: Tooth Decay Detection & Severity Analysis3emoji: π¦·4colorFrom: blue5colorTo: purple6sdk: docker7sdk_version: "4.36.0"8app_file: app.py9pinned: false10---11 12# π¦· Tooth Decay Detection and Severity Analysis Using Deep Learning13 14A computer vision project that uses YOLOv8 to detect dental conditions like tooth, caries, and cavity from images. The application features a **FastAPI backend** for API-based inference and a **Streamlit frontend** for interactive testing.15 16---17 18## π Features19 20* β‘ Real-time detection of **tooth**, **caries**, and **cavity**21* π Severity classification (Tooth β Caries β Cavity)22* π Natural language summary for each detection23* π **REST API** for integration with other applications24* π **Streamlit UI** for easy testing and visualization25* π³ **Docker support** for easy deployment26* βοΈ **Hugging Face Spaces** ready27 28---29 30## ποΈ Architecture31 32```33βββββββββββββββββββ ββββββββββββββββββββ34β Streamlit UI β ββHTTPβββΆβ FastAPI Server β35β (Frontend) β β (Backend) β36β Port: 7860 β β Port: 8000 β37βββββββββββββββββββ ββββββββββββββββββββ38 β39 βΌ40 βββββββββββββββββββ41 β YOLOv8 Model β42 β (best.pt) β43 βββββββββββββββββββ44```45 46---47 48## π§ Technologies Used49 50* **Python 3.11+**51* **YOLOv8 / Ultralytics** - Object detection model52* **FastAPI** - REST API backend53* **Streamlit** - Interactive frontend54* **OpenCV** - Image processing55* **Docker** - Containerization56 57---58 59## π Project Structure60 61```62π¦ Instance-segmentation63 β£ π api.py # FastAPI backend server64 β£ π app.py # Streamlit frontend65 β£ π train_model.py # Original Streamlit app (legacy)66 β£ π best.pt # YOLOv8 trained model67 β£ π requirements.txt # Python dependencies68 β£ π Dockerfile # Docker configuration69 β£ π start.sh # Linux/Mac startup script70 β£ π start.bat # Windows startup script71 β π README.md # This file72```73 74---75 76## π Quick Start77 78### Option 1: Local Development79 801. **Install dependencies**81```bash82pip install -r requirements.txt83```84 852. **Start the FastAPI backend**86```bash87uvicorn api:app --host 0.0.0.0 --port 800088```89 903. **Start the Streamlit frontend** (in a new terminal)91```bash92streamlit run app.py --server.port 786093```94 954. **Access the application**96 - Streamlit UI: http://localhost:786097 - FastAPI Docs: http://localhost:8000/docs98 99### Option 2: Using Startup Scripts100 101**Windows:**102```bash103start.bat104```105 106**Linux/Mac:**107```bash108chmod +x start.sh109./start.sh110```111 112### Option 3: Docker113 1141. **Build the Docker image**115```bash116docker build -t tooth-detection .117```118 1192. **Run the container**120```bash121docker run -p 7860:7860 -p 8000:8000 tooth-detection122```123 124---125 126## π API Endpoints127 128### `GET /`129Root endpoint with API information130 131### `GET /health`132Health check endpoint133```json134{135 "status": "healthy",136 "model_loaded": true,137 "model_path": "best.pt"138}139```140 141### `POST /predict`142Upload an image for tooth decay detection143 144**Request:**145- Method: `POST`146- Content-Type: `multipart/form-data`147- Body: `file` (image file)148 149**Response:**150```json151{152 "success": true,153 "severity": "Caries",154 "summary": "Caries (initial decay) detected...",155 "detections": [156 {157 "class": "caries",158 "confidence": 0.856,159 "bbox": [120.5, 45.2, 280.3, 190.7]160 }161 ],162 "total_detections": 1,163 "annotated_image": "base64_encoded_image..."164}165```166 167**Example using cURL:**168```bash169curl -X POST "http://localhost:8000/predict" \170 -F "file=@dental_image.jpg"171```172 173**Example using Python:**174```python175import requests176 177url = "http://localhost:8000/predict"178files = {"file": open("dental_image.jpg", "rb")}179response = requests.post(url, files=files)180print(response.json())181```182 183---184 185## β
Severity Logic186 187| Detected Labels | Severity |188| ---------------------- | ------------ |189| Contains "cavity" | Cavity |190| Else contains "caries" | Caries |191| Only "tooth" | Tooth |192| None | No Detection |193 194---195 196## π Deploying to Hugging Face Spaces197 1981. **Create a new Space** on [Hugging Face](https://huggingface.co/spaces)199 - Choose "Docker" as the SDK200 2012. **Upload files:**202 - `api.py`203 - `app.py`204 - `best.pt`205 - `requirements.txt`206 - `Dockerfile`207 - `start.sh`208 2093. **The Space will automatically build and deploy**210 2114. **Access your deployed app** at:212 - `https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME`213 214---215 216## π§ͺ Testing the Model217 2181. Upload a dental X-ray or intraoral image2192. The model will detect and classify:220 - **Tooth**: Healthy teeth221 - **Caries**: Early-stage decay222 - **Cavity**: Advanced decay2233. View the annotated image with bounding boxes2244. Read the severity level and summary225 226---227 228## π§ Future Enhancements229 230* Add authentication for API access231* Implement batch processing for multiple images232* Add model versioning and A/B testing233* Create mobile app using the API234* Add real-time webcam detection235* Integrate with dental clinic management systems236 237---238 239## π License240 241This project is for educational and research purposes.242 243---244 245## π¨βπ» Developer246 247Built with β€οΈ using YOLOv8, FastAPI, and Streamlit248 