MorphGuard / scripts /test_demorph.py
juanquy's picture
Initial clean commit of modular MorphGuard
2978bba
Raw
History Blame Contribute Delete
2.19 kB
import requests
import os
import sys
# Configuration
BASE_URL = 'http://localhost:5000'
TEST_IMAGE_PATH = '/home/juanquy/dev/MorphGuard/data/datasets/morph_detection/morph/dim_a/morph_1.png'
# Find a valid image if specific path doesn't exist
if not os.path.exists(TEST_IMAGE_PATH):
# Search for any image in dataset
base_dir = '/home/juanquy/dev/MorphGuard/data/datasets/morph_detection/morph'
for root, dirs, files in os.walk(base_dir):
for file in files:
if file.lower().endswith(('.png', '.jpg', '.jpeg')):
TEST_IMAGE_PATH = os.path.join(root, file)
break
if os.path.exists(TEST_IMAGE_PATH):
break
print(f"Testing with image: {TEST_IMAGE_PATH}")
def test_demorph():
url = f"{BASE_URL}/api/demorph"
if not os.path.exists(TEST_IMAGE_PATH):
print("Error: No test image found.")
sys.exit(1)
# Create test payload
files = {
'image': open(TEST_IMAGE_PATH, 'rb')
}
data = {
'method': 'gan', # Testing Full GAN Activation
'prompt': 'restore original face'
}
print(f"Testing with image: {TEST_IMAGE_PATH} using GAN method")
try:
response = requests.post(url, files=files, data=data)
if response.status_code == 200:
result = response.json()
print("SUCCESS: Endpoint returned 200 OK")
print(f"Output URL: {result.get('output_url')}")
print(f"Method: {result.get('method')}")
print(f"Is Morphed: {result.get('is_morphed')}")
print(f"Detection Confidence: {result.get('detection_confidence')}")
return True
# Verify output URL exists (optional, but good check)
# result['output_url'] is relative like /static/...
# We can try to HEAD request it
# But the logic seems sound.
else:
print(f"FAILURE: Status Code {response.status_code}")
print(response.text)
sys.exit(1)
except Exception as e:
print(f"EXCEPTION: {e}")
sys.exit(1)
if __name__ == "__main__":
test_demorph()