Repository navigation
Expand file tree
/
Copy pathdct_fast_api.py
More file actions
129 lines (95 loc) · 4.68 KB
/
Copy pathdct_fast_api.py
File metadata and controls
129 lines (95 loc) · 4.68 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from fastapi import FastAPI, UploadFile, File, Form
from fastapi.responses import FileResponse
import numpy as np
import cv2
import os
import uuid
import logging
# from dct_claude_quant_1 import ImprovedDCTSteganography
# Import the class instead of functions
from dct_claude_quant import ImprovedDCTSteganography
app = FastAPI()
steganography = ImprovedDCTSteganography(alpha=0.012)
# Set up logging
logging.basicConfig(level=logging.INFO)
# Define a save path in the home directory
SAVE_DIR = os.path.expanduser("~/dct_files/")
os.makedirs(SAVE_DIR, exist_ok=True) # Ensure the directory exists
@app.post("/embed")
async def embed_patient_id(file: UploadFile = File(...), patient_id: str = Form(...)):
try:
image_bytes = await file.read()
np_arr = np.frombuffer(image_bytes, np.uint8)
image = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
if image is None:
logging.error("❌ Failed to decode image!")
return {"error": "Invalid image file"}
# Save temporary file for get_max_message_length
temp_path = os.path.join(SAVE_DIR, "temp.png")
cv2.imwrite(temp_path, image)
steganography = ImprovedDCTSteganography()
max_length = steganography.get_max_message_length(temp_path)
if len(patient_id) > max_length:
return {"error": f"Patient ID too long. Maximum length: {max_length}"}
embedded_image = steganography.embed(image, patient_id)
filename = f"embedded_{uuid.uuid4().hex}.png"
output_path = os.path.join(SAVE_DIR, filename)
cv2.imwrite(output_path, embedded_image)
logging.info(f"🧬 Embedding message: {patient_id} into {file.filename}")
return {"download_url": f"http://localhost:8000/download/{filename}"}
# return FileResponse(output_path, media_type="image/png", filename=filename)
except Exception as e:
logging.error(f"❌ Internal Server Error: {str(e)}", exc_info=True)
return {"error": "An internal server error occurred"}
@app.post("/decode")
async def decode_patient_id(file: UploadFile = File(...)):
"""Decodes the hidden message from an uploaded image using the DCT function from `dct_claude_quant.py`."""
try:
logging.info("📥 Received image for decoding...")
image_bytes = await file.read()
np_arr = np.frombuffer(image_bytes, np.uint8)
image = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
if image is None:
logging.error("❌ Failed to decode image!")
return {"error": "Invalid image file. Please upload a valid PNG or JPEG file."}
logging.info("✅ Image loaded successfully. Starting DCT decoding...")
# Save temporary file for extract_text
temp_path = os.path.join(SAVE_DIR, "temp_decode.png")
cv2.imwrite(temp_path, image)
steganography = ImprovedDCTSteganography()
extracted_message = steganography.extract_text(temp_path)
if not extracted_message:
logging.warning("⚠️ No hidden message found in the image.")
return {"error": "No hidden message found in the image."}
logging.info(f"✅ Extracted message: {extracted_message}")
return {"extracted_message": extracted_message}
except Exception as e:
logging.error(f"❌ Internal Server Error: {str(e)}", exc_info=True)
return {"error": "An internal server error occurred."}
# @app.post("/decode")
# async def decode_patient_id(file: UploadFile = File(...)):
# try:
# image_bytes = await file.read()
# np_arr = np.frombuffer(image_bytes, np.uint8)
# image = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
# if image is None:
# logging.error("❌ Failed to decode image!")
# return {"error": "Invalid image file"}
# temp_path = os.path.join(SAVE_DIR, "temp_decode.png")
# cv2.imwrite(temp_path, image)
# steganography = ImprovedDCTSteganography()
# extracted_message = steganography.extract_text(temp_path)
# return {"extracted_message": extracted_message}
# except Exception as e:
# logging.error(f"❌ Internal Server Error: {str(e)}", exc_info=True)
# return {"error": "An internal server error occurred"}
@app.get("/download/{filename}")
async def download_file(filename: str):
"""Serves the embedded image for download."""
file_path = os.path.join(SAVE_DIR, filename)
print(f"Checking for file: {file_path}")
if not os.path.exists(file_path):
print(f"❌ File not found: {file_path}")
return {"error": f"File not found: {file_path}"}
print(f"✅ File found, serving: {file_path}")
return FileResponse(file_path, media_type="image/png", filename=filename)