问题描述 投票:0回答:0

import cv2 import numpy as np from PIL import Image, ImageChops, ImageEnhance, UnidentifiedImageError import os import tensorflow as tf import binascii class ImageAnalyzer: def __init__(self, model_path="../scripts/results/image/Model-2/MobileNetV2.keras"): self.model = tf.keras.models.load_model(model_path) def generate_unique_filename(self, extension=".jpg"): return binascii.hexlify(os.urandom(8)).decode() + extension def perform_ela(self, image_path, quality=90, threshold=30, save_path="output/"): if not os.path.exists(save_path): os.makedirs(save_path) unique_filename = self.generate_unique_filename(".jpg") temp_path = os.path.join(save_path, unique_filename) original = Image.open(image_path).convert("RGB") original.save(temp_path, "JPEG", quality=quality) compressed = Image.open(temp_path) ela_image = ImageChops.difference(original, compressed) extrema = ela_image.getextrema() max_diff = max([ex[1] for ex in extrema]) if max_diff == 0: max_diff = 1 scale = 255.0 / max_diff ela_image = ImageEnhance.Brightness(ela_image).enhance(scale) ela_image_path = os.path.join(save_path, self.generate_unique_filename(".png")) ela_image.save(ela_image_path) ela_cv = cv2.imread(ela_image_path) gray = cv2.cvtColor(ela_cv, cv2.COLOR_BGR2GRAY) _, thresh = cv2.threshold(gray, threshold, 255, cv2.THRESH_BINARY) total_pixels = thresh.size forged_pixels = np.count_nonzero(thresh) forged_percentage = (forged_pixels / total_pixels) * 100 highlight_path = os.path.join(save_path, self.generate_unique_filename(".png")) cv2.imwrite(highlight_path, thresh) is_forged = "Yes" if forged_percentage > 0.5 else "No" return { "forged": is_forged, "percentage": round(forged_percentage, 2), "ela_image": ela_image_path, "highlighted_image": highlight_path } def predict_image(self, image_path): image = Image.open(image_path).resize((224, 224)) image = np.array(image) / 255.0 image = np.expand_dims(image, axis=0) prediction = self.model.predict(image)[0] confidence_real = prediction[0] confidence_fake = prediction[1] label = "Real" if confidence_real > confidence_fake else "Fake" confidence = max(confidence_real, confidence_fake) * 100 return {"label": label, "confidence": round(confidence, 2)} def def_image(self, image): try: if not isinstance(image, Image.Image): raise ValueError("Invalid image format. Please upload a valid image file.") # Convert to RGB to remove alpha channels and ensure compatibility image = image.convert("RGB") # Save image as a temporary JPEG file image_path = "image/temp_image.jpg" image.save(image_path, format="JPEG") # Perform ELA analysis and prediction ela_result = self.perform_ela(image_path) prediction_result = self.predict_image(image_path) return { "forged": ela_result["forged"], "percentage": ela_result["percentage"], "ela_image": ela_result["ela_image"], "highlighted_image": ela_result["highlighted_image"], "prediction_label": prediction_result["label"], "prediction_confidence": prediction_result["confidence"] } except UnidentifiedImageError: raise ValueError("Corrupt or unsupported image file. Please upload a valid image.") except Exception as e: raise ValueError(f"An unexpected error occurred: {e}")

eRror:

PermissionError: [Errno 13] Permission denied: 'D:\\Uni\\FYP\\Model\\model\\Demo_Gradio'

我尝试创建一个新的目录,并且还对代码进行了更改,但到目前为止没有什么真正奏效的。
    

trory使用Python OS模块打开并读取文件夹数据

	

python tensorflow permissions artificial-intelligence gradio
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.