File size: 2,357 Bytes
b1f46e5
 
 
 
 
 
 
 
 
 
ceb119d
 
b1f46e5
 
 
 
 
ceb119d
b1f46e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from fastapi import FastAPI ,Request ,Form, UploadFile, File
from fastapi.responses import JSONResponse
from fastapi.responses import HTMLResponse, FileResponse
import os
import io
from PIL import ImageOps,Image ,ImageFilter
#from transformers import pipeline
import matplotlib.pyplot as plt
import numpy as np
import ast
import server

#http://localhost:8000
app = FastAPI()

# Root route
@app.get('/')
def main():
    return "Hello World taha"


@app.post('/predict')
async  def predict(supported_types_str: str = Form(),age: str = Form() , file: UploadFile = File(...)): 
    # Form(...) to accept input as web form ,may change when android /upload 

    supported_types=ast.literal_eval(supported_types_str)

    contents = await file.read()
    image = Image.open(io.BytesIO(contents))
       
    # Process the image (example: convert to grayscale)
    processed_image = image.convert("L")

    # Save the processed image to a temporary file
    output_file_path = "tmp_processed_image.png"
    processed_image.save(output_file_path)

    # Return the processed image for download
    return FileResponse(output_file_path, media_type='image/png', filename="tmp_processed_image.png")
    




@app.post('/predict2')
async def predict2(supported_types_str: str = Form(...), age: str = Form(...), file: UploadFile = File(...)):
    
        contents = await file.read()
        image = Image.open(io.BytesIO(contents))
        
        # Process the image (example: convert to grayscale)
        processed_image = image.convert("L")

        # Save the processed image to a BytesIO object
        img_byte_arr = io.BytesIO()
        processed_image.save(img_byte_arr, format='PNG')
        img_byte_arr.seek(0)  # Move to the beginning of the BytesIO buffer

        # Create an HTML response with the processed image
        html_content = f"""
        <html>
            <body>
                <h3>Processed Image:</h3>
                <img src="data:image/png;base64,{img_byte_arr.getvalue().decode('latin1')}" alt="Processed Image" style="max-width: 500px;"/>
                <br><br>
                <p>Your name: {supported_types_str}</p>
                <p>Your age: {age}</p>
                <p><a href="/download">Download Processed Image</a></p>
            </body>
        </html>
        """
        return HTMLResponse(content=html_content)