File size: 2,638 Bytes
29b0bbc
 
a44369e
 
29b0bbc
 
 
 
b5e6b91
a44369e
29b0bbc
 
 
 
 
 
 
 
 
9a12466
 
 
 
 
29b0bbc
 
b5e6b91
29b0bbc
 
1b399de
 
 
29b0bbc
 
 
 
1b399de
29b0bbc
 
1b399de
 
 
 
 
 
 
29b0bbc
1b399de
 
29b0bbc
 
 
 
1b399de
 
29b0bbc
 
1b399de
29b0bbc
b5e6b91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
74
75
76
77
78
79
80
81
from fastapi import FastAPI, File, UploadFile, HTTPException
from fastapi.responses import StreamingResponse
from image_enhancer import EnhancementMethod, Enhancer
from video_enhancer import VideoEnhancer
from pydantic import BaseModel
from PIL import Image
from io import BytesIO
import base64
import magic
from typing import List
class EnhancementRequest(BaseModel):
    method: EnhancementMethod = EnhancementMethod.gfpgan
    background_enhancement: bool = True
    upscale: int = 2

class _EnhanceBase(BaseModel):
    encoded_base_img: List[str]



app = FastAPI()

@app.get("/")
def greet_json():
    return {"Initializing GlamApp Enhancer"}

@app.post("/enhance/image/")
async def enhance_image(
    file: UploadFile = File(...),
    method: EnhancementMethod = EnhancementMethod.gfpgan,
    background_enhancement: bool = True,
    upscale: int = 2
):
    try:
        if not file.content_type.startswith('image/'):
            raise HTTPException(status_code=400, detail="Invalid file type")
        
        contents = await file.read()
        base64_encoded_image = base64.b64encode(contents).decode('utf-8')
        
        request = EnhancementRequest(
            method=method,
            background_enhancement=background_enhancement,
            upscale=upscale
        )
        
        enhancer = Enhancer(request.method, request.background_enhancement, request.upscale)
        enhanced_img, original_resolution, enhanced_resolution = await enhancer.enhance(base64_encoded_image)
        
        enhanced_image = Image.fromarray(enhanced_img)
        img_byte_arr = BytesIO()
        enhanced_image.save(img_byte_arr, format='PNG')
        img_byte_arr.seek(0)
        
        print(f"Original resolution: {original_resolution}, Enhanced resolution: {enhanced_resolution}")
        
        return StreamingResponse(img_byte_arr, media_type="image/png")
    
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))
        
@app.post("/enhance/video/")
async def enhance_video(file: UploadFile = File(...)):
    enhancer = VideoEnhancer()
    file_header = await file.read(1024)
    file.file.seek(0)
    mime = magic.Magic(mime=True)
    file_mime_type = mime.from_buffer(file_header)
    
    accepted_mime_types = [
        'video/mp4',
        'video/mpeg',
        'video/x-msvideo',
        'video/quicktime',
        'video/x-matroska',
        'video/webm'
    ]
    
    if file_mime_type not in accepted_mime_types:
        raise HTTPException(status_code=400, detail="Invalid file type. Please upload a video file.")
    return await enhancer.stream_enhanced_video(file)