| from deoldify import device | |
| from deoldify.device_id import DeviceId | |
| from deoldify.visualize import get_image_colorizer | |
| device.set(device=DeviceId.GPU0) # nếu bạn có GPU, nếu không có thì bỏ dòng này | |
| colorizer = get_image_colorizer(artistic=True) | |
| colorizer.plot_transformed_image( | |
| r"C:\Users\leamo\Downloads\as.jpg", # đường dẫn ảnh trắng đen | |
| render_factor=90, | |
| display_render_factor=True, | |
| figsize=(8,8), | |
| watermarked=False | |
| ) | |
| # from fastapi import FastAPI, File, UploadFile | |
| # from fastapi.responses import FileResponse | |
| # import uvicorn, os | |
| # from deoldify.visualize import get_image_colorizer | |
| # from datetime import datetime | |
| # app = FastAPI() | |
| # colorizer = get_image_colorizer(artistic=False) | |
| # UPLOAD_DIR = "static/uploads" | |
| # RESULT_DIR = "static/results" | |
| # os.makedirs(UPLOAD_DIR, exist_ok=True) | |
| # os.makedirs(RESULT_DIR, exist_ok=True) | |
| # @app.post("/colorize") | |
| # async def colorize_image(file: UploadFile = File(...)): | |
| # # Lưu ảnh upload tạm | |
| # input_path = os.path.join(UPLOAD_DIR, file.filename) | |
| # with open(input_path, "wb") as f: | |
| # f.write(await file.read()) | |
| # # Xử lý ảnh | |
| # timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") | |
| # output_path = os.path.join(RESULT_DIR, f"colorized_{timestamp}.jpg") | |
| # colorizer.plot_transformed_image( | |
| # path=input_path, | |
| # render_factor=35, | |
| # display_render_factor=False, | |
| # post_process=True | |
| # ) | |
| # # DeOldify mặc định lưu ở "result_images" → di chuyển sang static/results | |
| # if os.path.exists("result_images"): | |
| # for f_name in os.listdir("result_images"): | |
| # os.rename(f"result_images/{f_name}", output_path) | |
| # break | |
| # return FileResponse(output_path) | |
| # if __name__ == "__main__": | |
| # uvicorn.run(app, host="0.0.0.0", port=8000) | |